|
| 1 | +"""Render the chart with helm and assert on the resulting manifests.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +import yaml |
| 8 | + |
| 9 | +CHART = Path(__file__).resolve().parent.parent |
| 10 | +UPSTREAM_URL = "common.database.upstream.url.value=postgres://t:t@h/d" |
| 11 | + |
| 12 | +# Each component's workload kind, name suffix, and the values key holding its |
| 13 | +# own extraEnv/extraEnvFrom. |
| 14 | +COMPONENTS = [ |
| 15 | + ("Deployment", "zero-cache", "singleNode", ["--set", "singleNode.enabled=true"]), |
| 16 | + ("StatefulSet", "zero-cache-replication-manager", "replicationManager", []), |
| 17 | + ("StatefulSet", "zero-cache-view-syncer", "viewSyncer", []), |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def render(*args: str) -> str: |
| 22 | + result = subprocess.run( |
| 23 | + ["helm", "template", "z", str(CHART), "--set", UPSTREAM_URL, *args], |
| 24 | + capture_output=True, |
| 25 | + text=True, |
| 26 | + check=True, |
| 27 | + ) |
| 28 | + return result.stdout |
| 29 | + |
| 30 | + |
| 31 | +def workload(rendered: str, kind: str, name: str) -> dict: |
| 32 | + for doc in yaml.safe_load_all(rendered): |
| 33 | + if doc and doc.get("kind") == kind and doc["metadata"]["name"].endswith(name): |
| 34 | + return doc |
| 35 | + raise AssertionError(f"no {kind} ending in {name!r} in rendered output") |
| 36 | + |
| 37 | + |
| 38 | +def main_container(rendered: str, kind: str, name: str) -> dict: |
| 39 | + return workload(rendered, kind, name)["spec"]["template"]["spec"]["containers"][0] |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("kind,name,component,extra_args", COMPONENTS) |
| 43 | +def test_extra_env_empty_is_a_no_op(kind, name, component, extra_args): |
| 44 | + """Omitted and explicitly-empty extraEnv render byte-identical output.""" |
| 45 | + baseline = render(*extra_args) |
| 46 | + explicit = render( |
| 47 | + *extra_args, |
| 48 | + "--set-json", f'{{"{component}":{{"extraEnv":[],"extraEnvFrom":[]}}}}', |
| 49 | + "--set-json", '{"common":{"extraEnv":[],"extraEnvFrom":[]}}', |
| 50 | + ) |
| 51 | + assert baseline == explicit |
| 52 | + assert "envFrom" not in main_container(baseline, kind, name) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("kind,name,component,extra_args", COMPONENTS) |
| 56 | +def test_extra_env_appends_in_order(kind, name, component, extra_args): |
| 57 | + """Entries land after the chart's own env, common first, in listed order.""" |
| 58 | + baseline_env = main_container(render(*extra_args), kind, name)["env"] |
| 59 | + env = main_container( |
| 60 | + render( |
| 61 | + *extra_args, |
| 62 | + "--set", "common.extraEnv[0].name=COMMON_ONE", |
| 63 | + "--set", "common.extraEnv[0].value=c1", |
| 64 | + "--set", f"{component}.extraEnv[0].name=OWN_ONE", |
| 65 | + "--set", f"{component}.extraEnv[0].value=o1", |
| 66 | + "--set", f"{component}.extraEnv[1].name=OWN_TWO", |
| 67 | + "--set", f"{component}.extraEnv[1].value=o2", |
| 68 | + ), |
| 69 | + kind, |
| 70 | + name, |
| 71 | + )["env"] |
| 72 | + |
| 73 | + assert env[: len(baseline_env)] == baseline_env, "chart env must be untouched" |
| 74 | + assert env[len(baseline_env):] == [ |
| 75 | + {"name": "COMMON_ONE", "value": "c1"}, |
| 76 | + {"name": "OWN_ONE", "value": "o1"}, |
| 77 | + {"name": "OWN_TWO", "value": "o2"}, |
| 78 | + ] |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("kind,name,component,extra_args", COMPONENTS) |
| 82 | +def test_extra_env_overrides_chart_value_last(kind, name, component, extra_args): |
| 83 | + """A duplicate name is appended last, so Kubernetes' last-wins rule applies.""" |
| 84 | + env = main_container( |
| 85 | + render( |
| 86 | + *extra_args, |
| 87 | + "--set", f"{component}.extraEnv[0].name=ZERO_LOG_LEVEL", |
| 88 | + "--set", f"{component}.extraEnv[0].value=debug", |
| 89 | + ), |
| 90 | + kind, |
| 91 | + name, |
| 92 | + ) |
| 93 | + entries = [e for e in env["env"] if e["name"] == "ZERO_LOG_LEVEL"] |
| 94 | + assert len(entries) == 2 |
| 95 | + assert entries[-1]["value"] == "debug" |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("kind,name,component,extra_args", COMPONENTS) |
| 99 | +def test_extra_env_from_sources(kind, name, component, extra_args): |
| 100 | + container = main_container( |
| 101 | + render( |
| 102 | + *extra_args, |
| 103 | + "--set", "common.extraEnvFrom[0].configMapRef.name=common-cm", |
| 104 | + "--set", f"{component}.extraEnvFrom[0].secretRef.name=own-secret", |
| 105 | + ), |
| 106 | + kind, |
| 107 | + name, |
| 108 | + ) |
| 109 | + assert container["envFrom"] == [ |
| 110 | + {"configMapRef": {"name": "common-cm"}}, |
| 111 | + {"secretRef": {"name": "own-secret"}}, |
| 112 | + ] |
| 113 | + |
| 114 | + |
| 115 | +def test_extra_env_is_scoped_to_its_own_component(): |
| 116 | + """A component's extraEnv does not leak into the other component.""" |
| 117 | + rendered = render( |
| 118 | + "--set", "viewSyncer.extraEnv[0].name=VS_ONLY", |
| 119 | + "--set", "viewSyncer.extraEnv[0].value=v1", |
| 120 | + ) |
| 121 | + vs = main_container(rendered, "StatefulSet", "zero-cache-view-syncer") |
| 122 | + rm = main_container(rendered, "StatefulSet", "zero-cache-replication-manager") |
| 123 | + assert "VS_ONLY" in [e["name"] for e in vs["env"]] |
| 124 | + assert "VS_ONLY" not in [e["name"] for e in rm["env"]] |
| 125 | + |
| 126 | + |
| 127 | +def test_extra_env_skips_init_containers(): |
| 128 | + """extraEnv targets the zero-cache container only, not init containers.""" |
| 129 | + rendered = render( |
| 130 | + "--set", "common.extraEnv[0].name=COMMON_ONE", |
| 131 | + "--set", "common.extraEnv[0].value=c1", |
| 132 | + ) |
| 133 | + pod = workload(rendered, "StatefulSet", "zero-cache-view-syncer")["spec"]["template"]["spec"] |
| 134 | + for init in pod.get("initContainers", []): |
| 135 | + assert "COMMON_ONE" not in [e["name"] for e in init.get("env", [])] |
0 commit comments