Skip to content

Commit

Permalink
consistency checks
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Oct 13, 2023
1 parent c71faea commit e2bd314
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
27 changes: 27 additions & 0 deletions scenario/consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion tests/test_consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Relation,
Secret,
State,
Storage,
SubordinateRelation,
_CharmSpec,
)
Expand Down Expand Up @@ -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(
Expand All @@ -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"}}}
),
)

0 comments on commit e2bd314

Please sign in to comment.