Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 1 | # Copyright 2021 Red Hat, Inc. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
whoami-rajat | 027295a | 2021-12-12 12:31:59 -0500 | [diff] [blame] | 16 | import contextlib |
| 17 | |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 18 | from oslo_log import log |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 19 | |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 20 | from tempest.common import waiters |
| 21 | from tempest import config |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 22 | from tempest.lib.common.utils import data_utils |
| 23 | from tempest.lib.common.utils import test_utils |
| 24 | from tempest.lib import exceptions as lib_exc |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 25 | |
| 26 | from tempest.scenario import manager |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 27 | |
| 28 | CONF = config.CONF |
| 29 | |
| 30 | LOG = log.getLogger(__name__) |
| 31 | |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 32 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 33 | class ScenarioTest(manager.ScenarioTest): |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 34 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 35 | credentials = ['primary', 'admin'] |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 36 | |
| 37 | @classmethod |
| 38 | def setup_clients(cls): |
| 39 | super(ScenarioTest, cls).setup_clients() |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 40 | cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 41 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 42 | def _attached_volume_name( |
| 43 | self, disks_list_before_attach, ip_address, private_key): |
| 44 | ssh = self.get_remote_client(ip_address, private_key=private_key) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 45 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 46 | def _wait_for_volume_available_on_system(): |
| 47 | disks_list_after_attach = ssh.list_disks() |
| 48 | return len(disks_list_after_attach) > len(disks_list_before_attach) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 49 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 50 | if not test_utils.call_until_true(_wait_for_volume_available_on_system, |
| 51 | CONF.compute.build_timeout, |
| 52 | CONF.compute.build_interval): |
| 53 | raise lib_exc.TimeoutException |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 54 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 55 | disks_list_after_attach = ssh.list_disks() |
| 56 | volume_name = [item for item in disks_list_after_attach |
| 57 | if item not in disks_list_before_attach][0] |
| 58 | return volume_name |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 59 | |
whoami-rajat | 027295a | 2021-12-12 12:31:59 -0500 | [diff] [blame] | 60 | @contextlib.contextmanager |
| 61 | def mount_dev_path(self, ssh_client, dev_name, mount_path): |
| 62 | if dev_name is not None: |
| 63 | ssh_client.exec_command('sudo mount /dev/%s %s' % (dev_name, |
| 64 | mount_path)) |
| 65 | yield |
| 66 | ssh_client.exec_command('sudo umount %s' % mount_path) |
| 67 | else: |
| 68 | yield |
| 69 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 70 | def _get_file_md5(self, ip_address, filename, dev_name=None, |
| 71 | mount_path='/mnt', private_key=None, server=None): |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 72 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 73 | ssh_client = self.get_remote_client(ip_address, |
| 74 | private_key=private_key, |
| 75 | server=server) |
whoami-rajat | 027295a | 2021-12-12 12:31:59 -0500 | [diff] [blame] | 76 | with self.mount_dev_path(ssh_client, dev_name, mount_path): |
| 77 | md5_sum = ssh_client.exec_command( |
| 78 | 'sudo md5sum %s/%s|cut -c 1-32' % (mount_path, filename)) |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 79 | return md5_sum |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 80 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 81 | def _count_files(self, ip_address, dev_name=None, mount_path='/mnt', |
| 82 | private_key=None, server=None): |
| 83 | ssh_client = self.get_remote_client(ip_address, |
| 84 | private_key=private_key, |
| 85 | server=server) |
whoami-rajat | 027295a | 2021-12-12 12:31:59 -0500 | [diff] [blame] | 86 | with self.mount_dev_path(ssh_client, dev_name, mount_path): |
| 87 | count = ssh_client.exec_command( |
| 88 | 'sudo ls -l %s | wc -l' % mount_path) |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 89 | # We subtract 2 from the count since `wc -l` also includes the count |
| 90 | # of new line character and while creating the filesystem, a |
| 91 | # lost+found folder is also created |
| 92 | return int(count) - 2 |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 93 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 94 | def _make_fs(self, ip_address, private_key, server, dev_name, fs='ext4'): |
| 95 | ssh_client = self.get_remote_client(ip_address, |
| 96 | private_key=private_key, |
| 97 | server=server) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 98 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 99 | ssh_client.make_fs(dev_name, fs=fs) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 100 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 101 | def create_md5_new_file(self, ip_address, filename, dev_name=None, |
| 102 | mount_path='/mnt', private_key=None, server=None): |
| 103 | ssh_client = self.get_remote_client(ip_address, |
| 104 | private_key=private_key, |
| 105 | server=server) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 106 | |
whoami-rajat | 027295a | 2021-12-12 12:31:59 -0500 | [diff] [blame] | 107 | with self.mount_dev_path(ssh_client, dev_name, mount_path): |
| 108 | ssh_client.exec_command( |
| 109 | 'sudo dd bs=1024 count=100 if=/dev/urandom of=/%s/%s' % |
| 110 | (mount_path, filename)) |
| 111 | md5 = ssh_client.exec_command( |
| 112 | 'sudo md5sum -b %s/%s|cut -c 1-32' % (mount_path, filename)) |
| 113 | ssh_client.exec_command('sudo sync') |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 114 | return md5 |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 115 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 116 | def get_md5_from_file(self, instance, instance_ip, filename, |
| 117 | dev_name=None): |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 118 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 119 | md5_sum = self._get_file_md5(instance_ip, filename=filename, |
| 120 | dev_name=dev_name, |
| 121 | private_key=self.keypair['private_key'], |
| 122 | server=instance) |
| 123 | count = self._count_files(instance_ip, dev_name=dev_name, |
| 124 | private_key=self.keypair['private_key'], |
| 125 | server=instance) |
| 126 | return count, md5_sum |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 127 | |
whoami-rajat | 6dc9a2f | 2021-05-18 07:01:33 -0400 | [diff] [blame] | 128 | def write_data_to_device(self, ip_address, out_dev, in_dev='/dev/urandom', |
| 129 | bs=1024, count=100, private_key=None, |
| 130 | server=None, sha_sum=False): |
| 131 | ssh_client = self.get_remote_client( |
| 132 | ip_address, private_key=private_key, server=server) |
| 133 | |
| 134 | # Write data to device |
| 135 | write_command = ( |
| 136 | 'sudo dd bs=%(bs)s count=%(count)s if=%(in_dev)s of=%(out_dev)s ' |
| 137 | '&& sudo dd bs=%(bs)s count=%(count)s if=%(out_dev)s' % |
| 138 | {'bs': str(bs), 'count': str(count), 'in_dev': in_dev, |
| 139 | 'out_dev': out_dev}) |
| 140 | if sha_sum: |
| 141 | # If we want to read sha1sum instead of the device data |
| 142 | write_command += ' | sha1sum | head -c 40' |
| 143 | data = ssh_client.exec_command(write_command) |
| 144 | |
| 145 | return data |
| 146 | |
| 147 | def read_data_from_device(self, ip_address, in_dev, bs=1024, count=100, |
| 148 | private_key=None, server=None, sha_sum=False): |
| 149 | ssh_client = self.get_remote_client( |
| 150 | ip_address, private_key=private_key, server=server) |
| 151 | |
| 152 | # Read data from device |
| 153 | read_command = ('sudo dd bs=%(bs)s count=%(count)s if=%(in_dev)s' % |
| 154 | {'bs': bs, 'count': count, 'in_dev': in_dev}) |
| 155 | if sha_sum: |
| 156 | # If we want to read sha1sum instead of the device data |
| 157 | read_command += ' | sha1sum | head -c 40' |
| 158 | data = ssh_client.exec_command(read_command) |
| 159 | |
| 160 | return data |
| 161 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 162 | def _attach_and_get_volume_device_name(self, server, volume, instance_ip, |
| 163 | private_key): |
| 164 | ssh_client = self.get_remote_client( |
| 165 | instance_ip, private_key=private_key, |
| 166 | server=server) |
| 167 | # List disks before volume attachment |
| 168 | disks_list_before_attach = ssh_client.list_disks() |
| 169 | # Attach volume |
| 170 | attachment = self.attach_volume(server, volume) |
| 171 | # Find the difference between disks before and after attachment that |
| 172 | # gives us the volume device name |
| 173 | volume_device_name = self._attached_volume_name( |
| 174 | disks_list_before_attach, instance_ip, private_key) |
| 175 | return volume_device_name, attachment |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 176 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 177 | def create_volume_type(self, client=None, name=None, extra_specs=None): |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 178 | if not client: |
| 179 | client = self.os_admin.volume_types_client_latest |
| 180 | if not name: |
| 181 | class_name = self.__class__.__name__ |
| 182 | name = data_utils.rand_name(class_name + '-volume-type') |
| 183 | randomized_name = data_utils.rand_name('scenario-type-' + name) |
| 184 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 185 | LOG.debug("Creating a volume type: %s with extra_specs %s", |
| 186 | randomized_name, extra_specs) |
| 187 | if extra_specs is None: |
| 188 | extra_specs = {} |
| 189 | volume_type = self.admin_volume_types_client.create_volume_type( |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 190 | name=randomized_name, extra_specs=extra_specs)['volume_type'] |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 191 | self.addCleanup(self.cleanup_volume_type, volume_type) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 192 | return volume_type |
| 193 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 194 | def attach_volume(self, server, volume, device=None, tag=None): |
| 195 | """Attaches volume to server and waits for 'in-use' volume status. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 196 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 197 | The volume will be detached when the test tears down. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 198 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 199 | :param server: The server to which the volume will be attached. |
| 200 | :param volume: The volume to attach. |
| 201 | :param device: Optional mountpoint for the attached volume. Note that |
| 202 | this is not guaranteed for all hypervisors and is not recommended. |
| 203 | :param tag: Optional device role tag to apply to the volume. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 204 | """ |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 205 | attach_kwargs = dict(volumeId=volume['id']) |
| 206 | if device: |
| 207 | attach_kwargs['device'] = device |
| 208 | if tag: |
| 209 | attach_kwargs['tag'] = tag |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 210 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 211 | attachment = self.servers_client.attach_volume( |
| 212 | server['id'], **attach_kwargs)['volumeAttachment'] |
| 213 | # On teardown detach the volume and for multiattach volumes wait for |
| 214 | # the attachment to be removed. For non-multiattach volumes wait for |
| 215 | # the state of the volume to change to available. This is so we don't |
| 216 | # error out when trying to delete the volume during teardown. |
| 217 | if volume['multiattach']: |
| 218 | att = waiters.wait_for_volume_attachment_create( |
| 219 | self.volumes_client, volume['id'], server['id']) |
| 220 | self.addCleanup(waiters.wait_for_volume_attachment_remove, |
| 221 | self.volumes_client, volume['id'], |
| 222 | att['attachment_id']) |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 223 | else: |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 224 | self.addCleanup(waiters.wait_for_volume_resource_status, |
| 225 | self.volumes_client, volume['id'], 'available') |
| 226 | waiters.wait_for_volume_resource_status(self.volumes_client, |
| 227 | volume['id'], 'in-use') |
| 228 | # Ignore 404s on detach in case the server is deleted or the volume |
| 229 | # is already detached. |
| 230 | self.addCleanup(self._detach_volume, server, volume) |
| 231 | return attachment |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 232 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 233 | def _detach_volume(self, server, volume): |
| 234 | """Helper method to detach a volume. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 235 | |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 236 | Ignores 404 responses if the volume or server do not exist, or the |
| 237 | volume is already detached from the server. |
Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame] | 238 | """ |
Rajat Dhasmana | 638f230 | 2021-05-12 06:23:45 -0400 | [diff] [blame] | 239 | try: |
| 240 | volume = self.volumes_client.show_volume(volume['id'])['volume'] |
| 241 | # Check the status. You can only detach an in-use volume, otherwise |
| 242 | # the compute API will return a 400 response. |
| 243 | if volume['status'] == 'in-use': |
| 244 | self.servers_client.detach_volume(server['id'], volume['id']) |
| 245 | except lib_exc.NotFound: |
| 246 | # Ignore 404s on detach in case the server is deleted or the volume |
| 247 | # is already detached. |
| 248 | pass |