blob: 5a14a94507b07e3918a5247a7de19f638e3cea94 [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
Matt Riedemannebaf2452015-08-31 07:12:51 -070014from tempest_lib import decorators
Matthew Treinishc49fcbe2015-02-05 23:37:34 -050015
Fei Long Wangd39431f2015-05-14 11:30:48 +120016from tempest.common.utils import data_utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000017from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
fujioka yuuichi636f8db2013-08-09 12:05:24 +090019from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090020from tempest import test
fujioka yuuichi636f8db2013-08-09 12:05:24 +090021
Matthew Treinish6c072292014-01-29 19:15:52 +000022CONF = config.CONF
fujioka yuuichi636f8db2013-08-09 12:05:24 +090023
Nachi Ueno95b41282014-01-15 06:54:21 -080024LOG = log.getLogger(__name__)
25
26
Joseph Lanouxeef192f2014-08-01 14:32:53 +000027class TestVolumeBootPattern(manager.ScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090028
29 """
30 This test case attempts to reproduce the following steps:
31
32 * Create in Cinder some bootable volume importing a Glance image
33 * Boot an instance from the bootable volume
34 * Write content to the volume
35 * Delete an instance and Boot a new instance from the volume
36 * Check written content in the instance
37 * Create a volume snapshot while the instance is running
38 * Boot an additional instance from the new snapshot based volume
39 * Check written content in the instance booted from snapshot
40 """
JordanPbce55532014-03-19 12:10:32 +010041 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000042 def skip_checks(cls):
43 super(TestVolumeBootPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010044 if not CONF.volume_feature_enabled.snapshot:
45 raise cls.skipException("Cinder volume snapshots are disabled")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090046
47 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000048 img_uuid = CONF.compute.image_ref
Masayuki Igawa259c1132013-10-31 17:48:44 +090049 vol_name = data_utils.rand_name('volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090050 return self.create_volume(name=vol_name, imageRef=img_uuid)
51
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030052 def _get_bdm(self, vol_id, delete_on_termination=False):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090053 # NOTE(gfidente): the syntax for block_device_mapping is
54 # dev_name=id:type:size:delete_on_terminate
55 # where type needs to be "snap" if the server is booted
56 # from a snapshot, size instead can be safely left empty
Joseph Lanouxeef192f2014-08-01 14:32:53 +000057 bd_map = [{
58 'device_name': 'vda',
59 'volume_id': vol_id,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +030060 'delete_on_termination': str(int(delete_on_termination))}]
61 return {'block_device_mapping': bd_map}
62
63 def _boot_instance_from_volume(self, vol_id, keypair=None,
64 security_group=None,
65 delete_on_termination=False):
66 create_kwargs = dict()
67 if keypair:
68 create_kwargs['key_name'] = keypair['name']
69 if security_group:
70 create_kwargs['security_groups'] = [
71 {'name': security_group['name']}]
72 create_kwargs.update(self._get_bdm(
73 vol_id, delete_on_termination=delete_on_termination))
Xavier Queralt249cac32014-03-05 13:51:39 +010074 return self.create_server(image='', create_kwargs=create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090075
76 def _create_snapshot_from_volume(self, vol_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090077 snap_name = data_utils.rand_name('snapshot')
melanie witt87412222015-01-21 04:32:17 +000078 snap = self.snapshots_client.create_snapshot(
Joseph Lanouxeef192f2014-08-01 14:32:53 +000079 volume_id=vol_id,
80 force=True,
John Warrenff7faf62015-08-17 16:59:06 +000081 display_name=snap_name)['snapshot']
Yaroslav Lobankov46a78c32015-04-08 13:45:27 +030082 self.addCleanup(
83 self.snapshots_client.wait_for_resource_deletion, snap['id'])
84 self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +000085 self.snapshots_client.wait_for_snapshot_status(snap['id'], 'available')
86 self.assertEqual(snap_name, snap['display_name'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090087 return snap
88
89 def _create_volume_from_snapshot(self, snap_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090090 vol_name = data_utils.rand_name('volume')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090091 return self.create_volume(name=vol_name, snapshot_id=snap_id)
92
93 def _stop_instances(self, instances):
94 # NOTE(gfidente): two loops so we do not wait for the status twice
95 for i in instances:
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +000096 self.servers_client.stop_server(i['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090097 for i in instances:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000098 waiters.wait_for_server_status(self.servers_client,
99 i['id'], 'SHUTOFF')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900100
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900101 def _ssh_to_server(self, server, keypair):
Matthew Treinish6c072292014-01-29 19:15:52 +0000102 if CONF.compute.use_floatingip_for_ssh:
ghanshyam9a3a9a22015-08-18 17:03:55 +0900103 floating_ip = (self.floating_ips_client.create_floating_ip()
104 ['floating_ip'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000105 self.addCleanup(self.delete_wrapper,
106 self.floating_ips_client.delete_floating_ip,
107 floating_ip['id'])
108 self.floating_ips_client.associate_floating_ip_to_server(
109 floating_ip['ip'], server['id'])
110 ip = floating_ip['ip']
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900111 else:
Yaroslav Lobankovfc997072015-07-27 11:57:49 +0300112 ip = server
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900113
JordanP3fe2dc32014-11-17 13:06:01 +0100114 return self.get_remote_client(ip, private_key=keypair['private_key'],
115 log_console_of_servers=[server])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900116
117 def _get_content(self, ssh_client):
118 return ssh_client.exec_command('cat /tmp/text')
119
120 def _write_text(self, ssh_client):
Ken'ichi Ohmichi07308f12015-03-23 00:24:28 +0000121 text = data_utils.rand_name('text')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900122 ssh_client.exec_command('echo "%s" > /tmp/text; sync' % (text))
123
124 return self._get_content(ssh_client)
125
126 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000127 self.servers_client.delete_server(server['id'])
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000128 waiters.wait_for_server_termination(self.servers_client, server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900129
130 def _check_content_of_written_file(self, ssh_client, expected):
131 actual = self._get_content(ssh_client)
132 self.assertEqual(expected, actual)
133
Chris Hoge7579c1a2015-02-26 14:12:15 -0800134 @test.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Sean Dague3c634d12015-04-27 12:09:19 -0400135 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900136 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900137 def test_volume_boot_pattern(self):
138 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300139 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900140
141 # create an instance from volume
142 volume_origin = self._create_volume_from_image()
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000143 instance_1st = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300144 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900145
146 # write content to volume on instance
147 ssh_client_for_instance_1st = self._ssh_to_server(instance_1st,
148 keypair)
149 text = self._write_text(ssh_client_for_instance_1st)
150
151 # delete instance
152 self._delete_server(instance_1st)
153
154 # create a 2nd instance from volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000155 instance_2nd = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300156 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900157
158 # check the content of written file
159 ssh_client_for_instance_2nd = self._ssh_to_server(instance_2nd,
160 keypair)
161 self._check_content_of_written_file(ssh_client_for_instance_2nd, text)
162
163 # snapshot a volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000164 snapshot = self._create_snapshot_from_volume(volume_origin['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900165
166 # create a 3rd instance from snapshot
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000167 volume = self._create_volume_from_snapshot(snapshot['id'])
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300168 instance_from_snapshot = (
169 self._boot_instance_from_volume(volume['id'],
170 keypair, security_group))
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900171
172 # check the content of written file
173 ssh_client = self._ssh_to_server(instance_from_snapshot, keypair)
174 self._check_content_of_written_file(ssh_client, text)
175
176 # NOTE(gfidente): ensure resources are in clean state for
177 # deletion operations to succeed
178 self._stop_instances([instance_2nd, instance_from_snapshot])
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100179
Matt Riedemannebaf2452015-08-31 07:12:51 -0700180 @decorators.skip_because(bug='1489581')
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300181 @test.idempotent_id('36c34c67-7b54-4b59-b188-02a2f458a63b')
182 @test.services('compute', 'volume', 'image')
183 def test_create_ebs_image_and_check_boot(self):
184 # create an instance from volume
185 volume_origin = self._create_volume_from_image()
186 instance = self._boot_instance_from_volume(volume_origin['id'],
187 delete_on_termination=True)
188 # create EBS image
189 name = data_utils.rand_name('image')
190 image = self.create_server_snapshot(instance, name=name)
191
192 # delete instance
193 self._delete_server(instance)
194
195 # boot instance from EBS image
196 instance = self.create_server(image=image['id'])
197 # just ensure that instance booted
198
199 # delete instance
200 self._delete_server(instance)
201
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100202
203class TestVolumeBootPatternV2(TestVolumeBootPattern):
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300204 def _get_bdm(self, vol_id, delete_on_termination=False):
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300205 bd_map_v2 = [{
206 'uuid': vol_id,
207 'source_type': 'volume',
208 'destination_type': 'volume',
209 'boot_index': 0,
Andrey Pavlovc8bd4b12015-08-17 10:20:17 +0300210 'delete_on_termination': delete_on_termination}]
211 return {'block_device_mapping_v2': bd_map_v2}