From 726cc94687aef542f90d75178680d0b78425471f Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Thu, 4 Apr 2024 20:12:21 +1300 Subject: [PATCH] Rename Relation.relation_id to Relation.id. --- README.md | 8 ++++---- scenario/consistency_checker.py | 6 +++--- scenario/mocking.py | 6 ++---- scenario/ops_main_mock.py | 2 +- scenario/runtime.py | 2 +- scenario/state.py | 4 ++-- tests/test_consistency_checker.py | 12 +++--------- tests/test_e2e/test_deferred.py | 2 +- tests/test_e2e/test_network.py | 4 ++-- tests/test_e2e/test_play_assertions.py | 2 +- tests/test_e2e/test_relations.py | 2 +- tests/test_e2e/test_secrets.py | 4 +--- 12 files changed, 22 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 4f8b2a81..bcd6a3f9 100644 --- a/README.md +++ b/README.md @@ -477,7 +477,7 @@ needs to set up the process that will run `ops.main` with the right environment ### Working with relation IDs -Every time you instantiate `Relation` (or peer, or subordinate), the new instance will be given a unique `relation_id`. +Every time you instantiate `Relation` (or peer, or subordinate), the new instance will be given a unique `id`. To inspect the ID the next relation instance will have, you can call `state.next_relation_id`. ```python @@ -486,7 +486,7 @@ from scenario.state import next_relation_id next_id = next_relation_id(update=False) rel = Relation('foo') -assert rel.relation_id == next_id +assert rel.id == next_id ``` This can be handy when using `replace` to create new relations, to avoid relation ID conflicts: @@ -496,8 +496,8 @@ from scenario import Relation from scenario.state import next_relation_id rel = Relation('foo') -rel2 = rel.replace(local_app_data={"foo": "bar"}, relation_id=next_relation_id()) -assert rel2.relation_id == rel.relation_id + 1 +rel2 = rel.replace(local_app_data={"foo": "bar"}, id=next_relation_id()) +assert rel2.id == rel.id + 1 ``` If you don't do this, and pass both relations into a `State`, you will trigger a consistency checker error. diff --git a/scenario/consistency_checker.py b/scenario/consistency_checker.py index ea9ad489..a89402b6 100644 --- a/scenario/consistency_checker.py +++ b/scenario/consistency_checker.py @@ -468,13 +468,13 @@ def _get_relations(r): expected_sub = relation_meta.get("scope", "") == "container" relations = _get_relations(endpoint) for relation in relations: - if relation.relation_id in seen_ids: + if relation.id in seen_ids: errors.append( - f"duplicate relation ID: {relation.relation_id} is claimed " + f"duplicate relation ID: {relation.id} is claimed " f"by multiple Relation instances", ) - seen_ids.add(relation.relation_id) + seen_ids.add(relation.id) is_sub = isinstance(relation, SubordinateRelation) if is_sub and not expected_sub: errors.append( diff --git a/scenario/mocking.py b/scenario/mocking.py index d4c7aab3..841ca25a 100644 --- a/scenario/mocking.py +++ b/scenario/mocking.py @@ -163,7 +163,7 @@ def _get_relation_by_id( ) -> Union["Relation", "SubordinateRelation", "PeerRelation"]: try: return next( - filter(lambda r: r.relation_id == rel_id, self._state.relations), + filter(lambda r: r.id == rel_id, self._state.relations), ) except StopIteration: raise RelationNotFoundError() @@ -245,9 +245,7 @@ def status_get(self, *, is_app: bool = False): def relation_ids(self, relation_name): return [ - rel.relation_id - for rel in self._state.relations - if rel.endpoint == relation_name + rel.id for rel in self._state.relations if rel.endpoint == relation_name ] def relation_list(self, relation_id: int) -> Tuple[str, ...]: diff --git a/scenario/ops_main_mock.py b/scenario/ops_main_mock.py index c2eee10e..db300d8e 100644 --- a/scenario/ops_main_mock.py +++ b/scenario/ops_main_mock.py @@ -115,7 +115,7 @@ def setup_framework( # If we are in a RelationBroken event, we want to know which relation is # broken within the model, not only in the event's `.relation` attribute. broken_relation_id = ( - event.relation.relation_id # type: ignore + event.relation.id # type: ignore if event.name.endswith("_relation_broken") else None ) diff --git a/scenario/runtime.py b/scenario/runtime.py index a6aebab0..a315413f 100644 --- a/scenario/runtime.py +++ b/scenario/runtime.py @@ -208,7 +208,7 @@ def _get_event_env(self, state: "State", event: "Event", charm_root: Path): env.update( { "JUJU_RELATION": relation.endpoint, - "JUJU_RELATION_ID": str(relation.relation_id), + "JUJU_RELATION_ID": str(relation.id), "JUJU_REMOTE_APP": remote_app_name, }, ) diff --git a/scenario/state.py b/scenario/state.py index 8d14e493..84965a62 100644 --- a/scenario/state.py +++ b/scenario/state.py @@ -353,7 +353,7 @@ class RelationBase(_DCBase): """Interface name. Must match the interface name attached to this endpoint in metadata.yaml. If left empty, it will be automatically derived from metadata.yaml.""" - relation_id: int = dataclasses.field(default_factory=next_relation_id) + id: int = dataclasses.field(default_factory=next_relation_id) """Juju relation ID. Every new Relation instance gets a unique one, if there's trouble, override.""" @@ -1410,7 +1410,7 @@ def deferred(self, handler: Callable, event_id: int = 1) -> DeferredEvent: snapshot_data = { "relation_name": relation.endpoint, - "relation_id": relation.relation_id, + "relation_id": relation.id, "app_name": remote_app, "unit_name": f"{remote_app}/{self.relation_remote_unit_id}", } diff --git a/tests/test_consistency_checker.py b/tests/test_consistency_checker.py index ef92e6d9..f673d54f 100644 --- a/tests/test_consistency_checker.py +++ b/tests/test_consistency_checker.py @@ -338,9 +338,7 @@ def test_action_params_type(ptype, good, bad): def test_duplicate_relation_ids(): assert_inconsistent( - State( - relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=1)] - ), + State(relations=[Relation("foo", id=1), Relation("bar", id=1)]), Event("start"), _CharmSpec( MyCharm, @@ -353,17 +351,13 @@ def test_duplicate_relation_ids(): def test_relation_without_endpoint(): assert_inconsistent( - State( - relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=1)] - ), + State(relations=[Relation("foo", id=1), Relation("bar", id=1)]), Event("start"), _CharmSpec(MyCharm, meta={"name": "charlemagne"}), ) assert_consistent( - State( - relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=2)] - ), + State(relations=[Relation("foo", id=1), Relation("bar", id=2)]), Event("start"), _CharmSpec( MyCharm, diff --git a/tests/test_e2e/test_deferred.py b/tests/test_e2e/test_deferred.py index b084f6ff..34dcf529 100644 --- a/tests/test_e2e/test_deferred.py +++ b/tests/test_e2e/test_deferred.py @@ -150,7 +150,7 @@ def test_deferred_relation_event_from_relation(mycharm): assert out.deferred[0].name == "foo_relation_changed" assert out.deferred[0].snapshot_data == { "relation_name": rel.endpoint, - "relation_id": rel.relation_id, + "relation_id": rel.id, "app_name": "remote", "unit_name": "remote/1", } diff --git a/tests/test_e2e/test_network.py b/tests/test_e2e/test_network.py index 07808e1d..c3d271df 100644 --- a/tests/test_e2e/test_network.py +++ b/tests/test_e2e/test_network.py @@ -48,7 +48,7 @@ def test_ip_get(mycharm): interface="foo", remote_app_name="remote", endpoint="metrics-endpoint", - relation_id=1, + id=1, ), ], networks={"foo": Network.default(private_address="4.4.4.4")}, @@ -110,7 +110,7 @@ def test_no_relation_error(mycharm): interface="foo", remote_app_name="remote", endpoint="metrics-endpoint", - relation_id=1, + id=1, ), ], networks={"bar": Network.default()}, diff --git a/tests/test_e2e/test_play_assertions.py b/tests/test_e2e/test_play_assertions.py index b8b92d5a..d4523b37 100644 --- a/tests/test_e2e/test_play_assertions.py +++ b/tests/test_e2e/test_play_assertions.py @@ -102,7 +102,7 @@ def check_relation_data(charm): Relation( endpoint="relation_test", interface="azdrubales", - relation_id=1, + id=1, remote_app_name="karlos", remote_app_data={"yaba": "doodle"}, remote_units_data={0: {"foo": "bar"}, 1: {"baz": "qux"}}, diff --git a/tests/test_e2e/test_relations.py b/tests/test_e2e/test_relations.py index 212e12a6..037c1acf 100644 --- a/tests/test_e2e/test_relations.py +++ b/tests/test_e2e/test_relations.py @@ -388,7 +388,7 @@ def test_relation_ids(): initial_id = _next_relation_id_counter for i in range(10): rel = Relation("foo") - assert rel.relation_id == initial_id + i + assert rel.id == initial_id + i def test_broken_relation_not_in_model_relations(mycharm): diff --git a/tests/test_e2e/test_secrets.py b/tests/test_e2e/test_secrets.py index e8e75f7b..f0f87a4f 100644 --- a/tests/test_e2e/test_secrets.py +++ b/tests/test_e2e/test_secrets.py @@ -521,9 +521,7 @@ def __init__(self, *args): state = State( leader=True, relations=[ - Relation( - "bar", remote_app_name=relation_remote_app, relation_id=relation_id - ) + Relation("bar", remote_app_name=relation_remote_app, id=relation_id) ], )