Merge "Extend pytest so it can accept ansible args"

This commit is contained in:
Zuul 2019-07-07 13:47:06 +00:00 committed by Gerrit Code Review
commit 0dbcda2cbf
3 changed files with 35 additions and 6 deletions

View File

@ -0,0 +1,11 @@
---
features:
- |
A new argument has been added to the molecule test setup allowing
developers to run tests with Ansible command line arguments. This
feature is useful when testing roles that require augmentation,
like when tags are needed.
.. code-block:: console
pytest tests/test_molecule.py --scenario=${NEWROLENAME} --ansible-args='--tags xxx --skip-tags yyy'

View File

@ -17,3 +17,4 @@ import os
def pytest_addoption(parser):
parser.addoption('--scenario', help='scenario setting')
parser.addoption('--ansible-args', help='ansible args passed into test runner.')

View File

@ -18,11 +18,28 @@ import pytest
def test_molecule(pytestconfig):
cmd = ['python', '-m', 'molecule', 'test']
cmd = ['python', '-m', 'molecule']
scenario = pytestconfig.getoption("scenario")
if scenario:
cmd.extend(['--scenario-name', scenario])
else:
cmd.append('--all')
ansible_args = pytestconfig.getoption("ansible_args")
assert subprocess.call(cmd) == 0
if ansible_args:
cmd.append('converge')
if scenario:
cmd.extend(['--scenario-name', scenario])
cmd.append('--')
cmd.extend(ansible_args.split())
else:
cmd.append('test')
if scenario:
cmd.extend(['--scenario-name', scenario])
else:
cmd.append('--all')
try:
assert subprocess.call(cmd) == 0
finally:
if ansible_args:
cmd = ['python', '-m', 'molecule', 'destroy']
if scenario:
cmd.extend(['--scenario-name', scenario])
subprocess.call(cmd)