Skip to content

Commit

Permalink
test: handle warnings generated by our own tests (#1469)
Browse files Browse the repository at this point in the history
Our tests currently end with a very large list of warnings. This PR
fixes that:

* All of the Harness tests emit a `PendingDeprecationWarning` telling us
that Harness is going away and we should use Scenario. This seems like
something to simply ignore in our own tests, so I've added a `-W`
argument to Python to silence them in bulk.
* One `test_main` test raises a warning about controller storage going
away - there's another test that tests that warning happens, and the two
tests are otherwise identical. I've merged the two - we don't need them
both.
* Two of the secret tests check that `SecretInfo` objects can still be
created without providing the model UUID. I've added a `pytest.warns` to
(a) ensure that there's the warning, and (b) stop it appearing in the
tox output.
* Scenario should provide the model UUID when creating `SecretInfo`
objects. This is a one-line fix, so I've added it here - it stops
deprecation warnings, but is really more the code than the tests, but
since it's so small I think it's ok here.
* The `context.on` Scenario tests check that pre- and post- series
upgrade events work, and those raise warnings. I've split them off to a
separate test that verifies (and silences) the warning.

Fixes #1408
  • Loading branch information
tonyandrewmeyer authored Dec 16, 2024
1 parent 4e856c9 commit df856d2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 0 additions & 6 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,6 @@ def test_storage_no_storage(self):

def test_storage_with_storage(self):
# here we patch juju_backend_available, so it gets set up and falls over when used
with patch('ops.storage.juju_backend_available') as juju_backend_available:
juju_backend_available.return_value = True
with pytest.raises(FileNotFoundError, match='state-get'):
self._check(ops.CharmBase, use_juju_for_storage=True)

def test_controller_storage_deprecated(self):
with patch('ops.storage.juju_backend_available') as juju_backend_available:
juju_backend_available.return_value = True
with pytest.warns(DeprecationWarning, match='Controller storage'):
Expand Down
6 changes: 4 additions & 2 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3643,11 +3643,13 @@ def test_from_dict(self):
assert info.rotates is None
assert info.description is None

info = ops.SecretInfo.from_dict('5', {'revision': 9})
with pytest.warns(DeprecationWarning, match='`model_uuid` should always be provided'):
info = ops.SecretInfo.from_dict('5', {'revision': 9})
assert info.id == 'secret:5'
assert info.revision == 9

info = ops.SecretInfo.from_dict('secret://abcd/6', {'revision': 9})
with pytest.warns(DeprecationWarning, match='`model_uuid` should always be provided'):
info = ops.SecretInfo.from_dict('secret://abcd/6', {'revision': 9})
assert info.id == 'secret://abcd/6'
assert info.revision == 9

Expand Down
1 change: 1 addition & 0 deletions testing/src/scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def secret_info_get(
expires=secret.expire,
rotation=secret.rotate,
rotates=None, # not implemented yet.
model_uuid=self._state.model.uuid,
)

def secret_set(
Expand Down
21 changes: 19 additions & 2 deletions testing/tests/test_context_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def _on_event(self, event: ops.EventBase):
("update_status", ops.UpdateStatusEvent),
("config_changed", ops.ConfigChangedEvent),
("upgrade_charm", ops.UpgradeCharmEvent),
("pre_series_upgrade", ops.PreSeriesUpgradeEvent),
("post_series_upgrade", ops.PostSeriesUpgradeEvent),
("leader_elected", ops.LeaderElectedEvent),
],
)
Expand All @@ -72,6 +70,25 @@ def test_simple_events(event_name: str, event_kind: typing.Type[ops.EventBase]):
assert isinstance(mgr.charm.observed[0], event_kind)


@pytest.mark.parametrize(
"event_name, event_kind",
[
("pre_series_upgrade", ops.PreSeriesUpgradeEvent),
("post_series_upgrade", ops.PostSeriesUpgradeEvent),
],
)
def test_simple_deprecated_events(event_name, event_kind):
ctx = scenario.Context(ContextCharm, meta=META, actions=ACTIONS)
# These look like:
# ctx.run(ctx.on.pre_series_upgrade(), state)
with pytest.warns(DeprecationWarning):
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)


@pytest.mark.parametrize("as_kwarg", [True, False])
@pytest.mark.parametrize(
"event_name,event_kind,owner",
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ deps =
-e .
-e testing
commands =
pytest -n auto --ignore={[vars]tst_path}smoke -v --tb native {posargs}
pytest -n auto --ignore={[vars]tst_path}smoke -v --tb native \
-W 'ignore:Harness is deprecated:PendingDeprecationWarning' {posargs}

[testenv:coverage]
description = Run unit tests with coverage
Expand Down

0 comments on commit df856d2

Please sign in to comment.