from testinfra.utils.ansible_runner import AnsibleRunner testinfra_ansible = AnsibleRunner('.molecule/ansible_inventory') testinfra_hosts = testinfra_ansible.get_hosts('test') def test_command(Command): assert Command('docker ps | grep docker_registry').rc == 0
""" Test suite for the Molecule 'default' scenario. """ from os import environ from testinfra.utils.ansible_runner import AnsibleRunner import pytest # Create the `host` fixture parametrized over all configured test platforms. # Each `host` is a Testinfra Host instance. The inventory is created by the # Molecule framework, so this test suite must be run via *e.g.* `molecule test` # and not `pytest`. runner = AnsibleRunner(environ["MOLECULE_INVENTORY_FILE"]) testinfra_hosts = runner.get_hosts("all") @pytest.mark.parametrize("cmd", ("python3", "pip3", "virtualenv")) def test_commands(host, cmd): """ Test for installed Python commands. """ # For now this is just a basic smoke test. assert host.command(f"{cmd:s} --version").rc == 0 return
from ansible.playbook import Playbook import ansible except ImportError: raise RuntimeError("You must install ansible") import json import os PROJECT_ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) INVENTORY_FILE = os.path.join(PROJECT_ROOT_DIR, 'inventory') RUNNER = AnsibleRunner(INVENTORY_FILE) testinfra_hosts = RUNNER.get_hosts('all') def read_playbook(*args): args_list = list(args) cli = PlaybookCLI(args_list) parser = cli.base_parser( connect_opts=True, meta_opts=True, runas_opts=True, subset_opts=True, check_opts=True, inventory_opts=True, runtask_opts=True, vault_opts=True, fork_opts=True, module_opts=True,
from testinfra.utils.ansible_runner import AnsibleRunner runner = AnsibleRunner('.molecule/ansible_inventory') runner.options.connection = 'docker' testinfra_hosts = runner.get_hosts('yum_madison') def test_yum_madison(TestinfraBackend, Command): target = TestinfraBackend.get_hostname() expected_version = Command( "yum list | grep sudo | head -n 1 | awk '{ print $2'}") p = runner.run(target, 'yum_madison', 'name=sudo', check=False) assert p['versions'][0]['name'] == 'sudo' assert p['versions'][0]['version'] == expected_version.stdout.strip()
from testinfra.utils.ansible_runner import AnsibleRunner runner = AnsibleRunner('.molecule/ansible_inventory') runner.options.connection = 'docker' testinfra_hosts = runner.get_hosts('apt_madison') def test_apt_madison(TestinfraBackend, Command): target = TestinfraBackend.get_hostname() expected_version = Command( "dpkg -l | grep apt | head -n 1 | awk '{ print $3}'") p = runner.run(target, 'apt_madison', 'name=apt', check=False) assert p['versions'][0]['name'] == 'apt' assert p['versions'][0]['version'] == expected_version.stdout.strip()