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