Extend pytest so it can accept ansible args

This change adds the ability add ansible arguments to any
molecule test.

Change-Id: I7159e89d7037b2c669208014d4f814de2eb6f1ad
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2019-07-05 10:46:56 -05:00
parent 7ac1086ff5
commit 7d365401c6
No known key found for this signature in database
GPG Key ID: CE94BD890A47B20A
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")
ansible_args = pytestconfig.getoption("ansible_args")
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)