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

Capture sanity check warnings and display them #3214

Merged
merged 1 commit into from Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
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
Expand Up @@ -36,6 +36,14 @@ def append(self, item) -> None:
super(UserListMap, self).append(item)


class MoleculeRuntimeWarning(RuntimeWarning):
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
"""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
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only catch warnings coming from this invocation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what is happening with record, when the context is destroyed the filter is also reset. At least that is what docs are saying.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that we shouldn't suppress those warnings in run_command()

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
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
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