fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 13 | from oslo_log import log |
Matthew Treinish | c49fcbe | 2015-02-05 23:37:34 -0500 | [diff] [blame] | 14 | |
Fei Long Wang | d39431f | 2015-05-14 11:30:48 +1200 | [diff] [blame] | 15 | from tempest.common.utils import data_utils |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 16 | from tempest.common import waiters |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 17 | from tempest import config |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 18 | from tempest.scenario import manager |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 19 | from tempest import test |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 20 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 21 | CONF = config.CONF |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 22 | |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 23 | LOG = log.getLogger(__name__) |
| 24 | |
| 25 | |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 26 | class TestVolumeBootPattern(manager.ScenarioTest): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 27 | |
| 28 | """ |
| 29 | This test case attempts to reproduce the following steps: |
| 30 | |
| 31 | * Create in Cinder some bootable volume importing a Glance image |
| 32 | * Boot an instance from the bootable volume |
| 33 | * Write content to the volume |
| 34 | * Delete an instance and Boot a new instance from the volume |
| 35 | * Check written content in the instance |
| 36 | * Create a volume snapshot while the instance is running |
| 37 | * Boot an additional instance from the new snapshot based volume |
| 38 | * Check written content in the instance booted from snapshot |
| 39 | """ |
JordanP | bce5553 | 2014-03-19 12:10:32 +0100 | [diff] [blame] | 40 | @classmethod |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 41 | def skip_checks(cls): |
| 42 | super(TestVolumeBootPattern, cls).skip_checks() |
JordanP | bce5553 | 2014-03-19 12:10:32 +0100 | [diff] [blame] | 43 | if not CONF.volume_feature_enabled.snapshot: |
| 44 | raise cls.skipException("Cinder volume snapshots are disabled") |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 45 | |
| 46 | def _create_volume_from_image(self): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 47 | img_uuid = CONF.compute.image_ref |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 48 | vol_name = data_utils.rand_name('volume-origin') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 49 | return self.create_volume(name=vol_name, imageRef=img_uuid) |
| 50 | |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 51 | def _boot_instance_from_volume(self, vol_id, keypair, security_group): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 52 | # NOTE(gfidente): the syntax for block_device_mapping is |
| 53 | # dev_name=id:type:size:delete_on_terminate |
| 54 | # where type needs to be "snap" if the server is booted |
| 55 | # from a snapshot, size instead can be safely left empty |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 56 | bd_map = [{ |
| 57 | 'device_name': 'vda', |
| 58 | 'volume_id': vol_id, |
| 59 | 'delete_on_termination': '0'}] |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 60 | create_kwargs = { |
| 61 | 'block_device_mapping': bd_map, |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 62 | 'key_name': keypair['name'], |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 63 | 'security_groups': [{'name': security_group['name']}] |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 64 | } |
Xavier Queralt | 249cac3 | 2014-03-05 13:51:39 +0100 | [diff] [blame] | 65 | return self.create_server(image='', create_kwargs=create_kwargs) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 66 | |
| 67 | def _create_snapshot_from_volume(self, vol_id): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 68 | snap_name = data_utils.rand_name('snapshot') |
melanie witt | 8741222 | 2015-01-21 04:32:17 +0000 | [diff] [blame] | 69 | snap = self.snapshots_client.create_snapshot( |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 70 | volume_id=vol_id, |
| 71 | force=True, |
John Warren | ff7faf6 | 2015-08-17 16:59:06 +0000 | [diff] [blame] | 72 | display_name=snap_name)['snapshot'] |
Yaroslav Lobankov | 46a78c3 | 2015-04-08 13:45:27 +0300 | [diff] [blame] | 73 | self.addCleanup( |
| 74 | self.snapshots_client.wait_for_resource_deletion, snap['id']) |
| 75 | self.addCleanup(self.snapshots_client.delete_snapshot, snap['id']) |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 76 | self.snapshots_client.wait_for_snapshot_status(snap['id'], 'available') |
| 77 | self.assertEqual(snap_name, snap['display_name']) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 78 | return snap |
| 79 | |
| 80 | def _create_volume_from_snapshot(self, snap_id): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 81 | vol_name = data_utils.rand_name('volume') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 82 | return self.create_volume(name=vol_name, snapshot_id=snap_id) |
| 83 | |
| 84 | def _stop_instances(self, instances): |
| 85 | # NOTE(gfidente): two loops so we do not wait for the status twice |
| 86 | for i in instances: |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 87 | self.servers_client.stop(i['id']) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 88 | for i in instances: |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 89 | waiters.wait_for_server_status(self.servers_client, |
| 90 | i['id'], 'SHUTOFF') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 91 | |
| 92 | def _detach_volumes(self, volumes): |
| 93 | # NOTE(gfidente): two loops so we do not wait for the status twice |
| 94 | for v in volumes: |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 95 | self.volumes_client.detach_volume(v['id']) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 96 | for v in volumes: |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 97 | self.volumes_client.wait_for_volume_status(v['id'], 'available') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 98 | |
| 99 | def _ssh_to_server(self, server, keypair): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 100 | if CONF.compute.use_floatingip_for_ssh: |
ghanshyam | 9a3a9a2 | 2015-08-18 17:03:55 +0900 | [diff] [blame] | 101 | floating_ip = (self.floating_ips_client.create_floating_ip() |
| 102 | ['floating_ip']) |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 103 | self.addCleanup(self.delete_wrapper, |
| 104 | self.floating_ips_client.delete_floating_ip, |
| 105 | floating_ip['id']) |
| 106 | self.floating_ips_client.associate_floating_ip_to_server( |
| 107 | floating_ip['ip'], server['id']) |
| 108 | ip = floating_ip['ip'] |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 109 | else: |
Yaroslav Lobankov | fc99707 | 2015-07-27 11:57:49 +0300 | [diff] [blame] | 110 | ip = server |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 111 | |
JordanP | 3fe2dc3 | 2014-11-17 13:06:01 +0100 | [diff] [blame] | 112 | return self.get_remote_client(ip, private_key=keypair['private_key'], |
| 113 | log_console_of_servers=[server]) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 114 | |
| 115 | def _get_content(self, ssh_client): |
| 116 | return ssh_client.exec_command('cat /tmp/text') |
| 117 | |
| 118 | def _write_text(self, ssh_client): |
Ken'ichi Ohmichi | 07308f1 | 2015-03-23 00:24:28 +0000 | [diff] [blame] | 119 | text = data_utils.rand_name('text') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 120 | ssh_client.exec_command('echo "%s" > /tmp/text; sync' % (text)) |
| 121 | |
| 122 | return self._get_content(ssh_client) |
| 123 | |
| 124 | def _delete_server(self, server): |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 125 | self.servers_client.delete_server(server['id']) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 126 | waiters.wait_for_server_termination(self.servers_client, server['id']) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 127 | |
| 128 | def _check_content_of_written_file(self, ssh_client, expected): |
| 129 | actual = self._get_content(ssh_client) |
| 130 | self.assertEqual(expected, actual) |
| 131 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 132 | @test.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b') |
Sean Dague | 3c634d1 | 2015-04-27 12:09:19 -0400 | [diff] [blame] | 133 | @test.attr(type='smoke') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 134 | @test.services('compute', 'volume', 'image') |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 135 | def test_volume_boot_pattern(self): |
| 136 | keypair = self.create_keypair() |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 137 | security_group = self._create_security_group() |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 138 | |
| 139 | # create an instance from volume |
| 140 | volume_origin = self._create_volume_from_image() |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 141 | instance_1st = self._boot_instance_from_volume(volume_origin['id'], |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 142 | keypair, security_group) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 143 | |
| 144 | # write content to volume on instance |
| 145 | ssh_client_for_instance_1st = self._ssh_to_server(instance_1st, |
| 146 | keypair) |
| 147 | text = self._write_text(ssh_client_for_instance_1st) |
| 148 | |
| 149 | # delete instance |
| 150 | self._delete_server(instance_1st) |
| 151 | |
| 152 | # create a 2nd instance from volume |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 153 | instance_2nd = self._boot_instance_from_volume(volume_origin['id'], |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 154 | keypair, security_group) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 155 | |
| 156 | # check the content of written file |
| 157 | ssh_client_for_instance_2nd = self._ssh_to_server(instance_2nd, |
| 158 | keypair) |
| 159 | self._check_content_of_written_file(ssh_client_for_instance_2nd, text) |
| 160 | |
| 161 | # snapshot a volume |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 162 | snapshot = self._create_snapshot_from_volume(volume_origin['id']) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 163 | |
| 164 | # create a 3rd instance from snapshot |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 165 | volume = self._create_volume_from_snapshot(snapshot['id']) |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 166 | instance_from_snapshot = ( |
| 167 | self._boot_instance_from_volume(volume['id'], |
| 168 | keypair, security_group)) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 169 | |
| 170 | # check the content of written file |
| 171 | ssh_client = self._ssh_to_server(instance_from_snapshot, keypair) |
| 172 | self._check_content_of_written_file(ssh_client, text) |
| 173 | |
| 174 | # NOTE(gfidente): ensure resources are in clean state for |
| 175 | # deletion operations to succeed |
| 176 | self._stop_instances([instance_2nd, instance_from_snapshot]) |
Nikola Dipanov | 7cff03f | 2014-03-12 14:06:25 +0100 | [diff] [blame] | 177 | |
| 178 | |
| 179 | class TestVolumeBootPatternV2(TestVolumeBootPattern): |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 180 | def _boot_instance_from_volume(self, vol_id, keypair, security_group): |
| 181 | bd_map_v2 = [{ |
| 182 | 'uuid': vol_id, |
| 183 | 'source_type': 'volume', |
| 184 | 'destination_type': 'volume', |
| 185 | 'boot_index': 0, |
| 186 | 'delete_on_termination': False}] |
Nikola Dipanov | 7cff03f | 2014-03-12 14:06:25 +0100 | [diff] [blame] | 187 | create_kwargs = { |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 188 | 'block_device_mapping_v2': bd_map_v2, |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 189 | 'key_name': keypair['name'], |
Yaroslav Lobankov | 0089af5 | 2015-07-02 19:14:40 +0300 | [diff] [blame] | 190 | 'security_groups': [{'name': security_group['name']}] |
Nikola Dipanov | 7cff03f | 2014-03-12 14:06:25 +0100 | [diff] [blame] | 191 | } |
| 192 | return self.create_server(image='', create_kwargs=create_kwargs) |