blob: 102e9a32eb4cc8aa375c38ddff5f02c13ff43278 [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
Matthew Treinish6c072292014-01-29 19:15:52 +000016from tempest import config
fujioka yuuichi636f8db2013-08-09 12:05:24 +090017from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090018from tempest import test
fujioka yuuichi636f8db2013-08-09 12:05:24 +090019
Matthew Treinish6c072292014-01-29 19:15:52 +000020CONF = config.CONF
fujioka yuuichi636f8db2013-08-09 12:05:24 +090021
Nachi Ueno95b41282014-01-15 06:54:21 -080022LOG = log.getLogger(__name__)
23
24
Joseph Lanouxeef192f2014-08-01 14:32:53 +000025class TestVolumeBootPattern(manager.ScenarioTest):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090026
27 """
28 This test case attempts to reproduce the following steps:
29
30 * Create in Cinder some bootable volume importing a Glance image
31 * Boot an instance from the bootable volume
32 * Write content to the volume
33 * Delete an instance and Boot a new instance from the volume
34 * Check written content in the instance
35 * Create a volume snapshot while the instance is running
36 * Boot an additional instance from the new snapshot based volume
37 * Check written content in the instance booted from snapshot
38 """
JordanPbce55532014-03-19 12:10:32 +010039 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000040 def skip_checks(cls):
41 super(TestVolumeBootPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010042 if not CONF.volume_feature_enabled.snapshot:
43 raise cls.skipException("Cinder volume snapshots are disabled")
fujioka yuuichi636f8db2013-08-09 12:05:24 +090044
45 def _create_volume_from_image(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000046 img_uuid = CONF.compute.image_ref
Masayuki Igawa259c1132013-10-31 17:48:44 +090047 vol_name = data_utils.rand_name('volume-origin')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090048 return self.create_volume(name=vol_name, imageRef=img_uuid)
49
Yaroslav Lobankov0089af52015-07-02 19:14:40 +030050 def _boot_instance_from_volume(self, vol_id, keypair, security_group):
fujioka yuuichi636f8db2013-08-09 12:05:24 +090051 # NOTE(gfidente): the syntax for block_device_mapping is
52 # dev_name=id:type:size:delete_on_terminate
53 # where type needs to be "snap" if the server is booted
54 # from a snapshot, size instead can be safely left empty
Joseph Lanouxeef192f2014-08-01 14:32:53 +000055 bd_map = [{
56 'device_name': 'vda',
57 'volume_id': vol_id,
58 'delete_on_termination': '0'}]
fujioka yuuichi636f8db2013-08-09 12:05:24 +090059 create_kwargs = {
60 'block_device_mapping': bd_map,
Joseph Lanouxeef192f2014-08-01 14:32:53 +000061 'key_name': keypair['name'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +030062 'security_groups': [{'name': security_group['name']}]
fujioka yuuichi636f8db2013-08-09 12:05:24 +090063 }
Xavier Queralt249cac32014-03-05 13:51:39 +010064 return self.create_server(image='', create_kwargs=create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090065
66 def _create_snapshot_from_volume(self, vol_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090067 snap_name = data_utils.rand_name('snapshot')
melanie witt87412222015-01-21 04:32:17 +000068 snap = self.snapshots_client.create_snapshot(
Joseph Lanouxeef192f2014-08-01 14:32:53 +000069 volume_id=vol_id,
70 force=True,
71 display_name=snap_name)
Yaroslav Lobankov46a78c32015-04-08 13:45:27 +030072 self.addCleanup(
73 self.snapshots_client.wait_for_resource_deletion, snap['id'])
74 self.addCleanup(self.snapshots_client.delete_snapshot, snap['id'])
Joseph Lanouxeef192f2014-08-01 14:32:53 +000075 self.snapshots_client.wait_for_snapshot_status(snap['id'], 'available')
76 self.assertEqual(snap_name, snap['display_name'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090077 return snap
78
79 def _create_volume_from_snapshot(self, snap_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090080 vol_name = data_utils.rand_name('volume')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090081 return self.create_volume(name=vol_name, snapshot_id=snap_id)
82
83 def _stop_instances(self, instances):
84 # NOTE(gfidente): two loops so we do not wait for the status twice
85 for i in instances:
Joseph Lanouxeef192f2014-08-01 14:32:53 +000086 self.servers_client.stop(i['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090087 for i in instances:
Joseph Lanouxeef192f2014-08-01 14:32:53 +000088 self.servers_client.wait_for_server_status(i['id'], 'SHUTOFF')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090089
90 def _detach_volumes(self, volumes):
91 # NOTE(gfidente): two loops so we do not wait for the status twice
92 for v in volumes:
Joseph Lanouxeef192f2014-08-01 14:32:53 +000093 self.volumes_client.detach_volume(v['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +090094 for v in volumes:
Joseph Lanouxeef192f2014-08-01 14:32:53 +000095 self.volumes_client.wait_for_volume_status(v['id'], 'available')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090096
97 def _ssh_to_server(self, server, keypair):
Matthew Treinish6c072292014-01-29 19:15:52 +000098 if CONF.compute.use_floatingip_for_ssh:
David Kranze4e3b412015-02-10 10:50:42 -050099 floating_ip = self.floating_ips_client.create_floating_ip()
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000100 self.addCleanup(self.delete_wrapper,
101 self.floating_ips_client.delete_floating_ip,
102 floating_ip['id'])
103 self.floating_ips_client.associate_floating_ip_to_server(
104 floating_ip['ip'], server['id'])
105 ip = floating_ip['ip']
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900106 else:
Matthew Treinish6c072292014-01-29 19:15:52 +0000107 network_name_for_ssh = CONF.compute.network_for_ssh
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900108 ip = server.networks[network_name_for_ssh][0]
109
JordanP3fe2dc32014-11-17 13:06:01 +0100110 return self.get_remote_client(ip, private_key=keypair['private_key'],
111 log_console_of_servers=[server])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900112
113 def _get_content(self, ssh_client):
114 return ssh_client.exec_command('cat /tmp/text')
115
116 def _write_text(self, ssh_client):
Ken'ichi Ohmichi07308f12015-03-23 00:24:28 +0000117 text = data_utils.rand_name('text')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900118 ssh_client.exec_command('echo "%s" > /tmp/text; sync' % (text))
119
120 return self._get_content(ssh_client)
121
122 def _delete_server(self, server):
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000123 self.servers_client.delete_server(server['id'])
124 self.servers_client.wait_for_server_termination(server['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900125
126 def _check_content_of_written_file(self, ssh_client, expected):
127 actual = self._get_content(ssh_client)
128 self.assertEqual(expected, actual)
129
Chris Hoge7579c1a2015-02-26 14:12:15 -0800130 @test.idempotent_id('557cd2c2-4eb8-4dce-98be-f86765ff311b')
Sean Dague3c634d12015-04-27 12:09:19 -0400131 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900132 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900133 def test_volume_boot_pattern(self):
134 keypair = self.create_keypair()
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300135 security_group = self._create_security_group()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900136
137 # create an instance from volume
138 volume_origin = self._create_volume_from_image()
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000139 instance_1st = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300140 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900141
142 # write content to volume on instance
143 ssh_client_for_instance_1st = self._ssh_to_server(instance_1st,
144 keypair)
145 text = self._write_text(ssh_client_for_instance_1st)
146
147 # delete instance
148 self._delete_server(instance_1st)
149
150 # create a 2nd instance from volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000151 instance_2nd = self._boot_instance_from_volume(volume_origin['id'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300152 keypair, security_group)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900153
154 # check the content of written file
155 ssh_client_for_instance_2nd = self._ssh_to_server(instance_2nd,
156 keypair)
157 self._check_content_of_written_file(ssh_client_for_instance_2nd, text)
158
159 # snapshot a volume
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000160 snapshot = self._create_snapshot_from_volume(volume_origin['id'])
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900161
162 # create a 3rd instance from snapshot
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000163 volume = self._create_volume_from_snapshot(snapshot['id'])
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300164 instance_from_snapshot = (
165 self._boot_instance_from_volume(volume['id'],
166 keypair, security_group))
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900167
168 # check the content of written file
169 ssh_client = self._ssh_to_server(instance_from_snapshot, keypair)
170 self._check_content_of_written_file(ssh_client, text)
171
172 # NOTE(gfidente): ensure resources are in clean state for
173 # deletion operations to succeed
174 self._stop_instances([instance_2nd, instance_from_snapshot])
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100175
176
177class TestVolumeBootPatternV2(TestVolumeBootPattern):
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300178 def _boot_instance_from_volume(self, vol_id, keypair, security_group):
179 bd_map_v2 = [{
180 'uuid': vol_id,
181 'source_type': 'volume',
182 'destination_type': 'volume',
183 'boot_index': 0,
184 'delete_on_termination': False}]
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100185 create_kwargs = {
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300186 'block_device_mapping_v2': bd_map_v2,
Joseph Lanouxeef192f2014-08-01 14:32:53 +0000187 'key_name': keypair['name'],
Yaroslav Lobankov0089af52015-07-02 19:14:40 +0300188 'security_groups': [{'name': security_group['name']}]
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100189 }
190 return self.create_server(image='', create_kwargs=create_kwargs)