Count volumes vs partitions and remove hard-coded expected result
The initial patch fixes Mismatch error returned by attach/detach
shelved_or_offload_server Tempest tests because of counting disk
partitions instead of volumes.
Along with counting volumes instead of partitions, the current patch
proposes to remove hard-coded expected result from tests mentioned
above, since it may lead to another Mismatch errors. It may happen,
if an Openstack environment is configured with force_config_drive=True
(at least, this option is enabled in devstack at the moment), and if
it's configured to use block device for config drive instead of
loopback disk file, which is not expected by mentioned tests.
The proposed fix makes these tests more flexible and independent from
environments with specific configurations.
Also, the _create_and_attach() helper function has been splitted onto
smaller pieces to move some of them where they are really used and
to make the whole logic of the test suit cleaner.
Co-Authored-By: Anna Babich <ababich@mirantis.com>
Change-Id: I4736dc9292affa7374e72cba4e3309c1330ebabd
Closes-Bug: #1595503
diff --git a/tempest/api/compute/volumes/test_attach_volume.py b/tempest/api/compute/volumes/test_attach_volume.py
index a5c303c..80d2c78 100644
--- a/tempest/api/compute/volumes/test_attach_volume.py
+++ b/tempest/api/compute/volumes/test_attach_volume.py
@@ -64,18 +64,19 @@
self.volumes_client.wait_for_resource_deletion(self.volume['id'])
self.volume = None
- def _create_and_attach(self, shelve_server=False):
+ def _create_server(self):
# Start a server and wait for it to become ready
- self.admin_pass = self.image_ssh_password
- self.server = self.create_test_server(
+ server = self.create_test_server(
validatable=True,
wait_until='ACTIVE',
- adminPass=self.admin_pass)
+ adminPass=self.image_ssh_password)
# Record addresses so that we can ssh later
- self.server['addresses'] = self.servers_client.list_addresses(
- self.server['id'])['addresses']
+ server['addresses'] = self.servers_client.list_addresses(
+ server['id'])['addresses']
+ return server
+ def _create_and_attach_volume(self, server):
# Create a volume and wait for it to become ready
self.volume = self.volumes_client.create_volume(
size=CONF.volume.volume_size, display_name='test')['volume']
@@ -83,77 +84,60 @@
waiters.wait_for_volume_status(self.volumes_client,
self.volume['id'], 'available')
- if shelve_server:
- if CONF.validation.run_validation:
- # NOTE(andreaf) If we are going to shelve a server, we should
- # check first whether the server is ssh-able. Otherwise we
- # won't be able to distinguish failures introduced by shelve
- # from pre-existing ones. Also it's good to wait for cloud-init
- # to be done and sshd server to be running before shelving to
- # avoid breaking the VM
- linux_client = remote_client.RemoteClient(
- self.get_server_ip(self.server),
- self.image_ssh_user,
- self.admin_pass,
- self.validation_resources['keypair']['private_key'])
- linux_client.validate_authentication()
-
- # If validation went ok, or it was skipped, shelve the server
- compute.shelve_server(self.servers_client, self.server['id'])
-
# Attach the volume to the server
self.attachment = self.servers_client.attach_volume(
- self.server['id'],
+ server['id'],
volumeId=self.volume['id'],
device='/dev/%s' % self.device)['volumeAttachment']
waiters.wait_for_volume_status(self.volumes_client,
self.volume['id'], 'in-use')
- self.addCleanup(self._detach, self.server['id'], self.volume['id'])
+ self.addCleanup(self._detach, server['id'], self.volume['id'])
@test.idempotent_id('52e9045a-e90d-4c0d-9087-79d657faffff')
def test_attach_detach_volume(self):
# Stop and Start a server with an attached volume, ensuring that
# the volume remains attached.
- self._create_and_attach()
+ server = self._create_server()
+ self._create_and_attach_volume(server)
- self.servers_client.stop_server(self.server['id'])
- waiters.wait_for_server_status(self.servers_client, self.server['id'],
+ self.servers_client.stop_server(server['id'])
+ waiters.wait_for_server_status(self.servers_client, server['id'],
'SHUTOFF')
- self.servers_client.start_server(self.server['id'])
- waiters.wait_for_server_status(self.servers_client, self.server['id'],
+ self.servers_client.start_server(server['id'])
+ waiters.wait_for_server_status(self.servers_client, server['id'],
'ACTIVE')
if CONF.validation.run_validation:
linux_client = remote_client.RemoteClient(
- self.get_server_ip(self.server),
+ self.get_server_ip(server),
self.image_ssh_user,
- self.admin_pass,
+ self.image_ssh_password,
self.validation_resources['keypair']['private_key'],
- server=self.server,
+ server=server,
servers_client=self.servers_client)
partitions = linux_client.get_partitions()
self.assertIn(self.device, partitions)
- self._detach(self.server['id'], self.volume['id'])
+ self._detach(server['id'], self.volume['id'])
self.attachment = None
- self.servers_client.stop_server(self.server['id'])
- waiters.wait_for_server_status(self.servers_client, self.server['id'],
+ self.servers_client.stop_server(server['id'])
+ waiters.wait_for_server_status(self.servers_client, server['id'],
'SHUTOFF')
- self.servers_client.start_server(self.server['id'])
- waiters.wait_for_server_status(self.servers_client, self.server['id'],
+ self.servers_client.start_server(server['id'])
+ waiters.wait_for_server_status(self.servers_client, server['id'],
'ACTIVE')
if CONF.validation.run_validation:
linux_client = remote_client.RemoteClient(
- self.get_server_ip(self.server),
+ self.get_server_ip(server),
self.image_ssh_user,
- self.admin_pass,
+ self.image_ssh_password,
self.validation_resources['keypair']['private_key'],
- server=self.server,
+ server=server,
servers_client=self.servers_client)
partitions = linux_client.get_partitions()
@@ -162,18 +146,20 @@
@test.idempotent_id('7fa563fe-f0f7-43eb-9e22-a1ece036b513')
def test_list_get_volume_attachments(self):
# Create Server, Volume and attach that Volume to Server
- self._create_and_attach()
+ server = self._create_server()
+ self._create_and_attach_volume(server)
+
# List Volume attachment of the server
body = self.servers_client.list_volume_attachments(
- self.server['id'])['volumeAttachments']
+ server['id'])['volumeAttachments']
self.assertEqual(1, len(body))
self.assertIn(self.attachment, body)
# Get Volume attachment of the server
body = self.servers_client.show_volume_attachment(
- self.server['id'],
+ server['id'],
self.attachment['id'])['volumeAttachment']
- self.assertEqual(self.server['id'], body['serverId'])
+ self.assertEqual(server['id'], body['serverId'])
self.assertEqual(self.volume['id'], body['volumeId'])
self.assertEqual(self.attachment['id'], body['id'])
@@ -188,39 +174,71 @@
min_microversion = '2.20'
max_microversion = 'latest'
- def _unshelve_server_and_check_volumes(self, number_of_partition):
- # Unshelve the instance and check that there are expected volumes
- self.servers_client.unshelve_server(self.server['id'])
- waiters.wait_for_server_status(self.servers_client,
- self.server['id'],
- 'ACTIVE')
+ def _count_volumes(self, server):
+ # Count number of volumes on an instance
+ volumes = 0
if CONF.validation.run_validation:
linux_client = remote_client.RemoteClient(
- self.get_server_ip(self.server['id']),
+ self.get_server_ip(server),
self.image_ssh_user,
- self.admin_pass,
+ self.image_ssh_password,
self.validation_resources['keypair']['private_key'],
- server=self.server,
+ server=server,
servers_client=self.servers_client)
- command = 'grep [vs]d /proc/partitions | wc -l'
- nb_partitions = linux_client.exec_command(command).strip()
- self.assertEqual(number_of_partition, nb_partitions)
+ command = 'grep -c -E [vs]d.$ /proc/partitions'
+ volumes = int(linux_client.exec_command(command).strip())
+ return volumes
+
+ def _shelve_server(self, server):
+ # NOTE(andreaf) If we are going to shelve a server, we should
+ # check first whether the server is ssh-able. Otherwise we
+ # won't be able to distinguish failures introduced by shelve
+ # from pre-existing ones. Also it's good to wait for cloud-init
+ # to be done and sshd server to be running before shelving to
+ # avoid breaking the VM
+ if CONF.validation.run_validation:
+ linux_client = remote_client.RemoteClient(
+ self.get_server_ip(server),
+ self.image_ssh_user,
+ self.image_ssh_password,
+ self.validation_resources['keypair']['private_key'],
+ server=server,
+ servers_client=self.servers_client)
+ linux_client.validate_authentication()
+
+ # If validation went ok, or it was skipped, shelve the server
+ compute.shelve_server(self.servers_client, server['id'])
+
+ def _unshelve_server_and_check_volumes(self, server, number_of_volumes):
+ # Unshelve the instance and check that there are expected volumes
+ self.servers_client.unshelve_server(server['id'])
+ waiters.wait_for_server_status(self.servers_client,
+ server['id'],
+ 'ACTIVE')
+ if CONF.validation.run_validation:
+ counted_volumes = self._count_volumes(server)
+ self.assertEqual(number_of_volumes, counted_volumes)
@test.idempotent_id('13a940b6-3474-4c3c-b03f-29b89112bfee')
@testtools.skipUnless(CONF.compute_feature_enabled.shelve,
'Shelve is not available.')
def test_attach_volume_shelved_or_offload_server(self):
- self._create_and_attach(shelve_server=True)
+ # Create server, count number of volumes on it, shelve
+ # server and attach pre-created volume to shelved server
+ server = self._create_server()
+ num_vol = self._count_volumes(server)
+ self._shelve_server(server)
+ self._create_and_attach_volume(server)
- # Unshelve the instance and check that there are two volumes
- self._unshelve_server_and_check_volumes('2')
+ # Unshelve the instance and check that attached volume exists
+ self._unshelve_server_and_check_volumes(server, num_vol + 1)
# Get Volume attachment of the server
volume_attachment = self.servers_client.show_volume_attachment(
- self.server['id'],
+ server['id'],
self.attachment['id'])['volumeAttachment']
- self.assertEqual(self.server['id'], volume_attachment['serverId'])
+ self.assertEqual(server['id'], volume_attachment['serverId'])
self.assertEqual(self.attachment['id'], volume_attachment['id'])
# Check the mountpoint is not None after unshelve server even in
# case of shelved_offloaded.
@@ -230,11 +248,17 @@
@testtools.skipUnless(CONF.compute_feature_enabled.shelve,
'Shelve is not available.')
def test_detach_volume_shelved_or_offload_server(self):
- self._create_and_attach(shelve_server=True)
+ # Create server, count number of volumes on it, shelve
+ # server and attach pre-created volume to shelved server
+ server = self._create_server()
+ num_vol = self._count_volumes(server)
+ self._shelve_server(server)
+ self._create_and_attach_volume(server)
# Detach the volume
- self._detach(self.server['id'], self.volume['id'])
+ self._detach(server['id'], self.volume['id'])
self.attachment = None
- # Unshelve the instance and check that there is only one volume
- self._unshelve_server_and_check_volumes('1')
+ # Unshelve the instance and check that we have the expected number of
+ # volume(s)
+ self._unshelve_server_and_check_volumes(server, num_vol)