From 5a35c8abb4dc7dd200dd0bbd8f11b4d8840f013e Mon Sep 17 00:00:00 2001 From: "Chaozhe.Chen" Date: Thu, 18 Feb 2016 15:01:00 +0800 Subject: [PATCH] Test: use assert_has_calls() instead Some of the assertions in cinder test are sequential, should better use assert_has_calls() instead of assert_any_call(). And assert_has_calls() provide more clear messages in case of failure. Change-Id: I66d74d1b84f939eb02d00c6f84813beb7e7311e4 --- cinder/tests/unit/test_cmd.py | 103 ++++++++++++++++------------------ 1 file changed, 47 insertions(+), 56 deletions(-) diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index 4302e5a029c..55cf1bbfad6 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -1442,12 +1442,13 @@ class TestCinderVolumeUsageAuditCmd(test.TestCase): rpc_init.assert_called_once_with(CONF) last_completed_audit_period.assert_called_once_with() volume_get_active_by_window.assert_called_once_with(ctxt, begin, end) - notify_about_volume_usage.assert_any_call(ctxt, volume1, 'exists', - extra_usage_info=extra_info) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.start', extra_usage_info=local_extra_info) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.end', extra_usage_info=local_extra_info) + notify_about_volume_usage.assert_has_calls([ + mock.call(ctxt, volume1, 'exists', extra_usage_info=extra_info), + mock.call(ctxt, volume1, 'create.start', + extra_usage_info=local_extra_info), + mock.call(ctxt, volume1, 'create.end', + extra_usage_info=local_extra_info) + ]) @mock.patch('cinder.volume.utils.notify_about_volume_usage') @mock.patch('cinder.db.volume_get_active_by_window') @@ -1508,20 +1509,17 @@ class TestCinderVolumeUsageAuditCmd(test.TestCase): rpc_init.assert_called_once_with(CONF) last_completed_audit_period.assert_called_once_with() volume_get_active_by_window.assert_called_once_with(ctxt, begin, end) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'exists', extra_usage_info=extra_info) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.start', - extra_usage_info=local_extra_info_create) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.end', - extra_usage_info=local_extra_info_create) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'delete.start', - extra_usage_info=local_extra_info_delete) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'delete.end', - extra_usage_info=local_extra_info_delete) + notify_about_volume_usage.assert_has_calls([ + mock.call(ctxt, volume1, 'exists', extra_usage_info=extra_info), + mock.call(ctxt, volume1, 'create.start', + extra_usage_info=local_extra_info_create), + mock.call(ctxt, volume1, 'create.end', + extra_usage_info=local_extra_info_create), + mock.call(ctxt, volume1, 'delete.start', + extra_usage_info=local_extra_info_delete), + mock.call(ctxt, volume1, 'delete.end', + extra_usage_info=local_extra_info_delete) + ]) @mock.patch('cinder.volume.utils.notify_about_snapshot_usage') @mock.patch('cinder.objects.snapshot.SnapshotList.get_active_by_window') @@ -1587,14 +1585,13 @@ class TestCinderVolumeUsageAuditCmd(test.TestCase): last_completed_audit_period.assert_called_once_with() volume_get_active_by_window.assert_called_once_with(ctxt, begin, end) self.assertFalse(notify_about_volume_usage.called) - notify_about_snapshot_usage.assert_any_call(ctxt, snapshot1, 'exists', - extra_info) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'create.start', - extra_usage_info=local_extra_info_create) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'delete.start', - extra_usage_info=local_extra_info_delete) + notify_about_snapshot_usage.assert_has_calls([ + mock.call(ctxt, snapshot1, 'exists', extra_info), + mock.call(ctxt, snapshot1, 'create.start', + extra_usage_info=local_extra_info_create), + mock.call(ctxt, snapshot1, 'delete.start', + extra_usage_info=local_extra_info_delete) + ]) @mock.patch('cinder.volume.utils.notify_about_snapshot_usage') @mock.patch('cinder.objects.snapshot.SnapshotList.get_active_by_window') @@ -1663,32 +1660,26 @@ class TestCinderVolumeUsageAuditCmd(test.TestCase): rpc_init.assert_called_once_with(CONF) last_completed_audit_period.assert_called_once_with() volume_get_active_by_window.assert_called_once_with(ctxt, begin, end) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'exists', extra_usage_info=extra_info) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.start', - extra_usage_info=extra_info_volume_create) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'create.end', - extra_usage_info=extra_info_volume_create) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'delete.start', - extra_usage_info=extra_info_volume_delete) - notify_about_volume_usage.assert_any_call( - ctxt, volume1, 'delete.end', - extra_usage_info=extra_info_volume_delete) + notify_about_volume_usage.assert_has_calls([ + mock.call(ctxt, volume1, 'exists', extra_usage_info=extra_info), + mock.call(ctxt, volume1, 'create.start', + extra_usage_info=extra_info_volume_create), + mock.call(ctxt, volume1, 'create.end', + extra_usage_info=extra_info_volume_create), + mock.call(ctxt, volume1, 'delete.start', + extra_usage_info=extra_info_volume_delete), + mock.call(ctxt, volume1, 'delete.end', + extra_usage_info=extra_info_volume_delete) + ]) - notify_about_snapshot_usage.assert_any_call(ctxt, snapshot1, - 'exists', extra_info) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'create.start', - extra_usage_info=extra_info_snapshot_create) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'create.end', - extra_usage_info=extra_info_snapshot_create) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'delete.start', - extra_usage_info=extra_info_snapshot_delete) - notify_about_snapshot_usage.assert_any_call( - ctxt, snapshot1, 'delete.end', - extra_usage_info=extra_info_snapshot_delete) + notify_about_snapshot_usage.assert_has_calls([ + mock.call(ctxt, snapshot1, 'exists', extra_info), + mock.call(ctxt, snapshot1, 'create.start', + extra_usage_info=extra_info_snapshot_create), + mock.call(ctxt, snapshot1, 'create.end', + extra_usage_info=extra_info_snapshot_create), + mock.call(ctxt, snapshot1, 'delete.start', + extra_usage_info=extra_info_snapshot_delete), + mock.call(ctxt, snapshot1, 'delete.end', + extra_usage_info=extra_info_snapshot_delete) + ])