Fixed a race condition caused by module imports when running tests in paraller

This commit is contained in:
Joona Hoikkala 2018-10-17 16:35:40 +03:00
parent 373f8ac85a
commit 85ce3cabb4
No known key found for this signature in database
GPG key ID: D5AA86BBF9B29A5C

View file

@ -13,6 +13,7 @@ from acme.magic_typing import Dict, List, Set, Union # pylint: disable=unused-i
from certbot import errors
from certbot_apache.tests import util
class MockDBM(object):
# pylint: disable=missing-docstring
"""Main mock DBM class for Py3 dbm module"""
@ -125,10 +126,19 @@ class OCSPPrefetchTest(util.ApacheTest):
def call_mocked_py3(self, func, *args, **kwargs):
"""Calls methods with imports mocked to suit Py3 environment"""
if 'bsddb' in sys.modules.keys():
sys.modules['bsddb'] = None
sys.modules['dbm'] = MockDBM()
return self._call_mocked(func, *args, **kwargs)
real_import = six.moves.builtins.__import__
def mock_import(*args, **kwargs):
"""Mock import to raise ImportError for Py2 specific module to make
ApacheConfigurator pick the correct one for Python3 regardless of the
python version the tests are running under."""
if args[0] == "bsddb":
raise ImportError
return real_import(*args, **kwargs)
with mock.patch('six.moves.builtins.__import__', side_effect=mock_import):
sys.modules['dbm'] = MockDBM()
return self._call_mocked(func, *args, **kwargs)
@mock.patch("certbot_apache.override_debian.DebianConfigurator.enable_mod")
def test_ocsp_prefetch_enable_mods(self, mock_enable):
@ -259,9 +269,11 @@ class OCSPPrefetchTest(util.ApacheTest):
def test_ocsp_prefetch_preflight_check_noerror(self):
self.call_mocked_py2(self.config._ensure_ocsp_prefetch_compatibility)
self.call_mocked_py3(self.config._ensure_ocsp_prefetch_compatibility)
with mock.patch("dbm.ndbm") as mock_ndbm:
mock_ndbm.library = 'Not Berkeley DB'
mockdbm_path = "certbot_apache.tests.ocsp_prefetch_test.Mockdbm_impl"
with mock.patch(mockdbm_path) as mock_dbm:
mock_dbm.library = 'Not Berkeley DB'
self.assertRaises(errors.NotSupportedError,
self.call_mocked_py3,
self.config._ensure_ocsp_prefetch_compatibility)
def test_ocsp_prefetch_open_dbm_no_file(self):
@ -306,7 +318,7 @@ class OCSPPrefetchTest(util.ApacheTest):
db = self.call_mocked_py3(
self.config._ocsp_dbm_open, self.db_path)
db[b'key'] = expected_val
self.call_mocked_py2(self.config._ocsp_dbm_close, db)
self.call_mocked_py3(self.config._ocsp_dbm_close, db)
db2 = self.call_mocked_py3(self.config._ocsp_dbm_open, self.db_path)
self.assertEquals(db2[b'key'], expected_val)