Merge "Replace try/except/else with self.assertRaises."
diff --git a/tempest/tests/compute/volumes/test_attach_volume.py b/tempest/tests/compute/volumes/test_attach_volume.py
index 2679312..7c1a2d1 100644
--- a/tempest/tests/compute/volumes/test_attach_volume.py
+++ b/tempest/tests/compute/volumes/test_attach_volume.py
@@ -28,6 +28,12 @@
_interface = 'json'
run_ssh = tempest.config.TempestConfig().compute.run_ssh
+ def __init__(self, *args, **kwargs):
+ super(AttachVolumeTestJSON, self).__init__(*args, **kwargs)
+ self.server = None
+ self.volume = None
+ self.attached = False
+
@classmethod
def setUpClass(cls):
super(AttachVolumeTestJSON, cls).setUpClass()
@@ -37,9 +43,13 @@
self.servers_client.detach_volume(server_id, volume_id)
self.volumes_client.wait_for_volume_status(volume_id, 'available')
- def _delete(self, server_id, volume_id):
- self.volumes_client.delete_volume(volume_id)
- self.servers_client.delete_server(server_id)
+ def _delete(self, server, volume):
+ if self.volume:
+ self.volumes_client.delete_volume(self.volume['id'])
+ self.volume = None
+ if self.server:
+ self.servers_client.delete_server(self.server['id'])
+ self.server = None
def _create_and_attach(self):
name = rand_name('server')
@@ -49,6 +59,7 @@
self.image_ref,
self.flavor_ref,
adminPass='password')
+ self.server = server
self.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
# Record addresses so that we can ssh later
@@ -58,6 +69,7 @@
# Create a volume and wait for it to become ready
resp, volume = self.volumes_client.create_volume(1,
display_name='test')
+ self.volume = volume
self.volumes_client.wait_for_volume_status(volume['id'], 'available')
# Attach the volume to the server
@@ -65,18 +77,18 @@
device='/dev/%s' % self.device)
self.volumes_client.wait_for_volume_status(volume['id'], 'in-use')
- return server, volume
+ self.attached = True
@attr(type='positive')
@testtools.skipIf(not run_ssh, 'SSH required for this test')
def test_attach_detach_volume(self):
# Stop and Start a server with an attached volume, ensuring that
# the volume remains attached.
- server, volume = self._create_and_attach()
-
- attached = True
-
try:
+ self._create_and_attach()
+ server = self.server
+ volume = self.volume
+
self.servers_client.stop(server['id'])
self.servers_client.wait_for_server_status(server['id'], 'SHUTOFF')
@@ -101,10 +113,12 @@
self.ssh_user, server['adminPass'])
partitions = linux_client.get_partitions()
self.assertFalse(self.device in partitions)
+ except Exception:
+ self.fail("The test_attach_detach_volume is faild!")
finally:
- if attached:
+ if self.attached:
self._detach(server['id'], volume['id'])
- self._delete(server['id'], volume['id'])
+ self._delete(self.server, self.volume)
class AttachVolumeTestXML(AttachVolumeTestJSON):