From 3a871a73b88f8b75d64c88271b6fc6cdd4b1cfc0 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 9 Jul 2015 16:54:23 -0700 Subject: [PATCH] Fix failures introduced by the new version of mock 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 | 29 ------------------- .../unit/agent/linux/test_async_process.py | 6 ++-- neutron/tests/unit/agent/linux/test_daemon.py | 6 ++-- .../unit/agent/linux/test_external_process.py | 10 ++++--- .../tests/unit/agent/linux/test_interface.py | 2 +- .../tests/unit/agent/linux/test_polling.py | 4 +-- .../unit/api/rpc/handlers/test_dhcp_rpc.py | 6 ++-- .../metering/agents/test_metering_agent.py | 2 +- 8 files changed, 19 insertions(+), 46 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 4cb79914a..97e76eb12 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 7791485de..7d116d14b 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 5914c55f4..e93488023 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 079b370ae..db84de21e 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 0fdf3d744..7feb9981b 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 ec408a235..b38c86b2d 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 c17c57e27..78eb49308 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 11601873c..e95ef9bd3 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( -- 2.45.2