Rajat Dhasmana | 21d63a3 | 2020-01-14 17:41:22 +0000 | [diff] [blame^] | 1 | # Copyright 2020 Red Hat, Inc. |
| 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 | |
| 16 | from cinder_tempest_plugin.scenario import manager |
| 17 | from tempest.common import utils |
| 18 | from tempest.lib import decorators |
| 19 | |
| 20 | |
| 21 | class SnapshotDataIntegrityTests(manager.ScenarioTest): |
| 22 | |
| 23 | def setUp(self): |
| 24 | super(SnapshotDataIntegrityTests, self).setUp() |
| 25 | self.keypair = self.create_keypair() |
| 26 | self.security_group = self._create_security_group() |
| 27 | |
| 28 | def _get_file_md5(self, ip_address, filename, mount_path='/mnt', |
| 29 | private_key=None, server=None): |
| 30 | ssh_client = self.get_remote_client(ip_address, |
| 31 | private_key=private_key, |
| 32 | server=server) |
| 33 | |
| 34 | md5_sum = ssh_client.exec_command( |
| 35 | 'sudo md5sum %s/%s|cut -c 1-32' % (mount_path, filename)) |
| 36 | return md5_sum |
| 37 | |
| 38 | def _count_files(self, ip_address, mount_path='/mnt', private_key=None, |
| 39 | server=None): |
| 40 | ssh_client = self.get_remote_client(ip_address, |
| 41 | private_key=private_key, |
| 42 | server=server) |
| 43 | count = ssh_client.exec_command('sudo ls -l %s | wc -l' % mount_path) |
| 44 | return int(count) - 1 |
| 45 | |
| 46 | def _launch_instance_from_snapshot(self, snap): |
| 47 | volume_snap = self.create_volume(snapshot_id=snap['id'], |
| 48 | size=snap['size']) |
| 49 | |
| 50 | server_snap = self.boot_instance_from_resource( |
| 51 | source_id=volume_snap['id'], |
| 52 | source_type='volume', |
| 53 | keypair=self.keypair, |
| 54 | security_group=self.security_group) |
| 55 | |
| 56 | return server_snap |
| 57 | |
| 58 | def create_md5_new_file(self, ip_address, filename, mount_path='/mnt', |
| 59 | private_key=None, server=None): |
| 60 | ssh_client = self.get_remote_client(ip_address, |
| 61 | private_key=private_key, |
| 62 | server=server) |
| 63 | |
| 64 | ssh_client.exec_command( |
| 65 | 'sudo dd bs=1024 count=100 if=/dev/urandom of=/%s/%s' % |
| 66 | (mount_path, filename)) |
| 67 | md5 = ssh_client.exec_command( |
| 68 | 'sudo md5sum -b %s/%s|cut -c 1-32' % (mount_path, filename)) |
| 69 | ssh_client.exec_command('sudo sync') |
| 70 | return md5 |
| 71 | |
| 72 | def get_md5_from_file(self, instance, filename): |
| 73 | |
| 74 | instance_ip = self.get_server_ip(instance) |
| 75 | |
| 76 | md5_sum = self._get_file_md5(instance_ip, filename=filename, |
| 77 | private_key=self.keypair['private_key'], |
| 78 | server=instance) |
| 79 | count = self._count_files(instance_ip, |
| 80 | private_key=self.keypair['private_key'], |
| 81 | server=instance) |
| 82 | return count, md5_sum |
| 83 | |
| 84 | @decorators.idempotent_id('ff10644e-5a70-4a9f-9801-8204bb81fb61') |
| 85 | @utils.services('compute', 'volume', 'image', 'network') |
| 86 | def test_snapshot_data_integrity(self): |
| 87 | """This test checks the data integrity after creating and restoring |
| 88 | |
| 89 | snapshots. The procedure is as follows: |
| 90 | |
| 91 | 1) create a volume from image |
| 92 | 2) Boot an instance from the volume |
| 93 | 3) create file on vm and write data into it |
| 94 | 4) create snapshot |
| 95 | 5) repeat 3 and 4 two more times (simply creating 3 snapshots) |
| 96 | |
| 97 | Now restore the snapshots one by one into volume, create instances |
| 98 | from it and check the number of files and file content at each |
| 99 | point when snapshot was created. |
| 100 | """ |
| 101 | |
| 102 | # Create a volume from image |
| 103 | volume = self.create_volume_from_image() |
| 104 | |
| 105 | # create an instance from bootable volume |
| 106 | server = self.boot_instance_from_resource( |
| 107 | source_id=volume['id'], |
| 108 | source_type='volume', |
| 109 | keypair=self.keypair, |
| 110 | security_group=self.security_group) |
| 111 | |
| 112 | instance_ip = self.get_server_ip(server) |
| 113 | |
| 114 | # Write data to volume |
| 115 | file1_md5 = self.create_md5_new_file( |
| 116 | instance_ip, filename="file1", |
| 117 | private_key=self.keypair['private_key'], |
| 118 | server=instance_ip) |
| 119 | |
| 120 | # Create first snapshot |
| 121 | snapshot1 = self.create_volume_snapshot(volume['id'], force=True) |
| 122 | |
| 123 | # Write data to volume |
| 124 | file2_md5 = self.create_md5_new_file( |
| 125 | instance_ip, filename="file2", |
| 126 | private_key=self.keypair['private_key'], |
| 127 | server=instance_ip) |
| 128 | |
| 129 | # Create second snapshot |
| 130 | snapshot2 = self.create_volume_snapshot(volume['id'], force=True) |
| 131 | |
| 132 | # Write data to volume |
| 133 | file3_md5 = self.create_md5_new_file( |
| 134 | instance_ip, filename="file3", |
| 135 | private_key=self.keypair['private_key'], |
| 136 | server=instance_ip) |
| 137 | |
| 138 | # Create third snapshot |
| 139 | snapshot3 = self.create_volume_snapshot(volume['id'], force=True) |
| 140 | |
| 141 | # Create volume, instance and check file and contents for snap1 |
| 142 | instance_1 = self._launch_instance_from_snapshot(snapshot1) |
| 143 | count_snap_1, md5_file_1 = self.get_md5_from_file(instance_1, |
| 144 | 'file1') |
| 145 | |
| 146 | self.assertEqual(count_snap_1, 1) |
| 147 | self.assertEqual(file1_md5, md5_file_1) |
| 148 | |
| 149 | # Create volume, instance and check file and contents for snap2 |
| 150 | instance_2 = self._launch_instance_from_snapshot(snapshot2) |
| 151 | count_snap_2, md5_file_2 = self.get_md5_from_file(instance_2, |
| 152 | 'file2') |
| 153 | |
| 154 | self.assertEqual(count_snap_2, 2) |
| 155 | self.assertEqual(file2_md5, md5_file_2) |
| 156 | |
| 157 | # Create volume, instance and check file and contents for snap3 |
| 158 | instance_3 = self._launch_instance_from_snapshot(snapshot3) |
| 159 | count_snap_3, md5_file_3 = self.get_md5_from_file(instance_3, |
| 160 | 'file3') |
| 161 | |
| 162 | self.assertEqual(count_snap_3, 3) |
| 163 | self.assertEqual(file3_md5, md5_file_3) |