From: Miguel Angel Ajo Date: Mon, 31 Mar 2014 13:50:55 +0000 (+0200) Subject: fixes tests using called_once_ without assert X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=5efc0163490d02b6015286f2d25a186624be95c5;p=openstack-build%2Fneutron-build.git fixes tests using called_once_ without assert A few tests were using mock's called_once, or called_once_with_args instead of assert_called_once or assert_called_once_with_args. Those methods return a bool that needs to be actively checked. The tests are fixed to avoid them from passing if the call condition is not met. Change-Id: I21e5257b26b2a08cc8f0b108233d1d5cc0b97b89 Closes-bug: #1297875 (cherry picked from commit c6c4a20777921dc1b21e80edb96ccd957a054c68) --- diff --git a/neutron/tests/unit/agent/linux/test_async_process.py b/neutron/tests/unit/agent/linux/test_async_process.py index d56ad38b9..c26236b9a 100644 --- a/neutron/tests/unit/agent/linux/test_async_process.py +++ b/neutron/tests/unit/agent/linux/test_async_process.py @@ -244,7 +244,7 @@ class TestAsyncProcess(base.BaseTestCase): self.proc._kill_event = True with mock.patch.object(self.proc, '_kill') as mock_kill: self.proc.stop() - mock_kill.called_once() + mock_kill.assert_called_once_with() def test_stop_raises_exception_if_already_started(self): with testtools.ExpectedException(async_process.AsyncProcessException): diff --git a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py index 13464ca54..e6eebeffa 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py +++ b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py @@ -700,7 +700,7 @@ class TestOvsNeutronAgent(base.BaseTestCase): self.agent.daemon_loop() mock_get_pm.assert_called_with(True, 'sudo', constants.DEFAULT_OVSDBMON_RESPAWN) - mock_loop.called_once() + mock_loop.assert_called_once_with(polling_manager=mock.ANY) def test_setup_tunnel_port_error_negative(self): with contextlib.nested( diff --git a/neutron/tests/unit/test_db_migration.py b/neutron/tests/unit/test_db_migration.py index 9147a6701..2cd489a1e 100644 --- a/neutron/tests/unit/test_db_migration.py +++ b/neutron/tests/unit/test_db_migration.py @@ -181,5 +181,5 @@ class TestCli(base.BaseTestCase): mock_open.return_value.__exit__ = mock.Mock() cli.update_head_file(mock.sentinel.config) - mock_open.write.called_once_with('a') + mock_open.return_value.write.assert_called_once_with('a') fc.assert_called_once_with(mock.sentinel.config) diff --git a/neutron/tests/unit/test_dhcp_agent.py b/neutron/tests/unit/test_dhcp_agent.py index 1275e64fe..58836e8e2 100644 --- a/neutron/tests/unit/test_dhcp_agent.py +++ b/neutron/tests/unit/test_dhcp_agent.py @@ -718,7 +718,7 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): self.plugin.get_network_info.return_value = network with mock.patch.object(self.dhcp, 'disable_dhcp_helper') as disable: self.dhcp.refresh_dhcp_helper(network.id) - disable.called_once_with_args(network.id) + disable.assert_called_once_with(network.id) self.assertFalse(self.cache.called) self.assertFalse(self.call_driver.called) self.cache.assert_has_calls( @@ -1299,14 +1299,14 @@ class TestDeviceManager(base.BaseTestCase): expected = ('dhcp1ae5f96c-c527-5079-82ea-371a01645457-12345678-1234-' '5678-1234567890ab') - with mock.patch('socket.gethostbyname') as get_host: + with mock.patch('socket.gethostname') as get_host: with mock.patch('uuid.uuid5') as uuid5: uuid5.return_value = '1ae5f96c-c527-5079-82ea-371a01645457' get_host.return_value = 'localhost' dh = dhcp.DeviceManager(cfg.CONF, cfg.CONF.root_helper, None) - uuid5.called_once_with(uuid.NAMESPACE_DNS, 'localhost') self.assertEqual(dh.get_device_id(fake_net), expected) + uuid5.assert_called_once_with(uuid.NAMESPACE_DNS, 'localhost') def _get_device_manager_with_mock_device(self, conf, device): dh = dhcp.DeviceManager(conf, cfg.CONF.root_helper, None) diff --git a/neutron/tests/unit/test_metadata_agent.py b/neutron/tests/unit/test_metadata_agent.py index a87cdfd3f..6f7f926ac 100644 --- a/neutron/tests/unit/test_metadata_agent.py +++ b/neutron/tests/unit/test_metadata_agent.py @@ -319,12 +319,12 @@ class TestUnixDomainWSGIServer(base.BaseTestCase): with mock.patch.object(agent, 'logging') as logging: self.server._run('app', 'sock') - self.eventlet.wsgi.server.called_once_with( + self.eventlet.wsgi.server.assert_called_once_with( 'sock', 'app', - self.server.pool, - agent.UnixDomainHttpProtocol, - mock.ANY + protocol=agent.UnixDomainHttpProtocol, + log=mock.ANY, + custom_pool=self.server.pool ) self.assertTrue(len(logging.mock_calls)) diff --git a/neutron/tests/unit/test_post_mortem_debug.py b/neutron/tests/unit/test_post_mortem_debug.py index ffef668bb..037934242 100644 --- a/neutron/tests/unit/test_post_mortem_debug.py +++ b/neutron/tests/unit/test_post_mortem_debug.py @@ -38,8 +38,10 @@ class TestTesttoolsExceptionHandler(base.BaseTestCase): return_value=mock.Mock()): post_mortem_debug.exception_handler(exc_info) - mock_print_exception.called_once_with(*exc_info) - mock_post_mortem.called_once() + # traceback will become post_mortem_debug.FilteredTraceback + filtered_exc_info = (exc_info[0], exc_info[1], mock.ANY) + mock_print_exception.assert_called_once_with(*filtered_exc_info) + mock_post_mortem.assert_called_once_with(mock.ANY) class TestFilteredTraceback(base.BaseTestCase):