From 5eac09e66b4d4b2a25b6b57d8f97608f3a306156 Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Mon, 6 Nov 2017 22:40:59 -0500 Subject: [PATCH] Mock filecache.CACHE in unit tests This mocks the global filecache.CACHE in unit tests that modify the value. This is to avoid errors when other unit tests are running at the same time, that use that same variable. For example, ..unit.test_client.ClientTest.test_loader_arguments_token has failed with ... File "ironicclient/common/filecache.py", line 103, in retrieve_data data = _get_cache().get(key, expiration_time=expiry) AttributeError: 'int' object has no attribute 'get' Change-Id: I84b9c6699c98d1fa642247808b6ddea4fae1e8d0 --- ironicclient/tests/unit/common/test_filecache.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ironicclient/tests/unit/common/test_filecache.py b/ironicclient/tests/unit/common/test_filecache.py index 5df404674..edee043dd 100644 --- a/ironicclient/tests/unit/common/test_filecache.py +++ b/ironicclient/tests/unit/common/test_filecache.py @@ -29,6 +29,7 @@ class FileCacheTest(utils.BaseTestCase): result = filecache._build_key(None, None) self.assertEqual('None:None', result) + @mock.patch.object(filecache, 'CACHE', None) @mock.patch.object(os.environ, 'get', autospec=True) @mock.patch.object(os.path, 'exists', autospec=True) @mock.patch.object(os, 'makedirs', autospec=True) @@ -38,12 +39,12 @@ class FileCacheTest(utils.BaseTestCase): cache_val = 6 # If not present in the env, get will return the defaulted value mock_get.return_value = filecache.DEFAULT_EXPIRY - filecache.CACHE = None mock_exists.return_value = False cache_region = mock.Mock(spec=dogpile.cache.region.CacheRegion) cache_region.configure.return_value = cache_val mock_makeregion.return_value = cache_region self.assertEqual(cache_val, filecache._get_cache()) + self.assertEqual(cache_val, filecache.CACHE) mock_exists.assert_called_once_with(filecache.CACHE_DIR) mock_makedirs.assert_called_once_with(filecache.CACHE_DIR) mock_get.assert_called_once_with(filecache.CACHE_EXPIRY_ENV_VAR, @@ -53,6 +54,7 @@ class FileCacheTest(utils.BaseTestCase): arguments=mock.ANY, expiration_time=filecache.DEFAULT_EXPIRY) + @mock.patch.object(filecache, 'CACHE', None) @mock.patch.object(os.environ, 'get', autospec=True) @mock.patch.object(os.path, 'exists', autospec=True) @mock.patch.object(os, 'makedirs', autospec=True) @@ -62,12 +64,12 @@ class FileCacheTest(utils.BaseTestCase): cache_val = 5643 cache_expiry = '78' mock_get.return_value = cache_expiry - filecache.CACHE = None mock_exists.return_value = False cache_region = mock.Mock(spec=dogpile.cache.region.CacheRegion) cache_region.configure.return_value = cache_val mock_makeregion.return_value = cache_region self.assertEqual(cache_val, filecache._get_cache()) + self.assertEqual(cache_val, filecache.CACHE) mock_get.assert_called_once_with(filecache.CACHE_EXPIRY_ENV_VAR, mock.ANY) cache_region.configure.assert_called_once_with( @@ -75,6 +77,7 @@ class FileCacheTest(utils.BaseTestCase): arguments=mock.ANY, expiration_time=int(cache_expiry)) + @mock.patch.object(filecache, 'CACHE', None) @mock.patch.object(filecache.LOG, 'warning', autospec=True) @mock.patch.object(os.environ, 'get', autospec=True) @mock.patch.object(os.path, 'exists', autospec=True) @@ -86,12 +89,12 @@ class FileCacheTest(utils.BaseTestCase): cache_val = 5643 cache_expiry = 'Rollenhagen' mock_get.return_value = cache_expiry - filecache.CACHE = None mock_exists.return_value = False cache_region = mock.Mock(spec=dogpile.cache.region.CacheRegion) cache_region.configure.return_value = cache_val mock_makeregion.return_value = cache_region self.assertEqual(cache_val, filecache._get_cache()) + self.assertEqual(cache_val, filecache.CACHE) mock_get.assert_called_once_with(filecache.CACHE_EXPIRY_ENV_VAR, mock.ANY) cache_region.configure.assert_called_once_with( @@ -103,13 +106,13 @@ class FileCacheTest(utils.BaseTestCase): 'env_var': filecache.CACHE_EXPIRY_ENV_VAR} mock_log.assert_called_once_with(mock.ANY, log_dict) + @mock.patch.object(filecache, 'CACHE', 5552368) @mock.patch.object(os.path, 'exists', autospec=True) @mock.patch.object(os, 'makedirs', autospec=True) def test__get_cache_dir_already_exists(self, mock_makedirs, mock_exists): - cache_val = 5552368 mock_exists.return_value = True - filecache.CACHE = cache_val - self.assertEqual(cache_val, filecache._get_cache()) + self.assertEqual(5552368, filecache._get_cache()) + self.assertEqual(5552368, filecache.CACHE) self.assertEqual(0, mock_exists.call_count) self.assertEqual(0, mock_makedirs.call_count)