Skip to content

Commit

Permalink
actionoutput class
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Oct 20, 2023
1 parent 55cb4e0 commit da33305
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 20 additions & 2 deletions scenario/context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
import dataclasses
import tempfile
from collections import namedtuple
from contextlib import contextmanager
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -34,7 +34,25 @@

logger = scenario_logger.getChild("runtime")

ActionOutput = namedtuple("ActionOutput", ("state", "logs", "results", "failure"))

@dataclasses.dataclass
class ActionOutput:
"""Wraps the results of running an action event with `run_action`."""

state: State
"""The charm state after the action has been handled.
In most cases, actions are not expected to be affecting it."""
logs: List[str]
"""Any logs associated with the action output, set by the charm."""
results: Dict[str, str]
"""Key-value mapping assigned by the charm as a result of the action."""
failure: Optional[str]
"""If the action is not a successL: the message the charm set when failing the action."""

@property
def success(self) -> bool:
"""Return whether this action was a success."""
return self.failure is not None


class InvalidEventError(RuntimeError):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_e2e/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def handle_evt(charm: CharmBase, evt: ActionEvent):
out = ctx.run_action(action, State())

assert out.results == res_value
assert out.success is True


@pytest.mark.parametrize("res_value", ({"a": {"b": {"c"}}}, {"d": "e"}))
Expand All @@ -128,3 +129,4 @@ def handle_evt(charm: CharmBase, evt: ActionEvent):

assert out.failure == "failed becozz"
assert out.logs == ["log1", "log2"]
assert out.success is False

0 comments on commit da33305

Please sign in to comment.