diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 4cb79914aaa..97e76eb1284 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -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: diff --git a/neutron/tests/unit/agent/linux/test_async_process.py b/neutron/tests/unit/agent/linux/test_async_process.py index 7791485de6e..7d116d14ba3 100644 --- a/neutron/tests/unit/agent/linux/test_async_process.py +++ b/neutron/tests/unit/agent/linux/test_async_process.py @@ -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): diff --git a/neutron/tests/unit/agent/linux/test_daemon.py b/neutron/tests/unit/agent/linux/test_daemon.py index 5914c55f483..e9348802317 100644 --- a/neutron/tests/unit/agent/linux/test_daemon.py +++ b/neutron/tests/unit/agent/linux/test_daemon.py @@ -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): diff --git a/neutron/tests/unit/agent/linux/test_external_process.py b/neutron/tests/unit/agent/linux/test_external_process.py index 079b370aea2..db84de21e4b 100644 --- a/neutron/tests/unit/agent/linux/test_external_process.py +++ b/neutron/tests/unit/agent/linux/test_external_process.py @@ -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: diff --git a/neutron/tests/unit/agent/linux/test_interface.py b/neutron/tests/unit/agent/linux/test_interface.py index 0fdf3d744f0..7feb9981b7b 100644 --- a/neutron/tests/unit/agent/linux/test_interface.py +++ b/neutron/tests/unit/agent/linux/test_interface.py @@ -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()]) diff --git a/neutron/tests/unit/agent/linux/test_polling.py b/neutron/tests/unit/agent/linux/test_polling.py index ec408a235bc..b38c86b2d66 100644 --- a/neutron/tests/unit/agent/linux/test_polling.py +++ b/neutron/tests/unit/agent/linux/test_polling.py @@ -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): diff --git a/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py b/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py index c17c57e27aa..78eb49308a1 100644 --- a/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py +++ b/neutron/tests/unit/api/rpc/handlers/test_dhcp_rpc.py @@ -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')]) diff --git a/neutron/tests/unit/services/metering/agents/test_metering_agent.py b/neutron/tests/unit/services/metering/agents/test_metering_agent.py index 11601873c51..e95ef9bd383 100644 --- a/neutron/tests/unit/services/metering/agents/test_metering_agent.py +++ b/neutron/tests/unit/services/metering/agents/test_metering_agent.py @@ -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(