Skip to content

Commit

Permalink
Merge pull request #64 from canonical/fix-no-remote-unit-ids-evt
Browse files Browse the repository at this point in the history
fix error when no remote unit data is passed to relation
  • Loading branch information
PietroPasotti authored Oct 9, 2023
2 parents 8ee7720 + 518cb9e commit a3c2cd2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }
Expand Down
7 changes: 6 additions & 1 deletion scenario/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
5 changes: 1 addition & 4 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("-", "_")


Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_e2e/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a3c2cd2

Please sign in to comment.