Replace try...except...pass block with assertRaises

To make the test logic looks tidy and clean.

Change-Id: Id278524a8d4b8bf953f6c3d250e47be04c5edb55
This commit is contained in:
JUN JIE NAN 2014-01-03 16:36:53 +08:00
parent 51657807ae
commit 2572e753c2
9 changed files with 35 additions and 77 deletions

View File

@ -787,13 +787,11 @@ class StackControllerTest(ControllerTest, HeatTestCase):
self.m.ReplayAll()
try:
result = self.controller.lookup(req, tenant_id=identity.tenant,
stack_name=identity.stack_name)
except webob.exc.HTTPFound as found:
self.assertEqual(found.location, self._url(identity))
else:
self.fail('No redirect generated')
found = self.assertRaises(
webob.exc.HTTPFound, self.controller.lookup, req,
tenant_id=identity.tenant, stack_name=identity.stack_name)
self.assertEqual(found.location, self._url(identity))
self.m.VerifyAll()
def test_lookup_arn(self):
@ -803,13 +801,11 @@ class StackControllerTest(ControllerTest, HeatTestCase):
self.m.ReplayAll()
try:
result = self.controller.lookup(req, tenant_id=identity.tenant,
stack_name=identity.arn())
except webob.exc.HTTPFound as found:
self.assertEqual(found.location, self._url(identity))
else:
self.fail('No redirect generated')
found = self.assertRaises(
webob.exc.HTTPFound, self.controller.lookup,
req, tenant_id=identity.tenant, stack_name=identity.arn())
self.assertEqual(found.location, self._url(identity))
self.m.VerifyAll()
def test_lookup_nonexistant(self):
@ -851,16 +847,12 @@ class StackControllerTest(ControllerTest, HeatTestCase):
None).AndReturn(identity)
self.m.ReplayAll()
try:
result = self.controller.lookup(req, tenant_id=identity.tenant,
stack_name=identity.stack_name,
path='resources')
except webob.exc.HTTPFound as found:
self.assertEqual(found.location,
self._url(identity) + '/resources')
else:
self.fail('No redirect generated')
found = self.assertRaises(
webob.exc.HTTPFound, self.controller.lookup, req,
tenant_id=identity.tenant, stack_name=identity.stack_name,
path='resources')
self.assertEqual(found.location,
self._url(identity) + '/resources')
self.m.VerifyAll()
def test_lookup_resource_nonexistant(self):

View File

@ -37,13 +37,8 @@ class HOTemplateTest(HeatTestCase):
tmpl = parser.Template(hot_tpl_empty)
# check if we get the right class
self.assertTrue(isinstance(tmpl, hot.HOTemplate))
try:
# test getting an invalid section
tmpl['foobar']
except KeyError:
pass
else:
self.fail('Expected KeyError for invalid section')
# test getting an invalid section
self.assertNotIn('foobar', tmpl)
# test defaults for valid sections
self.assertEqual(tmpl[hot.VERSION], '2013-05-23')

View File

@ -264,15 +264,12 @@ class InstancesTest(HeatTestCase):
return_server.get()
self.m.ReplayAll()
try:
instance.check_create_complete(
(return_server, self.FakeVolumeAttach()))
except exception.Error as e:
self.assertEqual(
'Creation of server sample-server2 failed: Unknown (500)',
str(e))
else:
self.fail('Error not raised')
e = self.assertRaises(
exception.Error, instance.check_create_complete,
(return_server, self.FakeVolumeAttach()))
self.assertEqual(
'Creation of server sample-server2 failed: Unknown (500)',
str(e))
self.m.VerifyAll()

View File

@ -198,11 +198,8 @@ class LoadBalancerTest(HeatTestCase):
self.assertEqual('4.5.6.7', rsrc.FnGetAtt('DNSName'))
self.assertEqual('', rsrc.FnGetAtt('SourceSecurityGroup.GroupName'))
try:
rsrc.FnGetAtt('Foo')
raise Exception('Expected InvalidTemplateAttribute')
except exception.InvalidTemplateAttribute:
pass
self.assertRaises(exception.InvalidTemplateAttribute,
rsrc.FnGetAtt, 'Foo')
self.assertIsNone(rsrc.handle_update({}, {}, {}))

View File

@ -1452,11 +1452,8 @@ class NeutronFloatingIPTest(HeatTestCase):
self.assertIsNone(fip.FnGetAtt('show'))
self.assertEqual('fc68ea2c-b60b-4b4f-bd82-94ec81110766',
fip.FnGetAtt('show')['id'])
try:
fip.FnGetAtt('Foo')
raise Exception('Expected InvalidTemplateAttribute')
except exception.InvalidTemplateAttribute:
pass
self.assertRaises(exception.InvalidTemplateAttribute,
fip.FnGetAtt, 'Foo')
self.assertEqual(u'abcd1234', fip.FnGetAtt('floating_network_id'))
self.assertRaises(resource.UpdateReplace,

View File

@ -126,12 +126,7 @@ class TemplateTest(HeatTestCase):
def test_defaults(self):
empty = parser.Template({})
try:
empty[template.VERSION]
except KeyError:
pass
else:
self.fail('Expected KeyError for version not present')
self.assertNotIn(template.VERSION, empty)
self.assertEqual(empty[template.DESCRIPTION], 'No description')
self.assertEqual(empty[template.MAPPINGS], {})
self.assertEqual(empty[template.PARAMETERS], {})
@ -156,12 +151,7 @@ Mappings:
def test_invalid_section(self):
tmpl = parser.Template({'Foo': ['Bar']})
try:
tmpl['Foo']
except KeyError:
pass
else:
self.fail('Expected KeyError for invalid template key')
self.assertNotIn('Foo', tmpl)
def test_find_in_map(self):
tmpl = parser.Template(mapping_template)

View File

@ -150,11 +150,7 @@ class ResourceGroupTest(common.HeatTestCase):
stack = utils.parse_stack(template2)
snip = stack.t['Resources']['group1']
resgrp = resource_group.ResourceGroup('test', snip, stack)
try:
resgrp.validate()
except exception.StackValidationFailed as exc:
# this is not a normal exception failure but a test failure.
self.fail(str(exc))
self.assertIsNone(resgrp.validate())
@utils.stack_delete_after
def test_delete(self):

View File

@ -114,11 +114,8 @@ class s3Test(HeatTestCase):
self.assertEqual(url, rsrc.FnGetAtt('WebsiteURL'))
try:
rsrc.FnGetAtt('Foo')
raise Exception('Expected InvalidTemplateAttribute')
except exception.InvalidTemplateAttribute:
pass
self.assertRaises(exception.InvalidTemplateAttribute,
rsrc.FnGetAtt, 'Foo')
self.assertRaises(resource.UpdateReplace,
rsrc.handle_update, {}, {}, {})

View File

@ -160,11 +160,8 @@ class swiftTest(HeatTestCase):
self.assertEqual('17680980', rsrc.FnGetAtt('BytesUsed'))
self.assertEqual(headers, rsrc.FnGetAtt('HeadContainer'))
try:
rsrc.FnGetAtt('Foo')
raise Exception('Expected InvalidTemplateAttribute')
except swift.exception.InvalidTemplateAttribute:
pass
self.assertRaises(swift.exception.InvalidTemplateAttribute,
rsrc.FnGetAtt, 'Foo')
self.assertRaises(resource.UpdateReplace,
rsrc.handle_update, {}, {}, {})