|
| 1 | +"""Unit tests for DockerDeployment._build_env_args. |
| 2 | +
|
| 3 | +Covers the env-arg list assembled for `docker run -e ...`, focusing on the |
| 4 | +INSTANCE_ROCK_REGISTRY injection sourced from RuntimeConfig.instance_registry_mirrors. |
| 5 | +Existing entries (ROCK_LOGGING_*, ROCK_TIME_ZONE, ROCK_KATA_RUNTIME) are regression-checked. |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from rock.config import RuntimeConfig |
| 11 | +from rock.deployments.config import DockerDeploymentConfig |
| 12 | +from rock.deployments.docker import DockerDeployment |
| 13 | + |
| 14 | + |
| 15 | +def _make_deployment(**config_kwargs) -> DockerDeployment: |
| 16 | + with patch("rock.deployments.docker.DockerSandboxValidator"): |
| 17 | + return DockerDeployment.from_config(DockerDeploymentConfig(**config_kwargs)) |
| 18 | + |
| 19 | + |
| 20 | +class TestBuildEnvArgsInstanceRegistry: |
| 21 | + def test_no_mirrors_means_no_registry_env(self): |
| 22 | + deployment = _make_deployment() |
| 23 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 24 | + mock_env.ROCK_LOGGING_PATH = "" |
| 25 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 26 | + args = deployment._build_env_args() |
| 27 | + assert "INSTANCE_ROCK_REGISTRY" not in " ".join(args) |
| 28 | + |
| 29 | + def test_single_mirror_emitted_unquoted(self): |
| 30 | + deployment = _make_deployment( |
| 31 | + runtime_config=RuntimeConfig(instance_registry_mirrors=["reg-a.example.com/ns"]), |
| 32 | + ) |
| 33 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 34 | + mock_env.ROCK_LOGGING_PATH = "" |
| 35 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 36 | + args = deployment._build_env_args() |
| 37 | + assert "-e" in args |
| 38 | + assert "INSTANCE_ROCK_REGISTRY=reg-a.example.com/ns" in args |
| 39 | + |
| 40 | + def test_multiple_mirrors_comma_joined(self): |
| 41 | + deployment = _make_deployment( |
| 42 | + runtime_config=RuntimeConfig( |
| 43 | + instance_registry_mirrors=[ |
| 44 | + "reg-a.example.com/ns-1", |
| 45 | + "reg-b.example.com/ns-2", |
| 46 | + ], |
| 47 | + ), |
| 48 | + ) |
| 49 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 50 | + mock_env.ROCK_LOGGING_PATH = "" |
| 51 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 52 | + args = deployment._build_env_args() |
| 53 | + assert "INSTANCE_ROCK_REGISTRY=reg-a.example.com/ns-1,reg-b.example.com/ns-2" in args |
| 54 | + |
| 55 | + |
| 56 | +class TestBuildEnvArgsRegression: |
| 57 | + """The existing env entries must keep working after the extraction.""" |
| 58 | + |
| 59 | + def test_timezone_always_present(self): |
| 60 | + deployment = _make_deployment() |
| 61 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 62 | + mock_env.ROCK_LOGGING_PATH = "" |
| 63 | + mock_env.ROCK_TIME_ZONE = "Asia/Shanghai" |
| 64 | + args = deployment._build_env_args() |
| 65 | + assert "ROCK_TIME_ZONE=Asia/Shanghai" in args |
| 66 | + |
| 67 | + def test_logging_entries_present_when_logging_path_set(self): |
| 68 | + deployment = _make_deployment() |
| 69 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 70 | + mock_env.ROCK_LOGGING_PATH = "/var/log/rock" |
| 71 | + mock_env.ROCK_LOGGING_LEVEL = "INFO" |
| 72 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 73 | + args = deployment._build_env_args() |
| 74 | + assert "ROCK_LOGGING_PATH=/var/log/rock" in args |
| 75 | + assert "ROCK_LOGGING_LEVEL=INFO" in args |
| 76 | + |
| 77 | + def test_logging_entries_absent_when_logging_path_empty(self): |
| 78 | + deployment = _make_deployment() |
| 79 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 80 | + mock_env.ROCK_LOGGING_PATH = "" |
| 81 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 82 | + args = deployment._build_env_args() |
| 83 | + joined = " ".join(args) |
| 84 | + assert "ROCK_LOGGING_PATH" not in joined |
| 85 | + assert "ROCK_LOGGING_LEVEL" not in joined |
| 86 | + |
| 87 | + def test_kata_runtime_env_present_when_enabled(self): |
| 88 | + deployment = _make_deployment(use_kata_runtime=True) |
| 89 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 90 | + mock_env.ROCK_LOGGING_PATH = "" |
| 91 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 92 | + args = deployment._build_env_args() |
| 93 | + assert "ROCK_KATA_RUNTIME=true" in args |
| 94 | + |
| 95 | + def test_kata_runtime_env_absent_when_disabled(self): |
| 96 | + deployment = _make_deployment(use_kata_runtime=False) |
| 97 | + with patch("rock.deployments.docker.env_vars") as mock_env: |
| 98 | + mock_env.ROCK_LOGGING_PATH = "" |
| 99 | + mock_env.ROCK_TIME_ZONE = "UTC" |
| 100 | + args = deployment._build_env_args() |
| 101 | + assert "ROCK_KATA_RUNTIME=true" not in args |
0 commit comments