Files
ansible-freeipa/tests/test_playbook_runs.py
2020-05-19 19:21:53 -03:00

88 lines
2.2 KiB
Python

#!/usr/bin/env python
import os
import tempfile
from subprocess import Popen
from unittest import TestCase
import pytest
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
def get_inventory_content():
ipa_server_host = os.getenv("IPA_SERVER_HOST")
return "[ipaserver]\n{}".format(ipa_server_host).encode("utf8")
def run_playbook(playbook):
with tempfile.NamedTemporaryFile() as inventory_file:
inventory_file.write(get_inventory_content())
inventory_file.flush()
cmd = [
"ansible-playbook",
"-i",
inventory_file.name,
playbook,
]
process = Popen(cmd, cwd=SCRIPT_DIR)
process.wait()
return process
def list_test_yaml(dir_path):
yamls = []
for yaml_name in os.listdir(dir_path):
if yaml_name.startswith("test_") and yaml_name.endswith(".yml"):
yamls.append(
{
"path": os.path.join(dir_path, yaml_name),
"name": yaml_name.split(".")[0],
}
)
return yamls
def get_test_groups():
test_dirs = os.listdir(SCRIPT_DIR)
groups = {}
for test_group_dir in test_dirs:
group_dir_path = os.path.join(SCRIPT_DIR, test_group_dir)
if not os.path.isdir(group_dir_path):
continue
yamls = list_test_yaml(group_dir_path)
if yamls:
groups[test_group_dir] = yamls
return groups
def rename(newname):
def decorator(f):
f.__name__ = newname
return f
return decorator
# Dynamically create the TestCase classes with respective
# test_* methods.
for group_name, group_tests in get_test_groups().items():
_tests = {}
for test_config in group_tests:
test_name = test_config["name"].replace("-", "_")
@pytest.mark.skipif(
os.getenv("IPA_SERVER_HOST") is None,
reason="Environment variable IPA_SERVER_HOST must be set",
)
@rename(test_name)
def method(self):
result = run_playbook(test_config["path"])
assert result.returncode == 0
_tests[test_name] = method
globals()[group_name] = type(group_name, (TestCase,), _tests)