1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
#
from __future__ import print_function
import os
import pprint
import shutil
import sys
import tempfile
import pytest
from ipalib import api
from ipalib.cli import cli_plugins
import ipatests.util
try:
import ipaplatform # pylint: disable=unused-import
except ImportError:
ipaplatform = None
osinfo = None
else:
from ipaplatform.osinfo import osinfo
HERE = os.path.dirname(os.path.abspath(__file__))
class PytestIPADeprecationWarning(pytest.PytestWarning, DeprecationWarning):
"""Warning class for features that will be removed in a future version."""
pytest_plugins = [
'ipatests.pytest_ipa.additional_config',
'ipatests.pytest_ipa.deprecated_frameworks',
'ipatests.pytest_ipa.slicing',
'ipatests.pytest_ipa.beakerlib',
'ipatests.pytest_ipa.declarative',
'ipatests.pytest_ipa.nose_compat',
'ipatests.pytest_ipa.integration',
'pytester',
]
MARKERS = [
'tier0: basic unit tests and critical functionality',
'tier1: functional API tests',
'cs_acceptance: Acceptance test suite for Dogtag Certificate Server',
'ds_acceptance: Acceptance test suite for 389 Directory Server',
'skip_ipaclient_unittest: Skip in ipaclient unittest mode',
'needs_ipaapi: Test needs IPA API',
('skip_if_platform(platform, reason): Skip test on platform '
'(ID and ID_LIKE)'),
('skip_if_container(type, reason): Skip test on container '
'("any" or specific type)'),
]
NO_RECURSE_DIRS = [
# build directories
'ipaclient/build',
'ipalib/build',
'ipaplatform/build',
'ipapython/build',
'ipaserver/build',
'ipatests/build',
# install/share/wsgi.py
'install/share',
# integration plugin imports from ipaplatform
'ipatests/pytest_ipa',
'ipatests/azure',
]
INIVALUES = {
'python_classes': ['test_', 'Test'],
'python_files': ['test_*.py'],
'python_functions': ['test_*'],
}
def pytest_configure(config):
# add pytest markers
for marker in MARKERS:
config.addinivalue_line('markers', marker)
# do not recurse into build directories or install/share directory.
for norecursedir in NO_RECURSE_DIRS:
config.addinivalue_line('norecursedirs', norecursedir)
# addinivalue_line() adds duplicated entries and does not remove existing.
for name, values in INIVALUES.items():
current = config.getini(name)
current[:] = values
# set default JUnit prefix
if config.option.junitprefix is None:
config.option.junitprefix = 'ipa'
# always run doc tests
config.option.doctestmodules = True
# apply global options
ipatests.util.SKIP_IPAAPI = config.option.skip_ipaapi
ipatests.util.IPACLIENT_UNITTESTS = config.option.ipaclient_unittests
ipatests.util.PRETTY_PRINT = config.option.pretty_print
def pytest_addoption(parser):
group = parser.getgroup("IPA integration tests")
group.addoption(
'--ipaclient-unittests',
help='Run ipaclient unit tests only (no RPC and ipaserver)',
action='store_true'
)
group.addoption(
'--skip-ipaapi',
help='Do not run tests that depends on IPA API',
action='store_true',
)
def pytest_cmdline_main(config):
kwargs = dict(
context=u'cli', in_server=False, fallback=False
)
# FIXME: workaround for https://pagure.io/freeipa/issue/8317
kwargs.update(in_tree=True)
if not os.path.isfile(os.path.expanduser('~/.ipa/default.conf')):
# dummy domain/host for machines without ~/.ipa/default.conf
kwargs.update(domain=u'ipa.test', server=u'master.ipa.test')
api.bootstrap(**kwargs)
for klass in cli_plugins:
api.add_plugin(klass)
# XXX workaround until https://fedorahosted.org/freeipa/ticket/6408 has
# been resolved.
if os.path.isfile(api.env.conf_default):
api.finalize()
if config.option.verbose:
print('api.env: ')
pprint.pprint({k: api.env[k] for k in api.env})
print("uname: {}".format(os.uname()))
print("euid: {}, egid: {}".format(os.geteuid(), os.getegid()))
print("working dir: {}".format(os.path.abspath(os.getcwd())))
print('sys.version: {}'.format(sys.version))
def pytest_runtest_setup(item):
if isinstance(item, pytest.Function):
if item.get_closest_marker('skip_ipaclient_unittest'):
if item.config.option.ipaclient_unittests:
pytest.skip("Skip in ipaclient unittest mode")
if item.get_closest_marker('needs_ipaapi'):
if item.config.option.skip_ipaapi:
pytest.skip("Skip tests that needs an IPA API")
if osinfo is not None:
for mark in item.iter_markers(name="skip_if_platform"):
platform = mark.kwargs.get("platform")
if platform is None:
platform = mark.args[0]
reason = mark.kwargs["reason"]
if platform in osinfo.platform_ids:
pytest.skip(f"Skip test on platform {platform}: {reason}")
for mark in item.iter_markers(name="skip_if_container"):
container = mark.kwargs.get("container")
if container is None:
container = mark.args[0]
reason = mark.kwargs["reason"]
if osinfo.container is not None:
if container in ('any', osinfo.container):
pytest.skip(
f"Skip test on '{container}' container type: {reason}")
@pytest.fixture
def tempdir(request):
tempdir = tempfile.mkdtemp()
def fin():
shutil.rmtree(tempdir)
request.addfinalizer(fin)
return tempdir
|