|
16 | 16 | from typing import Optional |
17 | 17 | from unittest import mock |
18 | 18 |
|
| 19 | +from google.adk.telemetry import google_cloud |
| 20 | +from google.adk.telemetry.google_cloud import _DEFAULT_MTLS_TELEMETRY_TRACES_ENPOINT |
| 21 | +from google.adk.telemetry.google_cloud import _DEFAULT_TELEMETRY_TRACES_ENPOINT |
| 22 | +from google.adk.telemetry.google_cloud import _get_api_endpoint |
| 23 | +from google.adk.telemetry.google_cloud import _get_gcp_span_exporter |
| 24 | +from google.adk.telemetry.google_cloud import _use_client_cert_effective |
19 | 25 | from google.adk.telemetry.google_cloud import get_gcp_exporters |
20 | 26 | from google.adk.telemetry.google_cloud import get_gcp_resource |
| 27 | +import google.auth.credentials |
| 28 | +from google.auth.transport import mtls |
| 29 | +from google.auth.transport import requests |
| 30 | +from opentelemetry.exporter.otlp.proto.http import trace_exporter |
21 | 31 | import pytest |
22 | 32 |
|
23 | 33 |
|
@@ -89,3 +99,108 @@ def test_get_gcp_resource( |
89 | 99 | otel_resource.attributes.get("gcp.project_id", None) |
90 | 100 | == expected_project_id |
91 | 101 | ) |
| 102 | + |
| 103 | + |
| 104 | +@mock.patch.object(mtls, "should_use_client_cert", autospec=True) |
| 105 | +def test_use_client_cert_effective_from_mtls(mock_should_use): |
| 106 | + mock_should_use.return_value = True |
| 107 | + assert _use_client_cert_effective() |
| 108 | + |
| 109 | + mock_should_use.return_value = False |
| 110 | + assert not _use_client_cert_effective() |
| 111 | + |
| 112 | + |
| 113 | +def test_use_client_cert_effective_from_env( |
| 114 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 115 | +): |
| 116 | + with mock.patch.object( |
| 117 | + mtls, |
| 118 | + "should_use_client_cert", |
| 119 | + autospec=True, |
| 120 | + side_effect=AttributeError, |
| 121 | + ): |
| 122 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true") |
| 123 | + assert _use_client_cert_effective() |
| 124 | + |
| 125 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") |
| 126 | + assert not _use_client_cert_effective() |
| 127 | + |
| 128 | + # Test invalid value defaults to False |
| 129 | + monkeypatch.setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "maybe") |
| 130 | + assert not _use_client_cert_effective() |
| 131 | + assert ( |
| 132 | + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" |
| 133 | + " either `true` or `false`" |
| 134 | + in caplog.text |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize( |
| 139 | + "env_val, cert_source, expected", |
| 140 | + [ |
| 141 | + ("auto", lambda: b"cert", _DEFAULT_MTLS_TELEMETRY_TRACES_ENPOINT), |
| 142 | + ("auto", None, _DEFAULT_TELEMETRY_TRACES_ENPOINT), |
| 143 | + ("always", None, _DEFAULT_MTLS_TELEMETRY_TRACES_ENPOINT), |
| 144 | + ("never", lambda: b"cert", _DEFAULT_TELEMETRY_TRACES_ENPOINT), |
| 145 | + ("invalid", None, _DEFAULT_TELEMETRY_TRACES_ENPOINT), |
| 146 | + ], |
| 147 | +) |
| 148 | +def test_get_api_endpoint( |
| 149 | + env_val, |
| 150 | + cert_source, |
| 151 | + expected, |
| 152 | + monkeypatch: pytest.MonkeyPatch, |
| 153 | + caplog: pytest.LogCaptureFixture, |
| 154 | +): |
| 155 | + monkeypatch.setenv("GOOGLE_API_USE_MTLS_ENDPOINT", env_val) |
| 156 | + if env_val == "invalid": |
| 157 | + assert _get_api_endpoint(cert_source) == expected |
| 158 | + assert ( |
| 159 | + "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be one of" |
| 160 | + in caplog.text |
| 161 | + ) |
| 162 | + else: |
| 163 | + assert _get_api_endpoint(cert_source) == expected |
| 164 | + |
| 165 | + |
| 166 | +@mock.patch.object(requests, "AuthorizedSession", autospec=True) |
| 167 | +@mock.patch( |
| 168 | + "opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter", |
| 169 | + autospec=True, |
| 170 | +) |
| 171 | +@mock.patch( |
| 172 | + "google.adk.telemetry.google_cloud.BatchSpanProcessor", autospec=True |
| 173 | +) |
| 174 | +@mock.patch( |
| 175 | + "google.adk.telemetry.google_cloud._use_client_cert_effective", |
| 176 | + autospec=True, |
| 177 | +) |
| 178 | +@mock.patch( |
| 179 | + "google.auth.transport.mtls.has_default_client_cert_source", autospec=True |
| 180 | +) |
| 181 | +@mock.patch( |
| 182 | + "google.auth.transport.mtls.default_client_cert_source", autospec=True |
| 183 | +) |
| 184 | +def test_get_gcp_span_exporter_mtls( |
| 185 | + mock_default_cert: mock.MagicMock, |
| 186 | + mock_has_cert: mock.MagicMock, |
| 187 | + mock_use_cert: mock.MagicMock, |
| 188 | + mock_batch: mock.MagicMock, |
| 189 | + mock_exporter: mock.MagicMock, |
| 190 | + mock_session: mock.MagicMock, |
| 191 | +): |
| 192 | + credentials = mock.create_autospec( |
| 193 | + google.auth.credentials.Credentials, instance=True |
| 194 | + ) |
| 195 | + mock_use_cert.return_value = True |
| 196 | + mock_has_cert.return_value = True |
| 197 | + mock_default_cert.return_value = b"cert" |
| 198 | + |
| 199 | + _get_gcp_span_exporter(credentials) |
| 200 | + |
| 201 | + mock_session.assert_called_once_with(credentials=credentials) |
| 202 | + mock_session.return_value.configure_mtls_channel.assert_called_once() |
| 203 | + mock_exporter.assert_called_once_with( |
| 204 | + session=mock_session.return_value, |
| 205 | + endpoint=_DEFAULT_MTLS_TELEMETRY_TRACES_ENPOINT, |
| 206 | + ) |
0 commit comments