From e5e454c085aab9e4f15a3c89810cac48ff5f53f2 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Wed, 24 Jul 2024 13:11:30 +1200 Subject: [PATCH] Strengthen the consistency check: the Check must be in the correct Container. --- scenario/consistency_checker.py | 11 ++++++++--- tests/test_consistency_checker.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/scenario/consistency_checker.py b/scenario/consistency_checker.py index 8b7e9f4e..fcf93010 100644 --- a/scenario/consistency_checker.py +++ b/scenario/consistency_checker.py @@ -560,7 +560,9 @@ def check_containers_consistency( meta_containers = list(map(normalize_name, meta.get("containers", {}))) state_containers = [normalize_name(c.name) for c in state.containers] all_notices = {notice.id for c in state.containers for notice in c.notices} - all_checks = {check.name for c in state.containers for check in c.check_infos} + all_checks = { + (c.name, check.name) for c in state.containers for check in c.check_infos + } errors = [] # it's fine if you have containers in meta that are not in state.containers (yet), but it's @@ -585,10 +587,13 @@ def check_containers_consistency( f"the event being processed concerns notice {event.notice!r}, but that " "notice is not in any of the containers present in the state.", ) - if event.check_info and event.check_info.name not in all_checks: + if ( + event.check_info + and (evt_container_name, event.check_info.name) not in all_checks + ): errors.append( f"the event being processed concerns check {event.check_info.name}, but that " - "check is not in any of the containers present in the state.", + "check is not the {evt_container_name} container.", ) # - a container in state.containers is not in meta.containers diff --git a/tests/test_consistency_checker.py b/tests/test_consistency_checker.py index 8707bb93..01e0f37d 100644 --- a/tests/test_consistency_checker.py +++ b/tests/test_consistency_checker.py @@ -110,6 +110,21 @@ def test_workload_event_without_container(): ), _CharmSpec(MyCharm, {"containers": {"foo": {}}}), ) + # Ensure the check is in the correct container. + assert_inconsistent( + State(containers={Container("foo", check_infos={check}), Container("bar")}), + _Event( + "foo-pebble-check-recovered", container=Container("bar"), check_info=check + ), + _CharmSpec(MyCharm, {"containers": {"foo": {}, "bar": {}}}), + ) + assert_inconsistent( + State(containers={Container("foo", check_infos={check}), Container("bar")}), + _Event( + "bar-pebble-check-recovered", container=Container("bar"), check_info=check + ), + _CharmSpec(MyCharm, {"containers": {"foo": {}, "bar": {}}}), + ) def test_container_meta_mismatch():