diff --git a/scripts/execution_report_heartbeat.py b/scripts/execution_report_heartbeat.py index 8f784cb..377df6c 100644 --- a/scripts/execution_report_heartbeat.py +++ b/scripts/execution_report_heartbeat.py @@ -91,9 +91,12 @@ def _base_report_uris() -> list[str]: def _load_required_services() -> list[str]: + explicit_services = _split_values(os.environ.get("RUNTIME_HEARTBEAT_REQUIRED_SERVICES")) + if explicit_services: + return _unique_values(explicit_services) + services = [] for name in ( - "RUNTIME_HEARTBEAT_REQUIRED_SERVICES", "CLOUD_RUN_SERVICES", "CLOUD_RUN_SERVICE", ): @@ -124,12 +127,16 @@ def _load_required_services() -> list[str]: except json.JSONDecodeError: pass + return _unique_values(services) + + +def _unique_values(values: list[str]) -> list[str]: seen = set() unique = [] - for service in services: - if service not in seen: - seen.add(service) - unique.append(service) + for value in values: + if value not in seen: + seen.add(value) + unique.append(value) return unique diff --git a/tests/test_execution_report_heartbeat.py b/tests/test_execution_report_heartbeat.py new file mode 100644 index 0000000..046b5c8 --- /dev/null +++ b/tests/test_execution_report_heartbeat.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +import json + +from scripts import execution_report_heartbeat as heartbeat + + +def test_explicit_required_services_override_target_derived_services(monkeypatch): + monkeypatch.setenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", "svc-daily-a,svc-daily-b") + monkeypatch.setenv( + "CLOUD_RUN_SERVICE_TARGETS_JSON", + json.dumps( + { + "targets": [ + {"service": "svc-daily-a"}, + {"service": "svc-monthly"}, + ] + } + ), + ) + + assert heartbeat._load_required_services() == ["svc-daily-a", "svc-daily-b"] + + +def test_required_services_fall_back_to_cloud_run_targets(monkeypatch): + monkeypatch.delenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", raising=False) + monkeypatch.setenv( + "CLOUD_RUN_SERVICE_TARGETS_JSON", + json.dumps( + { + "targets": [ + {"service": "svc-a"}, + {"runtime_target": {"service_name": "svc-b"}}, + {"service": "svc-a"}, + ] + } + ), + ) + + assert heartbeat._load_required_services() == ["svc-a", "svc-b"]