From 7d3bab67eaddb7bfb75c4cab6da851aff7745209 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Oct 2023 17:01:18 +0200 Subject: [PATCH 1/2] test --- scenario/runtime.py | 7 ++++++- scenario/state.py | 5 +---- tests/test_e2e/test_relations.py | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/scenario/runtime.py b/scenario/runtime.py index 863d0f1d..0de7b991 100644 --- a/scenario/runtime.py +++ b/scenario/runtime.py @@ -231,13 +231,18 @@ def _get_event_env(self, state: "State", event: "Event", charm_root: Path): "but you probably should be parametrizing the event with `remote_unit_id` " "to be explicit.", ) - else: + elif len(remote_unit_ids) > 1: remote_unit_id = remote_unit_ids[0] logger.warning( "remote unit ID unset, and multiple remote unit IDs are present; " "We will pick the first one and hope for the best. You should be passing " "`remote_unit_id` to the Event constructor.", ) + else: + logger.warning( + "remote unit ID unset; no remote unit data present. " + "Is this a realistic scenario?", # TODO: is it? + ) if remote_unit_id is not None: remote_unit = f"{remote_app_name}/{remote_unit_id}" diff --git a/scenario/state.py b/scenario/state.py index 9b612e20..69d7cb8f 100644 --- a/scenario/state.py +++ b/scenario/state.py @@ -193,7 +193,7 @@ def _update_metadata( def normalize_name(s: str): - """Event names need underscores instead of dashes.""" + """Event names, in Scenario, uniformly use underscores instead of dashes.""" return s.replace("-", "_") @@ -927,9 +927,6 @@ def __call__(self, remote_unit_id: Optional[int] = None) -> "Event": return self.replace(relation_remote_unit_id=remote_unit_id) def __post_init__(self): - if "-" in self.path: - logger.warning(f"Only use underscores in event paths. {self.path!r}") - path = normalize_name(self.path) # bypass frozen dataclass object.__setattr__(self, "path", path) diff --git a/tests/test_e2e/test_relations.py b/tests/test_e2e/test_relations.py index 00f4926f..e5be3400 100644 --- a/tests/test_e2e/test_relations.py +++ b/tests/test_e2e/test_relations.py @@ -223,6 +223,42 @@ def callback(charm: CharmBase, event): ) +@pytest.mark.parametrize( + "evt_name", + ("changed", "broken", "departed", "joined", "created"), +) +def test_relation_events_no_remote_units(mycharm, evt_name, caplog): + relation = Relation( + endpoint="foo", + interface="foo", + remote_units_data={}, # no units + ) + + def callback(charm: CharmBase, event): + assert event.app # that's always present + assert not event.unit + + mycharm._call = callback + + trigger( + State( + relations=[ + relation, + ], + ), + getattr(relation, f"{evt_name}_event"), + mycharm, + meta={ + "name": "local", + "requires": { + "foo": {"interface": "foo"}, + }, + }, + ) + + assert "remote unit ID unset; no remote unit data present" in caplog.text + + @pytest.mark.parametrize("data", (set(), {}, [], (), 1, 1.0, None, b"")) def test_relation_unit_data_bad_types(mycharm, data): with pytest.raises(StateValidationError): From 518cb9e3505ee195810b45102d0a9e7c3fbb3325 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Oct 2023 17:03:19 +0200 Subject: [PATCH 2/2] vbump --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6b647aeb..9ac8cf8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta" [project] name = "ops-scenario" -version = "5.3" +version = "5.3.1" authors = [ { name = "Pietro Pasotti", email = "pietro.pasotti@canonical.com" }