blob: 810480b3c28366143ff9b838f527fc31381faf57 [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')
msidanae1f990b2018-06-05 12:10:24 +053079 # Note: This test is being skipped based on 'public_network_id'.
80 # It is being used in create_floating_ip() method which gets called
81 # from get_server_ip() method
zhufl6b7040a2017-01-18 16:38:34 +080082 @testtools.skipUnless(CONF.network.public_network_id,
83 'The public_network_id option must be specified.')
zhufl36214c52018-03-02 15:49:41 +080084 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
85 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +010086 @utils.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090087 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030088
89 """This test case attempts to reproduce the following steps:
90
91 * Create in Cinder some bootable volume importing a Glance image
92 * Boot an instance from the bootable volume
93 * Write content to the volume
94 * Delete an instance and Boot a new instance from the volume
95 * Check written content in the instance
96 * Create a volume snapshot while the instance is running
97 * Boot an additional instance from the new snapshot based volume
98 * Check written content in the instance booted from snapshot
99 """
100
Sean Dague52abbd92016-03-01 09:38:09 -0500101 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900102 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300103 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900104
105 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -0500106 LOG.info("Booting instance 1 from volume")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900107 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200108 instance_1st = self._boot_instance_from_resource(
109 source_id=volume_origin['id'],
110 source_type='volume',
111 keypair=keypair,
112 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100113 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900114
115 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100116 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -0500117 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200118 timestamp = self.create_timestamp(ip_instance_1st,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200119 private_key=keypair['private_key'],
120 server=instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900121
122 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100123 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900124 self._delete_server(instance_1st)
125
126 # create a 2nd instance from volume
lkuchlan8789c552017-01-08 11:40:27 +0200127 instance_2nd = self._boot_instance_from_resource(
128 source_id=volume_origin['id'],
129 source_type='volume',
130 keypair=keypair,
131 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100132 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900133
134 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100135 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500136 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200137 timestamp2 = self.get_timestamp(ip_instance_2nd,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200138 private_key=keypair['private_key'],
139 server=instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200140 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900141
142 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100143 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
lkuchlan73ed1f32017-07-06 16:22:12 +0300144 snapshot = self.create_volume_snapshot(volume_origin['id'], force=True)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900145
146 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100147 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500148 volume = self.create_volume(snapshot_id=snapshot['id'],
149 size=snapshot['size'])
150 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200151 server_from_snapshot = (
lkuchlan8789c552017-01-08 11:40:27 +0200152 self._boot_instance_from_resource(source_id=volume['id'],
153 source_type='volume',
154 keypair=keypair,
155 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500156 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900157
158 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100159 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500160 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500161 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200162 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200163 private_key=keypair['private_key'],
164 server=server_from_snapshot)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200165 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900166
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800167 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200168 @decorators.attr(type='slow')
zhufl36214c52018-03-02 15:49:41 +0800169 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
170 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100171 @utils.services('compute', 'image', 'volume')
lkuchlan8789c552017-01-08 11:40:27 +0200172 def test_create_server_from_volume_snapshot(self):
173 # Create a volume from an image
174 boot_volume = self._create_volume_from_image()
175
176 # Create a snapshot
lkuchlan73ed1f32017-07-06 16:22:12 +0300177 boot_snapshot = self.create_volume_snapshot(boot_volume['id'])
lkuchlan8789c552017-01-08 11:40:27 +0200178
179 # Create a server from a volume snapshot
180 server = self._boot_instance_from_resource(
181 source_id=boot_snapshot['id'],
182 source_type='snapshot',
183 delete_on_termination=True)
184
185 server_info = self.servers_client.show_server(server['id'])['server']
186
187 # The created volume when creating a server from a snapshot
188 created_volume = server_info['os-extended-volumes:volumes_attached']
189
lkuchlan9e22b852017-02-05 15:38:29 +0200190 self.assertNotEmpty(created_volume, "No volume attachment found.")
191
lkuchlan8789c552017-01-08 11:40:27 +0200192 created_volume_info = self.volumes_client.show_volume(
193 created_volume[0]['id'])['volume']
194
195 # Verify the server was created from the snapshot
196 self.assertEqual(
197 boot_volume['volume_image_metadata']['image_id'],
198 created_volume_info['volume_image_metadata']['image_id'])
199 self.assertEqual(boot_snapshot['id'],
200 created_volume_info['snapshot_id'])
201 self.assertEqual(server['id'],
202 created_volume_info['attachments'][0]['server_id'])
203 self.assertEqual(created_volume[0]['id'],
204 created_volume_info['attachments'][0]['volume_id'])
205
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800206 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
zhufl36214c52018-03-02 15:49:41 +0800207 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
208 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100209 @utils.services('compute', 'volume', 'image')
Matt Riedemann2db6c272018-03-22 18:57:19 -0400210 def test_image_defined_boot_from_volume(self):
211 # create an instance from image-backed volume
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300212 volume_origin = self._create_volume_from_image()
melanie wittca30e8c2018-08-07 21:39:52 +0000213 name = data_utils.rand_name(self.__class__.__name__ +
214 '-volume-backed-server')
215 instance1 = self._boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200216 source_id=volume_origin['id'],
217 source_type='volume',
melanie wittca30e8c2018-08-07 21:39:52 +0000218 delete_on_termination=True,
219 name=name)
Matt Riedemann2db6c272018-03-22 18:57:19 -0400220 # Create a snapshot image from the volume-backed server.
221 # The compute service will have the block service create a snapshot of
222 # the root volume and store its metadata in the image.
melanie wittca30e8c2018-08-07 21:39:52 +0000223 image = self.create_server_snapshot(instance1)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300224
Matt Riedemann2db6c272018-03-22 18:57:19 -0400225 # Create a server from the image snapshot which has an
226 # "image-defined block device mapping (BDM)" in it, i.e. the metadata
227 # about the volume snapshot. The compute service will use this to
228 # create a volume from the volume snapshot and use that as the root
229 # disk for the server.
melanie wittca30e8c2018-08-07 21:39:52 +0000230 name = data_utils.rand_name(self.__class__.__name__ +
231 '-image-snapshot-server')
232 instance2 = self.create_server(image_id=image['id'], name=name)
lianghao2e0ee042017-10-26 19:38:28 +0800233
Matt Riedemann2db6c272018-03-22 18:57:19 -0400234 # Verify the server was created from the image-defined BDM.
melanie wittca30e8c2018-08-07 21:39:52 +0000235 volume_attachments = instance2['os-extended-volumes:volumes_attached']
Matt Riedemann2db6c272018-03-22 18:57:19 -0400236 self.assertEqual(1, len(volume_attachments),
237 "No volume attachment found.")
238 created_volume = self.volumes_client.show_volume(
239 volume_attachments[0]['id'])['volume']
240 # Assert that the volume service also shows the server attachment.
241 self.assertEqual(1, len(created_volume['attachments']),
242 "No server attachment found for volume: %s" %
243 created_volume)
melanie wittca30e8c2018-08-07 21:39:52 +0000244 self.assertEqual(instance2['id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400245 created_volume['attachments'][0]['server_id'])
246 self.assertEqual(volume_attachments[0]['id'],
247 created_volume['attachments'][0]['volume_id'])
lianghao2e0ee042017-10-26 19:38:28 +0800248 self.assertEqual(
249 volume_origin['volume_image_metadata']['image_id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400250 created_volume['volume_image_metadata']['image_id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300251
Matt Riedemann2db6c272018-03-22 18:57:19 -0400252 # Delete the second server which should also delete the second volume
253 # created from the volume snapshot.
melanie wittca30e8c2018-08-07 21:39:52 +0000254 self._delete_server(instance2)
lkuchlan3023e752017-06-08 12:53:13 +0300255
melanie wittc50cc242018-05-01 22:00:39 +0000256 # Assert that the underlying volume is gone.
257 self.volumes_client.wait_for_resource_deletion(created_volume['id'])
258
melanie wittca30e8c2018-08-07 21:39:52 +0000259 # Delete the volume snapshot. We must do this before deleting the first
260 # server created in this test because the snapshot depends on the first
261 # instance's underlying volume (volume_origin).
262 # In glance v2, the image properties are flattened and in glance v1,
263 # the image properties are under the 'properties' key.
264 bdms = image.get('block_device_mapping')
265 if not bdms:
266 bdms = image['properties']['block_device_mapping']
267 bdms = jsonutils.loads(bdms)
268 snapshot_id = bdms[0]['snapshot_id']
269 self._delete_snapshot(snapshot_id)
270
271 # Now, delete the first server which will also delete the first
272 # image-backed volume.
273 self._delete_server(instance1)
274
275 # Assert that the underlying volume is gone.
276 self.volumes_client.wait_for_resource_deletion(volume_origin['id'])
277
lkuchlan3023e752017-06-08 12:53:13 +0300278 @decorators.idempotent_id('cb78919a-e553-4bab-b73b-10cf4d2eb125')
Attila Fazekas1c39a2f2017-07-18 19:39:06 +0200279 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
280 'Encrypted volume attach is not supported')
Andrea Frittolicd368412017-08-14 21:37:56 +0100281 @utils.services('compute', 'volume')
lkuchlan3023e752017-06-08 12:53:13 +0300282 def test_boot_server_from_encrypted_volume_luks(self):
283 # Create an encrypted volume
284 volume = self.create_encrypted_volume('nova.volume.encryptors.'
285 'luks.LuksEncryptor',
286 volume_type='luks')
287
288 self.volumes_client.set_bootable_volume(volume['id'], bootable=True)
289
290 # Boot a server from the encrypted volume
291 server = self._boot_instance_from_resource(
292 source_id=volume['id'],
293 source_type='volume',
294 delete_on_termination=False)
295
296 server_info = self.servers_client.show_server(server['id'])['server']
297 created_volume = server_info['os-extended-volumes:volumes_attached']
298 self.assertEqual(volume['id'], created_volume[0]['id'])