Skip to content

Commit

Permalink
run inventory tests with configurable inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Oct 9, 2020
1 parent 2cff556 commit 2c1bae3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test: $(MANIFEST) | tests/test_playbooks/vars/server.yml
$(PYTEST) $(TEST)

test-crud: $(MANIFEST) | tests/test_playbooks/vars/server.yml
$(PYTEST) 'tests/test_crud.py::test_crud'
$(PYTEST) 'tests/test_crud.py::test_crud' 'tests/test_crud.py::test_inventory'

test-check-mode: $(MANIFEST) | tests/test_playbooks/vars/server.yml
$(PYTEST) 'tests/test_crud.py::test_check_mode'
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def find_all_test_playbooks():
yield playbook.replace('.yml', '')


TEST_PLAYBOOKS = list(find_all_test_playbooks())
ALL_TEST_PLAYBOOKS = list(find_all_test_playbooks())
TEST_PLAYBOOKS = [playbook for playbook in ALL_TEST_PLAYBOOKS if not playbook.startswith('inventory_plugin')]
INVENTORY_PLAYBOOKS = set(ALL_TEST_PLAYBOOKS) - set(TEST_PLAYBOOKS)


def pytest_addoption(parser):
Expand Down
File renamed without changes.
35 changes: 21 additions & 14 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
import yaml

from .conftest import TEST_PLAYBOOKS
from .conftest import TEST_PLAYBOOKS, INVENTORY_PLAYBOOKS


if sys.version_info[0] == 2:
Expand All @@ -28,7 +28,7 @@ def get_foreman_url():
return server_yml_content['foreman_server_url']


def run_playbook_vcr(tmpdir, module, extra_vars=None, record=False, check_mode=False):
def run_playbook_vcr(tmpdir, module, extra_vars=None, inventory=None, record=False, check_mode=False):
if extra_vars is None:
extra_vars = {}
limit = None
Expand Down Expand Up @@ -58,15 +58,17 @@ def run_playbook_vcr(tmpdir, module, extra_vars=None, record=False, check_mode=F
fixture_dir = py.path.local(__file__).realpath() / '..' / 'fixtures'
fixture_dir.join(apidoc).copy(json_cache)

return run_playbook(module, extra_vars=extra_vars, limit=limit, check_mode=check_mode)
return run_playbook(module, extra_vars=extra_vars, limit=limit, inventory=inventory, check_mode=check_mode)


def run_playbook(module, extra_vars=None, limit=None, check_mode=False):
def run_playbook(module, extra_vars=None, limit=None, inventory=None, check_mode=False):
# Assemble parameters for playbook call
os.environ['ANSIBLE_CONFIG'] = os.path.join(os.getcwd(), 'ansible.cfg')
kwargs = {}
kwargs['playbook'] = os.path.join(os.getcwd(), 'tests', 'test_playbooks', '{}.yml'.format(module))
kwargs['inventory'] = os.path.join(os.getcwd(), 'tests', 'inventory')
if inventory is None:
inventory = os.path.join(os.getcwd(), 'tests', 'inventory', 'hosts')
kwargs['inventory'] = inventory
kwargs['verbosity'] = 4
if extra_vars:
kwargs['extravars'] = extra_vars
Expand All @@ -79,14 +81,6 @@ def run_playbook(module, extra_vars=None, limit=None, check_mode=False):

@pytest.mark.parametrize('module', TEST_PLAYBOOKS)
def test_crud(tmpdir, module, vcrmode):
if module == 'inventory_plugin':
try:
ansible_version = pkg_resources.get_distribution('ansible').version
except pkg_resources.DistributionNotFound:
ansible_version = pkg_resources.get_distribution('ansible-base').version
if distutils.version.LooseVersion(ansible_version) < distutils.version.LooseVersion('2.9'):
pytest.skip("This module should not be tested on Ansible before 2.9")

if vcrmode == "live":
run = run_playbook(module)
else:
Expand All @@ -97,7 +91,20 @@ def test_crud(tmpdir, module, vcrmode):

@pytest.mark.parametrize('module', TEST_PLAYBOOKS)
def test_check_mode(tmpdir, module):
if module in ['katello_manifest', 'inventory_plugin', 'templates_import']:
if module in ['katello_manifest', 'templates_import']:
pytest.skip("This module does not support check_mode.")
run = run_playbook_vcr(tmpdir, module, check_mode=True)
assert run.rc == 0


@pytest.mark.parametrize('module', INVENTORY_PLAYBOOKS)
def test_inventory(tmpdir, module):
try:
ansible_version = pkg_resources.get_distribution('ansible').version
except pkg_resources.DistributionNotFound:
ansible_version = pkg_resources.get_distribution('ansible-base').version
if distutils.version.LooseVersion(ansible_version) < distutils.version.LooseVersion('2.9'):
pytest.skip("This module should not be tested on Ansible before 2.9")
inventory = [os.path.join(os.getcwd(), 'tests', 'inventory', inv) for inv in ['hosts', "{}.foreman.yml".format(module)]]
run = run_playbook(module, inventory=inventory)
assert run.rc == 0

0 comments on commit 2c1bae3

Please sign in to comment.