blob: 07823893128a3d4f963cc78c15bc54a22f7a707a [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
fujioka yuuichi636f8db2013-08-09 12:05:24 +090034 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000035 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000036 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090037
melanie wittca30e8c2018-08-07 21:39:52 +000038 def _delete_snapshot(self, snapshot_id):
39 self.snapshots_client.delete_snapshot(snapshot_id)
40 self.snapshots_client.wait_for_resource_deletion(snapshot_id)
41
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080042 @decorators.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Matt Riedemann4b8a7b82019-02-14 14:07:11 -050043 @decorators.attr(type='slow')
msidanae1f990b2018-06-05 12:10:24 +053044 # Note: This test is being skipped based on 'public_network_id'.
45 # It is being used in create_floating_ip() method which gets called
46 # from get_server_ip() method
zhufl6b7040a2017-01-18 16:38:34 +080047 @testtools.skipUnless(CONF.network.public_network_id,
48 'The public_network_id option must be specified.')
zhufl36214c52018-03-02 15:49:41 +080049 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
50 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +010051 @utils.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090052 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030053 """This test case attempts to reproduce the following steps:
54
55 * Create in Cinder some bootable volume importing a Glance image
56 * Boot an instance from the bootable volume
57 * Write content to the volume
58 * Delete an instance and Boot a new instance from the volume
59 * Check written content in the instance
60 * Create a volume snapshot while the instance is running
61 * Boot an additional instance from the new snapshot based volume
62 * Check written content in the instance booted from snapshot
63 """
64
Sean Dague52abbd92016-03-01 09:38:09 -050065 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090066 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +030067 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +090068
69 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -050070 LOG.info("Booting instance 1 from volume")
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +000071 volume_origin = self.create_volume_from_image()
72 instance_1st = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +020073 source_id=volume_origin['id'],
74 source_type='volume',
75 keypair=keypair,
76 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +010077 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090078
79 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +010080 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -050081 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +020082 timestamp = self.create_timestamp(ip_instance_1st,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +020083 private_key=keypair['private_key'],
84 server=instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090085
86 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +010087 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090088 self._delete_server(instance_1st)
89
90 # create a 2nd instance from volume
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +000091 instance_2nd = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +020092 source_id=volume_origin['id'],
93 source_type='volume',
94 keypair=keypair,
95 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +010096 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090097
98 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +010099 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500100 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200101 timestamp2 = self.get_timestamp(ip_instance_2nd,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200102 private_key=keypair['private_key'],
103 server=instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200104 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900105
106 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100107 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
lkuchlan73ed1f32017-07-06 16:22:12 +0300108 snapshot = self.create_volume_snapshot(volume_origin['id'], force=True)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900109
110 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100111 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500112 volume = self.create_volume(snapshot_id=snapshot['id'],
113 size=snapshot['size'])
114 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200115 server_from_snapshot = (
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000116 self.boot_instance_from_resource(source_id=volume['id'],
117 source_type='volume',
118 keypair=keypair,
119 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500120 LOG.info("Booted third instance %s", server_from_snapshot)
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("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500124 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500125 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200126 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Slawek Kaplonski79d8b0f2018-07-30 22:43:41 +0200127 private_key=keypair['private_key'],
128 server=server_from_snapshot)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200129 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900130
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800131 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200132 @decorators.attr(type='slow')
zhufl36214c52018-03-02 15:49:41 +0800133 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
134 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100135 @utils.services('compute', 'image', 'volume')
lkuchlan8789c552017-01-08 11:40:27 +0200136 def test_create_server_from_volume_snapshot(self):
137 # Create a volume from an image
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000138 boot_volume = self.create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200139
140 # Create a snapshot
lkuchlan73ed1f32017-07-06 16:22:12 +0300141 boot_snapshot = self.create_volume_snapshot(boot_volume['id'])
lkuchlan8789c552017-01-08 11:40:27 +0200142
143 # Create a server from a volume snapshot
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000144 server = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200145 source_id=boot_snapshot['id'],
146 source_type='snapshot',
147 delete_on_termination=True)
148
149 server_info = self.servers_client.show_server(server['id'])['server']
150
151 # The created volume when creating a server from a snapshot
152 created_volume = server_info['os-extended-volumes:volumes_attached']
153
lkuchlan9e22b852017-02-05 15:38:29 +0200154 self.assertNotEmpty(created_volume, "No volume attachment found.")
155
lkuchlan8789c552017-01-08 11:40:27 +0200156 created_volume_info = self.volumes_client.show_volume(
157 created_volume[0]['id'])['volume']
158
159 # Verify the server was created from the snapshot
160 self.assertEqual(
161 boot_volume['volume_image_metadata']['image_id'],
162 created_volume_info['volume_image_metadata']['image_id'])
163 self.assertEqual(boot_snapshot['id'],
164 created_volume_info['snapshot_id'])
165 self.assertEqual(server['id'],
166 created_volume_info['attachments'][0]['server_id'])
167 self.assertEqual(created_volume[0]['id'],
168 created_volume_info['attachments'][0]['volume_id'])
169
Giulio Fidente0d2b3312020-03-04 10:15:03 +0100170 # Delete the server and wait
171 self._delete_server(server)
172
173 # Assert that the underlying volume is gone before class tearDown
174 # to prevent snapshot deletion from failing
175 self.volumes_client.wait_for_resource_deletion(created_volume[0]['id'])
176
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800177 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
zhufl36214c52018-03-02 15:49:41 +0800178 @testtools.skipUnless(CONF.volume_feature_enabled.snapshot,
179 'Cinder volume snapshots are disabled')
Andrea Frittolicd368412017-08-14 21:37:56 +0100180 @utils.services('compute', 'volume', 'image')
Matt Riedemann2db6c272018-03-22 18:57:19 -0400181 def test_image_defined_boot_from_volume(self):
182 # create an instance from image-backed volume
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000183 volume_origin = self.create_volume_from_image()
melanie wittca30e8c2018-08-07 21:39:52 +0000184 name = data_utils.rand_name(self.__class__.__name__ +
185 '-volume-backed-server')
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000186 instance1 = self.boot_instance_from_resource(
lkuchlan8789c552017-01-08 11:40:27 +0200187 source_id=volume_origin['id'],
188 source_type='volume',
melanie wittca30e8c2018-08-07 21:39:52 +0000189 delete_on_termination=True,
190 name=name)
Matt Riedemann2db6c272018-03-22 18:57:19 -0400191 # Create a snapshot image from the volume-backed server.
192 # The compute service will have the block service create a snapshot of
193 # the root volume and store its metadata in the image.
melanie wittca30e8c2018-08-07 21:39:52 +0000194 image = self.create_server_snapshot(instance1)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300195
Matt Riedemann2db6c272018-03-22 18:57:19 -0400196 # Create a server from the image snapshot which has an
197 # "image-defined block device mapping (BDM)" in it, i.e. the metadata
198 # about the volume snapshot. The compute service will use this to
199 # create a volume from the volume snapshot and use that as the root
200 # disk for the server.
melanie wittca30e8c2018-08-07 21:39:52 +0000201 name = data_utils.rand_name(self.__class__.__name__ +
202 '-image-snapshot-server')
203 instance2 = self.create_server(image_id=image['id'], name=name)
lianghao2e0ee042017-10-26 19:38:28 +0800204
Matt Riedemann2db6c272018-03-22 18:57:19 -0400205 # Verify the server was created from the image-defined BDM.
melanie wittca30e8c2018-08-07 21:39:52 +0000206 volume_attachments = instance2['os-extended-volumes:volumes_attached']
Matt Riedemann2db6c272018-03-22 18:57:19 -0400207 self.assertEqual(1, len(volume_attachments),
208 "No volume attachment found.")
209 created_volume = self.volumes_client.show_volume(
210 volume_attachments[0]['id'])['volume']
211 # Assert that the volume service also shows the server attachment.
212 self.assertEqual(1, len(created_volume['attachments']),
213 "No server attachment found for volume: %s" %
214 created_volume)
melanie wittca30e8c2018-08-07 21:39:52 +0000215 self.assertEqual(instance2['id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400216 created_volume['attachments'][0]['server_id'])
217 self.assertEqual(volume_attachments[0]['id'],
218 created_volume['attachments'][0]['volume_id'])
lianghao2e0ee042017-10-26 19:38:28 +0800219 self.assertEqual(
220 volume_origin['volume_image_metadata']['image_id'],
Matt Riedemann2db6c272018-03-22 18:57:19 -0400221 created_volume['volume_image_metadata']['image_id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300222
Matt Riedemann2db6c272018-03-22 18:57:19 -0400223 # Delete the second server which should also delete the second volume
224 # created from the volume snapshot.
melanie wittca30e8c2018-08-07 21:39:52 +0000225 self._delete_server(instance2)
lkuchlan3023e752017-06-08 12:53:13 +0300226
melanie wittc50cc242018-05-01 22:00:39 +0000227 # Assert that the underlying volume is gone.
228 self.volumes_client.wait_for_resource_deletion(created_volume['id'])
229
melanie wittca30e8c2018-08-07 21:39:52 +0000230 # Delete the volume snapshot. We must do this before deleting the first
231 # server created in this test because the snapshot depends on the first
232 # instance's underlying volume (volume_origin).
233 # In glance v2, the image properties are flattened and in glance v1,
234 # the image properties are under the 'properties' key.
235 bdms = image.get('block_device_mapping')
236 if not bdms:
237 bdms = image['properties']['block_device_mapping']
zhulingjie92b87a52019-02-21 01:05:54 +0800238 bdms = json.loads(bdms)
melanie wittca30e8c2018-08-07 21:39:52 +0000239 snapshot_id = bdms[0]['snapshot_id']
240 self._delete_snapshot(snapshot_id)
241
242 # Now, delete the first server which will also delete the first
243 # image-backed volume.
244 self._delete_server(instance1)
245
246 # Assert that the underlying volume is gone.
247 self.volumes_client.wait_for_resource_deletion(volume_origin['id'])
248
lkuchlan3023e752017-06-08 12:53:13 +0300249 @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
Rajat Dhasmanaa8bfa172020-01-14 17:34:44 +0000262 server = self.boot_instance_from_resource(
lkuchlan3023e752017-06-08 12:53:13 +0300263 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'])