]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix failures introduced by the new version of mock
authorKevin Benton <blak111@gmail.com>
Thu, 9 Jul 2015 23:54:23 +0000 (16:54 -0700)
committerKevin Benton <blak111@gmail.com>
Fri, 10 Jul 2015 00:54:32 +0000 (17:54 -0700)
This reverts commit 1b60df85ba3ad442c2e4e7e52538e1b9a1bf9378.

The new version of mock is now released which has the fix to
make mock.patch.stopall behave deterministically. The code to
stop double-mocking is no longer required.

Other fixes:

'assert_has_calls' that have changed behavior in the new version
(no longer accepts single calls).

Calls to non-existent assert methods that did not exist and were
silently passing.

Use of autospec on a class with decorated functions.

Closes-Bug: #1473369
Change-Id: I164a9af2a7f9ac0f0229ec3c5071f7a470445c98

neutron/tests/base.py
neutron/tests/unit/agent/linux/test_async_process.py
neutron/tests/unit/agent/linux/test_daemon.py
neutron/tests/unit/agent/linux/test_external_process.py
neutron/tests/unit/agent/linux/test_interface.py
neutron/tests/unit/agent/linux/test_polling.py
neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py
neutron/tests/unit/services/metering/agents/test_metering_agent.py

index 4cb79914aaa26d2fbb56b4204649bcb100b3bbc6..97e76eb1284eb9e51f168958d7051bbe6a78459e 100644 (file)
@@ -154,7 +154,6 @@ class DietTestCase(testtools.TestCase):
         self.useFixture(fixtures.NestedTempfile())
         self.useFixture(fixtures.TempHomeDir())
 
-        self.setup_double_mock_guard()
         self.addCleanup(mock.patch.stopall)
 
         if bool_from_env('OS_STDOUT_CAPTURE'):
@@ -167,34 +166,6 @@ class DietTestCase(testtools.TestCase):
         self.addOnException(self.check_for_systemexit)
         self.orig_pid = os.getpid()
 
-    def setup_double_mock_guard(self):
-        # mock.patch.stopall() uses a set in python < 3.4 so patches may not
-        # be unwound in the same order they were applied. This can leak mocks
-        # and cause tests down the line to fail.
-        # More info: http://bugs.python.org/issue21239
-        #
-        # Use mock to patch mock.patch.start to check if a target has already
-        # been patched and fail if it has.
-        self.first_traceback = {}
-        orig_start = mock._patch.start
-
-        def new_start(mself):
-            mytarget = mself.getter()
-            myattr = mself.attribute
-            for patch in mself._active_patches:
-                if (mytarget, myattr) == (patch.target, patch.attribute):
-                    key = str((patch.target, patch.attribute))
-                    self.fail("mock.patch was setup on an already patched "
-                              "target %s.%s. Stop the original patch before "
-                              "starting a new one. Traceback of 1st patch: %s"
-                              % (mytarget, myattr,
-                                 ''.join(self.first_traceback.get(key, []))))
-            self.first_traceback[
-                str((mytarget, myattr))] = traceback.format_stack()[:-2]
-            return orig_start(mself)
-
-        mock.patch('mock._patch.start', new=new_start).start()
-
     def check_for_systemexit(self, exc_info):
         if isinstance(exc_info[1], SystemExit):
             if os.getpid() != self.orig_pid:
index 7791485de6e5db1444b8a45f4bb3d401baf119ef..7d116d14ba3226626687e1315b86b722ac495cdd 100644 (file)
@@ -57,7 +57,7 @@ class TestAsyncProcess(base.BaseTestCase):
         with mock.patch.object(self.proc, '_kill') as kill:
             self.proc._handle_process_error()
 
-        kill.assert_has_calls(mock.call(respawning=False))
+        kill.assert_has_calls([mock.call(respawning=False)])
 
     def test__handle_process_error_kills_without_respawn(self):
         self.proc.respawn_interval = 1
@@ -66,8 +66,8 @@ class TestAsyncProcess(base.BaseTestCase):
                 with mock.patch('eventlet.sleep') as sleep:
                     self.proc._handle_process_error()
 
-        kill.assert_has_calls(mock.call(respawning=True))
-        sleep.assert_has_calls(mock.call(self.proc.respawn_interval))
+        kill.assert_has_calls([mock.call(respawning=True)])
+        sleep.assert_has_calls([mock.call(self.proc.respawn_interval)])
         spawn.assert_called_once_with()
 
     def _test__watch_process(self, callback, kill_event):
index 5914c55f48324a7d280819dd5ceca0c0d3206633..e934880231795f2550237b8adf315e052e2b127d 100644 (file)
@@ -49,7 +49,7 @@ class TestPrivileges(base.BaseTestCase):
             with mock.patch.object(daemon.LOG, 'critical') as log_critical:
                 self.assertRaises(exceptions.FailToDropPrivilegesExit,
                                   daemon.setuid, '321')
-                log_critical.assert_once_with(mock.ANY)
+                log_critical.assert_called_once_with(mock.ANY)
 
     def test_setgid_with_name(self):
         with mock.patch('grp.getgrnam', return_value=FakeEntry('gr_gid', 123)):
@@ -67,7 +67,7 @@ class TestPrivileges(base.BaseTestCase):
             with mock.patch.object(daemon.LOG, 'critical') as log_critical:
                 self.assertRaises(exceptions.FailToDropPrivilegesExit,
                                   daemon.setgid, '321')
-                log_critical.assert_once_with(mock.ANY)
+                log_critical.assert_called_once_with(mock.ANY)
 
     @mock.patch.object(os, 'setgroups')
     @mock.patch.object(daemon, 'setgid')
@@ -113,7 +113,7 @@ class TestPrivileges(base.BaseTestCase):
             with mock.patch.object(daemon.LOG, 'critical') as log_critical:
                 self.assertRaises(exceptions.FailToDropPrivilegesExit,
                                   daemon.drop_privileges, 'user')
-                log_critical.assert_once_with(mock.ANY)
+                log_critical.assert_called_once_with(mock.ANY)
 
 
 class TestPidfile(base.BaseTestCase):
index 079b370aea2ce8785f0ae6f8014a2ad0ac860f80..db84de21e4b0c7010e64dad0776445f161f0b16d 100644 (file)
@@ -171,8 +171,9 @@ class TestProcessManager(base.BaseTestCase):
 
                 with mock.patch.object(ep, 'utils') as utils:
                     manager.disable()
-                    utils.assert_has_calls(
-                        mock.call.execute(['kill', '-9', 4], run_as_root=True))
+                    utils.assert_has_calls([
+                        mock.call.execute(['kill', '-9', 4],
+                                          run_as_root=True)])
 
     def test_disable_namespace(self):
         with mock.patch.object(ep.ProcessManager, 'pid') as pid:
@@ -184,8 +185,9 @@ class TestProcessManager(base.BaseTestCase):
 
                 with mock.patch.object(ep, 'utils') as utils:
                     manager.disable()
-                    utils.assert_has_calls(
-                        mock.call.execute(['kill', '-9', 4], run_as_root=True))
+                    utils.assert_has_calls([
+                        mock.call.execute(['kill', '-9', 4],
+                                          run_as_root=True)])
 
     def test_disable_not_active(self):
         with mock.patch.object(ep.ProcessManager, 'pid') as pid:
index 0fdf3d744f0d6ccb44c106c74bc2239f46662543..7feb9981b7bea8a3e5a4a4bf5e8ce1256ecf9bd8 100644 (file)
@@ -695,4 +695,4 @@ class TestMidonetInterfaceDriver(TestBase):
         self.ip_dev.assert_has_calls([
             mock.call(self.device_name, namespace=self.namespace),
             mock.call().link.delete()])
-        self.ip.assert_has_calls(mock.call().garbage_collect_namespace())
+        self.ip.assert_has_calls([mock.call().garbage_collect_namespace()])
index ec408a235bc5d7b4a60bf30e5f10cf8dcc420ac5..b38c86b2d66de08ff2dc0e5d3c695046113941af 100644 (file)
@@ -32,8 +32,8 @@ class TestGetPollingManager(base.BaseTestCase):
                 with polling.get_polling_manager(minimize_polling=True) as pm:
                     self.assertEqual(pm.__class__,
                                      polling.InterfacePollingMinimizer)
-                mock_stop.assert_has_calls(mock.call())
-            mock_start.assert_has_calls(mock.call())
+                mock_stop.assert_has_calls([mock.call()])
+            mock_start.assert_has_calls([mock.call()])
 
 
 class TestInterfacePollingMinimizer(base.BaseTestCase):
index c17c57e27aa0de172603b56aa744b2ca26dce282..78eb49308a1c699b9cc7570051e46ff1cfcdbb14 100644 (file)
@@ -84,7 +84,7 @@ class TestDhcpRpcCallback(base.BaseTestCase):
     def _test__port_action_good_action(self, action, port, expected_call):
         self.callbacks._port_action(self.plugin, mock.Mock(),
                                     port, action)
-        self.plugin.assert_has_calls(expected_call)
+        self.plugin.assert_has_calls([expected_call])
 
     def test_port_action_create_port(self):
         self._test__port_action_good_action(
@@ -188,8 +188,8 @@ class TestDhcpRpcCallback(base.BaseTestCase):
                                         host='foo_host',
                                         port_id='foo_port_id',
                                         port=port)
-        self.plugin.assert_has_calls(
-            mock.call.update_port(mock.ANY, 'foo_port_id', expected_port))
+        self.plugin.assert_has_calls([
+            mock.call.update_port(mock.ANY, 'foo_port_id', expected_port)])
 
     def test_release_dhcp_port(self):
         port_retval = dict(id='port_id', fixed_ips=[dict(subnet_id='a')])
index 11601873c519fb704bc45c06453245cfbd4d7298..e95ef9bd3833ac05dc49b6e787f47a6815c209df 100644 (file)
@@ -63,7 +63,7 @@ class TestMeteringOperations(base.BaseTestCase):
         self.metering_rpc_patch = mock.patch(metering_rpc, return_value=[])
         self.metering_rpc_patch.start()
 
-        self.driver_patch = mock.patch(self.noop_driver, autospec=True)
+        self.driver_patch = mock.patch(self.noop_driver, spec=True)
         self.driver_patch.start()
 
         loopingcall_patch = mock.patch(