URL: https://github.com/freeipa/freeipa/pull/475
Author: tiran
 Title: #475: Add options to run only ipaclient unittests
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/475/head:pr475
git checkout pr475
From 9a1f7c67966e9db5402537277bd3eb194c819e03 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 17 Feb 2017 08:39:54 +0100
Subject: [PATCH 1/3] Add options to run only ipaclient unittests

A new option for ipa-run-tests makes the test runner ignore
subdirectories or skips tests that depend on the ipaserver package or on
a running framework for RPC integration tests. The new option enables
testing of client-only builds.

$ ipatests/ipa-run-tests --ipaclient-unittests
...
platform linux2 -- Python 2.7.13, pytest-2.9.2, py-1.4.32, pluggy-0.3.1
rootdir: /home/heimes/redhat, inifile: tox.ini
plugins: sourceorder-0.5, cov-2.3.0, betamax-0.7.1, multihost-1.1
collected 451 items

test_util.py ........
util.py ..
test_ipaclient/test_csrgen.py ..............ssss...
test_ipalib/test_aci.py ...................
test_ipalib/test_backend.py ........
test_ipalib/test_base.py ...............
test_ipalib/test_capabilities.py .
test_ipalib/test_cli.py ...
test_ipalib/test_config.py ...............
test_ipalib/test_crud.py ...............
test_ipalib/test_errors.py .......
test_ipalib/test_frontend.py ........................................
test_ipalib/test_messages.py ....
test_ipalib/test_output.py ...
test_ipalib/test_parameters.py .............................................................
test_ipalib/test_plugable.py ........
test_ipalib/test_rpc.py ......ssssssss
test_ipalib/test_text.py .............................
test_ipalib/test_x509.py ...
test_ipapython/test_cookie.py ............
test_ipapython/test_dn.py ...........................
test_ipapython/test_ipautil.py ..................................................................
test_ipapython/test_ipavalidate.py ..........
test_ipapython/test_kerberos.py ..............
test_ipapython/test_keyring.py ..........
test_ipapython/test_ssh.py ...............................
test_pkcs10/test_pkcs10.py .....

https://fedorahosted.org/freeipa/ticket/6517

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipatests/conftest.py                   | 63 +++++++++++++++++++++++++++++++++-
 ipatests/setup.py                      |  1 -
 ipatests/test_ipaclient/test_csrgen.py |  1 +
 ipatests/test_ipalib/test_rpc.py       |  2 ++
 ipatests/util.py                       | 15 ++++++--
 5 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/ipatests/conftest.py b/ipatests/conftest.py
index 511d7b7..6c13e23 100644
--- a/ipatests/conftest.py
+++ b/ipatests/conftest.py
@@ -3,17 +3,26 @@
 #
 from __future__ import print_function
 
+import fnmatch
 import os
 import pprint
+import re
 import sys
 
+import pytest
+
 from ipalib import api
 from ipalib.cli import cli_plugins
 try:
+    import ipaplatform
+except ImportError:
+    ipaplatform = None
+try:
     import ipaserver
 except ImportError:
     ipaserver = None
 
+HERE = os.path.dirname(os.path.abspath(__file__))
 
 pytest_plugins = [
     'ipatests.pytest_plugins.additional_config',
@@ -31,6 +40,7 @@
     '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',
 ]
 
 
@@ -46,6 +56,28 @@
     'install/share'
 ]
 
+
+SKIP_IPASERVER_PATTERNS = [
+    # fnmatch patterns
+    'test_cmdline/*',
+    'test_install/*',
+    'test_integration/*',
+    'test_ipaserver/*',
+    'test_webui/*',
+    'test_xmlrpc/*'
+]
+
+if ipaplatform is None:
+    # test depends on ipaplatform
+    SKIP_IPASERVER_PATTERNS.append('test_ipaclient/test_csrgen.py')
+
+SKIP_IPASERVER_RE = re.compile(
+    '(' +
+    '|'.join(fnmatch.translate(pat) for pat in SKIP_IPASERVER_PATTERNS) +
+    ')'
+)
+
+
 INIVALUES = {
     'python_classes': ['test_', 'Test'],
     'python_files': ['test_*.py'],
@@ -75,13 +107,27 @@ def pytest_configure(config):
     config.option.doctestmodules = True
 
 
+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'
+    )
+
+
 def pytest_cmdline_main(config):
     api.bootstrap(
         context=u'cli', in_server=False, in_tree=True, fallback=False
     )
     for klass in cli_plugins:
         api.add_plugin(klass)
-    api.finalize()
+
+    # XXX workaround until https://fedorahosted.org/freeipa/ticket/6408 has
+    # been resolved.
+    if ipaserver is not None:
+        api.finalize()
+
     if config.option.verbose:
         print('api.env: ')
         pprint.pprint({k: api.env[k] for k in api.env})
@@ -89,3 +135,18 @@ def pytest_cmdline_main(config):
         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_ignore_collect(path, config):
+    if config.option.ipaclient_unittests:
+        relative = path.relto(HERE)
+        if SKIP_IPASERVER_RE.match(relative):
+            return True
+
+
+def pytest_runtest_setup(item):
+    if isinstance(item, item.Function):
+        if item.get_marker('skip_ipaclient_unittest'):
+            # pylint: disable=no-member
+            if pytest.config.option.ipaclient_unittests:
+                pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/setup.py b/ipatests/setup.py
index 46d51ff..337b7d6 100644
--- a/ipatests/setup.py
+++ b/ipatests/setup.py
@@ -64,7 +64,6 @@
             "gssapi",
             "ipaclient",
             "ipalib",
-            "ipaplatform",
             "ipapython",
             "nose",
             "polib",
diff --git a/ipatests/test_ipaclient/test_csrgen.py b/ipatests/test_ipaclient/test_csrgen.py
index 556f8e0..24467f0 100644
--- a/ipatests/test_ipaclient/test_csrgen.py
+++ b/ipatests/test_ipaclient/test_csrgen.py
@@ -185,6 +185,7 @@ def test_rules_for_profile_nosuchprofile(self, rule_provider):
             rule_provider.rules_for_profile('nosuchprofile', 'certutil')
 
 
+@pytest.mark.skip_ipaclient_unittest
 class test_CSRGenerator(object):
     def test_userCert_OpenSSL(self, generator):
         principal = {
diff --git a/ipatests/test_ipalib/test_rpc.py b/ipatests/test_ipalib/test_rpc.py
index 80cf2e7..7fe0580 100644
--- a/ipatests/test_ipalib/test_rpc.py
+++ b/ipatests/test_ipalib/test_rpc.py
@@ -23,6 +23,7 @@
 from __future__ import print_function
 
 import nose
+import pytest
 import six
 # pylint: disable=import-error
 from six.moves.xmlrpc_client import Binary, Fault, dumps, loads
@@ -256,6 +257,7 @@ class user_add(Command):
         assert context.xmlclient.conn._calledall() is True
 
 
+@pytest.mark.skip_ipaclient_unittest
 class test_xml_introspection(object):
     @classmethod
     def setup_class(cls):
diff --git a/ipatests/util.py b/ipatests/util.py
index f55ef73..3c5c3af 100644
--- a/ipatests/util.py
+++ b/ipatests/util.py
@@ -39,12 +39,23 @@
 
 import ipalib
 from ipalib import api
-from ipalib.install.kinit import kinit_keytab, kinit_password
 from ipalib.plugable import Plugin
 from ipalib.request import context
 from ipapython.dn import DN
 from ipapython.ipautil import run
-from ipaplatform.paths import paths
+
+try:
+    # not available with client-only wheel packages
+    from ipalib.install.kinit import kinit_keytab, kinit_password
+except ImportError:
+    kinit_keytab = kinit_password = None
+
+try:
+    # not available with client-only wheel packages
+    from ipaplatform.paths import paths
+except ImportError:
+    paths = None
+
 
 if six.PY3:
     unicode = str

From 40dae807b4ac4e02112c4fc3e187f3acb2ac4e20 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Tue, 28 Feb 2017 17:55:58 +0100
Subject: [PATCH 2/3] Alternative approach with pytest.skip() in every package

---
 ipatests/conftest.py                             | 31 +-----------------------
 ipatests/pytest_plugins/integration.py           |  7 +++---
 ipatests/test_cmdline/__init__.py                |  6 +++++
 ipatests/test_install/__init__.py                |  5 ++++
 ipatests/test_integration/__init__.py            |  5 ++++
 ipatests/test_integration/tasks.py               |  6 +++++
 ipatests/test_ipaserver/__init__.py              |  6 +++++
 ipatests/test_ipaserver/test_install/__init__.py |  0
 ipatests/test_webui/__init__.py                  |  5 ++++
 ipatests/test_xmlrpc/__init__.py                 |  5 ++++
 10 files changed, 43 insertions(+), 33 deletions(-)
 create mode 100644 ipatests/test_ipaserver/test_install/__init__.py

diff --git a/ipatests/conftest.py b/ipatests/conftest.py
index 6c13e23..60b0e81 100644
--- a/ipatests/conftest.py
+++ b/ipatests/conftest.py
@@ -3,16 +3,15 @@
 #
 from __future__ import print_function
 
-import fnmatch
 import os
 import pprint
-import re
 import sys
 
 import pytest
 
 from ipalib import api
 from ipalib.cli import cli_plugins
+
 try:
     import ipaplatform
 except ImportError:
@@ -57,27 +56,6 @@
 ]
 
 
-SKIP_IPASERVER_PATTERNS = [
-    # fnmatch patterns
-    'test_cmdline/*',
-    'test_install/*',
-    'test_integration/*',
-    'test_ipaserver/*',
-    'test_webui/*',
-    'test_xmlrpc/*'
-]
-
-if ipaplatform is None:
-    # test depends on ipaplatform
-    SKIP_IPASERVER_PATTERNS.append('test_ipaclient/test_csrgen.py')
-
-SKIP_IPASERVER_RE = re.compile(
-    '(' +
-    '|'.join(fnmatch.translate(pat) for pat in SKIP_IPASERVER_PATTERNS) +
-    ')'
-)
-
-
 INIVALUES = {
     'python_classes': ['test_', 'Test'],
     'python_files': ['test_*.py'],
@@ -137,13 +115,6 @@ def pytest_cmdline_main(config):
         print('sys.version: {}'.format(sys.version))
 
 
-def pytest_ignore_collect(path, config):
-    if config.option.ipaclient_unittests:
-        relative = path.relto(HERE)
-        if SKIP_IPASERVER_RE.match(relative):
-            return True
-
-
 def pytest_runtest_setup(item):
     if isinstance(item, item.Function):
         if item.get_marker('skip_ipaclient_unittest'):
diff --git a/ipatests/pytest_plugins/integration.py b/ipatests/pytest_plugins/integration.py
index de37bf3..1252f8b 100644
--- a/ipatests/pytest_plugins/integration.py
+++ b/ipatests/pytest_plugins/integration.py
@@ -30,9 +30,6 @@
 
 from ipapython import ipautil
 from ipapython.ipa_log_manager import log_mgr
-from ipatests.test_integration import tasks
-from ipatests.test_integration.config import Config
-from ipatests.test_integration.env_config import get_global_config
 
 log = log_mgr.get_logger(__name__)
 
@@ -155,6 +152,10 @@ def integration_logs(class_integration_logs, request):
 def mh(request, class_integration_logs):
     """IPA's multihost fixture object
     """
+    from ipatests.test_integration import tasks
+    from ipatests.test_integration.config import Config
+    from ipatests.test_integration.env_config import get_global_config
+
     cls = request.cls
 
     domain_description = {
diff --git a/ipatests/test_cmdline/__init__.py b/ipatests/test_cmdline/__init__.py
index 9da42e7..af8867e 100644
--- a/ipatests/test_cmdline/__init__.py
+++ b/ipatests/test_cmdline/__init__.py
@@ -1,3 +1,9 @@
 #
 # Copyright (C) 2015  FreeIPA Contributors see COPYING for license
 #
+
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/test_install/__init__.py b/ipatests/test_install/__init__.py
index 0ca31f1..37be9a6 100644
--- a/ipatests/test_install/__init__.py
+++ b/ipatests/test_install/__init__.py
@@ -20,3 +20,8 @@
 """
 Package containing LDAP updates unit tests.
 """
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
\ No newline at end of file
diff --git a/ipatests/test_integration/__init__.py b/ipatests/test_integration/__init__.py
index b63d650..8779f2f 100644
--- a/ipatests/test_integration/__init__.py
+++ b/ipatests/test_integration/__init__.py
@@ -16,3 +16,8 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/test_integration/tasks.py b/ipatests/test_integration/tasks.py
index 6620d12..8d7c33a 100644
--- a/ipatests/test_integration/tasks.py
+++ b/ipatests/test_integration/tasks.py
@@ -28,6 +28,7 @@
 import time
 
 import dns
+import pytest
 from ldif import LDIFWriter
 from SSSDConfig import SSSDConfig
 from six import StringIO
@@ -44,6 +45,11 @@
 from ipalib.constants import DOMAIN_SUFFIX_NAME
 from ipalib.constants import DOMAIN_LEVEL_0
 
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
+
+
 log = log_mgr.get_logger(__name__)
 
 
diff --git a/ipatests/test_ipaserver/__init__.py b/ipatests/test_ipaserver/__init__.py
index 2192cc2..76942c7 100644
--- a/ipatests/test_ipaserver/__init__.py
+++ b/ipatests/test_ipaserver/__init__.py
@@ -20,3 +20,9 @@
 """
 Sub-package containing unit tests for `ipaserver` package.
 """
+
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/test_ipaserver/test_install/__init__.py b/ipatests/test_ipaserver/test_install/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/ipatests/test_webui/__init__.py b/ipatests/test_webui/__init__.py
index 56d10d9..cb2f361 100644
--- a/ipatests/test_webui/__init__.py
+++ b/ipatests/test_webui/__init__.py
@@ -20,3 +20,8 @@
 """
 Sub-package containing Web UI integration tests
 """
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/test_xmlrpc/__init__.py b/ipatests/test_xmlrpc/__init__.py
index 1a8ecf1..720c61b 100644
--- a/ipatests/test_xmlrpc/__init__.py
+++ b/ipatests/test_xmlrpc/__init__.py
@@ -20,3 +20,8 @@
 """
 Sub-package containing unit tests for `xmlrpc` package.
 """
+import pytest
+
+
+if pytest.config.getoption('ipaclient_unittests', False):
+    pytest.skip("Skip in ipaclient unittest mode")

From 42942133b23039f49138102d81e7d4af93d17354 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Thu, 2 Mar 2017 10:42:06 +0100
Subject: [PATCH 3/3] Fix pylint errors

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipatests/conftest.py                                     |  2 +-
 ipatests/test_install/__init__.py                        |  2 +-
 .../test_ipaserver/test_install/test_adtrustinstance.py  |  8 ++------
 ipatests/util.py                                         |  2 +-
 pylint_plugins.py                                        | 16 +++++++++++++++-
 5 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/ipatests/conftest.py b/ipatests/conftest.py
index 60b0e81..61e889d 100644
--- a/ipatests/conftest.py
+++ b/ipatests/conftest.py
@@ -13,7 +13,7 @@
 from ipalib.cli import cli_plugins
 
 try:
-    import ipaplatform
+    import ipaplatform  # pylint: disable=unused-import
 except ImportError:
     ipaplatform = None
 try:
diff --git a/ipatests/test_install/__init__.py b/ipatests/test_install/__init__.py
index 37be9a6..54ef9eb 100644
--- a/ipatests/test_install/__init__.py
+++ b/ipatests/test_install/__init__.py
@@ -24,4 +24,4 @@
 
 
 if pytest.config.getoption('ipaclient_unittests', False):
-    pytest.skip("Skip in ipaclient unittest mode")
\ No newline at end of file
+    pytest.skip("Skip in ipaclient unittest mode")
diff --git a/ipatests/test_ipaserver/test_install/test_adtrustinstance.py b/ipatests/test_ipaserver/test_install/test_adtrustinstance.py
index 6a050c8..86596c5 100755
--- a/ipatests/test_ipaserver/test_install/test_adtrustinstance.py
+++ b/ipatests/test_ipaserver/test_install/test_adtrustinstance.py
@@ -19,12 +19,8 @@
 """
 Test `adtrustinstance`
 """
-
-import os
-
-import six
-import nose
 import pytest
+import six
 
 from ipaserver.install import adtrustinstance
 
@@ -33,7 +29,7 @@
 
 
 @pytest.mark.tier0
-class test_adtrustinstance:
+class test_adtrustinstance(object):
     """
     Test `adtrustinstance`.
     """
diff --git a/ipatests/util.py b/ipatests/util.py
index 3c5c3af..4379c30 100644
--- a/ipatests/util.py
+++ b/ipatests/util.py
@@ -347,7 +347,7 @@ def assert_deepequal(expected, got, doc='', stack=tuple()):
     their elements does not matter.
     """
     try:
-        pretty_print = pytest.config.getoption("pretty_print")  # pylint: disable=no-member
+        pretty_print = pytest.config.getoption("pretty_print")
     except (AttributeError, ValueError):
         pretty_print = False
 
diff --git a/pylint_plugins.py b/pylint_plugins.py
index 45adf71..43450bf 100644
--- a/pylint_plugins.py
+++ b/pylint_plugins.py
@@ -6,9 +6,11 @@
 
 import copy
 import sys
+import textwrap
 
-from astroid import MANAGER
+from astroid import MANAGER, register_module_extender
 from astroid import scoped_nodes
+from astroid.builder import AstroidBuilder
 
 
 def register(linter):
@@ -252,3 +254,15 @@ def fix_ipa_classes(cls):
         fake_class(cls, ipa_class_members[class_name_with_module])
 
 MANAGER.register_transform(scoped_nodes.Class, fix_ipa_classes)
+
+
+def pytest_config_transform():
+    """pylint.config attribute
+    """
+    return AstroidBuilder(MANAGER).string_build(textwrap.dedent('''
+    from _pytest.config import get_config
+    config = get_config()
+    '''))
+
+
+register_module_extender(MANAGER, 'pytest', pytest_config_transform)
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to