Skip to content

Commit

Permalink
Store scenario results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Feb 25, 2025
1 parent 63788df commit 5c14cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from molecule import util
from molecule.api import MoleculeRuntimeWarning
from molecule.types import ScenarioResult


if TYPE_CHECKING:
Expand Down Expand Up @@ -120,6 +121,9 @@ def execute(self, action_args: list[str] | None = None) -> str: # noqa: ARG002

if not self._playbook:
LOG.warning("Skipping, %s action has no playbook.", self._config.action)
self._config.scenario.results.append(
ScenarioResult(subcommand=self._config.action, state="SKIPPED"),
)
return ""

with warnings.catch_warnings(record=True) as warns:
Expand All @@ -134,6 +138,10 @@ def execute(self, action_args: list[str] | None = None) -> str: # noqa: ARG002
)

if result.returncode != 0:
self._config.scenario.results.append(
ScenarioResult(subcommand=self._config.action, state="FAILED"),
)

from rich.markup import escape

util.sysexit_with_message(
Expand All @@ -142,6 +150,9 @@ def execute(self, action_args: list[str] | None = None) -> str: # noqa: ARG002
warns=warns,
)

self._config.scenario.results.append(
ScenarioResult(subcommand=self._config.action, state="PASSED"),
)
return result.stdout

def add_cli_arg(self, name: str, value: str | bool) -> None:
Expand Down
2 changes: 2 additions & 0 deletions src/molecule/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

if TYPE_CHECKING:
from molecule.config import Config
from molecule.types import ScenarioResult


LOG = logging.getLogger(__name__)
Expand All @@ -54,6 +55,7 @@ def __init__(self, config: Config) -> None:
"""
self._lock = None
self.config = config
self.results: list[ScenarioResult] = []
self._setup()

def __repr__(self) -> str:
Expand Down
12 changes: 12 additions & 0 deletions src/molecule/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,15 @@ class CommandArgs(TypedDict, total=False):
platform_name: str
scenario_name: str
subcommand: str


class ScenarioResult(TypedDict):
"""Dictionary containing the result of a Scenario run.
Attributes:
subcommand: The action that ran.
state: The outcome of the action ("PASSED", "FAILED", or "SKIPPED).
"""

subcommand: str | None
state: Literal["PASSED", "FAILED", "SKIPPED"]

0 comments on commit 5c14cac

Please sign in to comment.