Skip to content

Commit

Permalink
fix: key error on charm state upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed Jun 6, 2024
1 parent 379094b commit 47362b8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,23 +846,37 @@ def _check_immutable_config_change(
prev_state = json.loads(json_data)
logger.info("Previous charm state: %s", prev_state)

if prev_state["runner_config"]["runner_storage"] != runner_storage:
logger.error(
"Storage option changed from %s to %s, blocking the charm",
prev_state["runner_config"]["runner_storage"],
runner_storage,
)
raise ImmutableConfigChangedError(
msg="runner-storage config cannot be changed after deployment, redeploy if needed"
)
if prev_state["runner_config"]["base_image"] != base_image.value:
logger.error(
"Base image option changed from %s to %s, blocking the charm",
prev_state["runner_config"]["base_image"],
runner_storage,
try:
if prev_state["runner_config"]["runner_storage"] != runner_storage:
logger.error(
"Storage option changed from %s to %s, blocking the charm",
prev_state["runner_config"]["runner_storage"],
runner_storage,
)
raise ImmutableConfigChangedError(
msg=(
"runner-storage config cannot be changed after deployment, "
"redeploy if needed"
)
)
except KeyError as exc:
logger.warning(
"Key %s not found, this will be updated to current config.", exc.args[0]
)
raise ImmutableConfigChangedError(
msg="base-image config cannot be changed after deployment, redeploy if needed"

try:
if prev_state["runner_config"]["base_image"] != base_image.value:
logger.error(
"Base image option changed from %s to %s, blocking the charm",
prev_state["runner_config"]["base_image"],
runner_storage,
)
raise ImmutableConfigChangedError(
msg="base-image config cannot be changed after deployment, redeploy if needed"
)
except KeyError as exc:
logger.warning(
"Key %s not found, this will be updated to current config.", exc.args[0]
)

@classmethod
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,41 @@ def mock_charm_state_data():
}


@pytest.mark.parametrize(
"immutable_config",
[
pytest.param("runner_storage", id="Runner storage"),
pytest.param("base_image", id="Base image"),
],
)
def test_check_immutable_config_key_error(
mock_charm_state_path: Path,
mock_charm_state_data: dict[str, typing.Any],
immutable_config: str,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
):
"""
arrange: Mock CHARM_STATE_PATH and read_text method to return previous state with same config.
act: Call _check_immutable_config_change method.
assert: None is returned.
"""
mock_charm_state_data["runner_config"].pop(immutable_config)
monkeypatch.setattr(charm_state, "CHARM_STATE_PATH", mock_charm_state_path)
monkeypatch.setattr(
charm_state.CHARM_STATE_PATH,
"read_text",
MagicMock(return_value=json.dumps(mock_charm_state_data)),
)
state = CharmState(**mock_charm_state_data)

assert state._check_immutable_config_change(RunnerStorage.MEMORY, BaseImage.JAMMY) is None
assert any(
f"Key {immutable_config} not found, this will be updated to current config." in message
for message in caplog.messages
)


def test_check_immutable_config_change_no_previous_state(
mock_charm_state_path: Path, mock_charm_state_data: dict, monkeypatch: pytest.MonkeyPatch
):
Expand Down

0 comments on commit 47362b8

Please sign in to comment.