Skip to content

Commit 46532d7

Browse files
DeanChensjcopybara-github
authored andcommitted
test: Mock google auth in sample loading smoke tests
Mock `google.auth.default` and `google.auth.transport.mtls.has_default_client_cert_source` in `test_sample_loads` to prevent `DefaultCredentialsError` and mTLS extraction failures when loading samples that initialize GCP clients in environments without credentials (like GitHub Actions). Co-authored-by: Shangjie Chen <deanchen@google.com> PiperOrigin-RevId: 944658609
1 parent 7b08141 commit 46532d7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

tests/unittests/test_samples.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,33 @@ def test_sample_loads(sample_dir: Path, monkeypatch):
224224
"""Smoke test: every sample's agent imports and constructs a root agent."""
225225
for key, value in _DUMMY_ENV.items():
226226
monkeypatch.setenv(key, value)
227+
import google.auth
228+
import google.auth.credentials
229+
import google.auth.transport
230+
from google.auth.transport import mtls
231+
232+
class _DummyCredentials(google.auth.credentials.Credentials):
233+
234+
def __init__(self) -> None:
235+
super().__init__()
236+
self.token: str | None = "dummy-token"
237+
238+
def refresh(self, request: google.auth.transport.Request) -> None:
239+
self.token = "dummy-token"
240+
241+
monkeypatch.setattr(
242+
google.auth,
243+
"default",
244+
lambda *args, **kwargs: (
245+
_DummyCredentials(),
246+
"dummy-project",
247+
),
248+
)
249+
monkeypatch.setattr(
250+
mtls,
251+
"has_default_client_cert_source",
252+
lambda: False,
253+
)
227254
root_agent = _load_root_agent(sample_dir)
228255
assert root_agent is not None, f"{sample_dir} loaded no root agent"
229256
assert getattr(

0 commit comments

Comments
 (0)