diff --git a/newsfragments/272.internal.md b/newsfragments/272.internal.md new file mode 100644 index 00000000..98345571 --- /dev/null +++ b/newsfragments/272.internal.md @@ -0,0 +1 @@ +Support running manifest tests with multiple components. diff --git a/tests/manifests/__init__.py b/tests/manifests/__init__.py index 45f41734..a7e5bfbc 100644 --- a/tests/manifests/__init__.py +++ b/tests/manifests/__init__.py @@ -1,109 +1,256 @@ -# Copyright 2024 New Vector Ltd +# Copyright 2024-2025 New Vector Ltd # # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial -import copy -from typing import Any +import abc +from dataclasses import InitVar, dataclass, field +from typing import Any, Callable, Self -_raw_shared_components_details = { - "initSecrets": { - "hyphened_name": "init-secrets", - "has_image": False, - "has_service_monitor": False, - "has_ingress": False, - }, - "haproxy": {}, - "postgres": {"has_ingress": False, "paths_consistency_noqa": ["/docker-entrypoint-initdb.d"]}, -} +# We introduce 3 DataClasses to store details of the deployables this chart manages +# * ComponentDetails - details of a top-level deployable. This includes both the headlines +# components like Synapse, Element Web, etc and components that have their own independent +# properties at the root of the chart, like HAProxy & Postgres. These latter components might +# only be deployed if specific other top-level components are enabled however they are able to +# standalone. The shared components should be marked with `is_shared_component` which lets the +# manifest test setup know they don't have their own independent values files +# +# * SubComponentDetails - details of a dependent deployable. These are details of a deployable +# that belongs to / is only ever deployed as part of a top-level component. For example +# Synapse's Redis can never be deployed out of the context of Synapse. +# +# * DeployableDetails - a common base class for ComponentDetails and SubComponentDetails. All +# of the interesting properties (has_ingress, etc) we care to use to vary test assertions live +# here. The distinction between ComonentDetails and SubComponent details should be reserved for +# how the values files need to be manipulated. -_raw_component_details = { - "elementWeb": { - "hyphened_name": "element-web", - "has_service_monitor": False, - "paths_consistency_noqa": ["/etc/nginx"], - }, - "matrixAuthenticationService": { - "hyphened_name": "matrix-authentication-service", - "shared_components": ["initSecrets", "postgres"], - }, - "synapse": { - "additional_values_files": [ + +# We need to be able to put this and its subclasses into Sets, which means this must be hashable +# We can't be hashable if we have lists, dicts or anything else that isn't hashable. Dataclasses +# are hashable if we set frozen=true, however we can't do that with anything do with __post_init__ +# or even a custom __init__ method without object.__setattr__ hacks. We mark all fields bar name +# as hash=False and do unsafe_hash which should be safe enough. The alternative is custom factory +# methods that do the equivalent of __post_init__ +@dataclass(unsafe_hash=True) +class DeployableDetails(abc.ABC): + name: str = field(hash=True) + helm_key: str | None = field(default=None, hash=False) + + has_db: bool = field(default=False, hash=False) + has_image: bool | None = field(default=None, hash=False) + has_ingress: bool = field(default=True, hash=False) + has_workloads: bool = field(default=True, hash=False) + has_service_monitor: bool = field(default=True, hash=False) + + paths_consistency_noqa: tuple[str] = field(default=(), hash=False) + + def __post_init__(self): + if self.helm_key is None: + self.helm_key = self.name + if self.has_image is None: + self.has_image = self.has_workloads + + @abc.abstractmethod + def get_helm_values_fragment(self, values: dict[str, Any]) -> dict[str, Any]: + pass + + @abc.abstractmethod + def owns_manifest_named(seflf, manifest_name: str) -> bool: + pass + + @abc.abstractmethod + def should_visit_with_values( + self, if_condition: Callable[[Self], bool], ignore_uses_parent_properties: bool = False + ): + pass + + +@dataclass(unsafe_hash=True) +class SubComponentDetails(DeployableDetails): + uses_parent_properties: bool = field(default=False, hash=False) + parent_helm_key: str = field(default=None, hash=False) + + def get_helm_values_fragment(self, values: dict[str, Any]) -> dict[str, Any]: + return values.setdefault(self.parent_helm_key, {}).setdefault(self.helm_key, {}) + + def owns_manifest_named(self, manifest_name: str) -> bool: + return manifest_name.startswith(self.name) + + def should_visit_with_values( + self, if_condition: Callable[[Self], bool], ignore_uses_parent_properties: bool = False + ): + return (ignore_uses_parent_properties or not self.uses_parent_properties) and if_condition(self) + + +@dataclass(unsafe_hash=True) +class ComponentDetails(DeployableDetails): + sub_components: tuple[SubComponentDetails] = field(default=(), hash=False) + + active_component_names: tuple[str] = field(init=False, hash=False) + values_files: tuple[str] = field(init=False, hash=False) + secret_values_files: tuple[str] = field(init=False, hash=False) + + # Not available after construction + is_shared_component: InitVar[bool] = field(default=False, hash=False) + shared_component_names: InitVar[tuple[str]] = field(default=(), hash=False) + additional_values_files: InitVar[tuple[str]] = field(default=(), hash=False) + + def __post_init__( + self, + is_shared_component: bool, + shared_component_names: tuple[str], + additional_values_files: tuple[str], + ): + super().__post_init__() + + # Shared components don't have a -minimal-values.yaml + if is_shared_component: + self.active_component_names = (self.name,) + self.values_files = () + self.secret_values_files = () + return + + assert self.has_db == ("postgres" in shared_component_names) + + self.active_component_names = tuple([self.name] + list(shared_component_names)) + self.values_files = tuple([f"{self.name}-minimal-values.yaml"] + list(additional_values_files)) + + secret_values_files = [] + if "init-secrets" in shared_component_names: + secret_values_files += [ + f"{self.name}-secrets-in-helm-values.yaml", + f"{self.name}-secrets-externally-values.yaml", + ] + if "postgres" in shared_component_names: + secret_values_files += [ + f"{self.name}-postgres-secrets-in-helm-values.yaml", + f"{self.name}-postgres-secrets-externally-values.yaml", + ] + self.secret_values_files = tuple(secret_values_files) + + for sub_component in self.sub_components: + sub_component.parent_helm_key = self.helm_key + + def get_helm_values_fragment(self, values: dict[str, Any]) -> dict[str, Any]: + return values.setdefault(self.helm_key, {}) + + def owns_manifest_named(self, manifest_name: str) -> bool: + # We look at sub-components first as while they could have totally distinct names + # from their parent component, they could have have specific suffixes. If a + # sub-component owns this manifest it will claim it itself and the top-level + # component here doesn't own it. + for sub_component in self.sub_components: + if sub_component.owns_manifest_named(manifest_name): + return False + + return manifest_name.startswith(self.name) + + def should_visit_with_values( + self, if_condition: Callable[[Self], bool], ignore_uses_parent_properties: bool = False + ): + return if_condition(self) + + +all_components_details = [ + ComponentDetails( + name="init-secrets", + helm_key="initSecrets", + has_image=False, + has_ingress=False, + has_service_monitor=False, + is_shared_component=True, + ), + ComponentDetails(name="haproxy", has_ingress=False, is_shared_component=True), + ComponentDetails( + name="postgres", + has_ingress=False, + paths_consistency_noqa=("/docker-entrypoint-initdb.d",), + is_shared_component=True, + ), + ComponentDetails( + name="element-web", + helm_key="elementWeb", + has_service_monitor=False, + paths_consistency_noqa=("/etc/nginx",), + ), + ComponentDetails( + name="matrix-authentication-service", + helm_key="matrixAuthenticationService", + has_db=True, + shared_component_names=( + "init-secrets", + "postgres", + ), + ), + ComponentDetails( + name="synapse", + has_db=True, + additional_values_files=[ "synapse-worker-example-values.yaml", ], - "sub_components": { - "redis": { - "has_service_monitor": False, - }, - "checkConfigHook": {"has_service_monitor": False, "uses_parent_properties": True}, - }, - "shared_components": ["initSecrets", "haproxy", "postgres"], - }, - "wellKnownDelegation": { - "hyphened_name": "well-known", - "has_service_monitor": False, - "has_image": False, - "has_workloads": False, - "shared_components": ["haproxy"], - }, -} - - -def _enrich_components_to_test(details) -> dict[str, Any]: - _component_details = copy.deepcopy(details) - for component in details: - _component_details[component].setdefault("hyphened_name", component) - - values_files = _component_details[component].setdefault("additional_values_files", []) - values_files.append(f"{_component_details[component]['hyphened_name']}-minimal-values.yaml") - _component_details[component]["values_files"] = values_files - del _component_details[component]["additional_values_files"] - - _component_details[component].setdefault("has_ingress", True) - _component_details[component].setdefault("has_service_monitor", True) - _component_details[component].setdefault("has_workloads", True) - _component_details[component].setdefault("paths_consistency_noqa", []) - _component_details[component].setdefault("has_image", _component_details[component]["has_workloads"]) - _component_details[component].setdefault("sub_components", {}) - for sub_component in _component_details[component]["sub_components"]: - _component_details[component]["sub_components"][sub_component].setdefault("has_service_monitor", True) - _component_details[component]["sub_components"][sub_component].setdefault("uses_parent_properties", False) - - _component_details[component].setdefault("secret_values_files", []) - if "initSecrets" in _component_details[component].setdefault("shared_components", []): - _component_details[component]["secret_values_files"].append( - f"{_component_details[component]['hyphened_name']}-secrets-in-helm-values.yaml" - ) - _component_details[component]["secret_values_files"].append( - f"{_component_details[component]['hyphened_name']}-secrets-externally-values.yaml" - ) - if "postgres" in _component_details[component].setdefault("shared_components", []): - _component_details[component]["secret_values_files"].append( - f"{_component_details[component]['hyphened_name']}-postgres-secrets-in-helm-values.yaml" - ) - _component_details[component]["secret_values_files"].append( - f"{_component_details[component]['hyphened_name']}-postgres-secrets-externally-values.yaml" - ) - return _component_details - - -component_details = _enrich_components_to_test(_raw_component_details) -shared_components_details = _enrich_components_to_test(_raw_shared_components_details) - -values_files_to_components = { - values_file: component - for component, details in component_details.items() - for values_file in (details["values_files"] + details["secret_values_files"]) -} - -values_files_to_test = [ - values_file for details in component_details.values() for values_file in (details["values_files"]) + sub_components=[ + SubComponentDetails( + name="synapse-redis", + helm_key="redis", + has_ingress=False, + has_service_monitor=False, + ), + SubComponentDetails( + name="synapse-check-config-hook", + helm_key="checkConfigHook", + has_ingress=False, + has_service_monitor=False, + uses_parent_properties=True, + ), + ], + shared_component_names=["init-secrets", "haproxy", "postgres"], + ), + ComponentDetails( + name="well-known", + helm_key="wellKnownDelegation", + has_service_monitor=False, + has_workloads=False, + shared_component_names=["haproxy"], + ), ] + + +def _get_deployables_details_from_base_components_names(base_components_names: list[str]) -> tuple[DeployableDetails]: + component_names_to_details = { + component_details.name: component_details for component_details in all_components_details + } + deployables_details_in_use = set[DeployableDetails]() + for base_component_name in base_components_names: + for component_name in component_names_to_details[base_component_name].active_component_names: + component_details = component_names_to_details[component_name] + deployables_details_in_use.add(component_details) + deployables_details_in_use.update(component_details.sub_components) + return tuple(deployables_details_in_use) + + +_single_component_values_files_to_base_components_names: dict[str, list[str]] = { + values_file: [details.name] + for details in all_components_details + for values_file in (details.values_files + details.secret_values_files) +} + +_multi_component_values_files_to_base_components_names: dict[str, list[str]] = {} + + +values_files_to_deployables_details = { + values_file: _get_deployables_details_from_base_components_names(base_components_names) + for values_file, base_components_names in ( + _single_component_values_files_to_base_components_names | _multi_component_values_files_to_base_components_names + ).items() +} + +values_files_to_test = list(values_files_to_deployables_details.keys()) values_files_with_ingresses = [ values_file - for values_file, component in values_files_to_components.items() - if component_details[component]["has_ingress"] + for values_file, deployables_details in values_files_to_deployables_details.items() + if any([deployable_details.has_ingress for deployable_details in deployables_details]) ] -secrets_values_files_to_test = [ - values_file for details in component_details.values() for values_file in details["secret_values_files"] -] +_extra_secret_values_files_to_test = [] +secret_values_files_to_test = [ + values_file for details in all_components_details for values_file in details.secret_values_files +] + _extra_secret_values_files_to_test diff --git a/tests/manifests/test_basic.py b/tests/manifests/test_basic.py index 1bd984c8..259b0215 100644 --- a/tests/manifests/test_basic.py +++ b/tests/manifests/test_basic.py @@ -4,7 +4,7 @@ import pytest -from . import component_details, shared_components_details, values_files_to_test +from . import values_files_to_test @pytest.mark.parametrize("values_file", ["nothing-enabled-values.yaml"]) @@ -31,14 +31,13 @@ async def test_postgres_on_its_own_renders_nothing(values, make_templates): @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_values_file_renders_only_itself(release_name, component, templates): +async def test_values_file_renders_only_itself(release_name, deployables_details, templates): assert len(templates) > 0 - allowed_starts_with = [ - f"{release_name}-{component_details[component]['hyphened_name']}", - ] - for shared_component in component_details[component].get("shared_components", []): - allowed_starts_with.append(f"{release_name}-{shared_components_details[shared_component]['hyphened_name']}") + allowed_starts_with = [] + for deployable_details in deployables_details: + allowed_starts_with.append(f"{release_name}-{deployable_details.name}") + for template in templates: assert any(template["metadata"]["name"].startswith(allowed_start) for allowed_start in allowed_starts_with), ( f"{[template['metadata']['name']]} does not start with one of {allowed_starts_with}" diff --git a/tests/manifests/test_configs_and_mounts_consistency.py b/tests/manifests/test_configs_and_mounts_consistency.py index 30adbd23..6a4414bf 100644 --- a/tests/manifests/test_configs_and_mounts_consistency.py +++ b/tests/manifests/test_configs_and_mounts_consistency.py @@ -6,7 +6,7 @@ import re import pytest -from . import component_details, secrets_values_files_to_test, shared_components_details, values_files_to_test +from . import secret_values_files_to_test, values_files_to_test from .utils import get_or_empty @@ -130,9 +130,9 @@ def assert_exists_according_to_hook_weight(template, hook_weight, used_by): ) -@pytest.mark.parametrize("values_file", values_files_to_test + secrets_values_files_to_test) +@pytest.mark.parametrize("values_file", values_files_to_test + secret_values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_secrets_consistency(templates, other_secrets, component): +async def test_secrets_consistency(templates, other_secrets, template_to_deployable_details): """ Test to ensure that all configmaps and secrets are properly mounted and consistent across the cluster. @@ -145,6 +145,7 @@ async def test_secrets_consistency(templates, other_secrets, component): """ workloads = [t for t in templates if t["kind"] in ("Deployment", "StatefulSet", "Job")] for template in workloads: + deployable_details = template_to_deployable_details(template) # Gather all containers and initContainers from the template spec containers = template["spec"]["template"]["spec"].get("containers", []) + template["spec"]["template"][ "spec" @@ -207,11 +208,7 @@ async def test_secrets_consistency(templates, other_secrets, component): # If for some path, the configuration cannot be made explicit # we add them to the list of exceptions # For example, nginx container uses /etc/nginx natively. - if parent_path in component_details[component]["paths_consistency_noqa"] or parent_path in [ - path - for shared in component_details[component]["shared_components"] - for path in shared_components_details[shared]["paths_consistency_noqa"] - ]: + if parent_path in deployable_details.paths_consistency_noqa: continue mount_path_found = False diff --git a/tests/manifests/test_ingresses.py b/tests/manifests/test_ingresses.py index 4814e4f7..1367b976 100644 --- a/tests/manifests/test_ingresses.py +++ b/tests/manifests/test_ingresses.py @@ -2,25 +2,52 @@ # # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial +from typing import Any + import pytest -from . import component_details, values_files_to_test, values_files_with_ingresses +from . import DeployableDetails, values_files_to_test, values_files_with_ingresses +from .utils import iterate_deployables_ingress_parts @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_has_ingress(component, templates): - has_ingress = False - for template in templates: - if template["kind"] == "Ingress": - has_ingress = True +async def test_has_ingress(templates, template_to_deployable_details): + seen_deployables = set[DeployableDetails]() + seen_deployables_with_ingresses = set[DeployableDetails]() - assert has_ingress == component_details[component]["has_ingress"] + for template in templates: + deployable_details = template_to_deployable_details(template) + seen_deployables.add(deployable_details) + if template["kind"] == "Ingress": + seen_deployables_with_ingresses.add(deployable_details) + + for seen_deployable in seen_deployables: + assert seen_deployable.has_ingress == (seen_deployable in seen_deployables_with_ingresses) @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_ingress_is_expected_host(component, values, templates): +async def test_ingress_is_expected_host(deployables_details, values, templates): + def get_hosts_from_fragment(values_fragment, deployable_details): + if deployable_details.name == "well-known": + if not values_fragment.setdefault("ingress", {}).get("host"): + yield values["serverName"] + else: + yield values_fragment.setdefault("ingress", {})["host"] + else: + yield values_fragment["ingress"]["host"] + + def get_hosts(): + for deployable_details in deployables_details: + if deployable_details.has_ingress: + yield from get_hosts_from_fragment( + deployable_details.get_helm_values_fragment(values), deployable_details + ) + + expected_hosts = get_hosts() + + found_hosts = [] for template in templates: if template["kind"] == "Ingress": assert "rules" in template["spec"] @@ -28,14 +55,8 @@ async def test_ingress_is_expected_host(component, values, templates): for rule in template["spec"]["rules"]: assert "host" in rule - if component == "wellKnownDelegation": - if not values[component].setdefault("ingress", {}).get("host"): - expected_host = values["serverName"] - else: - expected_host = values[component].setdefault("ingress", {})["host"] - else: - expected_host = values[component]["ingress"]["host"] - assert rule["host"] == expected_host + found_hosts.append(rule["host"]) + assert set(found_hosts) == set(expected_hosts) @pytest.mark.parametrize("values_file", values_files_with_ingresses) @@ -66,10 +87,13 @@ async def test_no_ingress_annotations_by_default(templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_renders_component_ingress_annotations(component, values, make_templates): - values[component].setdefault("ingress", {})["annotations"] = { - "component": "set", - } +async def test_renders_component_ingress_annotations(deployables_details, values, make_templates): + def set_annotations(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["annotations"] = { + "component": "set", + } + + iterate_deployables_ingress_parts(deployables_details, values, set_annotations) for template in await make_templates(values): if template["kind"] == "Ingress": @@ -94,12 +118,15 @@ async def test_renders_global_ingress_annotations(values, make_templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_merges_global_and_component_ingress_annotations(component, values, make_templates): - values[component].setdefault("ingress", {})["annotations"] = { - "component": "set", - "merged": "from_component", - "global": None, - } +async def test_merges_global_and_component_ingress_annotations(deployables_details, values, make_templates): + def set_annotations(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["annotations"] = { + "component": "set", + "merged": "from_component", + "global": None, + } + + iterate_deployables_ingress_parts(deployables_details, values, set_annotations) values.setdefault("ingress", {})["annotations"] = { "global": "set", "merged": "from_global", @@ -130,8 +157,11 @@ async def test_no_ingress_tlsSecret_by_default(templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_uses_component_ingress_tlsSecret(component, values, make_templates): - values[component].setdefault("ingress", {})["tlsSecret"] = "component" +async def test_uses_component_ingress_tlsSecret(deployables_details, values, make_templates): + def set_tls_secret(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["tlsSecret"] = "component" + + iterate_deployables_ingress_parts(deployables_details, values, set_tls_secret) for template in await make_templates(values): if template["kind"] == "Ingress": @@ -158,8 +188,11 @@ async def test_uses_global_ingress_tlsSecret(values, make_templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_component_ingress_tlsSecret_beats_global(component, values, make_templates): - values[component].setdefault("ingress", {})["tlsSecret"] = "component" +async def test_component_ingress_tlsSecret_beats_global(deployables_details, values, make_templates): + def set_tls_secret(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["tlsSecret"] = "component" + + iterate_deployables_ingress_parts(deployables_details, values, set_tls_secret) values.setdefault("ingress", {})["tlsSecret"] = "global" for template in await make_templates(values): @@ -181,8 +214,11 @@ async def test_no_ingressClassName_by_default(templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_uses_component_ingressClassName(component, values, make_templates): - values[component].setdefault("ingress", {})["className"] = "component" +async def test_uses_component_ingressClassName(deployables_details, values, make_templates): + def set_ingress_className(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["className"] = "component" + + iterate_deployables_ingress_parts(deployables_details, values, set_ingress_className) for template in await make_templates(values): if template["kind"] == "Ingress": @@ -203,8 +239,11 @@ async def test_uses_global_ingressClassName(values, make_templates): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_component_ingressClassName_beats_global(component, values, make_templates): - values[component].setdefault("ingress", {})["className"] = "component" +async def test_component_ingressClassName_beats_global(deployables_details, values, make_templates): + def set_ingress_className(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["className"] = "component" + + iterate_deployables_ingress_parts(deployables_details, values, set_ingress_className) values.setdefault("ingress", {})["className"] = "global" for template in await make_templates(values): @@ -277,8 +316,11 @@ async def test_ingress_certManager_issuer(make_templates, values): @pytest.mark.parametrize("values_file", values_files_with_ingresses) @pytest.mark.asyncio_cooperative -async def test_component_ingress_tlsSecret_beats_certManager(component, values, make_templates): - values[component].setdefault("ingress", {})["tlsSecret"] = "component" +async def test_component_ingress_tlsSecret_beats_certManager(deployables_details, values, make_templates): + def set_tls_secret(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + values_fragment.setdefault("ingress", {})["tlsSecret"] = "component" + + iterate_deployables_ingress_parts(deployables_details, values, set_tls_secret) values.setdefault("certManager", {})["issuer"] = "issuer-name" for template in await make_templates(values): diff --git a/tests/manifests/test_labels.py b/tests/manifests/test_labels.py index abb21c01..8cc7b91f 100644 --- a/tests/manifests/test_labels.py +++ b/tests/manifests/test_labels.py @@ -6,7 +6,7 @@ from hashlib import sha1 import pytest -from . import component_details, secrets_values_files_to_test, values_files_to_test +from . import secret_values_files_to_test, values_files_to_test @pytest.mark.parametrize("values_file", values_files_to_test) @@ -52,32 +52,33 @@ async def test_templates_have_expected_labels(release_name, templates): ) -@pytest.mark.parametrize("values_file", secrets_values_files_to_test) +@pytest.mark.parametrize("values_file", secret_values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_templates_have_postgres_hash_label(release_name, component, templates, values): +async def test_templates_have_postgres_hash_label(release_name, templates, values, template_to_deployable_details): for template in templates: if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - if not template["metadata"]["name"].endswith(component_details[component].get("hyphened_name", component)): - continue labels = template["spec"]["template"]["metadata"]["labels"] - if "postgres" in component_details[component].get("shared_components", []): - assert "k8s.element.io/postgresPasswordHash" in labels, ( - f"{id} does not have postgres password hash label" - ) - if values[component].get("postgres", {}).get("password", {}).get("value", None): - expected = values[component]["postgres"]["password"]["value"] - elif values[component].get("postgres", {}).get("password", {}).get("secret", None): - secret_name = values[component]["postgres"]["password"]["secret"] - expected = f"{secret_name}-{values[component]['postgres']['password']['secretKey']}" - elif values["postgres"].get("essPasswords", {}).get(component, {}).get("value", None): - expected = values["postgres"]["essPasswords"][component]["value"] - elif values["postgres"].get("essPasswords", {}).get(component, {}).get("secret", None): - secret_name = values["postgres"]["essPasswords"][component]["secret"] - expected = f"{secret_name}-{values['postgres']['essPasswords'][component]['secretKey']}" - else: - expected = f"{release_name}-generated" - expected = expected.replace("{{ $.Release.Name }}", release_name) - assert labels["k8s.element.io/postgresPasswordHash"] == sha1(expected.encode()).hexdigest(), ( - f"{id} has incorrect postgres password hash, expect {expected} hashed as sha1" - ) + deployable_details = template_to_deployable_details(template) + if not deployable_details.has_db: + continue + + assert "k8s.element.io/postgresPasswordHash" in labels, f"{id} does not have postgres password hash label" + helm_key = deployable_details.helm_key + values_fragment = deployable_details.get_helm_values_fragment(values) + if values_fragment.get("postgres", {}).get("password", {}).get("value", None): + expected = deployable_details.get_helm_values_fragment(values)["postgres"]["password"]["value"] + elif values_fragment.get("postgres", {}).get("password", {}).get("secret", None): + secret_name = deployable_details.get_helm_values_fragment(values)["postgres"]["password"]["secret"] + expected = f"{secret_name}-{values_fragment['postgres']['password']['secretKey']}" + elif values["postgres"].get("essPasswords", {}).get(helm_key, {}).get("value", None): + expected = values["postgres"]["essPasswords"][helm_key]["value"] + elif values["postgres"].get("essPasswords", {}).get(helm_key, {}).get("secret", None): + secret_name = values["postgres"]["essPasswords"][helm_key]["secret"] + expected = f"{secret_name}-{values['postgres']['essPasswords'][helm_key]['secretKey']}" + else: + expected = f"{release_name}-generated" + expected = expected.replace("{{ $.Release.Name }}", release_name) + assert labels["k8s.element.io/postgresPasswordHash"] == sha1(expected.encode()).hexdigest(), ( + f"{id} has incorrect postgres password hash, expect {expected} hashed as sha1" + ) diff --git a/tests/manifests/test_manifest_test_infrastructure.py b/tests/manifests/test_manifest_test_infrastructure.py index c13c57b7..46518d50 100644 --- a/tests/manifests/test_manifest_test_infrastructure.py +++ b/tests/manifests/test_manifest_test_infrastructure.py @@ -6,25 +6,23 @@ from pathlib import Path import pytest -from . import component_details, secrets_values_files_to_test, shared_components_details, values_files_to_test +from . import all_components_details, secret_values_files_to_test, values_files_to_test def test_all_components_covered(): - expected_folders = [details["hyphened_name"] for details in component_details.values()] + expected_folders = [details.name for details in all_components_details] templates_folder = Path(__file__).parent.parent.parent / Path("charts/matrix-stack/templates") for contents in templates_folder.iterdir(): if not contents.is_dir(): continue - if contents.name in ("ess-library",) + tuple( - k if not v["hyphened_name"] else v["hyphened_name"] for k, v in shared_components_details.items() - ): + if contents.name in ("ess-library",): continue assert contents.name in expected_folders -@pytest.mark.parametrize("values_file", values_files_to_test + secrets_values_files_to_test) +@pytest.mark.parametrize("values_file", values_files_to_test + secret_values_files_to_test) @pytest.mark.asyncio_cooperative def test_component_has_values_file(values_file): ci_folder = Path(__file__).parent.parent.parent / Path("charts/matrix-stack/ci") diff --git a/tests/manifests/test_pod_pull_secrets.py b/tests/manifests/test_pod_pull_secrets.py index 7e8befbd..5c83a382 100644 --- a/tests/manifests/test_pod_pull_secrets.py +++ b/tests/manifests/test_pod_pull_secrets.py @@ -5,7 +5,7 @@ import pytest from . import values_files_to_test -from .utils import iterate_component_image_parts +from .utils import iterate_deployables_image_parts @pytest.mark.parametrize("values_file", values_files_to_test) @@ -28,15 +28,17 @@ async def test_sets_global_pull_secrets(values, make_templates): @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_local_pull_secrets(component, values, base_values, make_templates): +async def test_local_pull_secrets(deployables_details, values, base_values, make_templates): values["imagePullSecrets"] = [ {"name": "global-secret"}, ] values.setdefault("matrixTools", {}).setdefault("image", {})["pullSecrets"] = [{"name": "matrix-tools-secret"}] - iterate_component_image_parts( - component, + iterate_deployables_image_parts( + deployables_details, values, - lambda workload, values: workload.setdefault("image", {"pullSecrets": [{"name": "local-secret"}]}), + lambda values_fragment, deployable_details: values_fragment.setdefault( + "image", {"pullSecrets": [{"name": "local-secret"}]} + ), ) for template in await make_templates(values): diff --git a/tests/manifests/test_pod_securityContext.py b/tests/manifests/test_pod_securityContext.py index ec25de0e..15ff67a5 100644 --- a/tests/manifests/test_pod_securityContext.py +++ b/tests/manifests/test_pod_securityContext.py @@ -5,7 +5,7 @@ import pytest from . import values_files_to_test -from .utils import iterate_component_workload_parts +from .utils import iterate_deployables_workload_parts @pytest.mark.parametrize("values_file", values_files_to_test) @@ -38,11 +38,11 @@ async def test_sets_nonRoot_uids_gids_in_pod_securityContext_by_default(template @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_can_nuke_pod_securityContext_ids(component, values, make_templates): - iterate_component_workload_parts( - component, +async def test_can_nuke_pod_securityContext_ids(deployables_details, values, make_templates): + iterate_deployables_workload_parts( + deployables_details, values, - lambda workload, values: workload.setdefault( + lambda values_fragment, deployable_details: values_fragment.setdefault( "podSecurityContext", {"runAsUser": None, "runAsGroup": None, "fsGroup": None} ), ) @@ -85,9 +85,13 @@ async def test_sets_seccompProfile_in_pod_securityContext_by_default(templates): @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_can_nuke_pod_securityContext_seccompProfile(component, values, make_templates): - iterate_component_workload_parts( - component, values, lambda workload, values: workload.setdefault("podSecurityContext", {"seccompProfile": None}) +async def test_can_nuke_pod_securityContext_seccompProfile(deployables_details, values, make_templates): + iterate_deployables_workload_parts( + deployables_details, + values, + lambda values_fragment, deployable_details: values_fragment.setdefault( + "podSecurityContext", {"seccompProfile": None} + ), ) for template in await make_templates(values): diff --git a/tests/manifests/test_service_monitors.py b/tests/manifests/test_service_monitors.py index 1e15c595..4fa903e4 100644 --- a/tests/manifests/test_service_monitors.py +++ b/tests/manifests/test_service_monitors.py @@ -7,7 +7,8 @@ from typing import Any import pytest -from . import component_details, shared_components_details, values_files_to_test +from . import DeployableDetails, values_files_to_test +from .utils import iterate_deployables_parts def selector_match(labels: dict[str, str], selector: dict[str, str]) -> bool: @@ -63,47 +64,39 @@ def workload_ids_monitored(templates: Iterator[Any]) -> set[str]: @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_service_monitored_as_appropriate(component, values: dict, make_templates): +async def test_service_monitored_as_appropriate( + deployables_details, values: dict, make_templates, template_to_deployable_details +): # If the component and all its sub-components don't have ServiceMonitors we should assert that - if ( - not component_details[component]["has_service_monitor"] - and not any( - [sub_component["has_service_monitor"] for sub_component in component_details[component]["sub_components"]] - ) - and not any( - shared_components_details[shared_component].get("has_service_monitor", True) - for shared_component in component_details[component].get("shared_components", []) - ) - ): + if not any([deployable_details.has_service_monitor for deployable_details in deployables_details]): for template in await make_templates(values): - assert template["kind"] != "ServiceMonitor", f"{component} unexpectedly has a ServiceMonitor: {template=}" + deployable_details = template_to_deployable_details(template) + assert template["kind"] != "ServiceMonitor", ( + f"{deployable_details.name} unexpectedly has a ServiceMonitor: {template=}" + ) return - if component_details[component]["has_service_monitor"]: - # We disable rendering of all service monitors as they're default enabled - values[component].setdefault("serviceMonitors", {}).setdefault("enabled", False) - for sub_component in component_details[component]["sub_components"]: - # Subcomponents that don't have service monitors don't need to be tested - if component_details[component]["sub_components"][sub_component]["has_service_monitor"]: - values[component].setdefault(sub_component, {}).setdefault("serviceMonitors", {}).setdefault( - "enabled", False - ) + def disable_service_monitor(values_fragment: dict[str, Any], deployable_details: DeployableDetails): + if deployable_details.has_service_monitor: + values_fragment.setdefault("serviceMonitors", {}).setdefault("enabled", False) else: - values[component].setdefault(sub_component, {}).setdefault("labels", {}).setdefault( - "servicemonitor", "none" - ) - for shared_component in component_details[component].get("shared_components", []): - if shared_components_details[shared_component]["has_service_monitor"]: - values.setdefault(shared_component, {}).setdefault("serviceMonitors", {}).setdefault("enabled", False) - else: - values.setdefault(shared_component, {}).setdefault("labels", {}).setdefault("servicemonitor", "none") + values_fragment.setdefault("labels", {}).setdefault("servicemonitor", "none") + + iterate_deployables_parts( + deployables_details, + values, + disable_service_monitor, + lambda deployable_details: True, + ignore_uses_parent_properties=True, + ) # We should now have no ServiceMonitors rendered workloads_to_cover = set() for template in await make_templates(values): + deployable_details = template_to_deployable_details(template) assert template["kind"] != "ServiceMonitor", ( - f"{component} unexpectedly has a ServiceMonitor when all are turned off" + f"{deployable_details.name} unexpectedly has a ServiceMonitor when all are turned off" ) if ( template["kind"] in ["Deployment", "StatefulSet", "Job"] @@ -113,33 +106,17 @@ async def test_service_monitored_as_appropriate(component, values: dict, make_te seen_covered_workloads = set[str]() - if component_details[component]["has_service_monitor"]: - # We then render each component one by one, and extract its service monitor - values[component]["serviceMonitors"]["enabled"] = True - seen_covered_workloads.update(workload_ids_monitored(await make_templates(values))) - values[component]["serviceMonitors"]["enabled"] = False - - for sub_component in component_details[component]["sub_components"]: - # Subcomponents that don't have service monitors don't need to be tested - if not component_details[component]["sub_components"][sub_component]["has_service_monitor"]: + for deployable_details in deployables_details: + if not deployable_details.has_service_monitor: continue + # We then render each component & sub-component one by one, and extract its service monitor + # The rendered ServiceMonitors should not cover any workloads that have already been covered + deployable_details.get_helm_values_fragment(values)["serviceMonitors"]["enabled"] = True - values[component][sub_component]["serviceMonitors"]["enabled"] = True + new_monitored_workload_ids = workload_ids_monitored(await make_templates(values)) + assert seen_covered_workloads.intersection(new_monitored_workload_ids) == set() + seen_covered_workloads.update(new_monitored_workload_ids) - # Individual components and subcomponents should not share any ServiceMonitors - sub_component_workload_ids = workload_ids_monitored(await make_templates(values)) - assert seen_covered_workloads.intersection(sub_component_workload_ids) == set() - seen_covered_workloads.update(sub_component_workload_ids) - - values[component][sub_component]["serviceMonitors"]["enabled"] = False - - for shared_component in component_details[component]["shared_components"]: - if shared_components_details[shared_component]["has_service_monitor"]: - values.setdefault(shared_component, {})["serviceMonitors"]["enabled"] = True - # Shared components should not share any ServiceMonitors - shared_component_workload_ids = workload_ids_monitored(await make_templates(values)) - assert seen_covered_workloads.intersection(shared_component_workload_ids) == set() - seen_covered_workloads.update(shared_component_workload_ids) - values[shared_component]["serviceMonitors"]["enabled"] = False + deployable_details.get_helm_values_fragment(values)["serviceMonitors"]["enabled"] = False assert seen_covered_workloads.symmetric_difference(workloads_to_cover) == set() diff --git a/tests/manifests/test_serviceaccounts.py b/tests/manifests/test_serviceaccounts.py index 726f7b85..1db86456 100644 --- a/tests/manifests/test_serviceaccounts.py +++ b/tests/manifests/test_serviceaccounts.py @@ -6,8 +6,8 @@ import copy import pytest -from . import component_details, values_files_to_test -from .utils import iterate_component_workload_parts +from . import values_files_to_test +from .utils import iterate_deployables_workload_parts @pytest.mark.parametrize("values_file", values_files_to_test) @@ -57,12 +57,14 @@ async def test_uses_serviceaccount_named_as_per_pod_controller_by_default(templa @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_uses_serviceaccount_named_as_values_if_specified(component, values, make_templates): - def service_account_name(workload, values): - workload.setdefault("serviceAccount", {}).setdefault("name", f"{component}-pytest") - workload.setdefault("labels", {}).setdefault("expected.name", f"{component}-pytest") +async def test_uses_serviceaccount_named_as_values_if_specified(deployables_details, values, make_templates): + def service_account_name(values_fragment, deployable_details): + values_fragment.setdefault("serviceAccount", {}).setdefault("name", f"{deployable_details.name}-pytest") + values_fragment.setdefault("labels", {}).setdefault("expected.name", f"{deployable_details.name}-pytest") - iterate_component_workload_parts(component, values, service_account_name, ignore_uses_parent_properties=True) + iterate_deployables_workload_parts( + deployables_details, values, service_account_name, ignore_uses_parent_properties=True + ) workloads_by_id = {} serviceaccount_names = [] @@ -87,42 +89,41 @@ async def test_uses_serviceaccount_named_as_values_if_specified(component, value @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_does_not_create_serviceaccounts_if_configured_not_to(component, values, make_templates): - def disable_service_account(workload, values): - values.setdefault(workload, {}).setdefault("serviceAccount", {}).setdefault("create", False) - values.setdefault(workload, {}).setdefault("labels", {}).setdefault("serviceAccount", "none") +async def test_does_not_create_serviceaccounts_if_configured_not_to(deployables_details, values, make_templates): + def disable_service_account(values_fragment): + values_fragment.setdefault("serviceAccount", {}).setdefault("create", False) + values_fragment.setdefault("labels", {}).setdefault("serviceAccount", "none") - if component_details[component]["has_workloads"]: - for sub_component in [""] + list(component_details[component]["sub_components"].keys()): - sub_component_values = copy.deepcopy(values) - if sub_component == "": - disable_service_account(component, sub_component_values) - else: - disable_service_account(sub_component, sub_component_values[component]) + for deployable_details in deployables_details: + if not deployable_details.has_workloads: + continue - workloads_by_id = {} - serviceaccount_names = set() - covered_serviceaccount_names = set() - for template in await make_templates(sub_component_values): - if template["kind"] in ["Deployment", "StatefulSet", "Job"]: - id_suffix = f" (for {sub_component})" if sub_component != "" else "" - workloads_by_id[f"{template['kind']}/{template['metadata']['name']}{id_suffix}"] = template - elif template["kind"] == "ServiceAccount": - serviceaccount_names.add(template["metadata"]["name"]) + values_to_modify = copy.deepcopy(values) + disable_service_account(deployable_details.get_helm_values_fragment(values)) - for id, template in workloads_by_id.items(): - assert "serviceAccountName" in template["spec"]["template"]["spec"], ( - f"{id} does not set an explicit ServiceAccount" - ) + workloads_by_id = {} + serviceaccount_names = set() + covered_serviceaccount_names = set() + for template in await make_templates(values_to_modify): + if template["kind"] in ["Deployment", "StatefulSet", "Job"]: + id_suffix = f" (for {deployable_details.name})" + workloads_by_id[f"{template['kind']}/{template['metadata']['name']}{id_suffix}"] = template + elif template["kind"] == "ServiceAccount": + serviceaccount_names.add(template["metadata"]["name"]) - serviceaccount_name = template["spec"]["template"]["spec"]["serviceAccountName"] - if template["metadata"]["labels"].get("serviceAccount", "some") == "none": - assert serviceaccount_name not in serviceaccount_names, ( - f"{id} specified an existing ServiceAccount: {serviceaccount_name}" - ) - else: - covered_serviceaccount_names.add(serviceaccount_name) - - assert serviceaccount_names == covered_serviceaccount_names, ( - f"{id} created ServiceAccounts that it shouldn't have" + for id, template in workloads_by_id.items(): + assert "serviceAccountName" in template["spec"]["template"]["spec"], ( + f"{id} does not set an explicit ServiceAccount" ) + + serviceaccount_name = template["spec"]["template"]["spec"]["serviceAccountName"] + if template["metadata"]["labels"].get("serviceAccount", "some") == "none": + assert serviceaccount_name not in serviceaccount_names, ( + f"{id} specified an existing ServiceAccount: {serviceaccount_name}" + ) + else: + covered_serviceaccount_names.add(serviceaccount_name) + + assert serviceaccount_names == covered_serviceaccount_names, ( + f"{id} created ServiceAccounts that it shouldn't have" + ) diff --git a/tests/manifests/test_tolerations.py b/tests/manifests/test_tolerations.py index 97362c57..0afec666 100644 --- a/tests/manifests/test_tolerations.py +++ b/tests/manifests/test_tolerations.py @@ -5,7 +5,7 @@ import pytest from . import values_files_to_test -from .utils import iterate_component_workload_parts +from .utils import iterate_deployables_workload_parts specific_toleration = { "key": "component", @@ -36,9 +36,13 @@ async def test_no_tolerations_by_default(templates): @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_all_components_and_sub_components_render_tolerations(component, values, make_templates): - iterate_component_workload_parts( - component, values, lambda workload, values: workload.setdefault("tolerations", []).append(specific_toleration) +async def test_all_components_and_sub_components_render_tolerations(deployables_details, values, make_templates): + iterate_deployables_workload_parts( + deployables_details, + values, + lambda values_fragment, deployable_details: values_fragment.setdefault("tolerations", []).append( + specific_toleration + ), ) for template in await make_templates(values): @@ -68,9 +72,13 @@ async def test_global_tolerations_render(values, make_templates): @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative -async def test_merges_global_and_specific_tolerations(component, values, make_templates): - iterate_component_workload_parts( - component, values, lambda workload, values: workload.setdefault("tolerations", []).append(specific_toleration) +async def test_merges_global_and_specific_tolerations(deployables_details, values, make_templates): + iterate_deployables_workload_parts( + deployables_details, + values, + lambda values_fragment, deployable_details: values_fragment.setdefault("tolerations", []).append( + specific_toleration + ), ) # Add twice for uniqueness check. There's no 'overwriting' as if it isn't the same toleration, it gets kept diff --git a/tests/manifests/test_volumes_mounts.py b/tests/manifests/test_volumes_mounts.py index d7615852..b2a5119b 100644 --- a/tests/manifests/test_volumes_mounts.py +++ b/tests/manifests/test_volumes_mounts.py @@ -4,10 +4,10 @@ import pytest -from . import secrets_values_files_to_test, values_files_to_test +from . import secret_values_files_to_test, values_files_to_test -@pytest.mark.parametrize("values_file", values_files_to_test + secrets_values_files_to_test) +@pytest.mark.parametrize("values_file", values_files_to_test + secret_values_files_to_test) @pytest.mark.asyncio_cooperative async def test_volumes_mounts_exists(templates, other_secrets): configmaps_names = [t["metadata"]["name"] for t in templates if t["kind"] == "ConfigMap"] diff --git a/tests/manifests/utils.py b/tests/manifests/utils.py index e6d22859..612dea52 100644 --- a/tests/manifests/utils.py +++ b/tests/manifests/utils.py @@ -1,4 +1,4 @@ -# Copyright 2024 New Vector Ltd +# Copyright 2024-2025 New Vector Ltd # # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial @@ -7,13 +7,13 @@ import random import string from collections.abc import Iterator from pathlib import Path -from typing import Any +from typing import Any, Callable import pyhelm3 import pytest import yaml -from . import component_details, shared_components_details, values_files_to_components +from . import DeployableDetails, values_files_to_deployables_details @pytest.fixture(scope="session") @@ -32,8 +32,8 @@ async def chart(helm_client: pyhelm3.Client): @pytest.fixture(scope="function") -def component(values_file): - return values_files_to_components[values_file] +def deployables_details(values_file) -> tuple[DeployableDetails]: + return values_files_to_deployables_details[values_file] @pytest.fixture(scope="session") @@ -184,26 +184,94 @@ def make_templates(chart: pyhelm3.Chart, release_name: str): return _make_templates -def iterate_component_parts(component, values, setter, if_condition, ignore_uses_parent_properties): - if component_details[component][if_condition]: - setter(values[component], values) - for sub_component in component_details[component]["sub_components"]: - if ( - ignore_uses_parent_properties - or not component_details[component]["sub_components"][sub_component]["uses_parent_properties"] - ): - setter(values[component].setdefault(sub_component, {}), values) - for shared_component in component_details[component].get("shared_components", []): - if shared_components_details[shared_component][if_condition]: - setter(values.setdefault(shared_component, {}), values) +def iterate_deployables_parts( + deployables_details: tuple[DeployableDetails], + values: dict[str, Any], + visitor: Callable[[dict[str, Any], DeployableDetails], None], + if_condition: Callable[[DeployableDetails], bool], + ignore_uses_parent_properties: bool = False, +): + for deployable_details in deployables_details: + if deployable_details.should_visit_with_values(if_condition, ignore_uses_parent_properties): + visitor(deployable_details.get_helm_values_fragment(values), deployable_details) -def iterate_component_workload_parts(component, values, setter, ignore_uses_parent_properties=False): - iterate_component_parts(component, values, setter, "has_workloads", ignore_uses_parent_properties) +def iterate_deployables_workload_parts( + deployables_details: tuple[DeployableDetails], + values: dict[str, Any], + visitor: Callable[[dict[str, Any], DeployableDetails], None], + ignore_uses_parent_properties: bool = False, +): + iterate_deployables_parts( + deployables_details, + values, + visitor, + lambda deployable_details: deployable_details.has_workloads, + ignore_uses_parent_properties, + ) -def iterate_component_image_parts(component, values, setter): - iterate_component_parts(component, values, setter, "has_image", False) +def iterate_deployables_image_parts( + deployables_details: tuple[DeployableDetails], + values: dict[str, Any], + visitor: Callable[[dict[str, Any], DeployableDetails], None], +): + iterate_deployables_parts( + deployables_details, values, visitor, lambda deployable_details: deployable_details.has_image + ) + + +def iterate_deployables_ingress_parts( + deployables_details: tuple[DeployableDetails], + values: dict[str, Any], + visitor: Callable[[dict[str, Any], DeployableDetails], None], +): + iterate_deployables_parts( + deployables_details, values, visitor, lambda deployable_details: deployable_details.has_ingress + ) + + +def iterate_deployables_service_monitor_parts( + deployables_details: tuple[DeployableDetails], + values: dict[str, Any], + visitor: Callable[[dict[str, Any], DeployableDetails], None], +): + iterate_deployables_parts( + deployables_details, values, visitor, lambda deployable_details: deployable_details.has_service_monitor + ) + + +@pytest.fixture +def template_to_deployable_details(deployables_details: tuple[DeployableDetails]): + def _template_to_deployable_details(template: dict[str, Any]) -> DeployableDetails: + # As per test_labels this doesn't have the release_name prefixed to it + manifest_name: str = template["metadata"]["labels"]["app.kubernetes.io/name"] + + match = None + for deployable_details in deployables_details: + # We name the various DeployableDetails to match the name the chart should use for + # the manifest name and thus the app.kubernetes.io/name label above. e.g. A manifest + # belonging to Synapse should be named `-synapse(-)`. + # + # When we find a matching (sub-)component we ensure that there has been no other + # match (with the exception of matching both a sub-component and its parent) as + # otherwise we have no way of identifying the associated DeployableDeploys and + # thus which parts of the values files need manipulating for this deployable. + if deployable_details.owns_manifest_named(manifest_name): + assert match is None, ( + f"{template_id(template)} could belong to at least 2 (sub-)components: " + f"{match.name} and {deployable_details.name}" + ) + match = deployable_details + + assert match is not None, f"{template_id(template)} can't be linked to any (sub-)component" + return match + + return _template_to_deployable_details + + +def template_id(template: dict[str, Any]) -> str: + return f"{template['kind']}/{template['metadata']['name']}" def get_or_empty(d, key):