From 385bdf036a68d3a6005fe08b910807af5123d392 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Fri, 21 Jun 2024 11:19:05 +0200 Subject: [PATCH 1/2] fixed config mutation in state --- scenario/consistency_checker.py | 1 - scenario/mocking.py | 2 +- tests/test_e2e/test_config.py | 44 +++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/scenario/consistency_checker.py b/scenario/consistency_checker.py index e73602a4..50eb939d 100644 --- a/scenario/consistency_checker.py +++ b/scenario/consistency_checker.py @@ -353,7 +353,6 @@ def check_config_consistency( ) continue - # todo unify with snapshot's when merged. converters = { "string": str, "int": int, diff --git a/scenario/mocking.py b/scenario/mocking.py index af72a361..02d92043 100644 --- a/scenario/mocking.py +++ b/scenario/mocking.py @@ -263,7 +263,7 @@ def relation_list(self, relation_id: int) -> Tuple[str, ...]: ) def config_get(self): - state_config = self._state.config + state_config = self._state.config.copy() # dedup or we'll mutate the state! # add defaults charm_config = self._charm_spec.config diff --git a/tests/test_e2e/test_config.py b/tests/test_e2e/test_config.py index 27b25c29..00e46837 100644 --- a/tests/test_e2e/test_config.py +++ b/tests/test_e2e/test_config.py @@ -2,7 +2,7 @@ from ops.charm import CharmBase from ops.framework import Framework -from scenario.state import Event, Network, Relation, State, _CharmSpec +from scenario.state import State from tests.helpers import trigger @@ -53,9 +53,49 @@ def check_cfg(charm: CharmBase): config={ "options": { "foo": {"type": "string"}, - "baz": {"type": "integer", "default": 2}, + "baz": {"type": "int", "default": 2}, "qux": {"type": "boolean", "default": False}, }, }, post_event=check_cfg, ) + + +@pytest.mark.parametrize( + "cfg_in", + ( + {"foo": "bar"}, + {"baz": 4, "foo": "bar"}, + {"baz": 4, "foo": "bar", "qux": True}, + ), +) +def test_config_in_not_mutated(mycharm, cfg_in): + class MyCharm(CharmBase): + def __init__(self, framework: Framework): + super().__init__(framework) + for evt in self.on.events().values(): + self.framework.observe(evt, self._on_event) + + def _on_event(self, event): + # access the config to trigger a config-get + foo_cfg = self.config["foo"] + baz_cfg = self.config["baz"] + qux_cfg = self.config["qux"] + + state_out = trigger( + State( + config=cfg_in, + ), + "update_status", + MyCharm, + meta={"name": "foo"}, + config={ + "options": { + "foo": {"type": "string"}, + "baz": {"type": "int", "default": 2}, + "qux": {"type": "boolean", "default": False}, + }, + }, + ) + # check config was not mutated by scenario + assert state_out.config == cfg_in From 3a5bda0d8c92616c5683a9306c02a7791717682d Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Fri, 21 Jun 2024 11:20:57 +0200 Subject: [PATCH 2/2] vbump --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5907ae71..7c2b1995 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta" [project] name = "ops-scenario" -version = "6.1.0" +version = "6.1.1" authors = [ { name = "Pietro Pasotti", email = "pietro.pasotti@canonical.com" }