diff --git a/scenario/consistency_checker.py b/scenario/consistency_checker.py index 52d62649..e360a5c7 100644 --- a/scenario/consistency_checker.py +++ b/scenario/consistency_checker.py @@ -123,6 +123,9 @@ def check_event_consistency( if event._is_action_event: _check_action_event(charm_spec, event, errors, warnings) + if event._is_storage_event: + _check_storage_event(charm_spec, event, errors, warnings) + return Results(errors, warnings) @@ -190,6 +193,30 @@ def _check_action_event( _check_action_param_types(charm_spec, action, errors, warnings) +def _check_storage_event( + charm_spec: _CharmSpec, + event: "Event", + errors: List[str], + warnings: List[str], # noqa: U100 +): + storage = event.storage + if not storage: + errors.append( + "cannot construct a storage event without the Storage instance. " + "Please pass one.", + ) + elif not event.name.startswith(normalize_name(storage.name)): + errors.append( + f"storage event should start with storage name. {event.name} does " + f"not start with {storage.name}.", + ) + elif storage.name not in charm_spec.meta["storage"]: + errors.append( + f"storage event {event.name} refers to storage {storage.name} " + f"which is not declared in the charm metadata (metadata.yaml) under 'storage'.", + ) + + def _check_action_param_types( charm_spec: _CharmSpec, action: Action, diff --git a/tests/test_consistency_checker.py b/tests/test_consistency_checker.py index e26fb3ad..513e0ffe 100644 --- a/tests/test_consistency_checker.py +++ b/tests/test_consistency_checker.py @@ -12,6 +12,7 @@ Relation, Secret, State, + Storage, SubordinateRelation, _CharmSpec, ) @@ -342,7 +343,7 @@ def test_relation_without_endpoint(): relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=1)] ), Event("start"), - _CharmSpec(MyCharm, meta={}), + _CharmSpec(MyCharm, meta={"name": "charlemagne"}), ) assert_consistent( @@ -357,3 +358,27 @@ def test_relation_without_endpoint(): }, ), ) + + +def test_storage_event(): + storage = Storage("foo") + assert_inconsistent( + State(storage=[storage]), + Event("foo-storage-attached"), + _CharmSpec(MyCharm, meta={"name": "rupert"}), + ) + assert_inconsistent( + State(storage=[storage]), + Event("foo-storage-attached"), + _CharmSpec( + MyCharm, meta={"name": "rupert", "storage": {"foo": {"type": "filesystem"}}} + ), + ) + + assert_consistent( + State(storage=[storage]), + storage.attached_event, + _CharmSpec( + MyCharm, meta={"name": "rupert", "storage": {"foo": {"type": "filesystem"}}} + ), + )