Browse Source

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: Ifee66714db561fb329911395b2cfdd90c689b609
changes/41/358441/3
Béla Vancsics 5 years ago
parent
commit
dfcb265ae8
  1. 2
      heat/tests/api/test_wsgi.py
  2. 2
      heat/tests/autoscaling/test_heat_scaling_group.py
  3. 6
      heat/tests/autoscaling/test_scaling_group.py
  4. 12
      heat/tests/aws/test_instance.py
  5. 4
      heat/tests/aws/test_instance_network.py
  6. 4
      heat/tests/clients/test_clients.py
  7. 2
      heat/tests/clients/test_swift_client.py
  8. 4
      heat/tests/constraints/test_heat_constraints.py
  9. 2
      heat/tests/engine/test_dependencies.py
  10. 4
      heat/tests/engine/test_scheduler.py
  11. 2
      heat/tests/openstack/cinder/test_volume.py
  12. 6
      heat/tests/openstack/heat/test_instance_group_update_policy.py
  13. 2
      heat/tests/openstack/heat/test_random_string.py
  14. 6
      heat/tests/openstack/heat/test_resource_group.py
  15. 12
      heat/tests/openstack/nova/test_server.py
  16. 2
      heat/tests/openstack/nova/test_server_group.py
  17. 2
      heat/tests/openstack/swift/test_container.py
  18. 4
      heat/tests/test_engine_service.py
  19. 2
      heat/tests/test_lifecycle_plugin_utils.py
  20. 2
      heat/tests/test_metadata_refresh.py
  21. 4
      heat/tests/test_resource.py
  22. 9
      heat/tests/test_template_format.py
  23. 10
      heat/tests/test_watch.py
  24. 8
      heat_integrationtests/functional/test_resource_chain.py
  25. 2
      heat_integrationtests/functional/test_stack_events.py
  26. 4
      heat_integrationtests/scenario/test_server_software_config.py

2
heat/tests/api/test_wsgi.py

@ -380,7 +380,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_from_json_exceeds_max_json_mb(self):
cfg.CONF.set_override('max_json_body_size', 10, enforce_type=True)
body = json.dumps(['a'] * cfg.CONF.max_json_body_size)
self.assertTrue(len(body) > cfg.CONF.max_json_body_size)
self.assertGreater(len(body), cfg.CONF.max_json_body_size)
error = self.assertRaises(exception.RequestLimitExceeded,
wsgi.JSONRequestDeserializer().from_json,
body)

2
heat/tests/autoscaling/test_heat_scaling_group.py

@ -523,7 +523,7 @@ class RollingUpdatePolicyTest(common.HeatTestCase):
tmpl_batch_sz = int(tmpl_policy['max_batch_size'])
policy = stack['my-group'].properties['rolling_updates']
self.assertTrue(policy)
self.assertTrue(len(policy) == 3)
self.assertEqual(3, len(policy))
self.assertEqual(1, int(policy['min_in_service']))
self.assertEqual(tmpl_batch_sz, int(policy['max_batch_size']))
self.assertEqual(1, policy['pause_time'])

6
heat/tests/autoscaling/test_scaling_group.py

@ -631,7 +631,8 @@ class RollingUpdatePolicyTest(common.HeatTestCase):
self.assertEqual(1, len(grp.update_policy))
self.assertIn('AutoScalingRollingUpdate', grp.update_policy)
policy = grp.update_policy['AutoScalingRollingUpdate']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(1, int(policy['MinInstancesInService']))
self.assertEqual(tmpl_batch_sz, int(policy['MaxBatchSize']))
self.assertEqual('PT1S', policy['PauseTime'])
@ -646,7 +647,8 @@ class RollingUpdatePolicyTest(common.HeatTestCase):
self.assertEqual(1, len(grp.update_policy))
self.assertIn('AutoScalingRollingUpdate', grp.update_policy)
policy = grp.update_policy['AutoScalingRollingUpdate']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(0, int(policy['MinInstancesInService']))
self.assertEqual(1, int(policy['MaxBatchSize']))
self.assertEqual('PT0S', policy['PauseTime'])

12
heat/tests/aws/test_instance.py

@ -175,7 +175,7 @@ class InstancesTest(common.HeatTestCase):
instance = self._create_test_instance(return_server,
'in_create')
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
expected_ip = return_server.networks['public'][0]
expected_az = getattr(return_server, 'OS-EXT-AZ:availability_zone')
@ -197,7 +197,7 @@ class InstancesTest(common.HeatTestCase):
instance = self._create_test_instance(return_server,
'create_with_bdm')
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
expected_ip = return_server.networks['public'][0]
expected_az = getattr(return_server, 'OS-EXT-AZ:availability_zone')
@ -375,7 +375,7 @@ class InstancesTest(common.HeatTestCase):
scheduler.TaskRunner(instance.create)()
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
expected_ip = return_server.networks['public'][0]
expected_az = getattr(return_server, 'OS-EXT-AZ:availability_zone')
@ -638,7 +638,7 @@ class InstancesTest(common.HeatTestCase):
return_server)
self.m.ReplayAll()
scheduler.TaskRunner(instance.create)()
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
self.m.VerifyAll()
def test_instance_validate(self):
@ -671,7 +671,7 @@ class InstancesTest(common.HeatTestCase):
instance.status = vm_status
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
d1 = {'server': self.fc.client.get_servers_detail()[1]['servers'][0]}
d1['server']['status'] = vm_status
@ -706,7 +706,7 @@ class InstancesTest(common.HeatTestCase):
instance.resource_id = '1234'
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
self.m.StubOutWithMock(self.fc.client, 'delete_servers_1234')
self.fc.client.delete_servers_1234().AndRaise(

4
heat/tests/aws/test_instance_network.py

@ -168,7 +168,7 @@ class instancesTest(common.HeatTestCase):
instance.resource_id = '1234'
instance.status = vm_status
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
expected_ip = return_server.networks['public'][0]
self.assertEqual(expected_ip, instance.FnGetAtt('PublicIp'))
@ -324,7 +324,7 @@ class instancesTest(common.HeatTestCase):
return_server, 'in_create_wnic')
# this makes sure the auto increment worked on instance creation
self.assertTrue(instance.id > 0)
self.assertGreater(instance.id, 0)
expected_ip = return_server.networks['public'][0]
self.assertEqual(expected_ip, instance.FnGetAtt('PublicIp'))

4
heat/tests/clients/test_clients.py

@ -365,8 +365,8 @@ class TestClientPluginsInitialise(common.HeatTestCase):
self.assertEqual({}, plugin._client_instances)
self.assertTrue(clients.has_client(plugin_name))
self.assertIsInstance(plugin.service_types, list)
self.assertTrue(len(plugin.service_types) >= 1,
'service_types is not defined for plugin')
self.assertGreaterEqual(len(plugin.service_types), 1,
'service_types is not defined for plugin')
@mock.patch.object(client_plugin.ClientPlugin, 'invalidate')
def test_invalidate_all_clients(self, mock_invalidate):

2
heat/tests/clients/test_swift_client.py

@ -82,7 +82,7 @@ class SwiftUtilsTest(SwiftClientPluginTestCase):
self.assertThat(url, matchers.MatchesRegex(regexp))
timeout = int(url.split('=')[-1])
self.assertTrue(timeout < swift.MAX_EPOCH)
self.assertLess(timeout, swift.MAX_EPOCH)
def test_get_temp_url_no_account_key(self):
self.swift_client.url = ("http://fake-host.com:8080/v1/"

4
heat/tests/constraints/test_heat_constraints.py

@ -61,8 +61,8 @@ class ResourceTypeConstraintTest(common.HeatTestCase):
# Verify
self.assertFalse(result)
self.assertTrue('OS::Heat::None,OS::Heat::RandomString'
in self.constraint._error_message)
self.assertIn('OS::Heat::None,OS::Heat::RandomString',
self.constraint._error_message)
self.mock_env.get_class.assert_has_calls([mock.call(value[0]),
mock.call(value[1])])

2
heat/tests/engine/test_dependencies.py

@ -183,7 +183,7 @@ class dependenciesTest(common.HeatTestCase):
for n in ('last', 'middle'):
self.assertIn(n, order,
"'%s' not found in dependency order" % n)
self.assertTrue(order.index('last') > order.index('middle'))
self.assertGreater(order.index('last'), order.index('middle'))
def test_simple_multilevel_partial(self):
d = dependencies.Dependencies([('last', 'middle'),

4
heat/tests/engine/test_scheduler.py

@ -1283,8 +1283,8 @@ class TimeoutTest(common.HeatTestCase):
eventlet.sleep(0.01)
later = scheduler.Timeout(task, 10)
self.assertTrue(earlier < later)
self.assertTrue(later > earlier)
self.assertLess(earlier, later)
self.assertGreater(later, earlier)
self.assertEqual(earlier, earlier)
self.assertNotEqual(earlier, later)

2
heat/tests/openstack/cinder/test_volume.py

@ -1019,7 +1019,7 @@ class CinderVolumeTest(vt_base.BaseVolumeTest):
self.patchobject(rsrc, '_store_config_default_properties')
scheduler.TaskRunner(rsrc.create)()
# this makes sure the auto increment worked on volume creation
self.assertTrue(rsrc.id > 0)
self.assertGreater(rsrc.id, 0)
self.m.VerifyAll()

6
heat/tests/openstack/heat/test_instance_group_update_policy.py

@ -181,7 +181,8 @@ class InstanceGroupTest(common.HeatTestCase):
self.assertEqual(1, len(grp.update_policy))
self.assertIn('RollingUpdate', grp.update_policy)
policy = grp.update_policy['RollingUpdate']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(1, int(policy['MinInstancesInService']))
self.assertEqual(2, int(policy['MaxBatchSize']))
self.assertEqual('PT1S', policy['PauseTime'])
@ -196,7 +197,8 @@ class InstanceGroupTest(common.HeatTestCase):
self.assertEqual(1, len(grp.update_policy))
self.assertIn('RollingUpdate', grp.update_policy)
policy = grp.update_policy['RollingUpdate']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(0, int(policy['MinInstancesInService']))
self.assertEqual(1, int(policy['MaxBatchSize']))
self.assertEqual('PT0S', policy['PauseTime'])

2
heat/tests/openstack/heat/test_random_string.py

@ -92,7 +92,7 @@ Resources:
return stack
def assert_min(self, pattern, string, minimum):
self.assertTrue(len(re.findall(pattern, string)) >= minimum)
self.assertGreaterEqual(len(re.findall(pattern, string)), minimum)
def test_random_string(self):
stack = self.create_stack(self.template_random_string)

6
heat/tests/openstack/heat/test_resource_group.py

@ -992,7 +992,8 @@ class RollingUpdatePolicyTest(common.HeatTestCase):
self.assertEqual(2, len(grp.update_policy))
self.assertIn('rolling_update', grp.update_policy)
policy = grp.update_policy['rolling_update']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(1, int(policy['min_in_service']))
self.assertEqual(tmpl_batch_sz, int(policy['max_batch_size']))
self.assertEqual(1, policy['pause_time'])
@ -1006,7 +1007,8 @@ class RollingUpdatePolicyTest(common.HeatTestCase):
self.assertEqual(2, len(grp.update_policy))
self.assertIn('rolling_update', grp.update_policy)
policy = grp.update_policy['rolling_update']
self.assertTrue(policy and len(policy) > 0)
self.assertIsNotNone(policy)
self.assertGreater(len(policy), 0)
self.assertEqual(0, int(policy['min_in_service']))
self.assertEqual(1, int(policy['max_batch_size']))
self.assertEqual(0, policy['pause_time'])

12
heat/tests/openstack/nova/test_server.py

@ -394,7 +394,7 @@ class ServersTest(common.HeatTestCase):
server = self._create_test_server(return_server, server_name)
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
interfaces = [
self._create_fake_iface('1234', 'fa:16:3e:8c:22:aa', '4.5.6.7'),
@ -1066,7 +1066,7 @@ class ServersTest(common.HeatTestCase):
_, kwargs = mock_create.call_args
self.assertEqual(kwargs['scheduler_hints'], scheduler_hints)
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
def test_check_maximum(self):
msg = 'test_check_maximum'
@ -1451,7 +1451,7 @@ class ServersTest(common.HeatTestCase):
server.resource_id = '1234'
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
side_effect = [server, fakes_nova.fake_exception()]
self.patchobject(self.fc.servers, 'get', side_effect=side_effect)
@ -1465,7 +1465,7 @@ class ServersTest(common.HeatTestCase):
server.resource_id = '1234'
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
self.patchobject(self.fc.client, 'delete_servers_1234',
side_effect=fakes_nova.fake_exception())
@ -1479,7 +1479,7 @@ class ServersTest(common.HeatTestCase):
server.resource_id = '1234'
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
def make_error(*args):
return_server.status = "ERROR"
@ -1525,7 +1525,7 @@ class ServersTest(common.HeatTestCase):
server.resource_id = '1234'
# this makes sure the auto increment worked on server creation
self.assertTrue(server.id > 0)
self.assertGreater(server.id, 0)
def make_soft_delete(*args):
return_server.status = "SOFT_DELETED"

2
heat/tests/openstack/nova/test_server_group.py

@ -73,7 +73,7 @@ class NovaServerGroupTest(common.HeatTestCase):
n = name
def fake_create(name, policies):
self.assertTrue(len(name) > 1)
self.assertGreater(len(name), 1)
return FakeGroup(n)
self.sg_mgr.create = fake_create
scheduler.TaskRunner(self.sg.create)()

2
heat/tests/openstack/swift/test_container.py

@ -153,7 +153,7 @@ class SwiftTest(common.HeatTestCase):
# Verify Expected Calls
mock_put.assert_called_once_with(container_name, {})
self.assertTrue(mock_head.call_count > 0)
self.assertGreater(mock_head.call_count, 0)
@mock.patch('swiftclient.client.Connection.put_container')
def test_public_read(self, mock_put):

4
heat/tests/test_engine_service.py

@ -122,7 +122,7 @@ class StackCreateTest(common.HeatTestCase):
stack.create()
self.assertIsNotNone(stack['WebServer'])
self.assertTrue(int(stack['WebServer'].resource_id) > 0)
self.assertGreater(int(stack['WebServer'].resource_id), 0)
self.assertNotEqual(stack['WebServer'].ipaddress, '0.0.0.0')
def test_wordpress_single_instance_stack_adopt(self):
@ -188,7 +188,7 @@ class StackCreateTest(common.HeatTestCase):
self.assertIsNotNone(db_s)
self.assertIsNotNone(stack['WebServer'])
self.assertTrue(int(stack['WebServer'].resource_id) > 0)
self.assertGreater(int(stack['WebServer'].resource_id), 0)
self.patchobject(fc.servers, 'delete',
side_effect=fakes_nova.fake_exception())

2
heat/tests/test_lifecycle_plugin_utils.py

@ -56,7 +56,7 @@ class LifecyclePluginUtilsTest(common.HeatTestCase):
self.assertIsNotNone(pp_cinstances)
self.assertTrue(self.is_iterable(pp_cinstances),
"not iterable: %s" % pp_cinstances)
self.assertTrue(len(pp_cinstances) == 1)
self.assertEqual(1, len(pp_cinstances))
self.assertEqual(TestLifecycleCallout1, pp_cinstances[0].__class__)
def test_do_pre_and_post_callouts(self):

2
heat/tests/test_metadata_refresh.py

@ -284,7 +284,7 @@ class WaitConditionMetadataUpdateTest(common.HeatTestCase):
jsonutils.loads(inst.metadata_get(refresh=True)['test']))
# Verify outgoing calls
self.assertTrue(mock_available.call_count > 0)
self.assertGreater(mock_available.call_count, 0)
self.assertEqual(2, mock_handle.call_count)
self.assertEqual(2, mock_check.call_count)

4
heat/tests/test_resource.py

@ -4229,7 +4229,7 @@ class TestResourceMapping(common.HeatTestCase):
type_elements = r_type.split('::')
# type has fixed format
# Platform type::Service/Type::Optional Sub-sections::Name
self.assertTrue(len(type_elements) >= 3)
self.assertGreaterEqual(len(type_elements), 3)
# type should be OS or AWS
self.assertIn(type_elements[0], ('AWS', 'OS'))
# check that value is a class object
@ -4257,4 +4257,4 @@ class TestResourceMapping(common.HeatTestCase):
# that there is no regressions
# It's soft check and should not be a cause of the merge conflict
# Feel free to update it in some separate patch
self.assertTrue(num_of_types >= 137)
self.assertGreaterEqual(num_of_types, 137)

9
heat/tests/test_template_format.py

@ -47,9 +47,10 @@ class JsonToYamlTest(common.HeatTestCase):
if template_test_count >= self.expected_test_count:
break
self.assertTrue(template_test_count >= self.expected_test_count,
'Expected at least %d templates to be tested, not %d' %
(self.expected_test_count, template_test_count))
self.assertGreaterEqual(
template_test_count, self.expected_test_count,
'Expected at least %d templates to be tested, not %d' %
(self.expected_test_count, template_test_count))
def compare_json_vs_yaml(self, json_str, yml_str):
yml = template_format.parse(yml_str)
@ -103,7 +104,7 @@ class YamlMinimalTest(common.HeatTestCase):
config.cfg.CONF.max_template_size / 3)
limit = config.cfg.CONF.max_template_size
long_yaml = yaml.safe_dump(template)
self.assertTrue(len(long_yaml) > limit)
self.assertGreater(len(long_yaml), limit)
ex = self.assertRaises(exception.RequestLimitExceeded,
template_format.parse, long_yaml)
msg = ('Request limit exceeded: Template size (%(actual_len)s '

10
heat/tests/test_watch.py

@ -512,7 +512,7 @@ class WatchRuleTest(common.HeatTestCase):
actions = wr.evaluate()
self.assertEqual('ALARM', wr.state)
self.assertEqual(['DummyAction'], actions)
self.assertTrue(mock_get_resource.call_count > 0)
self.assertGreater(mock_get_resource.call_count, 0)
@mock.patch('heat.engine.stack.Stack.resource_by_refid')
def test_rule_actions_alarm_two_actions(self, mock_get_resource):
@ -542,7 +542,7 @@ class WatchRuleTest(common.HeatTestCase):
actions = wr.evaluate()
self.assertEqual('ALARM', wr.state)
self.assertEqual(['DummyAction', 'DummyAction'], actions)
self.assertTrue(mock_get_resource.call_count > 0)
self.assertGreater(mock_get_resource.call_count, 0)
@mock.patch('heat.engine.stack.Stack.resource_by_refid')
def test_rule_actions_ok_alarm(self, mock_get_resource):
@ -583,7 +583,7 @@ class WatchRuleTest(common.HeatTestCase):
actions = wr.evaluate()
self.assertEqual('NORMAL', wr.state)
self.assertEqual(['DummyAction'], actions)
self.assertTrue(mock_get_resource.call_count > 0)
self.assertGreater(mock_get_resource.call_count, 0)
@mock.patch('heat.engine.stack.Stack.resource_by_refid')
def test_rule_actions_nodata(self, mock_get_resource):
@ -622,7 +622,7 @@ class WatchRuleTest(common.HeatTestCase):
actions = wr.evaluate()
self.assertEqual('NODATA', wr.state)
self.assertEqual(['DummyAction'], actions)
self.assertTrue(mock_get_resource.call_count > 0)
self.assertGreater(mock_get_resource.call_count, 0)
@mock.patch('heat.engine.stack.Stack.resource_by_refid')
def test_to_ceilometer(self, mock_get_resource):
@ -951,7 +951,7 @@ class WatchRuleTest(common.HeatTestCase):
actions = wr.set_watch_state(watchrule.WatchRule.ALARM)
self.assertEqual(['DummyAction'], actions)
self.assertTrue(mock_get_resource.call_count > 0)
self.assertGreater(mock_get_resource.call_count, 0)
def test_set_watch_state_invalid(self):
# Setup

8
heat_integrationtests/functional/test_resource_chain.py

@ -58,7 +58,7 @@ class ResourceChainTests(functional_base.FunctionalTestsBase):
# Verify
stack = self.client.stacks.get(stack_id)
self.assertTrue(stack is not None)
self.assertIsNotNone(stack)
# Top-level resource for chain
expected = {'my-chain': 'OS::Heat::ResourceChain'}
@ -74,15 +74,15 @@ class ResourceChainTests(functional_base.FunctionalTestsBase):
# Outputs
resource_ids = self._stack_output(stack, 'resource-ids')
self.assertTrue(resource_ids is not None)
self.assertIsNotNone(resource_ids)
self.assertEqual(2, len(resource_ids))
resource_value = self._stack_output(stack, 'resource-0-value')
self.assertTrue(resource_value is not None)
self.assertIsNotNone(resource_value)
self.assertEqual(8, len(resource_value)) # from parameter
resource_attrs = self._stack_output(stack, 'all-resource-attrs')
self.assertTrue(resource_attrs is not None)
self.assertIsNotNone(resource_attrs)
self.assertIsInstance(resource_attrs, dict)
self.assertEqual(2, len(resource_attrs))
self.assertEqual(8, len(resource_attrs['0']))

2
heat_integrationtests/functional/test_stack_events.py

@ -74,7 +74,7 @@ outputs:
stack_event.resource_name)
# Resource events are a subset of the original stack event list
self.assertTrue(len(resource_events) < len(stack_events))
self.assertLess(len(resource_events), len(stack_events))
# Get the event details for each resource event
for resource_event in resource_events:

4
heat_integrationtests/scenario/test_server_software_config.py

@ -100,7 +100,7 @@ class SoftwareConfigIntegrationTest(scenario_base.ScenarioTestsBase):
res1['result'])
self.assertEqual(0, res1['status_code'])
self.assertEqual('Output to stderr\n', res1['stderr'])
self.assertTrue(len(res1['stdout']) > 0)
self.assertGreater(len(res1['stdout']), 0)
res2 = self._stack_output(stack, 'res2')
self.assertEqual(
@ -118,7 +118,7 @@ class SoftwareConfigIntegrationTest(scenario_base.ScenarioTestsBase):
res3['result'])
self.assertEqual(0, res3['status_code'])
self.assertEqual('', res3['stderr'])
self.assertTrue(len(res1['stdout']) > 0)
self.assertGreater(len(res1['stdout']), 0)
dep1_resource = self.client.resources.get(sid, 'dep1')
dep1_id = dep1_resource.physical_resource_id

Loading…
Cancel
Save