From 4a916d215bd251ce231171be6d14a29f532da28f Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Tue, 7 Aug 2018 14:13:23 +0800 Subject: [PATCH] Replace mox with mock Replace mox testing library by mock one in the file glance/tests/unit/api/test_cmd.py Change-Id: I2dde0ca59c4a80bd35c6c6b74c4d3231a97eaf74 partially-implements: blueprint replace-mox-with-mock --- glance/tests/unit/api/test_cmd.py | 41 +++++++++++++++++++------------ 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/glance/tests/unit/api/test_cmd.py b/glance/tests/unit/api/test_cmd.py index 21da156f3d..ca21f453c1 100644 --- a/glance/tests/unit/api/test_cmd.py +++ b/glance/tests/unit/api/test_cmd.py @@ -52,12 +52,19 @@ class TestGlanceApiCmd(test_utils.BaseTestCase): store.register_opts(CONF) - self.stubs.Set(glance.common.config, 'load_paste_app', - self._do_nothing) - self.stubs.Set(glance.common.wsgi.Server, 'start', - self._do_nothing) - self.stubs.Set(glance.common.wsgi.Server, 'wait', - self._do_nothing) + self.config_patcher = mock.patch( + 'glance.common.config.load_paste_app', + side_effect=self._do_nothing) + self.start_patcher = mock.patch('glance.common.wsgi.Server.start', + side_effect=self._do_nothing) + self.wait_patcher = mock.patch('glance.common.wsgi.Server.wait', + side_effect=self._do_nothing) + + self.addCleanup(mock.patch.stopall) + + self.config_patcher.start() + self.start_patcher.start() + self.wait_patcher.start() def tearDown(self): sys.stderr = sys.__stderr__ @@ -70,10 +77,10 @@ class TestGlanceApiCmd(test_utils.BaseTestCase): def test_worker_creation_failure(self): failure = exc.WorkerCreationFailure(reason='test') - self.stubs.Set(glance.common.wsgi.Server, 'start', - self._raise(failure)) - exit = self.assertRaises(SystemExit, glance.cmd.api.main) - self.assertEqual(2, exit.code) + with mock.patch('glance.common.wsgi.Server.start', + side_effect=self._raise(failure)): + exit = self.assertRaises(SystemExit, glance.cmd.api.main) + self.assertEqual(2, exit.code) @mock.patch.object(glance.common.config, 'parse_cache_args') @mock.patch.object(logging, 'setup') @@ -96,11 +103,12 @@ class TestGlanceApiCmd(test_utils.BaseTestCase): mock.call.mock_cache_clean()] self.assertEqual(expected_call_sequence, manager.mock_calls) + @mock.patch.object(glance.image_cache.cleaner.Cleaner, 'run') @mock.patch.object(glance.image_cache.base.CacheApp, '__init__') - def test_cache_cleaner_main_runtime_exception_handling(self, mock_cache): + def test_cache_cleaner_main_runtime_exception_handling(self, mock_cache, + mock_run): mock_cache.return_value = None - self.stubs.Set(glance.image_cache.cleaner.Cleaner, 'run', - self._raise(RuntimeError)) + mock_run.side_effect = self._raise(RuntimeError) exit = self.assertRaises(SystemExit, glance.cmd.cache_cleaner.main) self.assertEqual('ERROR: ', exit.code) @@ -125,11 +133,12 @@ class TestGlanceApiCmd(test_utils.BaseTestCase): mock.call.mock_cache_prune()] self.assertEqual(expected_call_sequence, manager.mock_calls) + @mock.patch.object(glance.image_cache.pruner.Pruner, 'run') @mock.patch.object(glance.image_cache.base.CacheApp, '__init__') - def test_cache_pruner_main_runtime_exception_handling(self, mock_cache): + def test_cache_pruner_main_runtime_exception_handling(self, mock_cache, + mock_run): mock_cache.return_value = None - self.stubs.Set(glance.image_cache.pruner.Pruner, 'run', - self._raise(RuntimeError)) + mock_run.side_effect = self._raise(RuntimeError) exit = self.assertRaises(SystemExit, glance.cmd.cache_pruner.main) self.assertEqual('ERROR: ', exit.code)