Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Commit

Permalink
Make mock optional and use unittest.mock
Browse files Browse the repository at this point in the history
The mock package is not available on some target platforms. An optional
dependenyc allows us to run a subset of tests on these platforms.

Starting with Python 3.3, the mock package is available as
unittest.mock. The variant in stdlib is a bit more strict, too.

Signed-off-by: Christian Heimes <[email protected]>
  • Loading branch information
tiran committed May 4, 2017
1 parent 2601b34 commit 9630c3d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
31 changes: 22 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@
#
# Copyright (C) 2016 Custodia project Contributors, for licensee see COPYING

import sys

import setuptools
from setuptools import setup

SETUPTOOLS_VERSION = tuple(int(v) for v in setuptools.__version__.split("."))


requirements = [
'custodia >= 0.4.0',
'ipalib >= 4.5.0',
'ipaclient >= 4.5.0',
'six',
]
# test requirements
test_requires = ['coverage', 'pytest', 'mock']
test_pylint_requires = ['pylint'] + test_requires
test_pep8_requires = ['flake8', 'flake8-import-order', 'pep8-naming']
test_requires = ['coverage', 'pytest']

extras_require = {
'test': test_requires,
'test_docs': ['docutils', 'markdown'],
'test_pep8': ['flake8', 'flake8-import-order', 'pep8-naming'],
'test_pylint': ['pylint'] + test_requires,
}

# backwards compatibility with old setuptools
# unittest.mock was added in Python 3.3
if SETUPTOOLS_VERSION < (18, 0, 0) and sys.version_info < (3, 3):
requirements.append('mock')
else:
extras_require[':python_version<"3.3"'] = ['mock']


with open('README') as f:
Expand Down Expand Up @@ -55,10 +73,5 @@
],
install_requires=requirements,
tests_require=test_requires,
extras_require={
'test': test_requires,
'test_docs': ['docutils', 'markdown'],
'test_pep8': test_pep8_requires,
'test_pylint': test_pylint_requires,
},
extras_require=extras_require,
)
18 changes: 12 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import ipalib
from ipalib.errors import NotFound

import mock

import pkg_resources

import pytest
Expand All @@ -22,6 +20,14 @@
from custodia.ipa.interface import IPAInterface
from custodia.ipa.vault import IPAVault, krb5_unparse_principal_name

try:
from unittest import mock
except ImportError:
try:
import mock
except ImportError:
mock = None


CONFIG = u"""
[store:ipa_service]
Expand Down Expand Up @@ -150,7 +156,6 @@
-----END CERTIFICATE-----
"""


vault_parametrize = pytest.mark.parametrize(
'plugin,vault_type,vault_args',
[
Expand All @@ -161,6 +166,7 @@
)


@pytest.mark.skipif(mock is None, reason='requires mock')
class BaseTest(object):
def setup_method(self, method):
self.parser = configparser.ConfigParser(
Expand Down Expand Up @@ -218,9 +224,9 @@ def test_api_init(self):

m_api.Backend.rpcclient.isconnected.return_value = False
with ipa:
m_api.Backend.rpcclient.connect.assert_called()
m_api.Backend.rpcclient.connect.assert_any_call()
m_api.Backend.rpcclient.isconnected.return_value = True
m_api.Backend.rpcclient.disconnect.assert_called()
m_api.Backend.rpcclient.disconnect.assert_any_call()

assert os.environ == dict(
NSS_STRICT_NOFORK='DISABLED',
Expand Down Expand Up @@ -262,7 +268,7 @@ def test_vault_set(self, plugin, vault_type, vault_args):
ipa = self.mkinstance('[email protected]', 'store:ipa_autodiscover')
ipa = IPAVault(self.parser, plugin)
assert ipa.vault_type == vault_type
self.m_api.Command.ping.assert_called_once()
self.m_api.Command.ping.assert_called_once_with()
ipa.set('directory/testkey', 'testvalue')
self.m_api.Command.vault_add.assert_called_once_with(
'directory__testkey',
Expand Down

0 comments on commit 9630c3d

Please sign in to comment.