"""What reaches `helm --values`: the catalog entry's values, with the size on top.""" from __future__ import annotations from datetime import UTC, datetime from typing import Any from uuid import uuid4 import pytest from services.worker.handlers import HandlerError, _deep_merge, _values_for from svcforge_core.domain.models import CatalogEntry, Instance, SizeSpec from svcforge_core.domain.states import InstanceState RESOURCES: dict[str, Any] = {"requests": {"cpu": "10m", "memory": "16Mi"}} def _entry(values: dict[str, Any] | None = None, replicas: int = 1) -> CatalogEntry: return CatalogEntry( service_type="redis", chart="oci://mirror.gcr.io/bitnamicharts/redis", chart_version="27.0.15", sizes={"small": SizeSpec(replicas=replicas, resources=RESOURCES)}, values=values or {}, ) def _instance(size: str = "small") -> Instance: now = datetime.now(UTC) return Instance( id=uuid4(), team="acme", service_type="redis", size=size, state=InstanceState.REQUESTED, namespace="tenant-acme", release_name="acme-redis-0f8b7d3e", chart_version="27.0.15", created_at=now, updated_at=now, ) # --------------------------------------------------------------------------- the merge def test_deep_merge_keeps_both_sides_of_a_shared_nested_key() -> None: """The reason this is not `base | override`. A shallow merge replaces the whole `global` map and silently drops imageRegistry, so the pod pulls from a registry nobody chose. """ merged = _deep_merge( {"global": {"imageRegistry": "mirror.gcr.io"}}, {"global": {"storageClass": "longhorn"}}, ) assert merged == {"global": {"imageRegistry": "mirror.gcr.io", "storageClass": "longhorn"}} def test_deep_merge_override_wins_on_a_scalar() -> None: assert _deep_merge({"a": 1}, {"a": 2}) == {"a": 2} def test_deep_merge_does_not_mutate_its_inputs() -> None: """The catalog is loaded once at startup and shared by every provision. Mutating `entry.values` here would leak one instance's size into the next one's values, and the second tenant would get the first tenant's replica count. """ base = {"global": {"imageRegistry": "mirror.gcr.io"}} _deep_merge(base, {"global": {"storageClass": "longhorn"}, "replicaCount": 3}) assert base == {"global": {"imageRegistry": "mirror.gcr.io"}} # --------------------------------------------------------------------------- what helm gets def test_entry_values_reach_helm() -> None: values = _values_for(_instance(), _entry({"global": {"imageRegistry": "mirror.gcr.io"}})) assert values["global"] == {"imageRegistry": "mirror.gcr.io"} assert values["replicaCount"] == 1 assert values["resources"] == RESOURCES def test_size_beats_entry_values() -> None: """An entry that sets replicaCount must not override the size the tenant asked for. Without this ordering every size deploys the same shape, and `medium` is a lie. """ entry = _entry({"replicaCount": 99}, replicas=3) assert _values_for(_instance(), entry)["replicaCount"] == 3 def test_entry_without_values_is_unchanged() -> None: assert _values_for(_instance(), _entry()) == {"replicaCount": 1, "resources": RESOURCES} def test_unknown_size_raises() -> None: with pytest.raises(HandlerError, match="not in catalog"): _values_for(_instance(size="enormous"), _entry())