blob: 414305d1250ede481702143811eb7edaf08c7515 [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000013from oslo_log import log
Matthew Treinishc49fcbe2015-02-05 23:37:34 -050014
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
fujioka yuuichi636f8db2013-08-09 12:05:24 +090022
Nachi Ueno95b41282014-01-15 06:54:21 -080023LOG = log.getLogger(__name__)
24
25
Joseph Lanouxeef192f2014-08-01 14:32:53 +000026class TestVolumeBootPattern(manager.ScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090027
28 """
29 This test case attempts to reproduce the following steps:
30
31 * Create in Cinder some bootable volume importing a Glance image
32 * Boot an instance from the bootable volume
33 * Write content to the volume
34 * Delete an instance and Boot a new instance from the volume
35 * Check written content in the instance
36 * Create a volume snapshot while the instance is running
37 * Boot an additional instance from the new snapshot based volume
38 * Check written content in the instance booted from snapshot
39 """
JordanPbce55532014-03-19 12:10:32 +010040 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000041 def skip_checks(cls):
42 super(TestVolumeBootPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010043 if not CONF.volume_feature_enabled.snapshot:
44 raise cls.skipException("Cinder volume snapshots are disabled")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090045
46 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000047 img_uuid = CONF.compute.image_ref
Masayuki Igawa259c1132013-10-31 17:48:44 +090048 vol_name = data_utils.rand_name('volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090049 return self.create_volume(name=vol_name, imageRef=img_uuid)
50
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030051 def _get_bdm(self, vol_id, delete_on_termination=False):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090052 # NOTE(gfidente): the syntax for block_device_mapping is
53 # dev_name=id:type:size:delete_on_terminate
54 # where type needs to be "snap" if the server is booted
55 # from a snapshot, size instead can be safely left empty
Joseph Lanouxeef192f2014-08-01 14:32:53 +000056 bd_map = [{
57 'device_name': 'vda',
58 'volume_id': vol_id,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030059 'delete_on_termination': str(int(delete_on_termination))}]
60 return {'block_device_mapping': bd_map}
61
62 def _boot_instance_from_volume(self, vol_id, keypair=None,
63 security_group=None,
64 delete_on_termination=False):
65 create_kwargs = dict()
66 if keypair:
67 create_kwargs['key_name'] = keypair['name']
68 if security_group:
69 create_kwargs['security_groups'] = [
70 {'name': security_group['name']}]
71 create_kwargs.update(self._get_bdm(
72 vol_id, delete_on_termination=delete_on_termination))
Xavier Queralt249cac32014-03-05 13:51:39 +010073 return self.create_server(image='', create_kwargs=create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090074
75 def _create_snapshot_from_volume(self, vol_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090076 snap_name = data_utils.rand_name('snapshot')
melanie witt87412222015-01-21 04:32:17 +000077 snap = self.snapshots_client.create_snapshot(
Joseph Lanouxeef192f2014-08-01 14:32:53 +000078 volume_id=vol_id,
79 force=True,
John Warrenff7faf62015-08-17 16:59:06 +000080 display_name=snap_name)['snapshot']
Yaroslav Lobankov46a78c32015-04-08 13:45:27 +030081 self.addCleanup(
82 self.snapshots_client.wait_for_resource_deletion, snap['id'])
83 self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +000084 self.snapshots_client.wait_for_snapshot_status(snap['id'], 'available')
Ivan Kolodyazhnybcfc32e2015-08-06 13:31:36 +030085
86 # NOTE(e0ne): Cinder API v2 uses name instead of display_name
87 if 'display_name' in snap:
88 self.assertEqual(snap_name, snap['display_name'])
89 else:
90 self.assertEqual(snap_name, snap['name'])
91
fujioka yuuichi636f8db2013-08-09 12:05:24 +090092 return snap
93
94 def _create_volume_from_snapshot(self, snap_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090095 vol_name = data_utils.rand_name('volume')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090096 return self.create_volume(name=vol_name, snapshot_id=snap_id)
97
Alexander Gubanov59cc3032015-11-05 11:58:03 +020098 def _get_server_ip(self, server):
Matthew Treinish6c072292014-01-29 19:15:52 +000099 if CONF.compute.use_floatingip_for_ssh:
Matt Riedemann3ed09042015-09-01 10:48:24 -0700100 ip = self.create_floating_ip(server)['ip']
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900101 else:
Yaroslav Lobankovfc997072015-07-27 11:57:49 +0300102 ip = server
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200103 return ip
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900104
105 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000106 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000107 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900108
Chris Hoge7579c1a2015-02-26 14:12:15 -0800109 @test.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Sean Dague3c634d12015-04-27 12:09:19 -0400110 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900111 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900112 def test_volume_boot_pattern(self):
113 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300114 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900115
116 # create an instance from volume
117 volume_origin = self._create_volume_from_image()
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000118 instance_1st = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300119 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900120
121 # write content to volume on instance
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200122 ip_instance_1st = self._get_server_ip(instance_1st)
123 timestamp = self.create_timestamp(ip_instance_1st,
124 private_key=keypair['private_key'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900125
126 # delete instance
127 self._delete_server(instance_1st)
128
129 # create a 2nd instance from volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000130 instance_2nd = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300131 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900132
133 # check the content of written file
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200134 ip_instance_2nd = self._get_server_ip(instance_2nd)
135 timestamp2 = self.get_timestamp(ip_instance_2nd,
136 private_key=keypair['private_key'])
137 self.assertEqual(timestamp, timestamp2)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900138
139 # snapshot a volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000140 snapshot = self._create_snapshot_from_volume(volume_origin['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900141
142 # create a 3rd instance from snapshot
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000143 volume = self._create_volume_from_snapshot(snapshot['id'])
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300144 instance_from_snapshot = (
145 self._boot_instance_from_volume(volume['id'],
146 keypair, security_group))
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900147
148 # check the content of written file
Alexander Gubanov59cc3032015-11-05 11:58:03 +0200149 ip_instance_from_snapshot = self._get_server_ip(instance_from_snapshot)
150 timestamp3 = self.get_timestamp(ip_instance_from_snapshot,
151 private_key=keypair['private_key'])
152 self.assertEqual(timestamp, timestamp3)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900153
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300154 @test.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
155 @test.services('compute', 'volume', 'image')
156 def test_create_ebs_image_and_check_boot(self):
157 # create an instance from volume
158 volume_origin = self._create_volume_from_image()
159 instance = self._boot_instance_from_volume(volume_origin['id'],
160 delete_on_termination=True)
161 # create EBS image
162 name = data_utils.rand_name('image')
163 image = self.create_server_snapshot(instance, name=name)
164
165 # delete instance
166 self._delete_server(instance)
167
168 # boot instance from EBS image
169 instance = self.create_server(image=image['id'])
170 # just ensure that instance booted
171
172 # delete instance
173 self._delete_server(instance)
174
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100175
176class TestVolumeBootPatternV2(TestVolumeBootPattern):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300177 def _get_bdm(self, vol_id, delete_on_termination=False):
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300178 bd_map_v2 = [{
179 'uuid': vol_id,
180 'source_type': 'volume',
181 'destination_type': 'volume',
182 'boot_index': 0,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300183 'delete_on_termination': delete_on_termination}]
184 return {'block_device_mapping_v2': bd_map_v2}