blob: 3bc5b0b95008d28761a9502abcda05c7ece6c1fb [file] [log] [blame]
fujioka yuuichi636f8db2013-08-09 12:05:24 +09001# 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
Sean Dague52abbd92016-03-01 09:38:09 -050013from oslo_log import log as logging
melanie wittca30e8c2018-08-07 21:39:52 +000014from oslo_serialization import jsonutils
zhufl6b7040a2017-01-18 16:38:34 +080015import testtools
Sean Dague52abbd92016-03-01 09:38:09 -050016
Andrea Frittolicd368412017-08-14 21:37:56 +010017from tempest.common import utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000018from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000019from tempest import config
Ken'ichi Ohmichibe4fb502017-03-10 10:04:48 -080020from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080021from tempest.lib import decorators
fujioka yuuichi636f8db2013-08-09 12:05:24 +090022from tempest.scenario import manager
23
Matthew Treinish6c072292014-01-29 19:15:52 +000024CONF = config.CONF
Sean Dague52abbd92016-03-01 09:38:09 -050025LOG = logging.getLogger(__name__)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090026
Nachi Ueno95b41282014-01-15 06:54:21 -080027
lkuchlan3023e752017-06-08 12:53:13 +030028class TestVolumeBootPattern(manager.EncryptionScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090029
Sean Dague02620fd2016-03-02 15:52:51 -050030 # Boot from volume scenario is quite slow, and needs extra
31 # breathing room to get through deletes in the time allotted.
32 TIMEOUT_SCALING_FACTOR = 2
33
fujioka yuuichi636f8db2013-08-09 12:05:24 +090034 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000035 img_uuid = CONF.compute.image_ref
zhuflc6ce5392016-08-17 14:34:37 +080036 vol_name = data_utils.rand_name(
37 self.__class__.__name__ + '-volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090038 return self.create_volume(name=vol_name, imageRef=img_uuid)
39
lkuchlan8789c552017-01-08 11:40:27 +020040 def _get_bdm(self, source_id, source_type, delete_on_termination=False):
Sean Dagued571e032017-02-27 12:27:10 -050041 bd_map_v2 = [{
42 'uuid': source_id,
43 'source_type': source_type,
44 'destination_type': 'volume',
45 'boot_index': 0,
46 'delete_on_termination': delete_on_termination}]
47 return {'block_device_mapping_v2': bd_map_v2}
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030048
lkuchlan8789c552017-01-08 11:40:27 +020049 def _boot_instance_from_resource(self, source_id,
50 source_type,
51 keypair=None,
52 security_group=None,
melanie wittca30e8c2018-08-07 21:39:52 +000053 delete_on_termination=False,
54 name=None):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030055 create_kwargs = dict()
56 if keypair:
57 create_kwargs['key_name'] = keypair['name']
58 if security_group:
59 create_kwargs['security_groups'] = [
60 {'name': security_group['name']}]
61 create_kwargs.update(self._get_bdm(
lkuchlan8789c552017-01-08 11:40:27 +020062 source_id,
63 source_type,
64 delete_on_termination=delete_on_termination))
melanie wittca30e8c2018-08-07 21:39:52 +000065 if name:
66 create_kwargs['name'] = name
lkuchlan8789c552017-01-08 11:40:27 +020067
zhufl13c9c892017-02-10 12:04:07 +080068 return self.create_server(image_id='', **create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090069
fujioka yuuichi636f8db2013-08-09 12:05:24 +090070 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000071 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000072 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090073
melanie wittca30e8c2018-08-07 21:39:52 +000074 def _delete_snapshot(self, snapshot_id):
75 self.snapshots_client.delete_snapshot(snapshot_id)
76 self.snapshots_client.wait_for_resource_deletion(snapshot_id)
77
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080078 @decorators.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Matt Riedemann4b8a7b82019-02-14 14:07:11 -050079 @decorators.attr(type='slow')
msidanae1f990b2018-06-05 12:10:24 +053080 # Note: This test is being skipped based on 'public_network_id'.
81 # It is being used in create_floating_ip() method which gets called
82 # from get_server_ip() method
zhufl6b7040a2017-01-18 16:38:34 +080083 @testtools.skipUnless(CONF.network.public_network_id,
84 'The public_network_id option must be specified.')
zhufl36214c52018-03-02 15:49:41 +080085 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
86 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +010087 @utils.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090088 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030089
90 """This test case attempts to reproduce the following steps:
91
92 * Create in Cinder some bootable volume importing a Glance image
93 * Boot an instance from the bootable volume
94 * Write content to the volume
95 * Delete an instance and Boot a new instance from the volume
96 * Check written content in the instance
97 * Create a volume snapshot while the instance is running
98 * Boot an additional instance from the new snapshot based volume
99 * Check written content in the instance booted from snapshot
100 """
101
Sean Dague52abbd92016-03-01 09:38:09 -0500102 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900103 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300104 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900105
106 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -0500107 LOG.info("Booting instance 1 from volume")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900108 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200109 instance_1st = self._boot_instance_from_resource(
110 source_id=volume_origin['id'],
111 source_type='volume',
112 keypair=keypair,
113 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100114 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900115
116 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100117 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -0500118 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200119 timestamp = self.create_timestamp(ip_instance_1st,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200120 private_key=keypair['private_key'],
121 server=instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900122
123 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100124 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900125 self._delete_server(instance_1st)
126
127 # create a 2nd instance from volume
lkuchlan8789c552017-01-08 11:40:27 +0200128 instance_2nd = self._boot_instance_from_resource(
129 source_id=volume_origin['id'],
130 source_type='volume',
131 keypair=keypair,
132 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100133 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900134
135 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100136 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500137 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200138 timestamp2 = self.get_timestamp(ip_instance_2nd,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200139 private_key=keypair['private_key'],
140 server=instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200141 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900142
143 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100144 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
lkuchlan73ed1f32017-07-06 16:22:12 +0300145 snapshot = self.create_volume_snapshot(volume_origin['id'], force=True)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900146
147 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100148 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500149 volume = self.create_volume(snapshot_id=snapshot['id'],
150 size=snapshot['size'])
151 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200152 server_from_snapshot = (
lkuchlan8789c552017-01-08 11:40:27 +0200153 self._boot_instance_from_resource(source_id=volume['id'],
154 source_type='volume',
155 keypair=keypair,
156 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500157 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900158
159 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100160 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500161 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500162 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200163 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200164 private_key=keypair['private_key'],
165 server=server_from_snapshot)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200166 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900167
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800168 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200169 @decorators.attr(type='slow')
zhufl36214c52018-03-02 15:49:41 +0800170 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
171 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100172 @utils.services('compute', 'image', 'volume')
lkuchlan8789c552017-01-08 11:40:27 +0200173 def test_create_server_from_volume_snapshot(self):
174 # Create a volume from an image
175 boot_volume = self._create_volume_from_image()
176
177 # Create a snapshot
lkuchlan73ed1f32017-07-06 16:22:12 +0300178 boot_snapshot = self.create_volume_snapshot(boot_volume['id'])
lkuchlan8789c552017-01-08 11:40:27 +0200179
180 # Create a server from a volume snapshot
181 server = self._boot_instance_from_resource(
182 source_id=boot_snapshot['id'],
183 source_type='snapshot',
184 delete_on_termination=True)
185
186 server_info = self.servers_client.show_server(server['id'])['server']
187
188 # The created volume when creating a server from a snapshot
189 created_volume = server_info['os-extended-volumes:volumes_attached']
190
lkuchlan9e22b852017-02-05 15:38:29 +0200191 self.assertNotEmpty(created_volume, "No volume attachment found.")
192
lkuchlan8789c552017-01-08 11:40:27 +0200193 created_volume_info = self.volumes_client.show_volume(
194 created_volume[0]['id'])['volume']
195
196 # Verify the server was created from the snapshot
197 self.assertEqual(
198 boot_volume['volume_image_metadata']['image_id'],
199 created_volume_info['volume_image_metadata']['image_id'])
200 self.assertEqual(boot_snapshot['id'],
201 created_volume_info['snapshot_id'])
202 self.assertEqual(server['id'],
203 created_volume_info['attachments'][0]['server_id'])
204 self.assertEqual(created_volume[0]['id'],
205 created_volume_info['attachments'][0]['volume_id'])
206
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800207 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
zhufl36214c52018-03-02 15:49:41 +0800208 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
209 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100210 @utils.services('compute', 'volume', 'image')
Matt Riedemann2db6c272018-03-22 18:57:19 -0400211 def test_image_defined_boot_from_volume(self):
212 # create an instance from image-backed volume
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300213 volume_origin = self._create_volume_from_image()
melanie wittca30e8c2018-08-07 21:39:52 +0000214 name = data_utils.rand_name(self.__class__.__name__ +
215 '-volume-backed-server')
216 instance1 = self._boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200217 source_id=volume_origin['id'],
218 source_type='volume',
melanie wittca30e8c2018-08-07 21:39:52 +0000219 delete_on_termination=True,
220 name=name)
Matt Riedemann2db6c272018-03-22 18:57:19 -0400221 # Create a snapshot image from the volume-backed server.
222 # The compute service will have the block service create a snapshot of
223 # the root volume and store its metadata in the image.
melanie wittca30e8c2018-08-07 21:39:52 +0000224 image = self.create_server_snapshot(instance1)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300225
Matt Riedemann2db6c272018-03-22 18:57:19 -0400226 # Create a server from the image snapshot which has an
227 # "image-defined block device mapping (BDM)" in it, i.e. the metadata
228 # about the volume snapshot. The compute service will use this to
229 # create a volume from the volume snapshot and use that as the root
230 # disk for the server.
melanie wittca30e8c2018-08-07 21:39:52 +0000231 name = data_utils.rand_name(self.__class__.__name__ +
232 '-image-snapshot-server')
233 instance2 = self.create_server(image_id=image['id'], name=name)
lianghao2e0ee042017-10-26 19:38:28 +0800234
Matt Riedemann2db6c272018-03-22 18:57:19 -0400235 # Verify the server was created from the image-defined BDM.
melanie wittca30e8c2018-08-07 21:39:52 +0000236 volume_attachments = instance2['os-extended-volumes:volumes_attached']
Matt Riedemann2db6c272018-03-22 18:57:19 -0400237 self.assertEqual(1, len(volume_attachments),
238 "No volume attachment found.")
239 created_volume = self.volumes_client.show_volume(
240 volume_attachments[0]['id'])['volume']
241 # Assert that the volume service also shows the server attachment.
242 self.assertEqual(1, len(created_volume['attachments']),
243 "No server attachment found for volume: %s" %
244 created_volume)
melanie wittca30e8c2018-08-07 21:39:52 +0000245 self.assertEqual(instance2['id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400246 created_volume['attachments'][0]['server_id'])
247 self.assertEqual(volume_attachments[0]['id'],
248 created_volume['attachments'][0]['volume_id'])
lianghao2e0ee042017-10-26 19:38:28 +0800249 self.assertEqual(
250 volume_origin['volume_image_metadata']['image_id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400251 created_volume['volume_image_metadata']['image_id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300252
Matt Riedemann2db6c272018-03-22 18:57:19 -0400253 # Delete the second server which should also delete the second volume
254 # created from the volume snapshot.
melanie wittca30e8c2018-08-07 21:39:52 +0000255 self._delete_server(instance2)
lkuchlan3023e752017-06-08 12:53:13 +0300256
melanie wittc50cc242018-05-01 22:00:39 +0000257 # Assert that the underlying volume is gone.
258 self.volumes_client.wait_for_resource_deletion(created_volume['id'])
259
melanie wittca30e8c2018-08-07 21:39:52 +0000260 # Delete the volume snapshot. We must do this before deleting the first
261 # server created in this test because the snapshot depends on the first
262 # instance's underlying volume (volume_origin).
263 # In glance v2, the image properties are flattened and in glance v1,
264 # the image properties are under the 'properties' key.
265 bdms = image.get('block_device_mapping')
266 if not bdms:
267 bdms = image['properties']['block_device_mapping']
268 bdms = jsonutils.loads(bdms)
269 snapshot_id = bdms[0]['snapshot_id']
270 self._delete_snapshot(snapshot_id)
271
272 # Now, delete the first server which will also delete the first
273 # image-backed volume.
274 self._delete_server(instance1)
275
276 # Assert that the underlying volume is gone.
277 self.volumes_client.wait_for_resource_deletion(volume_origin['id'])
278
lkuchlan3023e752017-06-08 12:53:13 +0300279 @decorators.idempotent_id('cb78919a-e553-4bab-b73b-10cf4d2eb125')
Attila Fazekas1c39a2f2017-07-18 19:39:06 +0200280 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
281 'Encrypted volume attach is not supported')
Andrea Frittolicd368412017-08-14 21:37:56 +0100282 @utils.services('compute', 'volume')
lkuchlan3023e752017-06-08 12:53:13 +0300283 def test_boot_server_from_encrypted_volume_luks(self):
284 # Create an encrypted volume
285 volume = self.create_encrypted_volume('nova.volume.encryptors.'
286 'luks.LuksEncryptor',
287 volume_type='luks')
288
289 self.volumes_client.set_bootable_volume(volume['id'], bootable=True)
290
291 # Boot a server from the encrypted volume
292 server = self._boot_instance_from_resource(
293 source_id=volume['id'],
294 source_type='volume',
295 delete_on_termination=False)
296
297 server_info = self.servers_client.show_server(server['id'])['server']
298 created_volume = server_info['os-extended-volumes:volumes_attached']
299 self.assertEqual(volume['id'], created_volume[0]['id'])