Improvements in snapshot data integrity tests
This patch addresses the comments in the snapshot data integrity tests[1].
[1] https://review.opendev.org/c/openstack/cinder-tempest-plugin/+/790899
Change-Id: Icef31c07cf2a4e8c822a288edfc4463d592668ff
whoami-rajat
4 years ago
| 12 | 12 | # License for the specific language governing permissions and limitations |
| 13 | 13 | # under the License. |
| 14 | 14 | |
| 15 | import contextlib | |
| 16 | ||
| 15 | 17 | from oslo_log import log |
| 16 | 18 | |
| 17 | 19 | from tempest.common import waiters |
| 54 | 56 | if item not in disks_list_before_attach][0] |
| 55 | 57 | return volume_name |
| 56 | 58 | |
| 57 | def _get_file_md5(self, ip_address, filename, dev_name=None, | |
| 58 | mount_path='/mnt', private_key=None, server=None): | |
| 59 | ||
| 60 | ssh_client = self.get_remote_client(ip_address, | |
| 61 | private_key=private_key, | |
| 62 | server=server) | |
| 59 | @contextlib.contextmanager | |
| 60 | def mount_dev_path(self, ssh_client, dev_name, mount_path): | |
| 63 | 61 | if dev_name is not None: |
| 64 | 62 | ssh_client.exec_command('sudo mount /dev/%s %s' % (dev_name, |
| 65 | 63 | mount_path)) |
| 66 | ||
| 67 | md5_sum = ssh_client.exec_command( | |
| 68 | 'sudo md5sum %s/%s|cut -c 1-32' % (mount_path, filename)) | |
| 69 | if dev_name is not None: | |
| 64 | yield | |
| 70 | 65 | ssh_client.exec_command('sudo umount %s' % mount_path) |
| 66 | else: | |
| 67 | yield | |
| 68 | ||
| 69 | def _get_file_md5(self, ip_address, filename, dev_name=None, | |
| 70 | mount_path='/mnt', private_key=None, server=None): | |
| 71 | ||
| 72 | ssh_client = self.get_remote_client(ip_address, | |
| 73 | private_key=private_key, | |
| 74 | server=server) | |
| 75 | with self.mount_dev_path(ssh_client, dev_name, mount_path): | |
| 76 | md5_sum = ssh_client.exec_command( | |
| 77 | 'sudo md5sum %s/%s|cut -c 1-32' % (mount_path, filename)) | |
| 71 | 78 | return md5_sum |
| 72 | 79 | |
| 73 | 80 | def _count_files(self, ip_address, dev_name=None, mount_path='/mnt', |
| 75 | 82 | ssh_client = self.get_remote_client(ip_address, |
| 76 | 83 | private_key=private_key, |
| 77 | 84 | server=server) |
| 78 | if dev_name is not None: | |
| 79 | ssh_client.exec_command('sudo mount /dev/%s %s' % (dev_name, | |
| 80 | mount_path)) | |
| 81 | count = ssh_client.exec_command('sudo ls -l %s | wc -l' % mount_path) | |
| 82 | if dev_name is not None: | |
| 83 | ssh_client.exec_command('sudo umount %s' % mount_path) | |
| 85 | with self.mount_dev_path(ssh_client, dev_name, mount_path): | |
| 86 | count = ssh_client.exec_command( | |
| 87 | 'sudo ls -l %s | wc -l' % mount_path) | |
| 84 | 88 | # We subtract 2 from the count since `wc -l` also includes the count |
| 85 | 89 | # of new line character and while creating the filesystem, a |
| 86 | 90 | # lost+found folder is also created |
| 99 | 103 | private_key=private_key, |
| 100 | 104 | server=server) |
| 101 | 105 | |
| 102 | if dev_name is not None: | |
| 103 | ssh_client.exec_command('sudo mount /dev/%s %s' % (dev_name, | |
| 104 | mount_path)) | |
| 105 | ssh_client.exec_command( | |
| 106 | 'sudo dd bs=1024 count=100 if=/dev/urandom of=/%s/%s' % | |
| 107 | (mount_path, filename)) | |
| 108 | md5 = ssh_client.exec_command( | |
| 109 | 'sudo md5sum -b %s/%s|cut -c 1-32' % (mount_path, filename)) | |
| 110 | ssh_client.exec_command('sudo sync') | |
| 111 | if dev_name is not None: | |
| 112 | ssh_client.exec_command('sudo umount %s' % mount_path) | |
| 106 | with self.mount_dev_path(ssh_client, dev_name, mount_path): | |
| 107 | ssh_client.exec_command( | |
| 108 | 'sudo dd bs=1024 count=100 if=/dev/urandom of=/%s/%s' % | |
| 109 | (mount_path, filename)) | |
| 110 | md5 = ssh_client.exec_command( | |
| 111 | 'sudo md5sum -b %s/%s|cut -c 1-32' % (mount_path, filename)) | |
| 112 | ssh_client.exec_command('sudo sync') | |
| 113 | 113 | return md5 |
| 114 | 114 | |
| 115 | 115 | def get_md5_from_file(self, instance, instance_ip, filename, |
| 35 | 35 | 1) Create an instance with ephemeral disk |
| 36 | 36 | 2) Create a volume, attach it to the instance and create a filesystem |
| 37 | 37 | on it and mount it |
| 38 | 3) Mount the volume, create a file and write data into it, Unmount it | |
| 38 | 3) Create a file and write data into it, Unmount it | |
| 39 | 39 | 4) create snapshot |
| 40 | 40 | 5) repeat 3 and 4 two more times (simply creating 3 snapshots) |
| 41 | 41 | |
| 92 | 92 | # Detach the volume |
| 93 | 93 | self.nova_volume_detach(server, volume) |
| 94 | 94 | |
| 95 | # Create volume from snapshot, attach it to instance and check file | |
| 96 | # and contents for snap1 | |
| 97 | volume_snap_1 = self.create_volume(snapshot_id=snapshot1['id']) | |
| 98 | volume_device_name, __ = self._attach_and_get_volume_device_name( | |
| 99 | server, volume_snap_1, instance_ip, self.keypair['private_key']) | |
| 100 | count_snap_1, md5_file_1 = self.get_md5_from_file( | |
| 101 | server, instance_ip, 'file1', dev_name=volume_device_name) | |
| 102 | # Detach the volume | |
| 103 | self.nova_volume_detach(server, volume_snap_1) | |
| 95 | snap_map = {1: snapshot1, 2: snapshot2, 3: snapshot3} | |
| 96 | file_map = {1: file1_md5, 2: file2_md5, 3: file3_md5} | |
| 104 | 97 | |
| 105 | self.assertEqual(count_snap_1, 1) | |
| 106 | self.assertEqual(file1_md5, md5_file_1) | |
| 98 | # Loop over 3 times to check the data integrity of all 3 snapshots | |
| 99 | for i in range(1, 4): | |
| 100 | # Create volume from snapshot, attach it to instance and check file | |
| 101 | # and contents for snap | |
| 102 | volume_snap = self.create_volume(snapshot_id=snap_map[i]['id']) | |
| 103 | volume_device_name, __ = self._attach_and_get_volume_device_name( | |
| 104 | server, volume_snap, instance_ip, self.keypair['private_key']) | |
| 105 | count_snap, md5_file = self.get_md5_from_file( | |
| 106 | server, instance_ip, 'file' + str(i), | |
| 107 | dev_name=volume_device_name) | |
| 108 | # Detach the volume | |
| 109 | self.nova_volume_detach(server, volume_snap) | |
| 107 | 110 | |
| 108 | # Create volume from snapshot, attach it to instance and check file | |
| 109 | # and contents for snap2 | |
| 110 | volume_snap_2 = self.create_volume(snapshot_id=snapshot2['id']) | |
| 111 | volume_device_name, __ = self._attach_and_get_volume_device_name( | |
| 112 | server, volume_snap_2, instance_ip, self.keypair['private_key']) | |
| 113 | count_snap_2, md5_file_2 = self.get_md5_from_file( | |
| 114 | server, instance_ip, 'file2', dev_name=volume_device_name) | |
| 115 | # Detach the volume | |
| 116 | self.nova_volume_detach(server, volume_snap_2) | |
| 117 | ||
| 118 | self.assertEqual(count_snap_2, 2) | |
| 119 | self.assertEqual(file2_md5, md5_file_2) | |
| 120 | ||
| 121 | # Create volume from snapshot, attach it to instance and check file | |
| 122 | # and contents for snap3 | |
| 123 | volume_snap_3 = self.create_volume(snapshot_id=snapshot3['id']) | |
| 124 | volume_device_name, __ = self._attach_and_get_volume_device_name( | |
| 125 | server, volume_snap_3, instance_ip, self.keypair['private_key']) | |
| 126 | count_snap_3, md5_file_3 = self.get_md5_from_file( | |
| 127 | server, instance_ip, 'file3', dev_name=volume_device_name) | |
| 128 | # Detach the volume | |
| 129 | self.nova_volume_detach(server, volume_snap_3) | |
| 130 | ||
| 131 | self.assertEqual(count_snap_3, 3) | |
| 132 | self.assertEqual(file3_md5, md5_file_3) | |
| 111 | self.assertEqual(count_snap, i) | |
| 112 | self.assertEqual(file_map[i], md5_file) | |