Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-of-run summary report #4405

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ prerun
printenv
profisioner
pycolors
Qalthos
quux
reget
reraised
Expand Down
86 changes: 56 additions & 30 deletions src/molecule/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,59 @@ def execute_cmdline_scenarios(

scenarios = _generate_scenarios(scenario_names, configs)

try:
_run_scenarios(scenarios, command_args)

# TODO @Qalthos: This should really be replaced with actual control flow, and not abusing SystemExit like this, # noqa: FIX002, TD003
# but this is what is here now.
except SystemExit: # noqa: TRY203
raise
finally:
import yaml

print(yaml.safe_dump(scenarios.results)) # noqa: T201


def _generate_scenarios(
scenario_names: list[str] | None,
configs: list[config.Config],
) -> molecule.scenarios.Scenarios:
"""Generate Scenarios object from names and configs.

Args:
scenario_names: Names of scenarios to include.
configs: List of Config objects to consider.

Returns:
Combined Scenarios object.
"""
scenarios = molecule.scenarios.Scenarios(
configs,
scenario_names,
)

if scenario_names is not None:
for scenario_name in scenario_names:
if scenario_name != "*" and scenarios:
LOG.info(
"%s scenario test matrix: %s",
scenario_name,
", ".join(scenarios.sequence(scenario_name)),
)

return scenarios


def _run_scenarios(scenarios: molecule.scenarios.Scenarios, command_args: CommandArgs) -> None:
"""Loop through Scenarios object and execute each.

Args:
scenarios: The Scenarios object holding all of the Scenario objects.
command_args: dict of command arguments.

Raises:
SystemExit: when a scenario fails prematurely.
"""
for scenario in scenarios:
if scenario.config.config["prerun"]:
role_name_check = scenario.config.config["role_name_check"]
Expand Down Expand Up @@ -175,36 +228,9 @@ def execute_cmdline_scenarios(
util.sysexit()
else:
raise


def _generate_scenarios(
scenario_names: list[str] | None,
configs: list[config.Config],
) -> molecule.scenarios.Scenarios:
"""Generate Scenarios object from names and configs.

Args:
scenario_names: Names of scenarios to include.
configs: List of Config objects to consider.

Returns:
Combined Scenarios object.
"""
scenarios = molecule.scenarios.Scenarios(
configs,
scenario_names,
)

if scenario_names is not None:
for scenario_name in scenario_names:
if scenario_name != "*" and scenarios:
LOG.info(
"%s scenario test matrix: %s",
scenario_name,
", ".join(scenarios.sequence(scenario_name)),
)

return scenarios
finally:
# Store results regardless
scenarios.results.append({"name": scenario.name, "results": scenario.results})


def execute_subcommand(
Expand Down
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
2 changes: 2 additions & 0 deletions src/molecule/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
if TYPE_CHECKING:
from molecule.config import Config
from molecule.scenario import Scenario
from molecule.types import ScenariosResults


LOG = logging.getLogger(__name__)
Expand All @@ -52,6 +53,7 @@ def __init__(
self._configs = configs
self._scenario_names = [] if scenario_names is None else scenario_names
self._scenarios = self.all
self.results: list[ScenariosResults] = []

def __iter__(self) -> Scenarios:
"""Make object iterable.
Expand Down
24 changes: 24 additions & 0 deletions src/molecule/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,27 @@ class CommandArgs(TypedDict, total=False):
platform_name: str
scenario_name: str
subcommand: str


class ScenarioResult(TypedDict):
"""Dictionary containing the result of a Scenario action.

Attributes:
subcommand: The action that ran.
state: The outcome of the action ("PASSED", "FAILED", or "SKIPPED).
"""

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


class ScenariosResults(TypedDict):
"""Dictionary containing the results of multiple Scenario runs.

Attributes:
name: The name of the scenario.
results: A list of ScenarioResult objects.
"""

name: str
results: list[ScenarioResult]