Fix 'has_calls' method calls in unit tests

The 'has_calls' method does not exist in assertion methods of mock.
Replace the 'has_calls' method with an 'assert_has_calls' method or
an 'assert_called_once_with' method.
Add an 'assertEqual' check before an 'assert_has_calls' method.

Change-Id: I4b606fce473d064b9bb00213696c075cea020aaf
Closes-Bug: #1840200
This commit is contained in:
Takashi NATSUME 2019-08-15 10:00:38 +09:00
parent ee6b69cadc
commit ad482e53fb
6 changed files with 39 additions and 23 deletions

View File

@ -322,7 +322,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
self.compute.update_available_resource(self.context, startup=True)
get_db_nodes.assert_called_once_with(self.context, use_slave=True,
startup=True)
update_mock.has_calls(
self.assertEqual(len(avail_nodes_l), update_mock.call_count)
update_mock.assert_has_calls(
[mock.call(self.context, node, startup=True)
for node in avail_nodes_l]
)
@ -566,7 +567,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
final_result = 'meow'
rp_mapping = {}
expected_sleep_times = [1, 2, 4, 8, 16, 30, 30, 30]
expected_sleep_times = [mock.call(t) for t in
(1, 2, 4, 8, 16, 30, 30)]
with mock.patch.object(
self.compute.network_api, 'allocate_for_instance',
@ -577,7 +579,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
is_vpn,
rp_mapping)
mock_sleep.has_calls(expected_sleep_times)
self.assertEqual(7, mock_sleep.call_count)
mock_sleep.assert_has_calls(expected_sleep_times)
self.assertEqual(final_result, res)
# Ensure save is not called in while allocating networks, the instance
# is saved after the allocation.

View File

@ -67,7 +67,8 @@ class HostOpsTestCase(test_base.HyperVBaseTestCase):
expected = [mock.call(fkey)
for fkey in os_win_const.PROCESSOR_FEATURE.keys()]
self._hostops._hostutils.is_cpu_feature_present.has_calls(expected)
self._hostops._hostutils.is_cpu_feature_present.assert_has_calls(
expected, any_order=True)
expected_response = self._get_mock_cpu_info()
self.assertEqual(expected_response, response)

View File

@ -101,12 +101,15 @@ class SnapshotOpsTestCase(test_base.HyperVBaseTestCase):
else:
mock_save_glance_image.assert_called_once_with(
self.context, mock.sentinel.IMAGE_ID, dest_vhd_path)
self._snapshotops._pathutils.copyfile.has_calls(expected)
self.assertEqual(len(expected),
self._snapshotops._pathutils.copyfile.call_count)
self._snapshotops._pathutils.copyfile.assert_has_calls(expected)
self.assertEqual(2, mock_update.call_count)
expected_update = [
mock.call(task_state=task_states.IMAGE_PENDING_UPLOAD),
mock.call(task_state=task_states.IMAGE_UPLOADING,
expected_state=task_states.IMAGE_PENDING_UPLOAD)]
mock_update.has_calls(expected_update)
mock_update.assert_has_calls(expected_update)
self._snapshotops._vmutils.remove_vm_snapshot.assert_called_once_with(
fake_snapshot_path)
self._snapshotops._pathutils.rmtree.assert_called_once_with(

View File

@ -1439,10 +1439,11 @@ class VMOpsTestCase(test_base.HyperVBaseTestCase):
self._vmops._pathutils.get_instance_dir.assert_called_once_with(
mock.sentinel.FAKE_VM_NAME,
remote_server=mock.sentinel.FAKE_DEST_HOST)
mock_copy.has_calls(mock.call(mock.sentinel.FAKE_DVD_PATH1,
mock.sentinel.FAKE_DEST_PATH),
mock.call(mock.sentinel.FAKE_DVD_PATH2,
mock.sentinel.FAKE_DEST_PATH))
self.assertEqual(2, mock_copy.call_count)
mock_copy.assert_has_calls([mock.call(mock.sentinel.FAKE_DVD_PATH1,
mock.sentinel.FAKE_DEST_PATH),
mock.call(mock.sentinel.FAKE_DVD_PATH2,
mock.sentinel.FAKE_DEST_PATH)])
def test_plug_vifs(self):
mock_instance = fake_instance.fake_instance_obj(self.context)

View File

@ -2496,13 +2496,19 @@ class IronicDriverTestCase(test.NoDBTestCase):
@mock.patch.object(cw.IronicClientWrapper, 'call')
def test_prepare_for_spawn(self, mock_call):
node = ironic_utils.get_test_node(driver='fake')
mock_call.side_effect = [node, None]
instance = fake_instance.fake_instance_obj(self.ctx,
node=node.uuid)
self.driver.prepare_for_spawn(instance)
expected_patch = [{'path': '/instance_uuid', 'op': 'add',
'value': instance.uuid}]
mock_call.has_calls(
[mock.call('node.get', node.uuid, mock.ANY),
self.assertEqual(2, mock_call.call_count)
mock_call.assert_has_calls(
[mock.call('node.get', node.uuid,
fields=('uuid', 'power_state', 'target_power_state',
'provision_state', 'target_provision_state',
'last_error', 'maintenance', 'properties',
'instance_uuid', 'traits', 'resource_class')),
mock.call('node.update', node.uuid,
expected_patch, retry_on_conflict=False)])
@ -2514,9 +2520,9 @@ class IronicDriverTestCase(test.NoDBTestCase):
expected_patch = [{'path': '/instance_uuid', 'op': 'add',
'value': instance.uuid}]
self.driver._set_instance_uuid(node, instance)
mock_call.has_calls(
[mock.call('node.update', node.uuid,
expected_patch, retry_on_conflict=False)])
mock_call.assert_called_once_with('node.update', node.uuid,
expected_patch,
retry_on_conflict=False)
def test_prepare_for_spawn_invalid_instance(self):
instance = fake_instance.fake_instance_obj(self.ctx,
@ -2563,9 +2569,10 @@ class IronicDriverTestCase(test.NoDBTestCase):
mock_vol.assert_called_once_with(instance)
mock_unvif.assert_called_once_with(node, instance, None)
mock_stop_fw.assert_called_once_with(instance, None)
expected_patch = [{'path': '/instance_uuid', 'op': 'remove'}]
mock_call.has_calls(
[mock.call('node.update', node.uuid, expected_patch)])
expected_patch = [{'path': '/instance_info', 'op': 'remove'},
{'path': '/instance_uuid', 'op': 'remove'}]
mock_call.assert_called_once_with('node.update', node.uuid,
expected_patch)
@mock.patch.object(ironic_driver.IronicDriver, '_stop_firewall')
@mock.patch.object(ironic_driver.IronicDriver, '_unplug_vifs')

View File

@ -14599,19 +14599,20 @@ class LibvirtConnTestCase(test.NoDBTestCase,
expected_xml = [
('<hostdev mode="subsystem" type="pci" managed="yes">\n'
' <source>\n'
' <address bus="0x00" domain="0x0000" \
function="0x0" slot="0x00"/>\n'
' <address bus="0x00" domain="0x0000" '
'function="0x0" slot="0x00"/>\n'
' </source>\n'
'</hostdev>\n'),
('<hostdev mode="subsystem" type="pci" managed="yes">\n'
' <source>\n'
' <address bus="0x00" domain="0x0000" \
function="0x1" slot="0x00"/>\n'
' <address bus="0x00" domain="0x0000" '
'function="0x1" slot="0x00"/>\n'
' </source>\n'
'</hostdev>\n')
]
mock_detachDeviceFlags.has_calls([
self.assertEqual(2, mock_detachDeviceFlags.call_count)
mock_detachDeviceFlags.assert_has_calls([
mock.call(expected_xml[0], flags=1),
mock.call(expected_xml[1], flags=1)
])