blob: 9486b96a738b49742d15083ced00355ac24f84d8 [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
14
Fei Long Wangd39431f2015-05-14 11:30:48 +120015from tempest.common.utils import data_utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000016from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000017from tempest import config
fujioka yuuichi636f8db2013-08-09 12:05:24 +090018from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090019from tempest import test
fujioka yuuichi636f8db2013-08-09 12:05:24 +090020
Matthew Treinish6c072292014-01-29 19:15:52 +000021CONF = config.CONF
Sean Dague52abbd92016-03-01 09:38:09 -050022LOG = logging.getLogger(__name__)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090023
Nachi Ueno95b41282014-01-15 06:54:21 -080024
Joseph Lanouxeef192f2014-08-01 14:32:53 +000025class TestVolumeBootPattern(manager.ScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090026
Sean Dague02620fd2016-03-02 15:52:51 -050027 # Boot from volume scenario is quite slow, and needs extra
28 # breathing room to get through deletes in the time allotted.
29 TIMEOUT_SCALING_FACTOR = 2
30
JordanPbce55532014-03-19 12:10:32 +010031 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000032 def skip_checks(cls):
33 super(TestVolumeBootPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010034 if not CONF.volume_feature_enabled.snapshot:
35 raise cls.skipException("Cinder volume snapshots are disabled")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090036
37 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000038 img_uuid = CONF.compute.image_ref
zhuflc6ce5392016-08-17 14:34:37 +080039 vol_name = data_utils.rand_name(
40 self.__class__.__name__ + '-volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090041 return self.create_volume(name=vol_name, imageRef=img_uuid)
42
lkuchlan8789c552017-01-08 11:40:27 +020043 def _get_bdm(self, source_id, source_type, delete_on_termination=False):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090044 # NOTE(gfidente): the syntax for block_device_mapping is
45 # dev_name=id:type:size:delete_on_terminate
46 # where type needs to be "snap" if the server is booted
47 # from a snapshot, size instead can be safely left empty
lkuchlan8789c552017-01-08 11:40:27 +020048
Joseph Lanouxeef192f2014-08-01 14:32:53 +000049 bd_map = [{
50 'device_name': 'vda',
lkuchlan8789c552017-01-08 11:40:27 +020051 '{}_id'.format(source_type): source_id,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030052 'delete_on_termination': str(int(delete_on_termination))}]
53 return {'block_device_mapping': bd_map}
54
lkuchlan8789c552017-01-08 11:40:27 +020055 def _boot_instance_from_resource(self, source_id,
56 source_type,
57 keypair=None,
58 security_group=None,
59 delete_on_termination=False):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030060 create_kwargs = dict()
61 if keypair:
62 create_kwargs['key_name'] = keypair['name']
63 if security_group:
64 create_kwargs['security_groups'] = [
65 {'name': security_group['name']}]
66 create_kwargs.update(self._get_bdm(
lkuchlan8789c552017-01-08 11:40:27 +020067 source_id,
68 source_type,
69 delete_on_termination=delete_on_termination))
70
lanoux5fc14522015-09-21 08:17:35 +000071 return self.create_server(
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053072 image_id='',
lanoux5fc14522015-09-21 08:17:35 +000073 wait_until='ACTIVE',
74 **create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090075
76 def _create_snapshot_from_volume(self, vol_id):
zhuflc6ce5392016-08-17 14:34:37 +080077 snap_name = data_utils.rand_name(
78 self.__class__.__name__ + '-snapshot')
melanie witt87412222015-01-21 04:32:17 +000079 snap = self.snapshots_client.create_snapshot(
Joseph Lanouxeef192f2014-08-01 14:32:53 +000080 volume_id=vol_id,
81 force=True,
John Warrenff7faf62015-08-17 16:59:06 +000082 display_name=snap_name)['snapshot']
Yaroslav Lobankov46a78c32015-04-08 13:45:27 +030083 self.addCleanup(
84 self.snapshots_client.wait_for_resource_deletion, snap['id'])
85 self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
Yaroslav Lobankov667aaa22016-03-24 23:13:28 -050086 waiters.wait_for_snapshot_status(self.snapshots_client,
87 snap['id'], 'available')
Ivan Kolodyazhnybcfc32e2015-08-06 13:31:36 +030088
89 # NOTE(e0ne): Cinder API v2 uses name instead of display_name
90 if 'display_name' in snap:
91 self.assertEqual(snap_name, snap['display_name'])
92 else:
93 self.assertEqual(snap_name, snap['name'])
94
fujioka yuuichi636f8db2013-08-09 12:05:24 +090095 return snap
96
fujioka yuuichi636f8db2013-08-09 12:05:24 +090097 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +000098 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +000099 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900100
Chris Hoge7579c1a2015-02-26 14:12:15 -0800101 @test.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Sean Dague3c634d12015-04-27 12:09:19 -0400102 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900103 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900104 def test_volume_boot_pattern(self):
lkuchlan2041cdd2016-08-15 13:50:43 +0300105
106 """This test case attempts to reproduce the following steps:
107
108 * Create in Cinder some bootable volume importing a Glance image
109 * Boot an instance from the bootable volume
110 * Write content to the volume
111 * Delete an instance and Boot a new instance from the volume
112 * Check written content in the instance
113 * Create a volume snapshot while the instance is running
114 * Boot an additional instance from the new snapshot based volume
115 * Check written content in the instance booted from snapshot
116 """
117
Sean Dague52abbd92016-03-01 09:38:09 -0500118 LOG.info("Creating keypair and security group")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900119 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300120 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900121
122 # create an instance from volume
Sean Dague52abbd92016-03-01 09:38:09 -0500123 LOG.info("Booting instance 1 from volume")
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900124 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200125 instance_1st = self._boot_instance_from_resource(
126 source_id=volume_origin['id'],
127 source_type='volume',
128 keypair=keypair,
129 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100130 LOG.info("Booted first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900131
132 # write content to volume on instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100133 LOG.info("Setting timestamp in instance %s", instance_1st)
Sean Dague20e98612016-01-06 14:33:28 -0500134 ip_instance_1st = self.get_server_ip(instance_1st)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200135 timestamp = self.create_timestamp(ip_instance_1st,
136 private_key=keypair['private_key'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900137
138 # delete instance
Jordan Pittier525ec712016-12-07 17:51:26 +0100139 LOG.info("Deleting first instance: %s", instance_1st)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900140 self._delete_server(instance_1st)
141
142 # create a 2nd instance from volume
lkuchlan8789c552017-01-08 11:40:27 +0200143 instance_2nd = self._boot_instance_from_resource(
144 source_id=volume_origin['id'],
145 source_type='volume',
146 keypair=keypair,
147 security_group=security_group)
Jordan Pittier525ec712016-12-07 17:51:26 +0100148 LOG.info("Booted second instance %s", instance_2nd)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900149
150 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100151 LOG.info("Getting timestamp in instance %s", instance_2nd)
Sean Dague20e98612016-01-06 14:33:28 -0500152 ip_instance_2nd = self.get_server_ip(instance_2nd)
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200153 timestamp2 = self.get_timestamp(ip_instance_2nd,
154 private_key=keypair['private_key'])
155 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900156
157 # snapshot a volume
Jordan Pittier525ec712016-12-07 17:51:26 +0100158 LOG.info("Creating snapshot from volume: %s", volume_origin['id'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000159 snapshot = self._create_snapshot_from_volume(volume_origin['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900160
161 # create a 3rd instance from snapshot
Jordan Pittier525ec712016-12-07 17:51:26 +0100162 LOG.info("Creating third instance from snapshot: %s", snapshot['id'])
Nuno Santosda899622016-11-17 12:32:53 -0500163 volume = self.create_volume(snapshot_id=snapshot['id'],
164 size=snapshot['size'])
165 LOG.info("Booting third instance from snapshot")
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200166 server_from_snapshot = (
lkuchlan8789c552017-01-08 11:40:27 +0200167 self._boot_instance_from_resource(source_id=volume['id'],
168 source_type='volume',
169 keypair=keypair,
170 security_group=security_group))
Nuno Santosda899622016-11-17 12:32:53 -0500171 LOG.info("Booted third instance %s", server_from_snapshot)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900172
173 # check the content of written file
Jordan Pittier525ec712016-12-07 17:51:26 +0100174 LOG.info("Logging into third instance to get timestamp: %s",
Sean Dague52abbd92016-03-01 09:38:09 -0500175 server_from_snapshot)
Sean Dague20e98612016-01-06 14:33:28 -0500176 server_from_snapshot_ip = self.get_server_ip(server_from_snapshot)
Alexander Gubanovc8829f82015-11-12 10:35:13 +0200177 timestamp3 = self.get_timestamp(server_from_snapshot_ip,
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200178 private_key=keypair['private_key'])
179 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900180
lkuchlan8789c552017-01-08 11:40:27 +0200181 @test.idempotent_id('05795fb2-b2a7-4c9f-8fac-ff25aedb1489')
182 @test.services('compute', 'image', 'volume')
183 def test_create_server_from_volume_snapshot(self):
184 # Create a volume from an image
185 boot_volume = self._create_volume_from_image()
186
187 # Create a snapshot
188 boot_snapshot = self._create_snapshot_from_volume(boot_volume['id'])
189
190 # Create a server from a volume snapshot
191 server = self._boot_instance_from_resource(
192 source_id=boot_snapshot['id'],
193 source_type='snapshot',
194 delete_on_termination=True)
195
196 server_info = self.servers_client.show_server(server['id'])['server']
197
198 # The created volume when creating a server from a snapshot
199 created_volume = server_info['os-extended-volumes:volumes_attached']
200
201 created_volume_info = self.volumes_client.show_volume(
202 created_volume[0]['id'])['volume']
203
204 # Verify the server was created from the snapshot
205 self.assertEqual(
206 boot_volume['volume_image_metadata']['image_id'],
207 created_volume_info['volume_image_metadata']['image_id'])
208 self.assertEqual(boot_snapshot['id'],
209 created_volume_info['snapshot_id'])
210 self.assertEqual(server['id'],
211 created_volume_info['attachments'][0]['server_id'])
212 self.assertEqual(created_volume[0]['id'],
213 created_volume_info['attachments'][0]['volume_id'])
214
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300215 @test.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
216 @test.services('compute', 'volume', 'image')
217 def test_create_ebs_image_and_check_boot(self):
218 # create an instance from volume
219 volume_origin = self._create_volume_from_image()
lkuchlan8789c552017-01-08 11:40:27 +0200220 instance = self._boot_instance_from_resource(
221 source_id=volume_origin['id'],
222 source_type='volume',
223 delete_on_termination=True)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300224 # create EBS image
zhuflf9d95722016-10-19 16:06:17 +0800225 image = self.create_server_snapshot(instance)
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300226
227 # delete instance
228 self._delete_server(instance)
229
230 # boot instance from EBS image
lanoux5fc14522015-09-21 08:17:35 +0000231 instance = self.create_server(
232 image_id=image['id'])
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300233 # just ensure that instance booted
234
235 # delete instance
236 self._delete_server(instance)
237
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100238
239class TestVolumeBootPatternV2(TestVolumeBootPattern):
lkuchlan8789c552017-01-08 11:40:27 +0200240 def _get_bdm(self, source_id, source_type, delete_on_termination=False):
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300241 bd_map_v2 = [{
lkuchlan8789c552017-01-08 11:40:27 +0200242 'uuid': source_id,
243 'source_type': source_type,
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300244 'destination_type': 'volume',
245 'boot_index': 0,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300246 'delete_on_termination': delete_on_termination}]
247 return {'block_device_mapping_v2': bd_map_v2}