Skip to content

Commit

Permalink
Capture sanity check warnings and display them (#3214)
Browse files Browse the repository at this point in the history
Captures runtime warnings raise by driver.sanity_check() and
display them at the end in case of failure.

Related: ansible-community/molecule-podman#76
  • Loading branch information
ssbarnea authored Aug 31, 2021
1 parent b351ea0 commit cc8f128
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ jobs:
- tox_env: docs
python-version: 3.9
- tox_env: py36
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.6
- tox_env: py37
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.7
- tox_env: py38
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.8
- tox_env: py39
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.9
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.6
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=448
PREFIX: PYTEST_REQPASS=449
python-version: 3.9
- tox_env: packaging
python-version: 3.9
Expand Down
8 changes: 8 additions & 0 deletions src/molecule/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def append(self, item) -> None:
super(UserListMap, self).append(item)


class MoleculeRuntimeWarning(RuntimeWarning):
"""A runtime warning used by Molecule and its plugins."""


class IncompatibleMoleculeRuntimeWarning(MoleculeRuntimeWarning):
"""A warning noting an unsupported runtime environment."""


@lru_cache()
def drivers(config=None) -> UserListMap:
"""Return list of active drivers."""
Expand Down
10 changes: 8 additions & 2 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"""Ansible-Playbook Provisioner Module."""

import logging
import warnings

from molecule import util
from molecule.api import MoleculeRuntimeWarning

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,12 +105,16 @@ def execute(self):
LOG.warning("Skipping, %s action has no playbook." % self._config.action)
return

self._config.driver.sanity_checks()
result = util.run_command(self._ansible_command, debug=self._config.debug)
with warnings.catch_warnings(record=True) as warns:
warnings.filterwarnings("default", category=MoleculeRuntimeWarning)
self._config.driver.sanity_checks()
result = util.run_command(self._ansible_command, debug=self._config.debug)

if result.returncode != 0:
util.sysexit_with_message(
f"Ansible return code was {result.returncode}, command was: {result.args}",
result.returncode,
warns=warns,
)

return result.stdout
Expand Down
17 changes: 17 additions & 0 deletions src/molecule/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import binascii
import io
import os
import warnings

import pytest

from molecule import util
from molecule.api import IncompatibleMoleculeRuntimeWarning, MoleculeRuntimeWarning
from molecule.console import console
from molecule.constants import MOLECULE_HEADER
from molecule.test.conftest import get_molecule_file, molecule_directory
Expand Down Expand Up @@ -93,6 +95,21 @@ def test_sysexit_with_message(patched_logger_critical):
patched_logger_critical.assert_called_once_with("foo")


def test_sysexit_with_warns(patched_logger_critical, patched_logger_warning):
with pytest.raises(SystemExit) as e:

with warnings.catch_warnings(record=True) as warns:
warnings.filterwarnings("default", category=MoleculeRuntimeWarning)
warnings.warn("xxx", category=IncompatibleMoleculeRuntimeWarning)

util.sysexit_with_message("foo", warns=warns)

assert 1 == e.value.code

patched_logger_critical.assert_called_once_with("foo")
patched_logger_warning.assert_called_once_with("xxx")


def test_sysexit_with_message_and_custom_code(patched_logger_critical):
with pytest.raises(SystemExit) as e:
util.sysexit_with_message("foo", 2)
Expand Down
11 changes: 9 additions & 2 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from dataclasses import dataclass
from functools import lru_cache # noqa
from subprocess import CalledProcessError, CompletedProcess
from typing import Any, Dict, List, MutableMapping, NoReturn, Optional, Union
from typing import Any, Dict, Iterable, List, MutableMapping, NoReturn, Optional, Union
from warnings import WarningMessage

import jinja2
import yaml
Expand Down Expand Up @@ -96,7 +97,10 @@ def sysexit(code: int = 1) -> NoReturn:


def sysexit_with_message(
msg: str, code: int = 1, detail: Optional[MutableMapping] = None
msg: str,
code: int = 1,
detail: Optional[MutableMapping] = None,
warns: Iterable[WarningMessage] = (),
) -> None:
"""Exit with an error message."""
# detail is usually a multi-line string which is not suitable for normal
Expand All @@ -108,6 +112,9 @@ def sysexit_with_message(
detail_str = str(detail)
print(detail_str)
LOG.critical(msg)

for warn in warns:
LOG.warning(warn.__dict__["message"].args[0])
sysexit(code)


Expand Down

0 comments on commit cc8f128

Please sign in to comment.