Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 1 | # Copyright 2013 NEC Corporation |
| 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 | |
Sean Dague | fe8a609 | 2013-07-27 08:15:55 -0400 | [diff] [blame] | 16 | import time |
| 17 | |
Adam Gandelman | fbc95ac | 2014-06-19 17:33:43 -0700 | [diff] [blame] | 18 | import testtools |
Sean Dague | fe8a609 | 2013-07-27 08:15:55 -0400 | [diff] [blame] | 19 | |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 20 | from tempest.common.utils import data_utils |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 21 | from tempest import config |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 22 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 23 | from tempest.openstack.common import log as logging |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 24 | from tempest.scenario import manager |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 25 | import tempest.test |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 26 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 27 | CONF = config.CONF |
| 28 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 29 | LOG = logging.getLogger(__name__) |
| 30 | |
| 31 | |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 32 | class TestStampPattern(manager.ScenarioTest): |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 33 | """ |
| 34 | This test is for snapshotting an instance/volume and attaching the volume |
| 35 | created from snapshot to the instance booted from snapshot. |
| 36 | The following is the scenario outline: |
| 37 | 1. Boot an instance "instance1" |
| 38 | 2. Create a volume "volume1" |
| 39 | 3. Attach volume1 to instance1 |
| 40 | 4. Create a filesystem on volume1 |
| 41 | 5. Mount volume1 |
| 42 | 6. Create a file which timestamp is written in volume1 |
| 43 | 7. Unmount volume1 |
| 44 | 8. Detach volume1 from instance1 |
| 45 | 9. Get a snapshot "snapshot_from_volume" of volume1 |
| 46 | 10. Get a snapshot "snapshot_from_instance" of instance1 |
| 47 | 11. Boot an instance "instance2" from snapshot_from_instance |
| 48 | 12. Create a volume "volume2" from snapshot_from_volume |
| 49 | 13. Attach volume2 to instance2 |
| 50 | 14. Check the existence of a file which created at 6. in volume2 |
| 51 | """ |
| 52 | |
JordanP | bce5553 | 2014-03-19 12:10:32 +0100 | [diff] [blame] | 53 | @classmethod |
| 54 | def setUpClass(cls): |
| 55 | super(TestStampPattern, cls).setUpClass() |
| 56 | |
| 57 | if not CONF.volume_feature_enabled.snapshot: |
| 58 | raise cls.skipException("Cinder volume snapshots are disabled") |
| 59 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 60 | def _wait_for_volume_snapshot_status(self, volume_snapshot, status): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 61 | self.snapshots_client.wait_for_snapshot_status(volume_snapshot['id'], |
| 62 | status) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 63 | |
| 64 | def _boot_image(self, image_id): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 65 | security_groups = [self.security_group] |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 66 | create_kwargs = { |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 67 | 'key_name': self.keypair['name'], |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 68 | 'security_groups': security_groups |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 69 | } |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 70 | return self.create_server(image=image_id, create_kwargs=create_kwargs) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 71 | |
| 72 | def _add_keypair(self): |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 73 | self.keypair = self.create_keypair() |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 74 | |
| 75 | def _create_floating_ip(self): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 76 | _, floating_ip = self.floating_ips_client.create_floating_ip() |
| 77 | self.addCleanup(self.delete_wrapper, |
| 78 | self.floating_ips_client.delete_floating_ip, |
| 79 | floating_ip['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 80 | return floating_ip |
| 81 | |
| 82 | def _add_floating_ip(self, server, floating_ip): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 83 | self.floating_ips_client.associate_floating_ip_to_server( |
| 84 | floating_ip['ip'], server['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 85 | |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 86 | def _ssh_to_server(self, server_or_ip): |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 87 | return self.get_remote_client(server_or_ip) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 88 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 89 | def _create_volume_snapshot(self, volume): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 90 | snapshot_name = data_utils.rand_name('scenario-snapshot-') |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 91 | _, snapshot = self.snapshots_client.create_snapshot( |
| 92 | volume['id'], display_name=snapshot_name) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 93 | |
| 94 | def cleaner(): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 95 | self.snapshots_client.delete_snapshot(snapshot['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 96 | try: |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 97 | while self.snapshots_client.get_snapshot(snapshot['id']): |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 98 | time.sleep(1) |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 99 | except exceptions.NotFound: |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 100 | pass |
| 101 | self.addCleanup(cleaner) |
| 102 | self._wait_for_volume_status(volume, 'available') |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 103 | self.snapshots_client.wait_for_snapshot_status(snapshot['id'], |
| 104 | 'available') |
| 105 | self.assertEqual(snapshot_name, snapshot['display_name']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 106 | return snapshot |
| 107 | |
| 108 | def _wait_for_volume_status(self, volume, status): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 109 | self.volumes_client.wait_for_volume_status(volume['id'], status) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 110 | |
| 111 | def _create_volume(self, snapshot_id=None): |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 112 | return self.create_volume(snapshot_id=snapshot_id) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 113 | |
| 114 | def _attach_volume(self, server, volume): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 115 | # TODO(andreaf) we should use device from config instead if vdb |
| 116 | _, attached_volume = self.servers_client.attach_volume( |
| 117 | server['id'], volume['id'], device='/dev/vdb') |
| 118 | attached_volume = attached_volume['volumeAttachment'] |
| 119 | self.assertEqual(volume['id'], attached_volume['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 120 | self._wait_for_volume_status(attached_volume, 'in-use') |
| 121 | |
| 122 | def _detach_volume(self, server, volume): |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 123 | self.servers_client.detach_volume(server['id'], volume['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 124 | self._wait_for_volume_status(volume, 'available') |
| 125 | |
Marc Solanas | b15d8b6 | 2014-02-07 00:04:15 -0800 | [diff] [blame] | 126 | def _wait_for_volume_available_on_the_system(self, server_or_ip): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 127 | ssh = self.get_remote_client(server_or_ip) |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 128 | |
| 129 | def _func(): |
| 130 | part = ssh.get_partitions() |
| 131 | LOG.debug("Partitions:%s" % part) |
| 132 | return 'vdb' in part |
| 133 | |
| 134 | if not tempest.test.call_until_true(_func, |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 135 | CONF.compute.build_timeout, |
| 136 | CONF.compute.build_interval): |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 137 | raise exceptions.TimeoutException |
| 138 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 139 | def _create_timestamp(self, server_or_ip): |
| 140 | ssh_client = self._ssh_to_server(server_or_ip) |
| 141 | ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb') |
| 142 | ssh_client.exec_command('sudo mount /dev/vdb /mnt') |
| 143 | ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"') |
| 144 | self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp') |
| 145 | ssh_client.exec_command('sudo umount /mnt') |
| 146 | |
| 147 | def _check_timestamp(self, server_or_ip): |
| 148 | ssh_client = self._ssh_to_server(server_or_ip) |
| 149 | ssh_client.exec_command('sudo mount /dev/vdb /mnt') |
| 150 | got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp') |
| 151 | self.assertEqual(self.timestamp, got_timestamp) |
| 152 | |
Giulio Fidente | 386ac8f | 2013-10-07 14:29:27 +0200 | [diff] [blame] | 153 | @tempest.test.skip_because(bug="1205344") |
Adam Gandelman | fbc95ac | 2014-06-19 17:33:43 -0700 | [diff] [blame] | 154 | @testtools.skipUnless(CONF.compute_feature_enabled.snapshot, |
| 155 | 'Snapshotting is not available.') |
Matthew Treinish | 2153ec0 | 2013-09-09 20:57:30 +0000 | [diff] [blame] | 156 | @tempest.test.services('compute', 'network', 'volume', 'image') |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 157 | def test_stamp_pattern(self): |
| 158 | # prepare for booting a instance |
| 159 | self._add_keypair() |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 160 | self.security_group = self._create_security_group() |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 161 | |
| 162 | # boot an instance and create a timestamp file in it |
| 163 | volume = self._create_volume() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 164 | server = self._boot_image(CONF.compute.image_ref) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 165 | |
| 166 | # create and add floating IP to server1 |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 167 | if CONF.compute.use_floatingip_for_ssh: |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 168 | floating_ip_for_server = self._create_floating_ip() |
| 169 | self._add_floating_ip(server, floating_ip_for_server) |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 170 | ip_for_server = floating_ip_for_server['ip'] |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 171 | else: |
| 172 | ip_for_server = server |
| 173 | |
| 174 | self._attach_volume(server, volume) |
Marc Solanas | b15d8b6 | 2014-02-07 00:04:15 -0800 | [diff] [blame] | 175 | self._wait_for_volume_available_on_the_system(ip_for_server) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 176 | self._create_timestamp(ip_for_server) |
| 177 | self._detach_volume(server, volume) |
| 178 | |
| 179 | # snapshot the volume |
| 180 | volume_snapshot = self._create_volume_snapshot(volume) |
| 181 | |
| 182 | # snapshot the instance |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 183 | snapshot_image = self.create_server_snapshot(server=server) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 184 | |
| 185 | # create second volume from the snapshot(volume2) |
| 186 | volume_from_snapshot = self._create_volume( |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 187 | snapshot_id=volume_snapshot['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 188 | |
| 189 | # boot second instance from the snapshot(instance2) |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 190 | server_from_snapshot = self._boot_image(snapshot_image['id']) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 191 | |
| 192 | # create and add floating IP to server_from_snapshot |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 193 | if CONF.compute.use_floatingip_for_ssh: |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 194 | floating_ip_for_snapshot = self._create_floating_ip() |
| 195 | self._add_floating_ip(server_from_snapshot, |
| 196 | floating_ip_for_snapshot) |
Andrea Frittoli | c0651b2 | 2014-09-17 16:34:01 +0100 | [diff] [blame] | 197 | ip_for_snapshot = floating_ip_for_snapshot['ip'] |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 198 | else: |
| 199 | ip_for_snapshot = server_from_snapshot |
| 200 | |
| 201 | # attach volume2 to instance2 |
| 202 | self._attach_volume(server_from_snapshot, volume_from_snapshot) |
Marc Solanas | b15d8b6 | 2014-02-07 00:04:15 -0800 | [diff] [blame] | 203 | self._wait_for_volume_available_on_the_system(ip_for_snapshot) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 204 | |
| 205 | # check the existence of the timestamp file in the volume2 |
| 206 | self._check_timestamp(ip_for_snapshot) |