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 | |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 18 | from cinderclient import exceptions as cinder_exceptions |
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 |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 21 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 22 | from tempest.openstack.common import log as logging |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 23 | from tempest.scenario import manager |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 24 | import tempest.test |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 25 | |
| 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | |
| 29 | class TestStampPattern(manager.OfficialClientTest): |
| 30 | """ |
| 31 | This test is for snapshotting an instance/volume and attaching the volume |
| 32 | created from snapshot to the instance booted from snapshot. |
| 33 | The following is the scenario outline: |
| 34 | 1. Boot an instance "instance1" |
| 35 | 2. Create a volume "volume1" |
| 36 | 3. Attach volume1 to instance1 |
| 37 | 4. Create a filesystem on volume1 |
| 38 | 5. Mount volume1 |
| 39 | 6. Create a file which timestamp is written in volume1 |
| 40 | 7. Unmount volume1 |
| 41 | 8. Detach volume1 from instance1 |
| 42 | 9. Get a snapshot "snapshot_from_volume" of volume1 |
| 43 | 10. Get a snapshot "snapshot_from_instance" of instance1 |
| 44 | 11. Boot an instance "instance2" from snapshot_from_instance |
| 45 | 12. Create a volume "volume2" from snapshot_from_volume |
| 46 | 13. Attach volume2 to instance2 |
| 47 | 14. Check the existence of a file which created at 6. in volume2 |
| 48 | """ |
| 49 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 50 | def _wait_for_volume_snapshot_status(self, volume_snapshot, status): |
| 51 | self.status_timeout(self.volume_client.volume_snapshots, |
| 52 | volume_snapshot.id, status) |
| 53 | |
| 54 | def _boot_image(self, image_id): |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 55 | create_kwargs = { |
| 56 | 'key_name': self.keypair.name |
| 57 | } |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 58 | return self.create_server(image=image_id, create_kwargs=create_kwargs) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 59 | |
| 60 | def _add_keypair(self): |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 61 | self.keypair = self.create_keypair() |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 62 | |
| 63 | def _create_floating_ip(self): |
| 64 | floating_ip = self.compute_client.floating_ips.create() |
| 65 | self.addCleanup(floating_ip.delete) |
| 66 | return floating_ip |
| 67 | |
| 68 | def _add_floating_ip(self, server, floating_ip): |
| 69 | server.add_floating_ip(floating_ip) |
| 70 | |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 71 | def _ssh_to_server(self, server_or_ip): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 72 | linux_client = self.get_remote_client(server_or_ip) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 73 | return linux_client.ssh_client |
| 74 | |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 75 | def _create_volume_snapshot(self, volume): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 76 | snapshot_name = data_utils.rand_name('scenario-snapshot-') |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 77 | volume_snapshots = self.volume_client.volume_snapshots |
| 78 | snapshot = volume_snapshots.create( |
| 79 | volume.id, display_name=snapshot_name) |
| 80 | |
| 81 | def cleaner(): |
| 82 | volume_snapshots.delete(snapshot) |
| 83 | try: |
| 84 | while volume_snapshots.get(snapshot.id): |
| 85 | time.sleep(1) |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 86 | except cinder_exceptions.NotFound: |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 87 | pass |
| 88 | self.addCleanup(cleaner) |
| 89 | self._wait_for_volume_status(volume, 'available') |
| 90 | self._wait_for_volume_snapshot_status(snapshot, 'available') |
Chang Bo Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 91 | self.assertEqual(snapshot_name, snapshot.display_name) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 92 | return snapshot |
| 93 | |
| 94 | def _wait_for_volume_status(self, volume, status): |
| 95 | self.status_timeout( |
| 96 | self.volume_client.volumes, volume.id, status) |
| 97 | |
| 98 | def _create_volume(self, snapshot_id=None): |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 99 | return self.create_volume(snapshot_id=snapshot_id) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 100 | |
| 101 | def _attach_volume(self, server, volume): |
| 102 | attach_volume_client = self.compute_client.volumes.create_server_volume |
| 103 | attached_volume = attach_volume_client(server.id, |
| 104 | volume.id, |
| 105 | '/dev/vdb') |
| 106 | self.assertEqual(volume.id, attached_volume.id) |
| 107 | self._wait_for_volume_status(attached_volume, 'in-use') |
| 108 | |
| 109 | def _detach_volume(self, server, volume): |
| 110 | detach_volume_client = self.compute_client.volumes.delete_server_volume |
| 111 | detach_volume_client(server.id, volume.id) |
| 112 | self._wait_for_volume_status(volume, 'available') |
| 113 | |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 114 | def _wait_for_volume_availible_on_the_system(self, server_or_ip): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 115 | ssh = self.get_remote_client(server_or_ip) |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 116 | conf = self.config |
| 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, |
| 124 | conf.compute.build_timeout, |
| 125 | conf.compute.build_interval): |
| 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 | |
Giulio Fidente | 386ac8f | 2013-10-07 14:29:27 +0200 | [diff] [blame] | 142 | @tempest.test.skip_because(bug="1205344") |
Matthew Treinish | 2153ec0 | 2013-09-09 20:57:30 +0000 | [diff] [blame] | 143 | @tempest.test.services('compute', 'network', 'volume', 'image') |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 144 | def test_stamp_pattern(self): |
| 145 | # prepare for booting a instance |
| 146 | self._add_keypair() |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 147 | self._create_loginable_secgroup_rule_nova() |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 148 | |
| 149 | # boot an instance and create a timestamp file in it |
| 150 | volume = self._create_volume() |
| 151 | server = self._boot_image(self.config.compute.image_ref) |
| 152 | |
| 153 | # create and add floating IP to server1 |
| 154 | if self.config.compute.use_floatingip_for_ssh: |
| 155 | floating_ip_for_server = self._create_floating_ip() |
| 156 | self._add_floating_ip(server, floating_ip_for_server) |
| 157 | ip_for_server = floating_ip_for_server.ip |
| 158 | else: |
| 159 | ip_for_server = server |
| 160 | |
| 161 | self._attach_volume(server, volume) |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 162 | self._wait_for_volume_availible_on_the_system(ip_for_server) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 163 | self._create_timestamp(ip_for_server) |
| 164 | self._detach_volume(server, volume) |
| 165 | |
| 166 | # snapshot the volume |
| 167 | volume_snapshot = self._create_volume_snapshot(volume) |
| 168 | |
| 169 | # snapshot the instance |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 170 | snapshot_image = self.create_server_snapshot(server=server) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 171 | |
| 172 | # create second volume from the snapshot(volume2) |
| 173 | volume_from_snapshot = self._create_volume( |
| 174 | snapshot_id=volume_snapshot.id) |
| 175 | |
| 176 | # boot second instance from the snapshot(instance2) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 177 | server_from_snapshot = self._boot_image(snapshot_image.id) |
Yuiko Takada | ebcf6af | 2013-07-09 05:10:55 +0000 | [diff] [blame] | 178 | |
| 179 | # create and add floating IP to server_from_snapshot |
| 180 | if self.config.compute.use_floatingip_for_ssh: |
| 181 | floating_ip_for_snapshot = self._create_floating_ip() |
| 182 | self._add_floating_ip(server_from_snapshot, |
| 183 | floating_ip_for_snapshot) |
| 184 | ip_for_snapshot = floating_ip_for_snapshot.ip |
| 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) |
Attila Fazekas | 70431ba | 2013-07-26 18:47:37 +0200 | [diff] [blame] | 190 | self._wait_for_volume_availible_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) |