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 c6c4a20777)
This commit is contained in:
Miguel Angel Ajo 2014-03-31 15:50:55 +02:00 committed by Mark McClain
parent 8fd5124098
commit 5efc016349
6 changed files with 14 additions and 12 deletions

View File

@ -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):

View File

@ -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(

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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):