blob: 1fc57e7764b4dba97a9f83def4076d1ec3af8e97 [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
zhufl6b7040a2017-01-18 16:38:34 +080014import testtools
Sean Dague52abbd92016-03-01 09:38:09 -050015
Andrea Frittolicd368412017-08-14 21:37:56 +010016from tempest.common import utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000017from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
Ken'ichi Ohmichibe4fb502017-03-10 10:04:48 -080019from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080020from tempest.lib import decorators
fujioka yuuichi636f8db2013-08-09 12:05:24 +090021from tempest.scenario import manager
22
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
Sean Dague52abbd92016-03-01 09:38:09 -050024LOG = logging.getLogger(__name__)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090025
Nachi Ueno95b41282014-01-15 06:54:21 -080026
lkuchlan3023e752017-06-08 12:53:13 +030027class TestVolumeBootPattern(manager.EncryptionScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090028
Sean Dague02620fd2016-03-02 15:52:51 -050029 # Boot from volume scenario is quite slow, and needs extra
30 # breathing room to get through deletes in the time allotted.
31 TIMEOUT_SCALING_FACTOR = 2
32
fujioka yuuichi636f8db2013-08-09 12:05:24 +090033 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000034 img_uuid = CONF.compute.image_ref
zhuflc6ce5392016-08-17 14:34:37 +080035 vol_name = data_utils.rand_name(
36 self.__class__.__name__ + '-volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090037 return self.create_volume(name=vol_name, imageRef=img_uuid)
38
lkuchlan8789c552017-01-08 11:40:27 +020039 def _get_bdm(self, source_id, source_type, delete_on_termination=False):
Sean Dagued571e032017-02-27 12:27:10 -050040 bd_map_v2 = [{
41 'uuid': source_id,
42 'source_type': source_type,
43 'destination_type': 'volume',
44 'boot_index': 0,
45 'delete_on_termination': delete_on_termination}]
46 return {'block_device_mapping_v2': bd_map_v2}
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030047
lkuchlan8789c552017-01-08 11:40:27 +020048 def _boot_instance_from_resource(self, source_id,
49 source_type,
50 keypair=None,
51 security_group=None,
52 delete_on_termination=False):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030053 create_kwargs = dict()
54 if keypair:
55 create_kwargs['key_name'] = keypair['name']
56 if security_group:
57 create_kwargs['security_groups'] = [
58 {'name': security_group['name']}]
59 create_kwargs.update(self._get_bdm(
lkuchlan8789c552017-01-08 11:40:27 +020060 source_id,
61 source_type,
62 delete_on_termination=delete_on_termination))
63
zhufl13c9c892017-02-10 12:04:07 +080064 return self.create_server(image_id='', **create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090065
fujioka yuuichi636f8db2013-08-09 12:05:24 +090066 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000067 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000068 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090069
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080070 @decorators.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
zhufl6b7040a2017-01-18 16:38:34 +080071 @testtools.skipUnless(CONF.network.public_network_id,
72 'The public_network_id option must be specified.')
zhufl36214c52018-03-02 15:49:41 +080073 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
74 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +010075 @utils.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090076 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030077
78 """This test case attempts to reproduce the following steps:
79
80 * Create in Cinder some bootable volume importing a Glance image
81 * Boot an instance from the bootable volume
82 * Write content to the volume
83 * Delete an instance and Boot a new instance from the volume
84 * Check written content in the instance
85 * Create a volume snapshot while the instance is running
86 * Boot an additional instance from the new snapshot based volume
87 * Check written content in the instance booted from snapshot
88 """
89
Sean Dague52abbd92016-03-01 09:38:09 -050090 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090091 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +030092 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +090093
94 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -050095 LOG.info("Booting instance 1 from volume")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090096 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +020097 instance_1st = self._boot_instance_from_resource(
98 source_id=volume_origin['id'],
99 source_type='volume',
100 keypair=keypair,
101 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100102 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900103
104 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100105 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -0500106 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200107 timestamp = self.create_timestamp(ip_instance_1st,
108 private_key=keypair['private_key'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900109
110 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100111 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900112 self._delete_server(instance_1st)
113
114 # create a 2nd instance from volume
lkuchlan8789c552017-01-08 11:40:27 +0200115 instance_2nd = self._boot_instance_from_resource(
116 source_id=volume_origin['id'],
117 source_type='volume',
118 keypair=keypair,
119 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100120 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900121
122 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100123 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500124 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200125 timestamp2 = self.get_timestamp(ip_instance_2nd,
126 private_key=keypair['private_key'])
127 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900128
129 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100130 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
lkuchlan73ed1f32017-07-06 16:22:12 +0300131 snapshot = self.create_volume_snapshot(volume_origin['id'], force=True)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900132
133 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100134 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500135 volume = self.create_volume(snapshot_id=snapshot['id'],
136 size=snapshot['size'])
137 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200138 server_from_snapshot = (
lkuchlan8789c552017-01-08 11:40:27 +0200139 self._boot_instance_from_resource(source_id=volume['id'],
140 source_type='volume',
141 keypair=keypair,
142 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500143 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900144
145 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100146 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500147 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500148 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200149 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200150 private_key=keypair['private_key'])
151 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900152
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800153 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200154 @decorators.attr(type='slow')
zhufl36214c52018-03-02 15:49:41 +0800155 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
156 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100157 @utils.services('compute', 'image', 'volume')
lkuchlan8789c552017-01-08 11:40:27 +0200158 def test_create_server_from_volume_snapshot(self):
159 # Create a volume from an image
160 boot_volume = self._create_volume_from_image()
161
162 # Create a snapshot
lkuchlan73ed1f32017-07-06 16:22:12 +0300163 boot_snapshot = self.create_volume_snapshot(boot_volume['id'])
lkuchlan8789c552017-01-08 11:40:27 +0200164
165 # Create a server from a volume snapshot
166 server = self._boot_instance_from_resource(
167 source_id=boot_snapshot['id'],
168 source_type='snapshot',
169 delete_on_termination=True)
170
171 server_info = self.servers_client.show_server(server['id'])['server']
172
173 # The created volume when creating a server from a snapshot
174 created_volume = server_info['os-extended-volumes:volumes_attached']
175
lkuchlan9e22b852017-02-05 15:38:29 +0200176 self.assertNotEmpty(created_volume, "No volume attachment found.")
177
lkuchlan8789c552017-01-08 11:40:27 +0200178 created_volume_info = self.volumes_client.show_volume(
179 created_volume[0]['id'])['volume']
180
181 # Verify the server was created from the snapshot
182 self.assertEqual(
183 boot_volume['volume_image_metadata']['image_id'],
184 created_volume_info['volume_image_metadata']['image_id'])
185 self.assertEqual(boot_snapshot['id'],
186 created_volume_info['snapshot_id'])
187 self.assertEqual(server['id'],
188 created_volume_info['attachments'][0]['server_id'])
189 self.assertEqual(created_volume[0]['id'],
190 created_volume_info['attachments'][0]['volume_id'])
191
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800192 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
zhufl36214c52018-03-02 15:49:41 +0800193 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
194 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100195 @utils.services('compute', 'volume', 'image')
Matt Riedemann2db6c272018-03-22 18:57:19 -0400196 def test_image_defined_boot_from_volume(self):
197 # create an instance from image-backed volume
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300198 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200199 instance = self._boot_instance_from_resource(
200 source_id=volume_origin['id'],
201 source_type='volume',
202 delete_on_termination=True)
Matt Riedemann2db6c272018-03-22 18:57:19 -0400203 # Create a snapshot image from the volume-backed server.
204 # The compute service will have the block service create a snapshot of
205 # the root volume and store its metadata in the image.
zhuflf9d95722016-10-19 16:06:17 +0800206 image = self.create_server_snapshot(instance)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300207
Matt Riedemann2db6c272018-03-22 18:57:19 -0400208 # Delete the first server which will also delete the first image-backed
209 # volume.
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300210 self._delete_server(instance)
211
Matt Riedemann2db6c272018-03-22 18:57:19 -0400212 # Create a server from the image snapshot which has an
213 # "image-defined block device mapping (BDM)" in it, i.e. the metadata
214 # about the volume snapshot. The compute service will use this to
215 # create a volume from the volume snapshot and use that as the root
216 # disk for the server.
zhufl13c9c892017-02-10 12:04:07 +0800217 instance = self.create_server(image_id=image['id'])
lianghao2e0ee042017-10-26 19:38:28 +0800218
Matt Riedemann2db6c272018-03-22 18:57:19 -0400219 # Verify the server was created from the image-defined BDM.
220 volume_attachments = instance['os-extended-volumes:volumes_attached']
221 self.assertEqual(1, len(volume_attachments),
222 "No volume attachment found.")
223 created_volume = self.volumes_client.show_volume(
224 volume_attachments[0]['id'])['volume']
225 # Assert that the volume service also shows the server attachment.
226 self.assertEqual(1, len(created_volume['attachments']),
227 "No server attachment found for volume: %s" %
228 created_volume)
lianghao2e0ee042017-10-26 19:38:28 +0800229 self.assertEqual(instance['id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400230 created_volume['attachments'][0]['server_id'])
231 self.assertEqual(volume_attachments[0]['id'],
232 created_volume['attachments'][0]['volume_id'])
lianghao2e0ee042017-10-26 19:38:28 +0800233 self.assertEqual(
234 volume_origin['volume_image_metadata']['image_id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400235 created_volume['volume_image_metadata']['image_id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300236
Matt Riedemann2db6c272018-03-22 18:57:19 -0400237 # Delete the second server which should also delete the second volume
238 # created from the volume snapshot.
239 # TODO(mriedem): Currently, the compute service fails to delete the
240 # volume it created because the volume still has the snapshot
241 # associated with it, and the cleanups for the volume snapshot which
242 # were added in create_server_snapshot above, don't run until after
243 # this is called. So we need to delete the volume snapshot and wait for
244 # it to be gone here first before deleting the server, and then we can
245 # also assert that the underlying volume is deleted when the server is
246 # deleted.
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300247 self._delete_server(instance)
lkuchlan3023e752017-06-08 12:53:13 +0300248
249 @decorators.idempotent_id('cb78919a-e553-4bab-b73b-10cf4d2eb125')
Attila Fazekas1c39a2f2017-07-18 19:39:06 +0200250 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
251 'Encrypted volume attach is not supported')
Andrea Frittolicd368412017-08-14 21:37:56 +0100252 @utils.services('compute', 'volume')
lkuchlan3023e752017-06-08 12:53:13 +0300253 def test_boot_server_from_encrypted_volume_luks(self):
254 # Create an encrypted volume
255 volume = self.create_encrypted_volume('nova.volume.encryptors.'
256 'luks.LuksEncryptor',
257 volume_type='luks')
258
259 self.volumes_client.set_bootable_volume(volume['id'], bootable=True)
260
261 # Boot a server from the encrypted volume
262 server = self._boot_instance_from_resource(
263 source_id=volume['id'],
264 source_type='volume',
265 delete_on_termination=False)
266
267 server_info = self.servers_client.show_server(server['id'])['server']
268 created_volume = server_info['os-extended-volumes:volumes_attached']
269 self.assertEqual(volume['id'], created_volume[0]['id'])