-
Notifications
You must be signed in to change notification settings - Fork 64
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
feature: integrate pytest-molecule
plugin
#124
Closed
Closed
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
a1bf2a1
integrate pytest-molecule plugin WIP
Ruchip16 2f68be2
Add python 3.8 back (#119)
cidrblock d4c7c3c
Remove password (#120)
cidrblock d35539d
Add token write for pypi (#122)
cidrblock 67dd05b
REmove ansible as dep (#127)
cidrblock 0de974d
Restore python 3.7 support (#128)
cidrblock e97022e
WIP
Ruchip16 8665f9a
Merge branch 'main' into integrate-molecule
Ruchip16 1648519
need review
Ruchip16 70657eb
molecule integration
Ruchip16 21b0de7
checks testing
Ruchip16 3a0adc3
fixes
Ruchip16 e07b0b2
tests for molecule
Ruchip16 373ef00
broken tests & fixes
Ruchip16 5c6418d
tox failing tests
Ruchip16 79802d9
constraint file fixture
Ruchip16 6e8325e
fixtures
Ruchip16 aff79e0
Merge branch 'main' into integrate-molecule
Ruchip16 ddf74b3
REmove ansible as dep (#127)
cidrblock 2dd8d4f
Restore python 3.7 support (#128)
cidrblock e7386d1
molecule integration
Ruchip16 5d1677a
naming conflicts fix
Ruchip16 699e7d2
chore: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e8652c8
Merge branch 'main' into integrate-molecule
audgirka e085bc2
Switch to ANSIBLE_COLLECTION_PATH (#139)
cidrblock 94a0af0
Fix for ansible 2.9 (#141)
cidrblock b0f07f5
integrate pytest-molecule plugin WIP
Ruchip16 6202eda
REmove ansible as dep (#127)
cidrblock f051d4a
WIP
Ruchip16 2aeb3a8
need review
Ruchip16 f53d18a
molecule integration
Ruchip16 97582b2
broken tests & fixes
Ruchip16 55baec0
REmove ansible as dep (#127)
cidrblock 6f7deb0
molecule integration
Ruchip16 8e2fe4d
ModuleNotFoundError in tests
Ruchip16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
coverage | ||
pytest>=6,<8.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.7 | ||
# This file is autogenerated by pip-compile with Python 3.9 | ||
# by the following command: | ||
# | ||
# pip-compile --extra=docs --extra=test --no-annotate --output-file=.config/requirements.txt --resolver=backtracking --strip-extras --unsafe-package=ruamel-yaml-clib pyproject.toml | ||
# | ||
ansible-core==2.15.0 | ||
attrs==22.2.0 | ||
cffi==1.15.1 | ||
coverage==7.2.2 | ||
cryptography==40.0.2 | ||
exceptiongroup==1.1.1 | ||
importlib-metadata==6.6.0 | ||
importlib-resources==5.0.7 | ||
iniconfig==2.0.0 | ||
jinja2==3.1.2 | ||
markupsafe==2.1.2 | ||
packaging==23.0 | ||
pluggy==1.0.0 | ||
pycparser==2.21 | ||
pytest==7.2.2 | ||
pyyaml==6.0 | ||
resolvelib==1.0.1 | ||
tomli==2.0.1 | ||
typing-extensions==4.5.0 | ||
zipp==3.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
"""pytest-molecule plugin implementation.""" | ||
# pylint: disable=protected-access | ||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
import shlex | ||
import subprocess | ||
import sys | ||
import warnings | ||
from shlex import quote | ||
|
||
import pkg_resources | ||
import pytest | ||
import yaml | ||
from molecule.api import drivers | ||
from molecule.config import ansible_version | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def molecule_pytest_configure(config): | ||
"""Pytest hook for loading our specific configuration.""" | ||
interesting_env_vars = [ | ||
"ANSIBLE", | ||
"MOLECULE", | ||
"DOCKER", | ||
"PODMAN", | ||
"VAGRANT", | ||
"VIRSH", | ||
"ZUUL", | ||
] | ||
|
||
# Add extra information that may be key for debugging failures | ||
if hasattr(config, "_metadata"): | ||
for package in ["molecule"]: | ||
config._metadata["Packages"][package] = pkg_resources.get_distribution( | ||
package, | ||
).version | ||
|
||
if "Tools" not in config._metadata: | ||
config._metadata["Tools"] = {} | ||
config._metadata["Tools"]["ansible"] = str(ansible_version()) | ||
|
||
# Adds interesting env vars | ||
env = "" | ||
for key, value in sorted(os.environ.items()): | ||
for var_name in interesting_env_vars: | ||
if key.startswith(var_name): | ||
env += f"{key}={value} " | ||
config._metadata["env"] = env | ||
|
||
# We hide DeprecationWarnings thrown by driver loading because these are | ||
# outside our control and worse: they are displayed even on projects that | ||
# have no molecule tests at all as pytest_configure() is called during | ||
# collection, causing spam. | ||
with warnings.catch_warnings(): | ||
warnings.filterwarnings("ignore", category=DeprecationWarning) | ||
|
||
config.option.molecule = {} | ||
for driver in map(str, drivers()): | ||
config.addinivalue_line( | ||
"markers", | ||
f"{driver}: mark test to run only when {driver} is available", | ||
) | ||
config.option.molecule[driver] = {"available": True} | ||
|
||
config.addinivalue_line( | ||
"markers", | ||
"no_driver: mark used for scenarios that do not contain driver info", | ||
) | ||
|
||
config.addinivalue_line( | ||
"markers", | ||
"molecule: mark used by all molecule scenarios", | ||
) | ||
|
||
# validate selinux availability | ||
if sys.platform == "linux" and os.path.isfile("/etc/selinux/config"): | ||
try: | ||
import selinux # noqa pylint: disable=unused-import,import-error,import-outside-toplevel | ||
except ImportError: | ||
logging.error( | ||
"It appears that you are trying to use " | ||
"molecule with a Python interpreter that does not have the " | ||
"libselinux python bindings installed. These can only be " | ||
"installed using your distro package manager and are specific " | ||
"to each python version. Common package names: " | ||
"libselinux-python python2-libselinux python3-libselinux", | ||
) | ||
# we do not re-raise this exception because missing or broken | ||
# selinux bindings are not guaranteed to fail molecule execution. | ||
|
||
|
||
class MoleculeFile(pytest.File): | ||
"""Wrapper class for molecule files.""" | ||
|
||
def collect(self): | ||
"""Test generator.""" | ||
if hasattr(MoleculeItem, "from_parent"): | ||
yield MoleculeItem.from_parent(name="test", parent=self) | ||
else: | ||
yield MoleculeItem("test", self) | ||
|
||
def __str__(self): | ||
"""Return test name string representation.""" | ||
return str(self.path.relative_to(os.getcwd())) | ||
|
||
|
||
class MoleculeItem(pytest.Item): | ||
"""A molecule test. | ||
|
||
Pytest supports multiple tests per file, molecule only one "test". | ||
""" | ||
|
||
def __init__(self, name, parent): | ||
"""Construct MoleculeItem.""" | ||
self.funcargs = {} | ||
super().__init__(name, parent) | ||
moleculeyml = self.path | ||
with open(str(moleculeyml), encoding="utf-8") as stream: | ||
# If the molecule.yml file is empty, YAML loader returns None. To | ||
# simplify things down the road, we replace None with an empty | ||
# dict. | ||
data = yaml.load(stream, Loader=yaml.SafeLoader) or {} | ||
|
||
# we add the driver as mark | ||
self.molecule_driver = data.get("driver", {}).get("name", "no_driver") | ||
self.add_marker(self.molecule_driver) | ||
|
||
# check for known markers and add them | ||
markers = data.get("markers", []) | ||
if "xfail" in markers: | ||
self.add_marker( | ||
pytest.mark.xfail( | ||
reason="Marked as broken by scenario configuration.", | ||
), | ||
) | ||
if "skip" in markers: | ||
self.add_marker( | ||
pytest.mark.skip(reason="Disabled by scenario configuration."), | ||
) | ||
|
||
# we also add platforms as marks | ||
for platform in data.get("platforms", []): | ||
platform_name = platform["name"] | ||
self.config.addinivalue_line( | ||
"markers", | ||
f"{platform_name}: molecule platform name is {platform_name}", | ||
) | ||
self.add_marker(platform_name) | ||
self.add_marker("molecule") | ||
if ( | ||
self.config.option.molecule_unavailable_driver | ||
and not self.config.option.molecule[self.molecule_driver]["available"] | ||
): | ||
self.add_marker(self.config.option.molecule_unavailable_driver) | ||
|
||
def runtest(self): | ||
"""Perform effective test run.""" | ||
folder = self.path.parent | ||
folders = folder.parts | ||
cwd = os.path.abspath(os.path.join(folder, "../..")) | ||
scenario = folders[-1] | ||
|
||
cmd = [sys.executable, "-m", "molecule"] | ||
if self.config.option.molecule_base_config: | ||
cmd.extend(("--base-config", self.config.option.molecule_base_config)) | ||
if self.config.option.skip_no_git_change: | ||
try: | ||
with subprocess.Popen( | ||
["git", "diff", self.config.option.skip_no_git_change, "--", "./"], | ||
cwd=cwd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
) as proc: | ||
proc.wait() | ||
if len(proc.stdout.readlines()) == 0: | ||
pytest.skip("No change in role") | ||
except subprocess.CalledProcessError as exc: | ||
pytest.fail( | ||
"Error checking git diff. Error code was: " | ||
+ str(exc.returncode) | ||
+ "\nError output was: " | ||
+ exc.output, | ||
) | ||
|
||
cmd.extend((self.name, "-s", scenario)) | ||
# We append the additional options to molecule call, allowing user to | ||
# control how molecule is called by pytest-molecule | ||
opts = os.environ.get("MOLECULE_OPTS") | ||
if opts: | ||
cmd.extend(shlex.split(opts)) | ||
|
||
print(f"running: {' '.join(quote(arg) for arg in cmd)} (from {cwd})") | ||
if self.config.getoption("--molecule"): # Check if --molecule option is enabled | ||
try: | ||
# Workaround for STDOUT/STDERR line ordering issue: | ||
# https://github.com/pytest-dev/pytest/issues/5449 | ||
with subprocess.Popen( | ||
cmd, | ||
cwd=cwd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
) as proc: | ||
for line in proc.stdout: | ||
print(line, end="") | ||
proc.wait() | ||
if proc.returncode != 0: | ||
pytest.fail( | ||
f"Error code {proc.returncode} returned by: {' '.join(cmd)}", | ||
pytrace=False, | ||
) | ||
except subprocess.CalledProcessError as exc: | ||
pytest.fail( | ||
f"Exception {exc} returned by: {' '.join(cmd)}", | ||
pytrace=False, | ||
) | ||
else: | ||
pytest.skip( | ||
"Molecule tests are disabled", | ||
) # Skip the test if --molecule option is not enabled | ||
|
||
def reportinfo(self): | ||
"""Return representation of test location when in verbose mode.""" | ||
return self.fspath, 0, f"usecase: {self.name}" | ||
|
||
def __str__(self): | ||
"""Return name of the test.""" | ||
return f"{self.name}[{self.molecule_driver}]" | ||
|
||
|
||
class MoleculeExceptionError(Exception): | ||
"""Custom exception for error reporting.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is why py3.7 and 3.8 are failing, core is pinned here, but to a version that isn't compatible with 3.7 or 3.8