Skip to content

Commit

Permalink
Use 'event' for the context object consistently.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Jul 24, 2024
1 parent 51a1110 commit efac1c4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 79 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ storage = scenario.Storage("foo")
# Setup storage with some content:
(storage.get_filesystem(ctx) / "myfile.txt").write_text("helloworld")

with ctx.manager(ctx.on.update_status(), scenario.State(storages={storage})) as mgr:
foo = mgr.charm.model.storages["foo"][0]
with ctx(ctx.on.update_status(), scenario.State(storages={storage})) as event:
foo = event.charm.model.storages["foo"][0]
loc = foo.location
path = loc / "myfile.txt"
assert path.exists()
Expand Down Expand Up @@ -899,9 +899,9 @@ import pathlib

ctx = scenario.Context(MyCharm, meta={'name': 'juliette', "resources": {"foo": {"type": "oci-image"}}})
resource = scenario.Resource(name='foo', path='/path/to/resource.tar')
with ctx.manager(ctx.on.start(), scenario.State(resources={resource})) as mgr:
with ctx(ctx.on.start(), scenario.State(resources={resource})) as event:
# If the charm, at runtime, were to call self.model.resources.fetch("foo"), it would get '/path/to/resource.tar' back.
path = mgr.charm.model.resources.fetch('foo')
path = event.charm.model.resources.fetch('foo')
assert path == pathlib.Path('/path/to/resource.tar')
```

Expand Down
120 changes: 60 additions & 60 deletions tests/test_context_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def test_simple_events(event_name, event_kind):
ctx = scenario.Context(ContextCharm, meta=META, actions=ACTIONS)
# These look like:
# ctx.run(ctx.on.install(), state)
with ctx(getattr(ctx.on, event_name)(), scenario.State()) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
assert isinstance(mgr.charm.observed[0], event_kind)
with ctx(getattr(ctx.on, event_name)(), scenario.State()) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
assert isinstance(event.charm.observed[0], event_kind)


@pytest.mark.parametrize("as_kwarg", [True, False])
Expand All @@ -95,11 +95,11 @@ def test_simple_secret_events(as_kwarg, event_name, event_kind, owner):
else:
args = (secret,)
kwargs = {}
with ctx(getattr(ctx.on, event_name)(*args, **kwargs), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(*args, **kwargs), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.secret.id == secret.id

Expand All @@ -123,11 +123,11 @@ def test_revision_secret_events(event_name, event_kind):
# ctx.run(ctx.on.secret_expired(secret=secret, revision=revision), state)
# The secret and revision must always be passed because the same event name
# is used for all secrets.
with ctx(getattr(ctx.on, event_name)(secret, revision=42), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(secret, revision=42), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.secret.id == secret.id
assert event.revision == 42
Expand Down Expand Up @@ -159,11 +159,11 @@ def test_storage_events(event_name, event_kind):
state_in = scenario.State(storages=[storage])
# These look like:
# ctx.run(ctx.on.storage_attached(storage), state)
with ctx(getattr(ctx.on, event_name)(storage), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(storage), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.storage.name == storage.name
assert event.storage.index == storage.index
Expand All @@ -172,12 +172,12 @@ def test_storage_events(event_name, event_kind):
def test_action_event_no_params():
ctx = scenario.Context(ContextCharm, meta=META, actions=ACTIONS)
# These look like:
# ctx.run_action(ctx.on.action(action), state)
with ctx(ctx.on.action("act"), scenario.State()) as mgr:
mgr.run_action()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
# ctx.run_action(ctx.on.action(action_name), state)
with ctx(ctx.on.action("act"), scenario.State()) as event:
event.run_action()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, ops.ActionEvent)


Expand All @@ -187,11 +187,11 @@ def test_action_event_with_params():
# ctx.run_action(ctx.on.action(action=action), state)
# So that any parameters can be included and the ID can be customised.
call_event = ctx.on.action("act", params={"param": "hello"})
with ctx(call_event, scenario.State()) as mgr:
mgr.run_action()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(call_event, scenario.State()) as event:
event.run_action()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, ops.ActionEvent)
assert event.id == call_event.action.id
assert event.params["param"] == call_event.action.params["param"]
Expand All @@ -203,11 +203,11 @@ def test_pebble_ready_event():
state_in = scenario.State(containers=[container])
# These look like:
# ctx.run(ctx.on.pebble_ready(container), state)
with ctx(ctx.on.pebble_ready(container), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(ctx.on.pebble_ready(container), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, ops.PebbleReadyEvent)
assert event.workload.name == container.name

Expand All @@ -232,11 +232,11 @@ def test_relation_app_events(as_kwarg, event_name, event_kind):
else:
args = (relation,)
kwargs = {}
with ctx(getattr(ctx.on, event_name)(*args, **kwargs), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(*args, **kwargs), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.relation.id == relation.id
assert event.app.name == relation.remote_app_name
Expand All @@ -249,10 +249,10 @@ def test_relation_complex_name():
ctx = scenario.Context(ContextCharm, meta=meta, actions=ACTIONS)
relation = scenario.Relation("foo-bar-baz")
state_in = scenario.State(relations=[relation])
with ctx(ctx.on.relation_created(relation), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
event = mgr.charm.observed[0]
with ctx(ctx.on.relation_created(relation), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
event = event.charm.observed[0]
assert isinstance(event, ops.RelationCreatedEvent)
assert event.relation.id == relation.id
assert event.app.name == relation.remote_app_name
Expand Down Expand Up @@ -282,11 +282,11 @@ def test_relation_unit_events_default_unit(event_name, event_kind):
# These look like:
# ctx.run(ctx.on.baz_relation_changed, state)
# The unit is chosen automatically.
with ctx(getattr(ctx.on, event_name)(relation), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(relation), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.relation.id == relation.id
assert event.app.name == relation.remote_app_name
Expand All @@ -308,11 +308,11 @@ def test_relation_unit_events(event_name, event_kind):
state_in = scenario.State(relations=[relation])
# These look like:
# ctx.run(ctx.on.baz_relation_changed(unit=unit_ordinal), state)
with ctx(getattr(ctx.on, event_name)(relation, remote_unit=2), state_in) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
with ctx(getattr(ctx.on, event_name)(relation, remote_unit=2), state_in) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, event_kind)
assert event.relation.id == relation.id
assert event.app.name == relation.remote_app_name
Expand All @@ -327,11 +327,11 @@ def test_relation_departed_event():
# ctx.run(ctx.on.baz_relation_departed(unit=unit_ordinal, departing_unit=unit_ordinal), state)
with ctx(
ctx.on.relation_departed(relation, remote_unit=2, departing_unit=1), state_in
) as mgr:
mgr.run()
assert len(mgr.charm.observed) == 2
assert isinstance(mgr.charm.observed[1], ops.CollectStatusEvent)
event = mgr.charm.observed[0]
) as event:
event.run()
assert len(event.charm.observed) == 2
assert isinstance(event.charm.observed[1], ops.CollectStatusEvent)
event = event.charm.observed[0]
assert isinstance(event, ops.RelationDepartedEvent)
assert event.relation.id == relation.id
assert event.app.name == relation.remote_app_name
Expand Down
12 changes: 6 additions & 6 deletions tests/test_e2e/test_cloud_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ def test_get_cloud_spec():
name="lxd-model", type="lxd", cloud_spec=scenario_cloud_spec
),
)
with ctx(ctx.on.start(), state=state) as mgr:
assert mgr.charm.model.get_cloud_spec() == expected_cloud_spec
with ctx(ctx.on.start(), state=state) as event:
assert event.charm.model.get_cloud_spec() == expected_cloud_spec


def test_get_cloud_spec_error():
ctx = scenario.Context(MyCharm, meta={"name": "foo"})
state = scenario.State(model=scenario.Model(name="lxd-model", type="lxd"))
with ctx(ctx.on.start(), state) as mgr:
with ctx(ctx.on.start(), state) as event:
with pytest.raises(ops.ModelError):
mgr.charm.model.get_cloud_spec()
event.charm.model.get_cloud_spec()


def test_get_cloud_spec_untrusted():
Expand All @@ -65,6 +65,6 @@ def test_get_cloud_spec_untrusted():
state = scenario.State(
model=scenario.Model(name="lxd-model", type="lxd", cloud_spec=cloud_spec),
)
with ctx(ctx.on.start(), state) as mgr:
with ctx(ctx.on.start(), state) as event:
with pytest.raises(ops.ModelError):
mgr.charm.model.get_cloud_spec()
event.charm.model.get_cloud_spec()
22 changes: 13 additions & 9 deletions tests/test_e2e/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ def test_ip_get(mycharm):
],
networks={Network.default("foo", private_address="4.4.4.4")},
),
) as mgr:
) as event:
# we have a network for the relation
rel = mgr.charm.model.get_relation("metrics-endpoint")
assert str(mgr.charm.model.get_binding(rel).network.bind_address) == "192.0.2.0"
rel = event.charm.model.get_relation("metrics-endpoint")
assert (
str(event.charm.model.get_binding(rel).network.bind_address) == "192.0.2.0"
)

# we have a network for a binding without relations on it
assert (
str(mgr.charm.model.get_binding("deadnodead").network.bind_address)
str(event.charm.model.get_binding("deadnodead").network.bind_address)
== "192.0.2.0"
)

# and an extra binding
assert str(mgr.charm.model.get_binding("foo").network.bind_address) == "4.4.4.4"
assert (
str(event.charm.model.get_binding("foo").network.bind_address) == "4.4.4.4"
)


def test_no_sub_binding(mycharm):
Expand All @@ -84,10 +88,10 @@ def test_no_sub_binding(mycharm):
SubordinateRelation("bar"),
]
),
) as mgr:
) as event:
with pytest.raises(RelationNotFoundError):
# sub relations have no network
mgr.charm.model.get_binding("bar").network
event.charm.model.get_binding("bar").network


def test_no_relation_error(mycharm):
Expand Down Expand Up @@ -115,6 +119,6 @@ def test_no_relation_error(mycharm):
],
networks={Network.default("bar")},
),
) as mgr:
) as event:
with pytest.raises(RelationNotFoundError):
net = mgr.charm.model.get_binding("foo").network
net = event.charm.model.get_binding("foo").network

0 comments on commit efac1c4

Please sign in to comment.