Skip to content

Commit

Permalink
Merge pull request canonical#145 from canonical/config-persistence
Browse files Browse the repository at this point in the history
fixed config mutation in state
  • Loading branch information
PietroPasotti committed Jun 21, 2024
2 parents 25a8eb9 + 3a5bda0 commit 77ce447
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion scenario/consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def check_config_consistency(
)
continue

# todo unify with snapshot's when merged.
converters = {
"string": str,
"int": int,
Expand Down
2 changes: 1 addition & 1 deletion scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 42 additions & 2 deletions tests/test_e2e/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit 77ce447

Please sign in to comment.