blob: 19bc311a572a60026d074754b4beec315eded9c9 [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
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000016from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000017from tempest import config
Ken'ichi Ohmichibe4fb502017-03-10 10:04:48 -080018from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080019from tempest.lib import decorators
fujioka yuuichi636f8db2013-08-09 12:05:24 +090020from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
fujioka yuuichi636f8db2013-08-09 12:05:24 +090022
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
JordanPbce55532014-03-19 12:10:32 +010033 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000034 def skip_checks(cls):
35 super(TestVolumeBootPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010036 if not CONF.volume_feature_enabled.snapshot:
37 raise cls.skipException("Cinder volume snapshots are disabled")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090038
39 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000040 img_uuid = CONF.compute.image_ref
zhuflc6ce5392016-08-17 14:34:37 +080041 vol_name = data_utils.rand_name(
42 self.__class__.__name__ + '-volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090043 return self.create_volume(name=vol_name, imageRef=img_uuid)
44
lkuchlan8789c552017-01-08 11:40:27 +020045 def _get_bdm(self, source_id, source_type, delete_on_termination=False):
Sean Dagued571e032017-02-27 12:27:10 -050046 bd_map_v2 = [{
47 'uuid': source_id,
48 'source_type': source_type,
49 'destination_type': 'volume',
50 'boot_index': 0,
51 'delete_on_termination': delete_on_termination}]
52 return {'block_device_mapping_v2': bd_map_v2}
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030053
lkuchlan8789c552017-01-08 11:40:27 +020054 def _boot_instance_from_resource(self, source_id,
55 source_type,
56 keypair=None,
57 security_group=None,
58 delete_on_termination=False):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030059 create_kwargs = dict()
60 if keypair:
61 create_kwargs['key_name'] = keypair['name']
62 if security_group:
63 create_kwargs['security_groups'] = [
64 {'name': security_group['name']}]
65 create_kwargs.update(self._get_bdm(
lkuchlan8789c552017-01-08 11:40:27 +020066 source_id,
67 source_type,
68 delete_on_termination=delete_on_termination))
69
zhufl13c9c892017-02-10 12:04:07 +080070 return self.create_server(image_id='', **create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090071
72 def _create_snapshot_from_volume(self, vol_id):
zhuflc6ce5392016-08-17 14:34:37 +080073 snap_name = data_utils.rand_name(
74 self.__class__.__name__ + '-snapshot')
melanie witt87412222015-01-21 04:32:17 +000075 snap = self.snapshots_client.create_snapshot(
Joseph Lanouxeef192f2014-08-01 14:32:53 +000076 volume_id=vol_id,
77 force=True,
John Warrenff7faf62015-08-17 16:59:06 +000078 display_name=snap_name)['snapshot']
Yaroslav Lobankov46a78c32015-04-08 13:45:27 +030079 self.addCleanup(
80 self.snapshots_client.wait_for_resource_deletion, snap['id'])
81 self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
lkuchlan52d7b0d2016-11-07 20:53:19 +020082 waiters.wait_for_volume_resource_status(self.snapshots_client,
83 snap['id'], 'available')
lkuchlan5cbc00a2017-03-26 11:49:54 +030084 self.assertEqual(snap_name, snap['name'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090085 return snap
86
fujioka yuuichi636f8db2013-08-09 12:05:24 +090087 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000088 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000089 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090090
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -080091 @decorators.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
zhufl6b7040a2017-01-18 16:38:34 +080092 @testtools.skipUnless(CONF.network.public_network_id,
93 'The public_network_id option must be specified.')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090094 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090095 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +030096
97 """This test case attempts to reproduce the following steps:
98
99 * Create in Cinder some bootable volume importing a Glance image
100 * Boot an instance from the bootable volume
101 * Write content to the volume
102 * Delete an instance and Boot a new instance from the volume
103 * Check written content in the instance
104 * Create a volume snapshot while the instance is running
105 * Boot an additional instance from the new snapshot based volume
106 * Check written content in the instance booted from snapshot
107 """
108
Sean Dague52abbd92016-03-01 09:38:09 -0500109 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900110 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300111 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900112
113 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -0500114 LOG.info("Booting instance 1 from volume")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900115 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200116 instance_1st = self._boot_instance_from_resource(
117 source_id=volume_origin['id'],
118 source_type='volume',
119 keypair=keypair,
120 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100121 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900122
123 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100124 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -0500125 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200126 timestamp = self.create_timestamp(ip_instance_1st,
127 private_key=keypair['private_key'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900128
129 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100130 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900131 self._delete_server(instance_1st)
132
133 # create a 2nd instance from volume
lkuchlan8789c552017-01-08 11:40:27 +0200134 instance_2nd = self._boot_instance_from_resource(
135 source_id=volume_origin['id'],
136 source_type='volume',
137 keypair=keypair,
138 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100139 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900140
141 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100142 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500143 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200144 timestamp2 = self.get_timestamp(ip_instance_2nd,
145 private_key=keypair['private_key'])
146 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900147
148 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100149 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000150 snapshot = self._create_snapshot_from_volume(volume_origin['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900151
152 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100153 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500154 volume = self.create_volume(snapshot_id=snapshot['id'],
155 size=snapshot['size'])
156 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200157 server_from_snapshot = (
lkuchlan8789c552017-01-08 11:40:27 +0200158 self._boot_instance_from_resource(source_id=volume['id'],
159 source_type='volume',
160 keypair=keypair,
161 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500162 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900163
164 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100165 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500166 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500167 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200168 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200169 private_key=keypair['private_key'])
170 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900171
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800172 @decorators.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
Jordan Pittier3b46d272017-04-12 16:17:28 +0200173 @decorators.attr(type='slow')
lkuchlan8789c552017-01-08 11:40:27 +0200174 @test.services('compute', 'image', 'volume')
175 def test_create_server_from_volume_snapshot(self):
176 # Create a volume from an image
177 boot_volume = self._create_volume_from_image()
178
179 # Create a snapshot
180 boot_snapshot = self._create_snapshot_from_volume(boot_volume['id'])
181
182 # Create a server from a volume snapshot
183 server = self._boot_instance_from_resource(
184 source_id=boot_snapshot['id'],
185 source_type='snapshot',
186 delete_on_termination=True)
187
188 server_info = self.servers_client.show_server(server['id'])['server']
189
190 # The created volume when creating a server from a snapshot
191 created_volume = server_info['os-extended-volumes:volumes_attached']
192
lkuchlan9e22b852017-02-05 15:38:29 +0200193 self.assertNotEmpty(created_volume, "No volume attachment found.")
194
lkuchlan8789c552017-01-08 11:40:27 +0200195 created_volume_info = self.volumes_client.show_volume(
196 created_volume[0]['id'])['volume']
197
198 # Verify the server was created from the snapshot
199 self.assertEqual(
200 boot_volume['volume_image_metadata']['image_id'],
201 created_volume_info['volume_image_metadata']['image_id'])
202 self.assertEqual(boot_snapshot['id'],
203 created_volume_info['snapshot_id'])
204 self.assertEqual(server['id'],
205 created_volume_info['attachments'][0]['server_id'])
206 self.assertEqual(created_volume[0]['id'],
207 created_volume_info['attachments'][0]['volume_id'])
208
Ken'ichi Ohmichic85a9512017-01-27 18:34:24 -0800209 @decorators.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300210 @test.services('compute', 'volume', 'image')
211 def test_create_ebs_image_and_check_boot(self):
212 # create an instance from volume
213 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200214 instance = self._boot_instance_from_resource(
215 source_id=volume_origin['id'],
216 source_type='volume',
217 delete_on_termination=True)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300218 # create EBS image
zhuflf9d95722016-10-19 16:06:17 +0800219 image = self.create_server_snapshot(instance)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300220
221 # delete instance
222 self._delete_server(instance)
223
224 # boot instance from EBS image
zhufl13c9c892017-02-10 12:04:07 +0800225 instance = self.create_server(image_id=image['id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300226 # just ensure that instance booted
227
228 # delete instance
229 self._delete_server(instance)
lkuchlan3023e752017-06-08 12:53:13 +0300230
231 @decorators.idempotent_id('cb78919a-e553-4bab-b73b-10cf4d2eb125')
Attila Fazekas1c39a2f2017-07-18 19:39:06 +0200232 @testtools.skipUnless(CONF.compute_feature_enabled.attach_encrypted_volume,
233 'Encrypted volume attach is not supported')
lkuchlan3023e752017-06-08 12:53:13 +0300234 @test.services('compute', 'volume')
235 def test_boot_server_from_encrypted_volume_luks(self):
236 # Create an encrypted volume
237 volume = self.create_encrypted_volume('nova.volume.encryptors.'
238 'luks.LuksEncryptor',
239 volume_type='luks')
240
241 self.volumes_client.set_bootable_volume(volume['id'], bootable=True)
242
243 # Boot a server from the encrypted volume
244 server = self._boot_instance_from_resource(
245 source_id=volume['id'],
246 source_type='volume',
247 delete_on_termination=False)
248
249 server_info = self.servers_client.show_server(server['id'])['server']
250 created_volume = server_info['os-extended-volumes:volumes_attached']
251 self.assertEqual(volume['id'], created_volume[0]['id'])