| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Unit tests for build.auth.auth_env.""" | |
| import os | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from unittest import mock | |
| from build.auth import auth_env | |
| class AuthTypeTest(unittest.TestCase): | |
| def test_values_and_str(self) -> None: | |
| self.assertEqual(str(auth_env.AuthType.LOAS), "loas") | |
| self.assertEqual(str(auth_env.AuthType.OAUTH), "oauth") | |
| self.assertEqual(str(auth_env.AuthType.MACHINE), "machine") | |
| self.assertEqual(str(auth_env.AuthType.NONE), "none") | |
| self.assertEqual(str(auth_env.AuthType.AUTO), "auto") | |
| def test_top_level_aliases(self) -> None: | |
| self.assertIs(auth_env.LOAS, auth_env.AuthType.LOAS) | |
| self.assertIs(auth_env.OAUTH, auth_env.AuthType.OAUTH) | |
| self.assertIs(auth_env.MACHINE, auth_env.AuthType.MACHINE) | |
| self.assertIs(auth_env.NONE, auth_env.AuthType.NONE) | |
| self.assertIs(auth_env.AUTO, auth_env.AuthType.AUTO) | |
| class MakeAuthEnvTest(unittest.TestCase): | |
| def test_defaults(self) -> None: | |
| env = auth_env.make_auth_env() | |
| self.assertEqual( | |
| env, | |
| { | |
| auth_env.ENV_RBE_AUTH_TYPE: "none", | |
| auth_env.ENV_RS_AUTH_TYPE: "none", | |
| }, | |
| ) | |
| def test_populated(self) -> None: | |
| env = auth_env.make_auth_env( | |
| rbe_auth=auth_env.LOAS, | |
| rs_auth="oauth", | |
| user="test-user", | |
| credentials_file=Path("/tmp/creds.json"), | |
| ) | |
| self.assertEqual( | |
| env, | |
| { | |
| auth_env.ENV_RBE_AUTH_TYPE: "loas", | |
| auth_env.ENV_RS_AUTH_TYPE: "oauth", | |
| auth_env.ENV_USER: "test-user", | |
| auth_env.ENV_GOOGLE_APPLICATION_CREDENTIALS: "/tmp/creds.json", | |
| }, | |
| ) | |
| class RbeAuthTypeTest(unittest.TestCase): | |
| def test_missing_returns_none(self) -> None: | |
| self.assertEqual(auth_env.rbe_auth_type({}), "none") | |
| def test_present_returns_value(self) -> None: | |
| self.assertEqual( | |
| auth_env.rbe_auth_type({auth_env.ENV_RBE_AUTH_TYPE: "loas"}), | |
| "loas", | |
| ) | |
| class RsAuthTypeTest(unittest.TestCase): | |
| def test_missing_returns_none(self) -> None: | |
| self.assertEqual(auth_env.rs_auth_type({}), "none") | |
| def test_present_returns_value(self) -> None: | |
| self.assertEqual( | |
| auth_env.rs_auth_type({auth_env.ENV_RS_AUTH_TYPE: "oauth"}), | |
| "oauth", | |
| ) | |
| class IsLoasTest(unittest.TestCase): | |
| def test_empty_env_returns_false(self) -> None: | |
| self.assertFalse(auth_env.is_loas({})) | |
| def test_rbe_loas_returns_true(self) -> None: | |
| self.assertTrue(auth_env.is_loas({auth_env.ENV_RBE_AUTH_TYPE: "loas"})) | |
| def test_rs_loas_returns_true(self) -> None: | |
| self.assertTrue(auth_env.is_loas({auth_env.ENV_RS_AUTH_TYPE: "loas"})) | |
| def test_non_loas_returns_false(self) -> None: | |
| env = { | |
| auth_env.ENV_RBE_AUTH_TYPE: "oauth", | |
| auth_env.ENV_RS_AUTH_TYPE: "machine", | |
| } | |
| self.assertFalse(auth_env.is_loas(env)) | |
| class RequiresGcertTest(unittest.TestCase): | |
| def test_empty_env_returns_false(self) -> None: | |
| self.assertFalse(auth_env.requires_gcert({})) | |
| def test_rbe_loas_returns_true(self) -> None: | |
| self.assertTrue( | |
| auth_env.requires_gcert({auth_env.ENV_RBE_AUTH_TYPE: "loas"}) | |
| ) | |
| def test_rs_loas_returns_true(self) -> None: | |
| self.assertTrue( | |
| auth_env.requires_gcert({auth_env.ENV_RS_AUTH_TYPE: "loas"}) | |
| ) | |
| def test_non_loas_returns_false(self) -> None: | |
| env = { | |
| auth_env.ENV_RBE_AUTH_TYPE: "oauth", | |
| auth_env.ENV_RS_AUTH_TYPE: "machine", | |
| } | |
| self.assertFalse(auth_env.requires_gcert(env)) | |
| class CredentialsFileTest(unittest.TestCase): | |
| def test_missing_returns_none(self) -> None: | |
| self.assertIsNone(auth_env.credentials_file({})) | |
| def test_present_returns_path(self) -> None: | |
| self.assertEqual( | |
| auth_env.credentials_file( | |
| {auth_env.ENV_GOOGLE_APPLICATION_CREDENTIALS: "/path/to/creds"} | |
| ), | |
| Path("/path/to/creds"), | |
| ) | |
| class ReclientCredshelperPathTest(unittest.TestCase): | |
| def test_path_construction(self) -> None: | |
| fuchsia_dir = Path("/fuchsia/root") | |
| path = auth_env.reclient_credshelper_path(fuchsia_dir, "linux-x64") | |
| self.assertEqual( | |
| path, | |
| Path( | |
| "/fuchsia/root/prebuilt/third_party/reclient/linux-x64/credshelper" | |
| ), | |
| ) | |
| class DetectSystemAuthTypeTest(unittest.TestCase): | |
| def setUp(self) -> None: | |
| super().setUp() | |
| auth_env.detect_system_auth_type.cache_clear() | |
| self.addCleanup(auth_env.detect_system_auth_type.cache_clear) | |
| def test_detection_caches_result(self) -> None: | |
| from build.auth import select_auth_method | |
| with mock.patch.object( | |
| select_auth_method, | |
| "select_auth_method", | |
| return_value=select_auth_method.AuthType.LOAS, | |
| ) as mock_select: | |
| res1 = auth_env.detect_system_auth_type() | |
| res2 = auth_env.detect_system_auth_type() | |
| self.assertEqual(res1, auth_env.AuthType.LOAS) | |
| self.assertEqual(res2, auth_env.AuthType.LOAS) | |
| self.assertEqual(mock_select.call_count, 1) | |
| class CachedAuthTypeTest(unittest.TestCase): | |
| def setUp(self) -> None: | |
| super().setUp() | |
| temp_dir = tempfile.TemporaryDirectory() | |
| self.addCleanup(temp_dir.cleanup) | |
| self.fuchsia_dir = Path(temp_dir.name) | |
| self.config = self.fuchsia_dir / ".fx/config/build-auth" | |
| self.config.parent.mkdir(parents=True) | |
| def test_returns_cached_value(self) -> None: | |
| self.config.write_text("# comment\nauth_type=oauth\n") | |
| self.assertEqual(auth_env.cached_auth_type(self.fuchsia_dir), "oauth") | |
| def test_missing_file_or_key(self) -> None: | |
| self.assertIsNone(auth_env.cached_auth_type(self.fuchsia_dir)) | |
| self.config.write_text("loas_cert_type=unrestricted\n") | |
| self.assertIsNone(auth_env.cached_auth_type(self.fuchsia_dir)) | |
| def test_strips_whitespace_comments_and_quotes(self) -> None: | |
| for content in ( | |
| "auth_type = oauth\n", | |
| "auth_type=oauth # set by hand\n", | |
| "auth_type='oauth'\n", | |
| ' auth_type="oauth" \n', | |
| ): | |
| self.config.write_text(content) | |
| self.assertEqual( | |
| auth_env.cached_auth_type(self.fuchsia_dir), "oauth", content | |
| ) | |
| def test_invalid_utf8(self) -> None: | |
| self.config.write_bytes(b"# \xff\nauth_type=loas\n") | |
| self.assertEqual(auth_env.cached_auth_type(self.fuchsia_dir), "loas") | |
| class ResolveBuildAuthTypeTest(unittest.TestCase): | |
| def setUp(self) -> None: | |
| super().setUp() | |
| auth_env.detect_system_auth_type.cache_clear() | |
| self.addCleanup(auth_env.detect_system_auth_type.cache_clear) | |
| def test_cached_auth_type_used_when_auto(self) -> None: | |
| from build.auth import select_auth_method | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| fuchsia_dir = Path(temp_dir) | |
| cfg = fuchsia_dir / ".fx/config/build-auth" | |
| cfg.parent.mkdir(parents=True) | |
| cfg.write_text("auth_type=oauth\n") | |
| with mock.patch.object( | |
| select_auth_method, "select_auth_method" | |
| ) as mock_select: | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", env={}, fuchsia_dir=fuchsia_dir | |
| ) | |
| mock_select.assert_not_called() | |
| self.assertEqual(res, "oauth") | |
| # The inherited parent environment still takes precedence. | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", | |
| env={auth_env.ENV_RBE_AUTH_TYPE: "loas"}, | |
| fuchsia_dir=fuchsia_dir, | |
| ) | |
| self.assertEqual(res, "loas") | |
| def test_cached_auto_falls_back_to_detection(self) -> None: | |
| from build.auth import select_auth_method | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| fuchsia_dir = Path(temp_dir) | |
| cfg = fuchsia_dir / ".fx/config/build-auth" | |
| cfg.parent.mkdir(parents=True) | |
| cfg.write_text("auth_type=auto\n") | |
| with mock.patch.object( | |
| select_auth_method, | |
| "select_auth_method", | |
| return_value=select_auth_method.AuthType.LOAS, | |
| ) as mock_select: | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", env={}, fuchsia_dir=fuchsia_dir | |
| ) | |
| mock_select.assert_called_once() | |
| self.assertEqual(res, "loas") | |
| def test_machine_credentials_precedence(self) -> None: | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type=auth_env.LOAS, | |
| use_machine_credentials=True, | |
| env={auth_env.ENV_RBE_AUTH_TYPE: "oauth"}, | |
| ) | |
| self.assertEqual(res, "machine") | |
| def test_explicit_auth_type_used(self) -> None: | |
| for val in ("loas", "oauth", "machine", "none"): | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type=val, | |
| use_machine_credentials=False, | |
| env={}, | |
| ) | |
| self.assertEqual(res, val) | |
| def test_inherited_env_used_when_auto(self) -> None: | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", | |
| use_machine_credentials=False, | |
| env={auth_env.ENV_RS_AUTH_TYPE: "oauth"}, | |
| service_env_var=auth_env.ENV_RS_AUTH_TYPE, | |
| ) | |
| self.assertEqual(res, "oauth") | |
| def test_system_detection_fallback(self) -> None: | |
| from build.auth import select_auth_method | |
| with ( | |
| mock.patch.object( | |
| select_auth_method, | |
| "select_auth_method", | |
| return_value=select_auth_method.AuthType.LOAS, | |
| ), | |
| mock.patch.dict(os.environ, {}, clear=True), | |
| ): | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", | |
| use_machine_credentials=False, | |
| env=None, | |
| ) | |
| self.assertEqual(res, "loas") | |
| def test_system_detection_uses_explicit_env(self) -> None: | |
| from build.auth import select_auth_method | |
| custom_env = {"SWARMING_BOT_ID": "bot-123"} | |
| with mock.patch.object( | |
| select_auth_method, | |
| "select_auth_method", | |
| return_value=select_auth_method.AuthType.MACHINE, | |
| ) as mock_select: | |
| res = auth_env.resolve_build_auth_type( | |
| auth_type="auto", | |
| use_machine_credentials=False, | |
| env=custom_env, | |
| ) | |
| self.assertEqual(res, "machine") | |
| mock_select.assert_called_once() | |
| self.assertEqual(mock_select.call_args.kwargs["env"], custom_env) | |
| def test_invalid_type_raises(self) -> None: | |
| with self.assertRaises(ValueError): | |
| auth_env.resolve_build_auth_type( | |
| auth_type="bogus_type", | |
| use_machine_credentials=False, | |
| env={}, | |
| ) | |
| if __name__ == "__main__": | |
| unittest.main() |