diff --git a/cli/administration/deploy/development/profile.py b/cli/administration/deploy/development/profile.py index ab3e80f61d..61de2cce0a 100644 --- a/cli/administration/deploy/development/profile.py +++ b/cli/administration/deploy/development/profile.py @@ -3,6 +3,16 @@ from __future__ import annotations import os +import sys + +from utils.env.handlers.infinito.cache import paths + +_NOTICED: set[str] = set() + + +def infra_owns_cache() -> bool: + """True iff the infra runner marked the cache base as its own.""" + return (paths.base() / "infra.owner").is_file() def _declared(name: str) -> bool | None: @@ -32,6 +42,15 @@ class Profile: def cache_stack_enabled(self) -> bool: """True iff the pull-through cache stack should be loaded.""" + if infra_owns_cache(): + if not _NOTICED: + _NOTICED.add("infra.owner") + print( + f"NOTICE: the infra runner owns {paths.base()} (infra.owner); " + "the legacy cache stack stays off", + file=sys.stderr, + ) + return False declared = _declared("INFINITO_CACHE_STACK") return (not self.is_ci()) if declared is None else declared diff --git a/tests/unit/python/cli/administration/deploy/development/test_compose.py b/tests/unit/python/cli/administration/deploy/development/test_compose.py index ad3ef7d75d..48cffd99bb 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_compose.py +++ b/tests/unit/python/cli/administration/deploy/development/test_compose.py @@ -15,6 +15,14 @@ from cli.administration.deploy.development.compose import ( class TestComposeUpRetries(unittest.TestCase): + def setUp(self) -> None: + patcher = patch( + "cli.administration.deploy.development.profile.infra_owns_cache", + return_value=False, + ) + patcher.start() + self.addCleanup(patcher.stop) + def _compose(self) -> Compose: return Compose(repo_root=Path("/tmp/infinito-nexus"), distro="arch") diff --git a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py index 04e4bf7469..13916aaf06 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py +++ b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py @@ -54,6 +54,14 @@ _WORKTREE = { class TestComposeFileArgs(unittest.TestCase): + def setUp(self) -> None: + patcher = patch( + "cli.administration.deploy.development.profile.infra_owns_cache", + return_value=False, + ) + patcher.start() + self.addCleanup(patcher.stop) + @patch.dict(os.environ, _LOCAL_PRIMARY, clear=False) def test_primary_instance_loads_the_cache_stack(self) -> None: self.assertEqual( diff --git a/tests/unit/python/cli/administration/deploy/development/test_profile.py b/tests/unit/python/cli/administration/deploy/development/test_profile.py index 1f20c12018..80c69a728b 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_profile.py +++ b/tests/unit/python/cli/administration/deploy/development/test_profile.py @@ -2,10 +2,15 @@ from __future__ import annotations +import contextlib +import io import os +import tempfile import unittest +from pathlib import Path from unittest.mock import patch +from cli.administration.deploy.development import profile from cli.administration.deploy.development.profile import Profile _BLANK_CI_ENV = { @@ -56,7 +61,17 @@ class TestProfileIsCI(unittest.TestCase): self.assertFalse(Profile().is_ci()) -class TestProfileCacheStackEnabled(unittest.TestCase): +class _WithoutInfraOwner(unittest.TestCase): + def setUp(self) -> None: + patcher = patch( + "cli.administration.deploy.development.profile.infra_owns_cache", + return_value=False, + ) + patcher.start() + self.addCleanup(patcher.stop) + + +class TestProfileCacheStackEnabled(_WithoutInfraOwner): @patch.dict(os.environ, _BLANK_CI_ENV, clear=False) def test_active_locally_when_no_ci_signal_set(self) -> None: self.assertTrue(Profile().cache_stack_enabled()) @@ -80,7 +95,7 @@ class TestProfileCacheStackEnabled(unittest.TestCase): _ON_CI = {**_BLANK_CI_ENV, "GITHUB_ACTIONS": "true"} -class TestProfileDeclaredCapabilities(unittest.TestCase): +class TestProfileDeclaredCapabilities(_WithoutInfraOwner): @patch.dict(os.environ, {**_ON_CI, "INFINITO_CACHE_STACK": "true"}, clear=False) def test_declaration_wins_over_the_ci_default(self) -> None: p = Profile() @@ -156,7 +171,7 @@ class TestProfileInstance(unittest.TestCase): self.assertEqual(Profile().instance(), 0) -class TestProfileOwnsCacheStack(unittest.TestCase): +class TestProfileOwnsCacheStack(_WithoutInfraOwner): @patch.dict(os.environ, _BLANK_CI_ENV, clear=False) def test_owns_the_stack_when_no_network_was_handed_over(self) -> None: self.assertTrue(Profile().owns_cache_stack()) @@ -178,5 +193,58 @@ class TestProfileOwnsCacheStack(unittest.TestCase): self.assertFalse(Profile().owns_cache_stack()) +class TestProfileInfraOwner(unittest.TestCase): + def setUp(self) -> None: + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.base = Path(tmp.name) + patcher = patch.object(profile.paths, "base", return_value=self.base) + patcher.start() + self.addCleanup(patcher.stop) + profile._NOTICED.clear() + self.addCleanup(profile._NOTICED.clear) + + def _mark_owner(self) -> None: + (self.base / "infra.owner").write_text("infinito-infra\n", encoding="utf-8") + + def test_infra_owns_cache_follows_the_marker_file(self) -> None: + self.assertFalse(profile.infra_owns_cache()) + self._mark_owner() + self.assertTrue(profile.infra_owns_cache()) + + def test_a_directory_named_like_the_marker_is_not_the_marker(self) -> None: + (self.base / "infra.owner").mkdir() + self.assertFalse(profile.infra_owns_cache()) + + @patch.dict(os.environ, {**_BLANK_CI_ENV, "INFINITO_CACHE_STACK": "true"}) + def test_marker_switches_the_stack_off_despite_the_declaration(self) -> None: + self._mark_owner() + p = Profile() + with contextlib.redirect_stderr(io.StringIO()): + self.assertFalse(p.cache_stack_enabled()) + self.assertFalse(p.owns_cache_stack()) + + @patch.dict(os.environ, _BLANK_CI_ENV) + def test_notice_is_printed_once_per_process(self) -> None: + self._mark_owner() + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + Profile().cache_stack_enabled() + Profile().cache_stack_enabled() + self.assertEqual(stderr.getvalue().count("NOTICE"), 1) + self.assertIn("infra.owner", stderr.getvalue()) + + @patch.dict(os.environ, {**_BLANK_CI_ENV, "INFINITO_CACHE_STACK": "true"}) + def test_without_the_marker_the_declaration_still_applies(self) -> None: + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + self.assertTrue(Profile().cache_stack_enabled()) + self.assertEqual(stderr.getvalue(), "") + + @patch.dict(os.environ, _BLANK_CI_ENV) + def test_without_the_marker_local_default_is_unchanged(self) -> None: + self.assertTrue(Profile().cache_stack_enabled()) + + if __name__ == "__main__": # pragma: no cover unittest.main()