Repository navigation
CLI unit tests run the real gcloud: an ADC browser login on every run, and they unset the default project #7880
Description
🔴 Required Information
Describe the Bug:
On a machine with the gcloud CLI installed, running the unit tests under tests/unittests/cli runs real gcloud commands:
test_handle_login_with_google_option_3opens a browser forgcloud auth application-default loginon every run. Signing in doesn't help, and the run blocks until the sign-in finishes or times out.test_handle_login_with_google_option_2reads the real default project. If one is set, the test unsets it withgcloud config unset project, becauseclick.confirmis mocked to always answer yes.
Both tests are in tests/unittests/cli/utils/test_cli_create.py, and both reach code they don't mock:
- The autouse
mock_gcp_metadata_offfixture intests/unittests/conftest.pyhides Application Default Credentials, by setting_cloud_sdk.get_application_default_credentials_pathto''. Sogcp_utils.check_adc()is always False inside the suite.option_3is the onlyhandle_login_with_googletest that doesn't mockcheck_adc, so it always callsgcp_utils.login_adc(), which runsgcloud auth application-default login. On CI, where gcloud isn't installed, that raisesRuntimeError, which becomesclick.Abort. So the test passes there without ever reaching the "3. Abandon" choice it is meant to cover. option_2doesn't mock_onboarding.get_gcp_project_from_gcloud, unliketest_handle_login_with_google_option_2_unset_project. After the mocked Express sign-up it runsgcloud config get-value project, compares the result with the new project, and then runsgcloud config unset project.
Steps to Reproduce:
- Install the gcloud CLI and set a default project (
gcloud config set project my-project). - Run
pytest tests/unittests/cli/utils/test_cli_create.py. - Check
gcloud config get-value project, and gcloud's own logs in~/.config/gcloud/logs/.
Expected Behavior:
Unit tests don't run the gcloud CLI, open a browser, or change the developer's gcloud configuration.
Observed Behavior:
gcloud's log from one test run (project id redacted):
17.42.27 Running [gcloud.config.get-value] with arguments: [SECTION/PROPERTY: "project"] -> <my-project>
17.42.27 Running [gcloud.config.unset] with arguments: [SECTION/PROPERTY: "project"] -> Unset property [core/project].
17.42.37 Running [gcloud.auth.application-default.login] with arguments: []
Every later run opened the sign-in again (17.45.29, 17.52.41 and 17.56.32 in the same log). Each run also spends about 9 seconds in the two tests waiting on gcloud.
Environment Details:
- ADK Library Version (pip show google-adk): 2.13.0 (also on
mainat e9e8f61) - Desktop OS: macOS
- Python Version (python -V): 3.11.16
Model Information:
- Are you using LiteLLM: No
- Which model is being used: not applicable (unit tests)
🟡 Optional Information
Minimal Reproduction Code:
To see which tests reach gcloud without touching the real one, put a fake gcloud first on PATH that logs $PYTEST_CURRENT_TEST and exits 1:
test=[tests/unittests/cli/utils/test_cli_create.py::test_handle_login_with_google_option_2 (call)] args=[config get-value project]
test=[tests/unittests/cli/utils/test_cli_create.py::test_handle_login_with_google_option_3 (call)] args=[auth application-default login]
How often has this issue occurred?:
- Always (100%), on any machine with gcloud installed
Activity