Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.

Commit 28f638a

Browse files
authored
Support test setups that use base config file (#88)
* Refactor option addition This is a preparatory commit that will make adding future command-line switches a bit easier. * Add base config option Up until now, it was fairly inconvenient to run molecule test suites that used based configuration file to reduce the duplication of content in the scenario configs. This commit adds a command line flag and related INI config option that allows us to pass --base-config flag to molecule when running tests.
1 parent d18a4a2 commit 28f638a

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

pytest_molecule/__init__.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,33 @@
2222
from _pytest.nodes import Node
2323

2424

25+
def _addoption(group, parser, ini_dest, default, help_msg):
26+
opt = "--" + ini_dest.replace("_", "-")
27+
group.addoption(opt, action="store", dest=ini_dest, default=default, help=help_msg)
28+
parser.addini(ini_dest, help_msg, default=default)
29+
30+
2531
def pytest_addoption(parser):
2632
"""Inject new command line options to pytest."""
2733
group = parser.getgroup("molecule")
28-
help_msg = (
34+
35+
_addoption(
36+
group,
37+
parser,
38+
"molecule_unavailable_driver",
39+
None,
2940
"What marker to add to molecule scenarios when driver is "
30-
"unavailable. (ex: skip, xfail). Default: None"
41+
"unavailable. (ex: skip, xfail). Default: None",
3142
)
32-
default = None
33-
dest = "molecule_unavailable_driver"
34-
35-
group.addoption(
36-
"--molecule-unavailable-driver",
37-
action="store",
38-
dest=dest,
39-
default=default,
40-
help=help_msg,
43+
_addoption(
44+
group,
45+
parser,
46+
"molecule_base_config",
47+
None,
48+
"Path to the molecule base config file. The value of this option is "
49+
"passed to molecule via the --base-config flag. Default: None",
4150
)
4251

43-
parser.addini(dest, help_msg, default=default)
44-
4552

4653
def pytest_configure(config):
4754
"""Pytest hook for loading our specific configuration."""
@@ -169,8 +176,11 @@ def runtest(self):
169176
cwd = os.path.abspath(os.path.join(self.fspath.dirname, "../.."))
170177
scenario = folders[-1]
171178
# role = folders[-3] # noqa
172-
cmd = [sys.executable, "-m", "molecule", self.name, "-s", scenario]
173179

180+
cmd = [sys.executable, "-m", "molecule"]
181+
if self.config.option.molecule_base_config:
182+
cmd.extend(("--base-config", self.config.option.molecule_base_config))
183+
cmd.extend((self.name, "-s", scenario))
174184
# We append the additional options to molecule call, allowing user to
175185
# control how molecule is called by pytest-molecule
176186
opts = os.environ.get("MOLECULE_OPTS")

tests/roles/base_config/base.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
scenario:
3+
test_sequence:
4+
- converge
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- name: Converge
2+
hosts: all
3+
gather_facts: false
4+
tasks:
5+
- name: Converge on s1
6+
debug:
7+
msg: Scenario 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
driver:
3+
name: delegated
4+
5+
platforms:
6+
- name: localhost
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: Prepare
3+
hosts: all
4+
gather_facts: false
5+
tasks:
6+
- name: This should not be executed if base config file is loaded
7+
fail:
8+
msg: Base config file loading is not working as expected

tox.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ commands =
2020
pytest --collect-only
2121
# pytest already needs built wheel in dist/
2222
pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} --molecule-unavailable-driver= -k foo
23+
pytest \
24+
--molecule-base-config base.yml \
25+
--color=yes \
26+
{tty:-s} \
27+
-k base_config
2328
# all extras installed in order to detect potential conflicts
2429
extras =
2530
ansi

0 commit comments

Comments
 (0)