blob: 6ebee48ffc402ce9ed5544eb47ecc66d00d2247b [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
zhulingjie92b87a52019-02-21 01:05:54 +080014from oslo_serialization import jsonutils as json
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
Ghanshyam Mann2803b572023-08-04 12:11:59 -070034 @classmethod
35 def skip_checks(cls):
36 super(TestVolumeBootPattern, cls).skip_checks()
37 if not CONF.service_available.cinder:
38 raise cls.skipException("Cinder is not available")
39
fujioka yuuichi636f8db2013-08-09 12:05:24 +090040 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000041 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000042 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090043
melanie wittca30e8c2018-08-07 21:39:52 +000044 def _delete_snapshot(self, snapshot_id):
45 self.snapshots_client.delete_snapshot(snapshot_id)
46 self.snapshots_client.wait_for_resource_deletion(snapshot_id)
47
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080048 @decorators.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Matt Riedemann4b8a7b82019-02-14 14:07:11 -050049 @decorators.attr(type='slow')
msidanae1f990b2018-06-05 12:10:24 +053050 # Note: This test is being skipped based on 'public_network_id'.
51 # It is being used in create_floating_ip() method which gets called
52 # from get_server_ip() method
zhufl6b7040a2017-01-18 16:38:34 +080053 @testtools.skipUnless(CONF.network.public_network_id,
54 'The public_network_id option must be specified.')
zhufl36214c52018-03-02 15:49:41 +080055 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
56 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +010057 @utils.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090058 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030059 """This test case attempts to reproduce the following steps:
60
61 * Create in Cinder some bootable volume importing a Glance image
62 * Boot an instance from the bootable volume
63 * Write content to the volume
64 * Delete an instance and Boot a new instance from the volume
65 * Check written content in the instance
66 * Create a volume snapshot while the instance is running
67 * Boot an additional instance from the new snapshot based volume
68 * Check written content in the instance booted from snapshot
69 """
70
Sean Dague52abbd92016-03-01 09:38:09 -050071 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090072 keypair = self.create_keypair()
Soniya Vyas582c1702021-02-22 18:26:17 +053073 security_group = self.create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +090074
75 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -050076 LOG.info("Booting instance 1 from volume")
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +000077 volume_origin = self.create_volume_from_image()
78 instance_1st = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +020079 source_id=volume_origin['id'],
80 source_type='volume',
81 keypair=keypair,
82 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +010083 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090084
85 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +010086 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -050087 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +020088 timestamp = self.create_timestamp(ip_instance_1st,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +020089 private_key=keypair['private_key'],
90 server=instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090091
92 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +010093 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090094 self._delete_server(instance_1st)
95
96 # create a 2nd instance from volume
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +000097 instance_2nd = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +020098 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 second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900103
104 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100105 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500106 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200107 timestamp2 = self.get_timestamp(ip_instance_2nd,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200108 private_key=keypair['private_key'],
109 server=instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200110 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900111
112 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100113 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
lkuchlan73ed1f32017-07-06 16:22:12 +0300114 snapshot = self.create_volume_snapshot(volume_origin['id'], force=True)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900115
116 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100117 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500118 volume = self.create_volume(snapshot_id=snapshot['id'],
119 size=snapshot['size'])
120 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200121 server_from_snapshot = (
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000122 self.boot_instance_from_resource(source_id=volume['id'],
123 source_type='volume',
124 keypair=keypair,
125 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500126 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900127
128 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100129 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500130 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500131 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200132 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200133 private_key=keypair['private_key'],
134 server=server_from_snapshot)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200135 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900136
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800137 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200138 @decorators.attr(type='slow')
zhufl36214c52018-03-02 15:49:41 +0800139 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
140 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100141 @utils.services('compute', 'image', 'volume')
lkuchlan8789c552017-01-08 11:40:27 +0200142 def test_create_server_from_volume_snapshot(self):
143 # Create a volume from an image
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000144 boot_volume = self.create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200145
146 # Create a snapshot
lkuchlan73ed1f32017-07-06 16:22:12 +0300147 boot_snapshot = self.create_volume_snapshot(boot_volume['id'])
lkuchlan8789c552017-01-08 11:40:27 +0200148
149 # Create a server from a volume snapshot
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000150 server = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200151 source_id=boot_snapshot['id'],
152 source_type='snapshot',
153 delete_on_termination=True)
154
155 server_info = self.servers_client.show_server(server['id'])['server']
156
157 # The created volume when creating a server from a snapshot
158 created_volume = server_info['os-extended-volumes:volumes_attached']
159
lkuchlan9e22b852017-02-05 15:38:29 +0200160 self.assertNotEmpty(created_volume, "No volume attachment found.")
161
lkuchlan8789c552017-01-08 11:40:27 +0200162 created_volume_info = self.volumes_client.show_volume(
163 created_volume[0]['id'])['volume']
164
165 # Verify the server was created from the snapshot
166 self.assertEqual(
167 boot_volume['volume_image_metadata']['image_id'],
168 created_volume_info['volume_image_metadata']['image_id'])
169 self.assertEqual(boot_snapshot['id'],
170 created_volume_info['snapshot_id'])
171 self.assertEqual(server['id'],
172 created_volume_info['attachments'][0]['server_id'])
173 self.assertEqual(created_volume[0]['id'],
174 created_volume_info['attachments'][0]['volume_id'])
175
Giulio Fidente0d2b3312020-03-04 10:15:03 +0100176 # Delete the server and wait
177 self._delete_server(server)
178
179 # Assert that the underlying volume is gone before class tearDown
180 # to prevent snapshot deletion from failing
181 self.volumes_client.wait_for_resource_deletion(created_volume[0]['id'])
182
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800183 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
zhufl36214c52018-03-02 15:49:41 +0800184 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
185 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100186 @utils.services('compute', 'volume', 'image')
Matt Riedemann2db6c272018-03-22 18:57:19 -0400187 def test_image_defined_boot_from_volume(self):
188 # create an instance from image-backed volume
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000189 volume_origin = self.create_volume_from_image()
melanie wittca30e8c2018-08-07 21:39:52 +0000190 name = data_utils.rand_name(self.__class__.__name__ +
191 '-volume-backed-server')
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000192 instance1 = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200193 source_id=volume_origin['id'],
194 source_type='volume',
melanie wittca30e8c2018-08-07 21:39:52 +0000195 delete_on_termination=True,
Dan Smith49c2b3b2023-04-26 15:52:22 -0700196 wait_until='SSHABLE',
melanie wittca30e8c2018-08-07 21:39:52 +0000197 name=name)
Matt Riedemann2db6c272018-03-22 18:57:19 -0400198 # Create a snapshot image from the volume-backed server.
199 # The compute service will have the block service create a snapshot of
200 # the root volume and store its metadata in the image.
melanie wittca30e8c2018-08-07 21:39:52 +0000201 image = self.create_server_snapshot(instance1)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300202
Matt Riedemann2db6c272018-03-22 18:57:19 -0400203 # Create a server from the image snapshot which has an
204 # "image-defined block device mapping (BDM)" in it, i.e. the metadata
205 # about the volume snapshot. The compute service will use this to
206 # create a volume from the volume snapshot and use that as the root
207 # disk for the server.
melanie wittca30e8c2018-08-07 21:39:52 +0000208 name = data_utils.rand_name(self.__class__.__name__ +
209 '-image-snapshot-server')
Dan Smith49c2b3b2023-04-26 15:52:22 -0700210 instance2 = self.create_server(image_id=image['id'], name=name,
211 wait_until='SSHABLE')
lianghao2e0ee042017-10-26 19:38:28 +0800212
Matt Riedemann2db6c272018-03-22 18:57:19 -0400213 # Verify the server was created from the image-defined BDM.
melanie wittca30e8c2018-08-07 21:39:52 +0000214 volume_attachments = instance2['os-extended-volumes:volumes_attached']
Matt Riedemann2db6c272018-03-22 18:57:19 -0400215 self.assertEqual(1, len(volume_attachments),
216 "No volume attachment found.")
217 created_volume = self.volumes_client.show_volume(
218 volume_attachments[0]['id'])['volume']
219 # Assert that the volume service also shows the server attachment.
220 self.assertEqual(1, len(created_volume['attachments']),
221 "No server attachment found for volume: %s" %
222 created_volume)
melanie wittca30e8c2018-08-07 21:39:52 +0000223 self.assertEqual(instance2['id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400224 created_volume['attachments'][0]['server_id'])
225 self.assertEqual(volume_attachments[0]['id'],
226 created_volume['attachments'][0]['volume_id'])
lianghao2e0ee042017-10-26 19:38:28 +0800227 self.assertEqual(
228 volume_origin['volume_image_metadata']['image_id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400229 created_volume['volume_image_metadata']['image_id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300230
Matt Riedemann2db6c272018-03-22 18:57:19 -0400231 # Delete the second server which should also delete the second volume
232 # created from the volume snapshot.
melanie wittca30e8c2018-08-07 21:39:52 +0000233 self._delete_server(instance2)
lkuchlan3023e752017-06-08 12:53:13 +0300234
melanie wittc50cc242018-05-01 22:00:39 +0000235 # Assert that the underlying volume is gone.
236 self.volumes_client.wait_for_resource_deletion(created_volume['id'])
237
melanie wittca30e8c2018-08-07 21:39:52 +0000238 # Delete the volume snapshot. We must do this before deleting the first
239 # server created in this test because the snapshot depends on the first
240 # instance's underlying volume (volume_origin).
241 # In glance v2, the image properties are flattened and in glance v1,
242 # the image properties are under the 'properties' key.
243 bdms = image.get('block_device_mapping')
244 if not bdms:
245 bdms = image['properties']['block_device_mapping']
zhulingjie92b87a52019-02-21 01:05:54 +0800246 bdms = json.loads(bdms)
melanie wittca30e8c2018-08-07 21:39:52 +0000247 snapshot_id = bdms[0]['snapshot_id']
248 self._delete_snapshot(snapshot_id)
249
250 # Now, delete the first server which will also delete the first
251 # image-backed volume.
252 self._delete_server(instance1)
253
254 # Assert that the underlying volume is gone.
255 self.volumes_client.wait_for_resource_deletion(volume_origin['id'])
256
Gorka Eguileor5e6fc7a2022-03-31 10:59:17 +0200257 def _do_test_boot_server_from_encrypted_volume_luks(self, provider):
lkuchlan3023e752017-06-08 12:53:13 +0300258 # Create an encrypted volume
Gorka Eguileor5e6fc7a2022-03-31 10:59:17 +0200259 volume = self.create_encrypted_volume(provider,
260 volume_type=provider)
lkuchlan3023e752017-06-08 12:53:13 +0300261
262 self.volumes_client.set_bootable_volume(volume['id'], bootable=True)
263
264 # Boot a server from the encrypted volume
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000265 server = self.boot_instance_from_resource(
lkuchlan3023e752017-06-08 12:53:13 +0300266 source_id=volume['id'],
267 source_type='volume',
268 delete_on_termination=False)
269
270 server_info = self.servers_client.show_server(server['id'])['server']
271 created_volume = server_info['os-extended-volumes:volumes_attached']
272 self.assertEqual(volume['id'], created_volume[0]['id'])
Gorka Eguileor5e6fc7a2022-03-31 10:59:17 +0200273
274 @decorators.idempotent_id('cb78919a-e553-4bab-b73b-10cf4d2eb125')
275 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
276 'Encrypted volume attach is not supported')
277 @utils.services('compute', 'volume')
278 def test_boot_server_from_encrypted_volume_luks(self):
279 """LUKs v1 decrypts volume through libvirt."""
280 self._do_test_boot_server_from_encrypted_volume_luks('luks')
281
282 @decorators.idempotent_id('5ab6100f-1b31-4dd0-a774-68cfd837ef77')
283 @testtools.skipIf(CONF.volume.storage_protocol == 'ceph',
284 'Ceph only supports LUKSv2 if doing host attach.')
285 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
286 'Encrypted volume attach is not supported')
287 @utils.services('compute', 'volume')
288 def test_boot_server_from_encrypted_volume_luksv2(self):
289 """LUKs v2 decrypts volume through os-brick."""
290 self._do_test_boot_server_from_encrypted_volume_luks('luks2')