From a56045b3e0f358369aba31870cfb6e5fc130e369 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Wed, 27 Nov 2024 22:07:02 +1300 Subject: [PATCH 1/2] Handle warnings generated by our own tests. --- test/test_main.py | 6 ------ test/test_model.py | 6 ++++-- testing/src/scenario/mocking.py | 1 + testing/tests/test_context_on.py | 24 ++++++++++++++++++++++-- tox.ini | 3 ++- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/test/test_main.py b/test/test_main.py index 2ce616268..26e3a0e83 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -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'): diff --git a/test/test_model.py b/test/test_model.py index 541eeb1f5..ae1fbaa81 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -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 diff --git a/testing/src/scenario/mocking.py b/testing/src/scenario/mocking.py index b95b553be..3e1bcd4f9 100644 --- a/testing/src/scenario/mocking.py +++ b/testing/src/scenario/mocking.py @@ -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( diff --git a/testing/tests/test_context_on.py b/testing/tests/test_context_on.py index 32759fd49..30491af8d 100644 --- a/testing/tests/test_context_on.py +++ b/testing/tests/test_context_on.py @@ -1,4 +1,5 @@ import copy +import warnings import ops import pytest @@ -55,8 +56,6 @@ def _on_event(self, event): ("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), ], ) @@ -71,6 +70,27 @@ def test_simple_events(event_name, event_kind): 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 warnings.catch_warnings( + record=False, action="ignore", category=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", diff --git a/tox.ini b/tox.ini index ef9ee7f90..ea6b0d3f6 100644 --- a/tox.ini +++ b/tox.ini @@ -103,7 +103,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 From c606f083b266b67b028498a6336c92b174e9922f Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Thu, 28 Nov 2024 10:22:21 +1300 Subject: [PATCH 2/2] Use pytest.warns rather than warnings.catch_warnings. --- testing/tests/test_context_on.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/testing/tests/test_context_on.py b/testing/tests/test_context_on.py index 30491af8d..21b3755cb 100644 --- a/testing/tests/test_context_on.py +++ b/testing/tests/test_context_on.py @@ -1,5 +1,4 @@ import copy -import warnings import ops import pytest @@ -81,9 +80,7 @@ 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 warnings.catch_warnings( - record=False, action="ignore", category=DeprecationWarning - ): + with pytest.warns(DeprecationWarning): with ctx(getattr(ctx.on, event_name)(), scenario.State()) as mgr: mgr.run() assert len(mgr.charm.observed) == 2