Blame tests/test_playbook_runs.py

Packit Service 0a38ef
#!/usr/bin/env python
Packit Service 0a38ef
Packit Service a166ed
# Authors:
Packit Service a166ed
#   Sergio Oliveira Campos <seocam@redhat.com>
Packit Service a166ed
#
Packit Service a166ed
# Copyright (C) 2020 Red Hat
Packit Service a166ed
# see file 'COPYING' for use and warranty information
Packit Service a166ed
#
Packit Service a166ed
# This program is free software; you can redistribute it and/or modify
Packit Service a166ed
# it under the terms of the GNU General Public License as published by
Packit Service a166ed
# the Free Software Foundation, either version 3 of the License, or
Packit Service a166ed
# (at your option) any later version.
Packit Service a166ed
#
Packit Service a166ed
# This program is distributed in the hope that it will be useful,
Packit Service a166ed
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a166ed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a166ed
# GNU General Public License for more details.
Packit Service a166ed
#
Packit Service a166ed
# You should have received a copy of the GNU General Public License
Packit Service a166ed
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 0a38ef
Packit Service 0a38ef
import pytest
Packit Service a166ed
import functools
Packit Service 0a38ef
Packit Service a166ed
from unittest import TestCase
Packit Service 0a38ef
Packit Service a166ed
from utils import get_test_playbooks, get_server_host, run_playbook
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
def prepare_test(test_name, test_path):
Packit Service a166ed
    """Decorator for the tests generated automatically from playbooks.
Packit Service a166ed
Packit Service a166ed
    Injects 2 arguments to the test (`test_path` and `test_name`) and
Packit Service a166ed
    name the test method using test name (to ensure test reports are useful).
Packit Service a166ed
    """
Packit Service 0a38ef
    def decorator(func):
Packit Service 0a38ef
        @functools.wraps(func)
Packit Service 0a38ef
        def wrapper(*args, **kwargs):
Packit Service 0a38ef
            kwargs["test_path"] = test_path
Packit Service a166ed
            kwargs["test_name"] = test_name
Packit Service 0a38ef
            return func(*args, **kwargs)
Packit Service 0a38ef
Packit Service 0a38ef
        return wrapper
Packit Service 0a38ef
Packit Service 0a38ef
    decorator.__name__ = test_name
Packit Service 0a38ef
    return decorator
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
# Dynamically create the TestCase classes with respective
Packit Service 0a38ef
#   test_* methods.
Packit Service a166ed
for test_dir_name, playbooks_in_dir in get_test_playbooks().items():
Packit Service 0a38ef
    _tests = {}
Packit Service a166ed
    for playbook in playbooks_in_dir:
Packit Service a166ed
        test_name = playbook["name"].replace("-", "_")
Packit Service a166ed
        test_path = playbook["path"]
Packit Service 0a38ef
Packit Service 0a38ef
        @pytest.mark.skipif(
Packit Service a166ed
            not get_server_host(),
Packit Service 0a38ef
            reason="Environment variable IPA_SERVER_HOST must be set",
Packit Service 0a38ef
        )
Packit Service a166ed
        @pytest.mark.playbook
Packit Service 0a38ef
        @prepare_test(test_name, test_path)
Packit Service a166ed
        def method(self, test_path, test_name):
Packit Service a166ed
            run_playbook(test_path)
Packit Service 0a38ef
Packit Service 0a38ef
        _tests[test_name] = method
Packit Service a166ed
    globals()[test_dir_name] = type(test_dir_name, tuple([TestCase]), _tests,)