From 09adb96836eccd02df311e99c367d332ae6e6aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9la=20Vancsics?= Date: Mon, 3 Apr 2017 16:23:30 +0200 Subject: [PATCH] Use more specific asserts in tests Instead of assertTrue and assertFalse use more specific asserts. They are compatible with Python 2.7[1] and 3.4[2] [1]: https://docs.python.org/2.7/library/unittest.html [2]: https://docs.python.org/3.4/library/unittest.html Change-Id: Iea7306f8ab6e3253fd68007829b7e8b51afb790a --- ironic/tests/unit/api/v1/test_expose.py | 12 ++++++------ .../tests/unit/api/v1/test_notification_utils.py | 2 +- ironic/tests/unit/api/v1/test_portgroups.py | 2 +- ironic/tests/unit/common/test_states.py | 4 ++-- .../drivers/modules/drac/test_periodic_task.py | 2 +- .../unit/drivers/modules/ilo/test_management.py | 2 +- .../unit/drivers/modules/oneview/test_deploy.py | 4 ++-- .../drivers/modules/oneview/test_deploy_utils.py | 16 ++++++++-------- .../unit/drivers/modules/test_deploy_utils.py | 8 ++++---- .../tests/unit/drivers/modules/test_ipmitool.py | 2 +- ironic/tests/unit/drivers/test_utils.py | 2 +- 11 files changed, 28 insertions(+), 28 deletions(-) diff --git a/ironic/tests/unit/api/v1/test_expose.py b/ironic/tests/unit/api/v1/test_expose.py index 085a569773..3de1130ccb 100644 --- a/ironic/tests/unit/api/v1/test_expose.py +++ b/ironic/tests/unit/api/v1/test_expose.py @@ -52,12 +52,12 @@ class TestExposedAPIMethodsCheckPolicy(test_base.TestCase): for func in self.exposed_methods: src = inspect.getsource(func) - self.assertTrue('policy.authorize' in src, - 'policy.authorize call not found in exposed ' - 'method %s' % func) - self.assertTrue('context.to_policy_values' in src, - 'context.to_policy_values call not found in ' - 'exposed method %s' % func) + self.assertIn('policy.authorize', src, + 'policy.authorize call not found in exposed ' + 'method %s' % func) + self.assertIn('context.to_policy_values', src, + 'context.to_policy_values call not found in ' + 'exposed method %s' % func) def test_chasis_api_policy(self): self._test('ironic.api.controllers.v1.chassis') diff --git a/ironic/tests/unit/api/v1/test_notification_utils.py b/ironic/tests/unit/api/v1/test_notification_utils.py index a3172070b0..c2a4d07870 100644 --- a/ironic/tests/unit/api/v1/test_notification_utils.py +++ b/ironic/tests/unit/api/v1/test_notification_utils.py @@ -147,7 +147,7 @@ class APINotifyTestCase(tests_base.TestCase): self.assertEqual('11:22:33:77:88:99', payload.address) self.assertEqual({'a': 25}, payload.local_link_connection) self.assertEqual({'as': 34}, payload.extra) - self.assertEqual(False, payload.pxe_enabled) + self.assertIs(False, payload.pxe_enabled) def test_portgroup_notification(self): node_uuid = uuidutils.generate_uuid() diff --git a/ironic/tests/unit/api/v1/test_portgroups.py b/ironic/tests/unit/api/v1/test_portgroups.py index 40120990a5..57f0b5284d 100644 --- a/ironic/tests/unit/api/v1/test_portgroups.py +++ b/ironic/tests/unit/api/v1/test_portgroups.py @@ -855,7 +855,7 @@ class TestPatch(test_api_base.BaseApiTest): headers=self.headers) self.assertEqual('application/json', response.content_type) self.assertEqual(http_client.OK, response.status_code) - self.assertEqual(False, response.json['standalone_ports_supported']) + self.assertIs(False, response.json['standalone_ports_supported']) def test_update_portgroup_standalone_ports_supported_bad_api_version( self, mock_upd): diff --git a/ironic/tests/unit/common/test_states.py b/ironic/tests/unit/common/test_states.py index 577fd52d3f..d841ac3bbe 100644 --- a/ironic/tests/unit/common/test_states.py +++ b/ironic/tests/unit/common/test_states.py @@ -32,7 +32,7 @@ class StatesTest(base.TestCase): # Assumption: A state variable name is all UPPERCASE and contents # are a string. if key.upper() == key and isinstance(value, six.string_types): - self.assertTrue( - (len(value) <= 15), + self.assertLessEqual( + len(value), 15, "Value for state: {} is greater than 15 characters".format( key)) diff --git a/ironic/tests/unit/drivers/modules/drac/test_periodic_task.py b/ironic/tests/unit/drivers/modules/drac/test_periodic_task.py index 05787306a6..baebd52a2e 100644 --- a/ironic/tests/unit/drivers/modules/drac/test_periodic_task.py +++ b/ironic/tests/unit/drivers/modules/drac/test_periodic_task.py @@ -139,7 +139,7 @@ class DracPeriodicTaskTestCase(db_base.DbTestCase): self.assertEqual(['42'], self.node.driver_internal_info['raid_config_job_ids']) self.assertEqual({}, self.node.raid_config) - self.assertEqual(False, self.node.maintenance) + self.assertIs(False, self.node.maintenance) @mock.patch.object(drac_common, 'get_drac_client', spec_set=True, autospec=True) diff --git a/ironic/tests/unit/drivers/modules/ilo/test_management.py b/ironic/tests/unit/drivers/modules/ilo/test_management.py index 26f24a59bf..fa2e2a4589 100644 --- a/ironic/tests/unit/drivers/modules/ilo/test_management.py +++ b/ironic/tests/unit/drivers/modules/ilo/test_management.py @@ -390,7 +390,7 @@ class IloManagementTestCase(db_base.DbTestCase): 'fw_location_for_another_filepath', 'chassis'), ] clean_step_mock.assert_has_calls(calls) - self.assertTrue(clean_step_mock.call_count == 5) + self.assertEqual(5, clean_step_mock.call_count) def test_update_firmware_throws_if_invalid_update_mode_provided(self): with task_manager.acquire(self.context, self.node.uuid, diff --git a/ironic/tests/unit/drivers/modules/oneview/test_deploy.py b/ironic/tests/unit/drivers/modules/oneview/test_deploy.py index 737f96e8b7..04703a3963 100644 --- a/ironic/tests/unit/drivers/modules/oneview/test_deploy.py +++ b/ironic/tests/unit/drivers/modules/oneview/test_deploy.py @@ -240,7 +240,7 @@ class OneViewPeriodicTasks(db_base.DbTestCase): self.assertTrue(self.node.maintenance) self.assertEqual(common.NODE_IN_USE_BY_ONEVIEW, self.node.maintenance_reason) - self.assertFalse('oneview_error' in self.node.driver_internal_info) + self.assertNotIn('oneview_error', self.node.driver_internal_info) def test_node_stay_clean_failed_when_no_oneview_error( self, mock_is_node_in_use_by_oneview, mock_node_get @@ -256,7 +256,7 @@ class OneViewPeriodicTasks(db_base.DbTestCase): self.assertFalse(self.node.maintenance) self.assertNotEqual(common.NODE_IN_USE_BY_ONEVIEW, self.node.maintenance_reason) - self.assertFalse('oneview_error' in self.node.driver_internal_info) + self.assertNotIn('oneview_error', self.node.driver_internal_info) @mock.patch.object(common, 'get_oneview_client', spec_set=True, autospec=True) diff --git a/ironic/tests/unit/drivers/modules/oneview/test_deploy_utils.py b/ironic/tests/unit/drivers/modules/oneview/test_deploy_utils.py index c20cdbb9ea..ca0e7dcbac 100644 --- a/ironic/tests/unit/drivers/modules/oneview/test_deploy_utils.py +++ b/ironic/tests/unit/drivers/modules/oneview/test_deploy_utils.py @@ -110,12 +110,12 @@ class OneViewDeployUtilsTestCase(db_base.DbTestCase): '/rest/server-profiles/1234556789' task.node.driver_info = driver_info - self.assertTrue( - 'applied_server_profile_uri' in task.node.driver_info + self.assertIn( + 'applied_server_profile_uri', task.node.driver_info ) deploy_utils.tear_down(ov_client, task) - self.assertFalse( - 'applied_server_profile_uri' in task.node.driver_info + self.assertNotIn( + 'applied_server_profile_uri', task.node.driver_info ) self.assertTrue( ov_client.delete_server_profile.called @@ -403,8 +403,8 @@ class OneViewDeployUtilsTestCase(db_base.DbTestCase): oneview_client, task.node ) self.assertTrue(oneview_client.delete_server_profile.called) - self.assertTrue( - 'applied_server_profile_uri' not in task.node.driver_info + self.assertNotIn( + 'applied_server_profile_uri', task.node.driver_info ) @mock.patch.object(objects.Node, 'save') @@ -439,6 +439,6 @@ class OneViewDeployUtilsTestCase(db_base.DbTestCase): task.node ) self.assertTrue(ov_client.delete_server_profile.called) - self.assertTrue( - 'applied_server_profile_uri' in task.node.driver_info + self.assertIn( + 'applied_server_profile_uri', task.node.driver_info ) diff --git a/ironic/tests/unit/drivers/modules/test_deploy_utils.py b/ironic/tests/unit/drivers/modules/test_deploy_utils.py index 103e952503..68930eae79 100644 --- a/ironic/tests/unit/drivers/modules/test_deploy_utils.py +++ b/ironic/tests/unit/drivers/modules/test_deploy_utils.py @@ -1625,9 +1625,9 @@ class AgentMethodsTestCase(db_base.DbTestCase): utils.agent_add_clean_params(task) self.assertEqual(2, task.node.driver_internal_info[ 'agent_erase_devices_iterations']) - self.assertEqual(False, task.node.driver_internal_info[ + self.assertIs(False, task.node.driver_internal_info[ 'agent_erase_devices_zeroize']) - self.assertEqual(True, task.node.driver_internal_info[ + self.assertIs(True, task.node.driver_internal_info[ 'agent_continue_if_ata_erase_failed']) @mock.patch.object(pxe.PXEBoot, 'prepare_ramdisk', autospec=True) @@ -1649,8 +1649,8 @@ class AgentMethodsTestCase(db_base.DbTestCase): power_mock.assert_called_once_with(task, states.REBOOT) self.assertEqual(1, task.node.driver_internal_info[ 'agent_erase_devices_iterations']) - self.assertEqual(True, task.node.driver_internal_info[ - 'agent_erase_devices_zeroize']) + self.assertIs(True, task.node.driver_internal_info[ + 'agent_erase_devices_zeroize']) if manage_boot: prepare_ramdisk_mock.assert_called_once_with( mock.ANY, mock.ANY, {'a': 'b'}) diff --git a/ironic/tests/unit/drivers/modules/test_ipmitool.py b/ironic/tests/unit/drivers/modules/test_ipmitool.py index f5a8c22eaf..d6d85268bb 100644 --- a/ironic/tests/unit/drivers/modules/test_ipmitool.py +++ b/ironic/tests/unit/drivers/modules/test_ipmitool.py @@ -2057,7 +2057,7 @@ class IPMIToolDriverTestCase(db_base.DbTestCase): self.info['force_boot_device'] = True self.driver.management.set_boot_device(task, boot_devices.PXE) task.node.refresh() - self.assertEqual( + self.assertIs( False, task.node.driver_internal_info['is_next_boot_persistent'] ) diff --git a/ironic/tests/unit/drivers/test_utils.py b/ironic/tests/unit/drivers/test_utils.py index 4a1bc7f94b..d2d5be86e6 100644 --- a/ironic/tests/unit/drivers/test_utils.py +++ b/ironic/tests/unit/drivers/test_utils.py @@ -169,7 +169,7 @@ class UtilsTestCase(db_base.DbTestCase): ret = driver_utils.force_persistent_boot(task, 'pxe', False) self.assertIsNone(ret) task.node.refresh() - self.assertEqual( + self.assertIs( False, task.node.driver_internal_info['is_next_boot_persistent'])