Refactor and update test_instances and test_volumes_api

Add method wait_for_volume_status to base.py to deduplicate code
in test_instances.py and test_volumes_api.py.
Removed volume_id_from_cli_create function as obsolete.

Change-Id: I44e35e181515923a677b336ed48bc705cf673134
This commit is contained in:
Kyrylo Romanenko 2015-07-30 12:13:54 +03:00
parent 9efeb79660
commit 089318d831
3 changed files with 29 additions and 58 deletions

@ -11,6 +11,7 @@
# under the License.
import os
import time
import fixtures
import os_client_config
@ -170,3 +171,22 @@ class ClientTestBase(testtools.TestCase):
def nova(self, *args, **kwargs):
return self.cli_clients.nova(*args,
**kwargs)
def wait_for_volume_status(self, volume, status, timeout=60,
poll_interval=1):
"""Wait until volume reaches given status.
:param volume_id: uuid4 id of given volume
:param status: expected status of volume
:param timeout: timeout in seconds
:param poll_interval: poll interval in seconds
"""
start_time = time.time()
while time.time() - start_time < timeout:
volume = self.client.volumes.get(volume.id)
if volume.status == status:
break
time.sleep(poll_interval)
else:
self.fail("Volume %s did not reach status %s after %d s"
% (volume.id, status, timeout))

@ -10,41 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import time
import uuid
from novaclient.tests.functional import base
def volume_id_from_cli_create(output):
"""Scrape the volume id out of the 'volume create' command
The cli for Nova automatically routes requests to the volumes
service end point. However the nova api low level commands don't
redirect to the correct service endpoint, so for volumes commands
(even setup ones) we use the cli for magic routing.
This function lets us get the id out of the prettytable that's
dumped on the cli during create.
"""
for line in output.split("\n"):
fields = line.split()
if len(fields) > 4:
if fields[1] == "id":
return fields[3]
def volume_at_status(output, volume_id, status):
for line in output.split("\n"):
fields = line.split()
if len(fields) > 4:
if fields[1] == volume_id:
return fields[3] == status
raise Exception("Volume %s did not reach status '%s' in output: %s"
% (volume_id, status, output))
class TestInstanceCLI(base.ClientTestBase):
def test_attach_volume(self):
@ -84,30 +54,19 @@ class TestInstanceCLI(base.ClientTestBase):
# create a volume for attachment. We use the CLI because it
# magic routes to cinder, however the low level API does not.
volume_id = volume_id_from_cli_create(
self.nova('volume-create', params="1"))
self.addCleanup(self.nova, 'volume-delete', params=volume_id)
volume = self.client.volumes.create(1)
self.addCleanup(self.nova, 'volume-delete', params=volume.id)
# allow volume to become available
for x in xrange(60):
volumes = self.nova('volume-list')
if volume_at_status(volumes, volume_id, 'available'):
break
time.sleep(1)
else:
self.fail("Volume %s not available after 60s" % volume_id)
self.wait_for_volume_status(volume, 'available')
# attach the volume
self.nova('volume-attach', params="%s %s" % (name, volume_id))
self.nova('volume-attach', params="%s %s" % (name, volume.id))
# volume needs to transition to 'in-use' to be attached
for x in xrange(60):
volumes = self.nova('volume-list')
if volume_at_status(volumes, volume_id, 'in-use'):
break
time.sleep(1)
else:
self.fail("Volume %s not attached after 60s" % volume_id)
self.wait_for_volume_status(volume, 'in-use')
# clean up on success
self.nova('volume-detach', params="%s %s" % (name, volume_id))
self.nova('volume-detach', params="%s %s" % (name, volume.id))
self.wait_for_volume_status(volume, 'available')
self.nova('volume-delete', params=volume.id)

@ -44,15 +44,7 @@ class TestVolumesAPI(base.ClientTestBase):
self.addCleanup(volume.delete)
# Wait for the volume to become available
for x in six.moves.range(60):
volume = self.client.volumes.get(volume.id)
if volume.status == 'available':
break
elif volume.status == 'error':
self.fail('Volume %s is in error state' % volume.id)
time.sleep(1)
else:
self.fail('Volume %s not available after 60s' % volume.id)
self.wait_for_volume_status(volume, 'available')
# List all volumes
self.client.volumes.list()