From 7d365401c6546566d7244920a5d90fb30fc3ce86 Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Fri, 5 Jul 2019 10:46:56 -0500 Subject: [PATCH] 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 --- ...rgs-to-test-commands-e6b14d561af5de7d.yaml | 11 +++++++ tests/conftest.py | 1 + tests/test_molecule.py | 29 +++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/add-ansible-args-to-test-commands-e6b14d561af5de7d.yaml diff --git a/releasenotes/notes/add-ansible-args-to-test-commands-e6b14d561af5de7d.yaml b/releasenotes/notes/add-ansible-args-to-test-commands-e6b14d561af5de7d.yaml new file mode 100644 index 000000000..7d8852e1a --- /dev/null +++ b/releasenotes/notes/add-ansible-args-to-test-commands-e6b14d561af5de7d.yaml @@ -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' diff --git a/tests/conftest.py b/tests/conftest.py index ee1b37f9c..dd871b960 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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.') diff --git a/tests/test_molecule.py b/tests/test_molecule.py index 3a493c084..dc26792ad 100644 --- a/tests/test_molecule.py +++ b/tests/test_molecule.py @@ -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)