Standardises expected exception layout
Standardises how we handle expected exceptions to use
assertRaise rather than using try/except/else
Fixes bug 1132577
Change-Id: Idfefe4c9337e9b832d34a490da21ac32ec65edd1
diff --git a/tempest/tests/compute/floating_ips/test_floating_ips_actions.py b/tempest/tests/compute/floating_ips/test_floating_ips_actions.py
index 0f63016..d9f9613 100644
--- a/tempest/tests/compute/floating_ips/test_floating_ips_actions.py
+++ b/tempest/tests/compute/floating_ips/test_floating_ips_actions.py
@@ -114,42 +114,26 @@
# Negative test:Deletion of a nonexistent floating IP
# from project should fail
- #Deleting the non existent floating IP
- try:
- resp, body = self.client.delete_floating_ip(self.non_exist_id)
- except Exception:
- pass
- else:
- self.fail('Should not be able to delete a nonexistent floating IP')
+ # Deleting the non existent floating IP
+ self.assertRaises(exceptions.NotFound, self.client.delete_floating_ip,
+ self.non_exist_id)
@attr(type='negative')
def test_associate_nonexistant_floating_ip(self):
# Negative test:Association of a non existent floating IP
# to specific server should fail
- #Associating non existent floating IP
- try:
- resp, body = \
- self.client.associate_floating_ip_to_server("0.0.0.0",
- self.server_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to associate'
- ' a nonexistent floating IP')
+ # Associating non existent floating IP
+ self.assertRaises(exceptions.NotFound,
+ self.client.associate_floating_ip_to_server,
+ "0.0.0.0", self.server_id)
@attr(type='negative')
def test_dissociate_nonexistant_floating_ip(self):
# Negative test:Dissociation of a non existent floating IP should fail
- #Dissociating non existent floating IP
- try:
- resp, body = \
- self.client.disassociate_floating_ip_from_server("0.0.0.0",
- self.server_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to dissociate'
- ' a nonexistent floating IP')
+ # Dissociating non existent floating IP
+ self.assertRaises(exceptions.NotFound,
+ self.client.disassociate_floating_ip_from_server,
+ "0.0.0.0", self.server_id)
@attr(type='positive')
def test_associate_already_associated_floating_ip(self):
@@ -171,38 +155,24 @@
self.client.associate_floating_ip_to_server(self.floating_ip,
self.new_server_id)
- #Make sure no longer associated with old server
- try:
- self.client.disassociate_floating_ip_from_server(
- self.floating_ip,
- self.server_id)
- except (exceptions.NotFound, exceptions.BadRequest):
- pass
- else:
- self.fail('The floating IP should be associated to the second '
- 'server')
+ self.addCleanup(self.servers_client.delete_server, self.new_server_id)
if (resp['status'] is not None):
- #Dissociation of the floating IP associated in this method
- resp, _ = \
- self.client.disassociate_floating_ip_from_server(
- self.floating_ip,
- self.new_server_id)
- #Deletion of server created in this method
- resp, body = self.servers_client.delete_server(self.new_server_id)
+ self.addCleanup(self.client.disassociate_floating_ip_from_server,
+ self.floating_ip,
+ self.new_server_id)
+
+ # Make sure no longer associated with old server
+ self.assertRaises((exceptions.NotFound, exceptions.BadRequest),
+ self.client.disassociate_floating_ip_from_server,
+ self.floating_ip, self.server_id)
@attr(type='negative')
def test_associate_ip_to_server_without_passing_floating_ip(self):
# Negative test:Association of empty floating IP to specific server
# should raise NotFound exception
- try:
- resp, body =\
- self.client.associate_floating_ip_to_server('',
- self.server_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Association of floating IP to specific server'
- ' with out passing floating IP should raise BadRequest')
+ self.assertRaises(exceptions.NotFound,
+ self.client.associate_floating_ip_to_server,
+ '', self.server_id)
class FloatingIPsTestXML(FloatingIPsTestJSON):
diff --git a/tempest/tests/compute/floating_ips/test_list_floating_ips.py b/tempest/tests/compute/floating_ips/test_list_floating_ips.py
index 11b9465..b795909 100644
--- a/tempest/tests/compute/floating_ips/test_list_floating_ips.py
+++ b/tempest/tests/compute/floating_ips/test_list_floating_ips.py
@@ -90,14 +90,8 @@
non_exist_id = rand_name('999')
if non_exist_id not in floating_ip_id:
break
- try:
- resp, body = \
- self.client.get_floating_ip_details(non_exist_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to GET the details from a'
- 'nonexistant floating IP')
+ self.assertRaises(exceptions.NotFound,
+ self.client.get_floating_ip_details, non_exist_id)
class FloatingIPDetailsTestXML(FloatingIPDetailsTestJSON):
diff --git a/tempest/tests/compute/images/test_image_metadata.py b/tempest/tests/compute/images/test_image_metadata.py
index fa69395..311ee8e 100644
--- a/tempest/tests/compute/images/test_image_metadata.py
+++ b/tempest/tests/compute/images/test_image_metadata.py
@@ -111,68 +111,41 @@
def test_list_nonexistant_image_metadata(self):
# Negative test: List on nonexistant image
# metadata should not happen
- try:
- resp, resp_metadata = self.client.list_image_metadata(999)
- except exceptions.NotFound:
- pass
- else:
- self.fail('List on nonexistant image metadata should'
- 'not happen')
+ self.assertRaises(exceptions.NotFound, self.client.list_image_metadata,
+ 999)
@attr(type='negative')
def test_update_nonexistant_image_metadata(self):
# Negative test:An update should not happen for a nonexistant image
meta = {'key1': 'alt1', 'key2': 'alt2'}
- try:
- resp, metadata = self.client.update_image_metadata(999, meta)
- except exceptions.NotFound:
- pass
- else:
- self.fail('An update shouldnt happen for nonexistant image')
+ self.assertRaises(exceptions.NotFound,
+ self.client.update_image_metadata, 999, meta)
@attr(type='negative')
def test_get_nonexistant_image_metadata_item(self):
# Negative test: Get on nonexistant image should not happen
- try:
- resp, metadata = self.client.get_image_metadata_item(999, 'key2')
- except exceptions.NotFound:
- pass
- else:
- self.fail('Get on nonexistant image should not happen')
+ self.assertRaises(exceptions.NotFound,
+ self.client.get_image_metadata_item, 999, 'key2')
@attr(type='negative')
def test_set_nonexistant_image_metadata(self):
# Negative test: Metadata should not be set to a nonexistant image
meta = {'key1': 'alt1', 'key2': 'alt2'}
- try:
- resp, meta = self.client.set_image_metadata(999, meta)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Metadata should not be set to a nonexistant image')
+ self.assertRaises(exceptions.NotFound, self.client.set_image_metadata,
+ 999, meta)
@attr(type='negative')
def test_set_nonexistant_image_metadata_item(self):
# Negative test: Metadata item should not be set to a
# nonexistant image
meta = {'key1': 'alt'}
- try:
- resp, body = self.client.set_image_metadata_item(999, 'key1', meta)
- resp, metadata = self.client.list_image_metadata(999)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Metadata item should not be set to a nonexistant image')
+ self.assertRaises(exceptions.NotFound,
+ self.client.set_image_metadata_item, 999, 'key1',
+ meta)
@attr(type='negative')
def test_delete_nonexistant_image_metadata_item(self):
# Negative test: Shouldnt be able to delete metadata
- # item from nonexistant image
- try:
- resp, body = self.client.delete_image_metadata_item(999, 'key1')
- resp, metadata = self.client.list_image_metadata(999)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to delete metadata item from a'
- 'nonexistant image')
+ # item from nonexistant image
+ self.assertRaises(exceptions.NotFound,
+ self.client.delete_image_metadata_item, 999, 'key1')
diff --git a/tempest/tests/compute/images/test_images.py b/tempest/tests/compute/images/test_images.py
index 1e90202..a61cef6 100644
--- a/tempest/tests/compute/images/test_images.py
+++ b/tempest/tests/compute/images/test_images.py
@@ -133,40 +133,24 @@
@attr(type='negative')
def test_create_image_specify_uuid_35_characters_or_less(self):
# Return an error if Image ID passed is 35 characters or less
- try:
- snapshot_name = rand_name('test-snap-')
- test_uuid = ('a' * 35)
- self.assertRaises(exceptions.NotFound, self.client.create_image,
- test_uuid, snapshot_name)
- except Exception:
- self.fail("Should return 404 Not Found if server uuid is 35"
- " characters or less")
+ snapshot_name = rand_name('test-snap-')
+ test_uuid = ('a' * 35)
+ self.assertRaises(exceptions.NotFound, self.client.create_image,
+ test_uuid, snapshot_name)
@attr(type='negative')
def test_create_image_specify_uuid_37_characters_or_more(self):
# Return an error if Image ID passed is 37 characters or more
- try:
- snapshot_name = rand_name('test-snap-')
- test_uuid = ('a' * 37)
- self.assertRaises(exceptions.NotFound, self.client.create_image,
- test_uuid, snapshot_name)
- except Exception:
- self.fail("Should return 404 Not Found if server uuid is 37"
- " characters or more")
+ snapshot_name = rand_name('test-snap-')
+ test_uuid = ('a' * 37)
+ self.assertRaises(exceptions.NotFound, self.client.create_image,
+ test_uuid, snapshot_name)
@attr(type='negative')
def test_delete_image_with_invalid_image_id(self):
# An image should not be deleted with invalid image id
- try:
- # Delete an image with invalid image id
- resp, _ = self.client.delete_image('!@$%^&*()')
-
- except exceptions.NotFound:
- pass
-
- else:
- self.fail("DELETE image request should rasie NotFound exception "
- "when requested with invalid image")
+ self.assertRaises(exceptions.NotFound, self.client.delete_image,
+ '!@$%^&*()')
@attr(type='negative')
def test_delete_non_existent_image(self):
@@ -179,44 +163,25 @@
@attr(type='negative')
def test_delete_image_blank_id(self):
# Return an error while trying to delete an image with blank Id
-
- try:
- self.assertRaises(exceptions.NotFound, self.client.delete_image,
- '')
- except Exception:
- self.fail("Did not return HTTP 404 NotFound for blank image id")
+ self.assertRaises(exceptions.NotFound, self.client.delete_image, '')
@attr(type='negative')
def test_delete_image_non_hex_string_id(self):
# Return an error while trying to delete an image with non hex id
-
image_id = '11a22b9-120q-5555-cc11-00ab112223gj'
- try:
- self.assertRaises(exceptions.NotFound, self.client.delete_image,
- image_id)
- except Exception:
- self.fail("Did not return HTTP 404 NotFound for non hex image")
+ self.assertRaises(exceptions.NotFound, self.client.delete_image,
+ image_id)
@attr(type='negative')
def test_delete_image_negative_image_id(self):
# Return an error while trying to delete an image with negative id
-
- try:
- self.assertRaises(exceptions.NotFound, self.client.delete_image,
- -1)
- except Exception:
- self.fail("Did not return HTTP 404 NotFound for negative image id")
+ self.assertRaises(exceptions.NotFound, self.client.delete_image, -1)
@attr(type='negative')
def test_delete_image_id_is_over_35_character_limit(self):
# Return an error while trying to delete image with id over limit
-
- try:
- self.assertRaises(exceptions.NotFound, self.client.delete_image,
- '11a22b9-120q-5555-cc11-00ab112223gj-3fac')
- except Exception:
- self.fail("Did not return HTTP 404 NotFound for image id that "
- "exceeds 35 character ID length limit")
+ self.assertRaises(exceptions.NotFound, self.client.delete_image,
+ '11a22b9-120q-5555-cc11-00ab112223gj-3fac')
class ImagesTestXML(ImagesTestJSON):
diff --git a/tempest/tests/compute/images/test_images_oneserver.py b/tempest/tests/compute/images/test_images_oneserver.py
index 6d3f043..d89b6dd 100644
--- a/tempest/tests/compute/images/test_images_oneserver.py
+++ b/tempest/tests/compute/images/test_images_oneserver.py
@@ -61,40 +61,28 @@
@testtools.skip("Until Bug 1006725 is fixed")
def test_create_image_specify_multibyte_character_image_name(self):
# Return an error if the image name has multi-byte characters
- try:
- snapshot_name = rand_name('\xef\xbb\xbf')
- self.assertRaises(exceptions.BadRequest,
- self.client.create_image, self.server['id'],
- snapshot_name)
- except Exception:
- self.fail("Should return 400 Bad Request if multi byte characters"
- " are used for image name")
+ snapshot_name = rand_name('\xef\xbb\xbf')
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_image, self.server['id'],
+ snapshot_name)
@attr(type='negative')
@testtools.skip("Until Bug 1005423 is fixed")
def test_create_image_specify_invalid_metadata(self):
# Return an error when creating image with invalid metadata
- try:
- snapshot_name = rand_name('test-snap-')
- meta = {'': ''}
- self.assertRaises(exceptions.BadRequest, self.client.create_image,
- self.server['id'], snapshot_name, meta)
-
- except Exception:
- self.fail("Should raise 400 Bad Request if meta data is invalid")
+ snapshot_name = rand_name('test-snap-')
+ meta = {'': ''}
+ self.assertRaises(exceptions.BadRequest, self.client.create_image,
+ self.server['id'], snapshot_name, meta)
@attr(type='negative')
@testtools.skip("Until Bug 1005423 is fixed")
def test_create_image_specify_metadata_over_limits(self):
# Return an error when creating image with meta data over 256 chars
- try:
- snapshot_name = rand_name('test-snap-')
- meta = {'a' * 260: 'b' * 260}
- self.assertRaises(exceptions.OverLimit, self.client.create_image,
- self.server['id'], snapshot_name, meta)
-
- except Exception:
- self.fail("Should raise 413 Over Limit if meta data was too long")
+ snapshot_name = rand_name('test-snap-')
+ meta = {'a' * 260: 'b' * 260}
+ self.assertRaises(exceptions.OverLimit, self.client.create_image,
+ self.server['id'], snapshot_name, meta)
@attr(type='negative')
@testtools.skipUnless(compute.MULTI_USER,
@@ -151,38 +139,28 @@
def test_create_second_image_when_first_image_is_being_saved(self):
# Disallow creating another image when first image is being saved
- try:
- # Create first snapshot
- snapshot_name = rand_name('test-snap-')
- resp, body = self.client.create_image(self.server['id'],
- snapshot_name)
- self.assertEqual(202, resp.status)
- image_id = parse_image_id(resp['location'])
- self.image_ids.append(image_id)
+ # Create first snapshot
+ snapshot_name = rand_name('test-snap-')
+ resp, body = self.client.create_image(self.server['id'],
+ snapshot_name)
+ self.assertEqual(202, resp.status)
+ image_id = parse_image_id(resp['location'])
+ self.image_ids.append(image_id)
- # Create second snapshot
- alt_snapshot_name = rand_name('test-snap-')
- self.client.create_image(self.server['id'],
- alt_snapshot_name)
- except exceptions.Duplicate:
- self.client.wait_for_image_status(image_id, 'ACTIVE')
-
- else:
- self.fail("Should not allow creating an image when another image "
- "of the server is still being saved")
+ # Create second snapshot
+ alt_snapshot_name = rand_name('test-snap-')
+ self.assertRaises(exceptions.Duplicate, self.client.create_image,
+ self.server['id'], alt_snapshot_name)
+ self.client.wait_for_image_status(image_id, 'ACTIVE')
@attr(type='negative')
@testtools.skip("Until Bug 1004564 is fixed")
def test_create_image_specify_name_over_256_chars(self):
# Return an error if snapshot name over 256 characters is passed
- try:
- snapshot_name = rand_name('a' * 260)
- self.assertRaises(exceptions.BadRequest, self.client.create_image,
- self.server['id'], snapshot_name)
- except Exception:
- self.fail("Should return 400 Bad Request if image name is over 256"
- " characters")
+ snapshot_name = rand_name('a' * 260)
+ self.assertRaises(exceptions.BadRequest, self.client.create_image,
+ self.server['id'], snapshot_name)
@attr(type='negative')
def test_delete_image_that_is_not_yet_active(self):
diff --git a/tempest/tests/compute/images/test_list_image_filters.py b/tempest/tests/compute/images/test_list_image_filters.py
index 5038a65..472f7fb 100644
--- a/tempest/tests/compute/images/test_list_image_filters.py
+++ b/tempest/tests/compute/images/test_list_image_filters.py
@@ -225,9 +225,4 @@
@attr(type='negative')
def test_get_nonexistant_image(self):
# Negative test: GET on non existant image should fail
- try:
- resp, image = self.client.get_image(999)
- except Exception:
- pass
- else:
- self.fail('GET on non existant image should fail')
+ self.assertRaises(exceptions.NotFound, self.client.get_image, 999)
diff --git a/tempest/tests/compute/keypairs/test_keypairs.py b/tempest/tests/compute/keypairs/test_keypairs.py
index aefc5ff..b48b439 100644
--- a/tempest/tests/compute/keypairs/test_keypairs.py
+++ b/tempest/tests/compute/keypairs/test_keypairs.py
@@ -134,48 +134,32 @@
# Keypair should not be created with a non RSA public key
k_name = rand_name('keypair-')
pub_key = "ssh-rsa JUNK nova@ubuntu"
- try:
- resp, _ = self.client.create_keypair(k_name, pub_key)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Expected BadRequest for invalid public key')
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_keypair, k_name, pub_key)
@attr(type='negative')
@testtools.skip("Skipped until the Bug #1086980 is resolved")
def test_keypair_delete_nonexistant_key(self):
# Non-existant key deletion should throw a proper error
k_name = rand_name("keypair-non-existant-")
- try:
- resp, _ = self.client.delete_keypair(k_name)
- except exceptions.NotFound:
- pass
- else:
- self.fail('nonexistent key')
+ self.assertRaises(exceptions.NotFound, self.client.delete_keypair,
+ k_name)
@attr(type='negative')
def test_create_keypair_with_empty_public_key(self):
# Keypair should not be created with an empty public key
k_name = rand_name("keypair-")
pub_key = ' '
- try:
- resp, _ = self.client.create_keypair(k_name, pub_key)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Expected BadRequest for empty public key')
+ self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
+ k_name, pub_key)
@attr(type='negative')
def test_create_keypair_when_public_key_bits_exceeds_maximum(self):
# Keypair should not be created when public key bits are too long
k_name = rand_name("keypair-")
pub_key = 'ssh-rsa ' + 'A' * 2048 + ' openstack@ubuntu'
- try:
- resp, _ = self.client.create_keypair(k_name, pub_key)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Expected BadRequest for too long public key')
+ self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
+ k_name, pub_key)
@attr(type='negative')
def test_create_keypair_with_duplicate_name(self):
@@ -184,47 +168,30 @@
resp, _ = self.client.create_keypair(k_name)
self.assertEqual(200, resp.status)
#Now try the same keyname to ceate another key
- try:
- resp, _ = self.client.create_keypair(k_name)
- #Expect a HTTP 409 Conflict Error
- except exceptions.Duplicate:
- pass
- else:
- self.fail('duplicate name')
+ self.assertRaises(exceptions.Duplicate, self.client.create_keypair,
+ k_name)
resp, _ = self.client.delete_keypair(k_name)
self.assertEqual(202, resp.status)
@attr(type='negative')
def test_create_keypair_with_empty_name_string(self):
# Keypairs with name being an empty string should not be created
- try:
- resp, _ = self.client.create_keypair('')
- except exceptions.BadRequest:
- pass
- else:
- self.fail('empty string')
+ self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
+ '')
@attr(type='negative')
def test_create_keypair_with_long_keynames(self):
# Keypairs with name longer than 255 chars should not be created
k_name = 'keypair-'.ljust(260, '0')
- try:
- resp, _ = self.client.create_keypair(k_name)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('too long')
+ self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
+ k_name)
@attr(type='negative')
def test_create_keypair_invalid_name(self):
# Keypairs with name being an invalid name should not be created
k_name = 'key_/.\@:'
- try:
- resp, _ = self.client.create_keypair(k_name)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('invalid name')
+ self.assertRaises(exceptions.BadRequest, self.client.create_keypair,
+ k_name)
class KeyPairsTestXML(KeyPairsTestJSON):
diff --git a/tempest/tests/compute/security_groups/test_security_group_rules.py b/tempest/tests/compute/security_groups/test_security_group_rules.py
index 5063fd3..32ac52b 100644
--- a/tempest/tests/compute/security_groups/test_security_group_rules.py
+++ b/tempest/tests/compute/security_groups/test_security_group_rules.py
@@ -135,21 +135,14 @@
def test_security_group_rules_create_with_invalid_id(self):
# Negative test: Creation of Security Group rule should FAIL
# with invalid Parent group id
- #Adding rules to the invalid Security Group id
+ # Adding rules to the invalid Security Group id
parent_group_id = rand_name('999')
ip_protocol = 'tcp'
from_port = 22
to_port = 22
- try:
- resp, rule =\
- self.client.create_security_group_rule(parent_group_id,
- ip_protocol,
- from_port, to_port)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Security Group rule should not be created '
- 'with invalid parent group id')
+ self.assertRaises(exceptions.NotFound,
+ self.client.create_security_group_rule,
+ parent_group_id, ip_protocol, from_port, to_port)
@attr(type='negative')
def test_security_group_rules_create_with_invalid_ip_protocol(self):
@@ -165,18 +158,11 @@
ip_protocol = rand_name('999')
from_port = 22
to_port = 22
- try:
- resp, rule =\
- self.client.create_security_group_rule(parent_group_id,
- ip_protocol,
- from_port, to_port)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group rule should not be created '
- 'with invalid ip_protocol')
- #Deleting the Security Group created in this method
- resp, _ = self.client.delete_security_group(securitygroup['id'])
+
+ self.addCleanup(self.client.delete_security_group, securitygroup['id'])
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group_rule,
+ parent_group_id, ip_protocol, from_port, to_port)
@attr(type='negative')
def test_security_group_rules_create_with_invalid_from_port(self):
@@ -192,18 +178,10 @@
ip_protocol = 'tcp'
from_port = rand_name('999')
to_port = 22
- try:
- resp, rule =\
- self.client.create_security_group_rule(parent_group_id,
- ip_protocol,
- from_port, to_port)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group rule should not be created'
- 'with invalid from_port')
- #Deleting the Security Group created in this method
- resp, _ = self.client.delete_security_group(securitygroup['id'])
+ self.addCleanup(self.client.delete_security_group, securitygroup['id'])
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group_rule,
+ parent_group_id, ip_protocol, from_port, to_port)
@attr(type='negative')
def test_security_group_rules_create_with_invalid_to_port(self):
@@ -219,30 +197,18 @@
ip_protocol = 'tcp'
from_port = 22
to_port = rand_name('999')
- try:
- resp, rule =\
- self.client.create_security_group_rule(parent_group_id,
- ip_protocol,
- from_port, to_port)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group rule should not be created'
- 'with invalid from_port')
- #Deleting the Security Group created in this method
- resp, _ = self.client.delete_security_group(securitygroup['id'])
+ self.addCleanup(self.client.delete_security_group, securitygroup['id'])
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group_rule,
+ parent_group_id, ip_protocol, from_port, to_port)
@attr(type='negative')
def test_security_group_rules_delete_with_invalid_id(self):
# Negative test: Deletion of Security Group rule should be FAIL
# with invalid rule id
- try:
- self.client.delete_security_group_rule(rand_name('999'))
- except exceptions.NotFound:
- pass
- else:
- self.fail('Security Group Rule should not be deleted '
- 'with nonexistant rule id')
+ self.assertRaises(exceptions.NotFound,
+ self.client.delete_security_group_rule,
+ rand_name('999'))
class SecurityGroupRulesTestXML(SecurityGroupRulesTestJSON):
diff --git a/tempest/tests/compute/security_groups/test_security_groups.py b/tempest/tests/compute/security_groups/test_security_groups.py
index c086280..e5b0380 100644
--- a/tempest/tests/compute/security_groups/test_security_groups.py
+++ b/tempest/tests/compute/security_groups/test_security_groups.py
@@ -116,100 +116,60 @@
non_exist_id = rand_name('999')
if non_exist_id not in security_group_id:
break
- try:
- resp, body = \
- self.client.get_security_group(non_exist_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to GET the details from a '
- 'nonexistant Security Group')
+ self.assertRaises(exceptions.NotFound, self.client.get_security_group,
+ non_exist_id)
@attr(type='negative')
def test_security_group_create_with_invalid_group_name(self):
# Negative test: Security Group should not be created with group name
# as an empty string/with white spaces/chars more than 255
s_description = rand_name('description-')
- #Create Security Group with empty string as group name
- try:
- resp, _ = self.client.create_security_group("", s_description)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with EMPTY Name')
- #Create Security Group with white space in group name
- try:
- resp, _ = self.client.create_security_group(" ", s_description)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with WHITE SPACE in Name')
- #Create Security Group with group name longer than 255 chars
+ # Create Security Group with empty string as group name
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, "", s_description)
+ # Create Security Group with white space in group name
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, " ",
+ s_description)
+ # Create Security Group with group name longer than 255 chars
s_name = 'securitygroup-'.ljust(260, '0')
- try:
- resp, _ = self.client.create_security_group(s_name, s_description)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with more than 255 chars in Name')
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, s_name,
+ s_description)
@attr(type='negative')
def test_security_group_create_with_invalid_group_description(self):
# Negative test:Security Group should not be created with description
# as an empty string/with white spaces/chars more than 255
s_name = rand_name('securitygroup-')
- #Create Security Group with empty string as description
- try:
- resp, _ = self.client.create_security_group(s_name, "")
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with EMPTY Description')
- #Create Security Group with white space in description
- try:
- resp, _ = self.client.create_security_group(s_name, " ")
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with WHITE SPACE in Description')
- #Create Security Group with group description longer than 255 chars
+ # Create Security Group with empty string as description
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, s_name, "")
+ # Create Security Group with white space in description
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, s_name, " ")
+ # Create Security Group with group description longer than 255 chars
s_description = 'description-'.ljust(260, '0')
- try:
- resp, _ = self.client.create_security_group(s_name, s_description)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with more than 255 chars in Description')
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, s_name,
+ s_description)
@attr(type='negative')
def test_security_group_create_with_duplicate_name(self):
# Negative test:Security Group with duplicate name should not
# be created
- try:
- s_name = rand_name('securitygroup-')
- s_description = rand_name('description-')
- resp, security_group =\
+ s_name = rand_name('securitygroup-')
+ s_description = rand_name('description-')
+ resp, security_group =\
self.client.create_security_group(s_name, s_description)
- self.assertEqual(200, resp.status)
- #Now try the Security Group with the same 'Name'
- try:
- resp, _ =\
- self.client.create_security_group(s_name, s_description)
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Security Group should not be created '
- 'with duplicate Group Name')
- finally:
- #Delete the Security Group created in this method
- resp, _ = self.client.delete_security_group(security_group['id'])
- self.assertEqual(202, resp.status)
+ self.assertEqual(200, resp.status)
+
+ self.addCleanup(self.client.delete_security_group,
+ security_group['id'])
+ # Now try the Security Group with the same 'Name'
+ self.assertRaises(exceptions.BadRequest,
+ self.client.create_security_group, s_name,
+ s_description)
@attr(type='negative')
def test_delete_nonexistant_security_group(self):
@@ -223,25 +183,15 @@
non_exist_id = rand_name('999')
if non_exist_id not in security_group_id:
break
- try:
- resp, body = self.client.delete_security_group(non_exist_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to delete a nonexistant '
- 'Security Group')
+ self.assertRaises(exceptions.NotFound,
+ self.client.delete_security_group, non_exist_id)
@attr(type='negative')
def test_delete_security_group_without_passing_id(self):
# Negative test:Deletion of a Security Group with out passing ID
# should Fail
- try:
- resp, body = self.client.delete_security_group('')
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to delete a Security Group'
- 'with out passing ID')
+ self.assertRaises(exceptions.NotFound,
+ self.client.delete_security_group, '')
def test_server_security_groups(self):
# Checks that security groups may be added and linked to a server
diff --git a/tempest/tests/compute/servers/test_server_addresses.py b/tempest/tests/compute/servers/test_server_addresses.py
index b811d52..c69f68d 100644
--- a/tempest/tests/compute/servers/test_server_addresses.py
+++ b/tempest/tests/compute/servers/test_server_addresses.py
@@ -43,26 +43,15 @@
@attr(type='negative', category='server-addresses')
def test_list_server_addresses_invalid_server_id(self):
# List addresses request should fail if server id not in system
-
- try:
- self.client.list_addresses('999')
- except exceptions.NotFound:
- pass
- else:
- self.fail('The server rebuild for a non existing server should not'
- ' be allowed')
+ self.assertRaises(exceptions.NotFound, self.client.list_addresses,
+ '999')
@attr(type='negative', category='server-addresses')
def test_list_server_addresses_by_network_neg(self):
# List addresses by network should fail if network name not valid
-
- try:
- self.client.list_addresses_by_network(self.server['id'], 'invalid')
- except exceptions.NotFound:
- pass
- else:
- self.fail('The server rebuild for a non existing server should not'
- ' be allowed')
+ self.assertRaises(exceptions.NotFound,
+ self.client.list_addresses_by_network,
+ self.server['id'], 'invalid')
@attr(type='smoke', category='server-addresses')
def test_list_server_addresses(self):
diff --git a/tempest/tests/compute/volumes/test_volumes_negative.py b/tempest/tests/compute/volumes/test_volumes_negative.py
index c0fa565..306b93b 100644
--- a/tempest/tests/compute/volumes/test_volumes_negative.py
+++ b/tempest/tests/compute/volumes/test_volumes_negative.py
@@ -39,18 +39,13 @@
non_exist_id = rand_name('999')
if non_exist_id not in volume_id_list:
break
- #Trying to GET a non existant volume
- try:
- resp, body = self.client.get_volume(non_exist_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to GET the details from a '
- 'nonexistant volume')
+ # Trying to GET a non existant volume
+ self.assertRaises(exceptions.NotFound, self.client.get_volume,
+ non_exist_id)
def test_volume_delete_nonexistant_volume_id(self):
# Negative: Should not be able to delete nonexistant Volume
- #Creating nonexistant volume id
+ # Creating nonexistant volume id
volume_id_list = list()
resp, body = self.client.list_volumes()
for i in range(len(body)):
@@ -59,13 +54,9 @@
non_exist_id = rand_name('999')
if non_exist_id not in volume_id_list:
break
- #Trying to DELETE a non existant volume
- try:
- resp, body = self.client.delete_volume(non_exist_id)
- except exceptions.NotFound:
- pass
- else:
- self.fail('Should not be able to DELETE a nonexistant volume')
+ # Trying to DELETE a non existant volume
+ self.assertRaises(exceptions.NotFound, self.client.delete_volume,
+ non_exist_id)
def test_create_volume_with_invalid_size(self):
# Negative: Should not be able to create volume with invalid size
diff --git a/tempest/tests/identity/admin/test_roles.py b/tempest/tests/identity/admin/test_roles.py
index 53c5b0d..f71bed0 100644
--- a/tempest/tests/identity/admin/test_roles.py
+++ b/tempest/tests/identity/admin/test_roles.py
@@ -95,15 +95,9 @@
role1_id = body.get('id')
self.assertTrue('status' in resp)
self.assertTrue(resp['status'].startswith('2'))
-
- try:
- resp, body = self.client.create_role(role_name)
- # this should raise an exception
- self.fail('Should not be able to create a duplicate role name.'
- ' %s' % role_name)
- except exceptions.Duplicate:
- pass
- self.client.delete_role(role1_id)
+ self.addCleanup(self.client.delete_role, role1_id)
+ self.assertRaises(exceptions.Duplicate, self.client.create_role,
+ role_name)
def test_assign_user_role(self):
# Assign a role to a user on a tenant
diff --git a/tempest/tests/identity/admin/test_tenants.py b/tempest/tests/identity/admin/test_tenants.py
index a3c9246..8155eb5 100644
--- a/tempest/tests/identity/admin/test_tenants.py
+++ b/tempest/tests/identity/admin/test_tenants.py
@@ -151,14 +151,10 @@
self.data.tenants.append(tenant)
tenant1_id = body.get('id')
- try:
- resp, body = self.client.create_tenant(tenant_name)
- # this should have raised an exception
- self.fail('Should not be able to create a duplicate tenant name')
- except exceptions.Duplicate:
- pass
- self.client.delete_tenant(tenant1_id)
- self.data.tenants.remove(tenant)
+ self.addCleanup(self.client.delete_tenant, tenant1_id)
+ self.addCleanup(self.data.tenants.remove, tenant)
+ self.assertRaises(exceptions.Duplicate, self.client.create_tenant,
+ tenant_name)
@attr(type='negative')
def test_create_tenant_by_unauthorized_user(self):
diff --git a/tempest/tests/image/test_images.py b/tempest/tests/image/test_images.py
index 6ac852e..3a70b3d 100644
--- a/tempest/tests/image/test_images.py
+++ b/tempest/tests/image/test_images.py
@@ -47,21 +47,13 @@
@attr(type='negative')
def test_register_with_invalid_container_format(self):
# Negative tests for invalid data supplied to POST /images
- try:
- resp, body = self.client.create_image('test', 'wrong', 'vhd')
- except exceptions.BadRequest:
- pass
- else:
- self.fail('Invalid container format should not be accepted')
+ self.assertRaises(exceptions.BadRequest, self.client.create_image,
+ 'test', 'wrong', 'vhd')
@attr(type='negative')
def test_register_with_invalid_disk_format(self):
- try:
- resp, body = self.client.create_image('test', 'bare', 'wrong')
- except exceptions.BadRequest:
- pass
- else:
- self.fail("Invalid disk format should not be accepted")
+ self.assertRaises(exceptions.BadRequest, self.client.create_image,
+ 'test', 'bare', 'wrong')
@attr(type='image')
def test_register_then_upload(self):