Add support to define which playbook tests to execute with pytest.

pytest provide the means to skip tests based on patterns, but writing
these patterns for ansible-freeipa might not be feasible.

This PR allows the selection of playbook tests and modules that will
be executed with pytest using the environmentt variables IPA_ENABLED_TESTS
IPA_ENABLED_MODULES, IPA_DISABLED_TESTS or IPA_DISABLED_MODULES.

When using IPA_ENABLED_MODULES, all modules will be disabled, and only
the modules in the enabled list will be tested. If using the test
filter, IPA_ENABLED_TESTS, all tests are disabled, unless they are in
the enabled test lists.

If the IPA_DISABLED_* version is used, tests and modules are enabled by
default, and the list is used to disable the module or specific test.

To disable a test or module in Azure CI, edit the file
`tests/azure/variables` and add the desired tests or modules to the
parameter variables `enabled_modules`, 'enabled_tests`, `disabled_tests`
or `disable_modules`.

Note that, if added to the `master` branch, this will affect the tests
for every pipeline that it is include (including 'nightly'), so it should
be used with care.

It can be used with TEMP commits to enable only the desired tests,
speeding up upstream tests.
This commit is contained in:
Rafael Guterres Jeffman
2020-08-17 20:58:54 -03:00
parent 4ff5aaa172
commit 13cff6354b
6 changed files with 123 additions and 11 deletions

View File

@@ -24,11 +24,12 @@ import functools
from unittest import TestCase
from utils import get_test_playbooks, get_server_host, run_playbook
from utils import get_test_playbooks, get_skip_conditions, run_playbook
def prepare_test(test_name, test_path):
"""Decorator for the tests generated automatically from playbooks.
def prepare_test(testname, testpath):
"""
Decorate tests generated automatically from playbooks.
Injects 2 arguments to the test (`test_path` and `test_name`) and
name the test method using test name (to ensure test reports are useful).
@@ -36,13 +37,13 @@ def prepare_test(test_name, test_path):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs["test_path"] = test_path
kwargs["test_name"] = test_name
kwargs["test_path"] = testpath
kwargs["test_name"] = testname
return func(*args, **kwargs)
return wrapper
decorator.__name__ = test_name
decorator.__name__ = testname
return decorator
@@ -50,18 +51,21 @@ def prepare_test(test_name, test_path):
# test_* methods.
for test_dir_name, playbooks_in_dir in get_test_playbooks().items():
_tests = {}
for playbook in playbooks_in_dir:
test_name = playbook["name"].replace("-", "_")
test_path = playbook["path"]
@pytest.mark.skipif(
not get_server_host(),
reason="Environment variable IPA_SERVER_HOST must be set",
)
skip = get_skip_conditions(test_dir_name, test_name) or {}
# pylint: disable=W0621,W0640,W0613
@pytest.mark.skipif(**skip)
@pytest.mark.playbook
@prepare_test(test_name, test_path)
def method(self, test_path, test_name):
run_playbook(test_path)
# pylint: enable=W0621,W0640,W0613
_tests[test_name] = method
globals()[test_dir_name] = type(test_dir_name, tuple([TestCase]), _tests,)