blob: bf5d1f6037fefb7979c204a96295493d2f8a350b [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
Matthew Treinishb7144eb2013-12-13 22:57:35 +000013from cinderclient import exceptions as cinder_exc
14
Masayuki Igawa259c1132013-10-31 17:48:44 +090015from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000016from tempest import config
Nachi Ueno95b41282014-01-15 06:54:21 -080017from tempest.openstack.common import log
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
fujioka yuuichi636f8db2013-08-09 12:05:24 +090026class TestVolumeBootPattern(manager.OfficialClientTest):
27
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
41 def setUpClass(cls):
42 super(TestVolumeBootPattern, cls).setUpClass()
43
44 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
52 def _boot_instance_from_volume(self, vol_id, keypair):
53 # 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
57 bd_map = {
58 'vda': vol_id + ':::0'
59 }
Grishkin0f1e11c2014-05-04 20:44:52 +040060 security_groups = [self.security_group.name]
fujioka yuuichi636f8db2013-08-09 12:05:24 +090061 create_kwargs = {
62 'block_device_mapping': bd_map,
Grishkin0f1e11c2014-05-04 20:44:52 +040063 'key_name': keypair.name,
64 'security_groups': security_groups
fujioka yuuichi636f8db2013-08-09 12:05:24 +090065 }
Xavier Queralt249cac32014-03-05 13:51:39 +010066 return self.create_server(image='', create_kwargs=create_kwargs)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090067
68 def _create_snapshot_from_volume(self, vol_id):
69 volume_snapshots = self.volume_client.volume_snapshots
Masayuki Igawa259c1132013-10-31 17:48:44 +090070 snap_name = data_utils.rand_name('snapshot')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090071 snap = volume_snapshots.create(volume_id=vol_id,
72 force=True,
73 display_name=snap_name)
Matthew Treinishb7144eb2013-12-13 22:57:35 +000074 self.addCleanup_with_wait(self.volume_client.volume_snapshots, snap.id,
75 exc_type=cinder_exc.NotFound)
fujioka yuuichi636f8db2013-08-09 12:05:24 +090076 self.status_timeout(volume_snapshots,
77 snap.id,
78 'available')
79 return snap
80
81 def _create_volume_from_snapshot(self, snap_id):
Masayuki Igawa259c1132013-10-31 17:48:44 +090082 vol_name = data_utils.rand_name('volume')
fujioka yuuichi636f8db2013-08-09 12:05:24 +090083 return self.create_volume(name=vol_name, snapshot_id=snap_id)
84
85 def _stop_instances(self, instances):
86 # NOTE(gfidente): two loops so we do not wait for the status twice
87 for i in instances:
88 self.compute_client.servers.stop(i)
89 for i in instances:
90 self.status_timeout(self.compute_client.servers,
91 i.id,
92 'SHUTOFF')
93
94 def _detach_volumes(self, volumes):
95 # NOTE(gfidente): two loops so we do not wait for the status twice
96 for v in volumes:
97 self.volume_client.volumes.detach(v)
98 for v in volumes:
99 self.status_timeout(self.volume_client.volumes,
100 v.id,
101 'available')
102
103 def _ssh_to_server(self, server, keypair):
Matthew Treinish6c072292014-01-29 19:15:52 +0000104 if CONF.compute.use_floatingip_for_ssh:
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900105 floating_ip = self.compute_client.floating_ips.create()
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000106 self.addCleanup(self.delete_wrapper, floating_ip)
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900107 server.add_floating_ip(floating_ip)
108 ip = floating_ip.ip
109 else:
Matthew Treinish6c072292014-01-29 19:15:52 +0000110 network_name_for_ssh = CONF.compute.network_for_ssh
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900111 ip = server.networks[network_name_for_ssh][0]
112
Nachi Ueno95b41282014-01-15 06:54:21 -0800113 try:
Elena Ezhova91db24e2014-02-28 20:47:10 +0400114 return self.get_remote_client(
Nachi Ueno95b41282014-01-15 06:54:21 -0800115 ip,
116 private_key=keypair.private_key)
117 except Exception:
118 LOG.exception('ssh to server failed')
119 self._log_console_output()
120 raise
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900121
122 def _get_content(self, ssh_client):
123 return ssh_client.exec_command('cat /tmp/text')
124
125 def _write_text(self, ssh_client):
Masayuki Igawa259c1132013-10-31 17:48:44 +0900126 text = data_utils.rand_name('text-')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900127 ssh_client.exec_command('echo "%s" > /tmp/text; sync' % (text))
128
129 return self._get_content(ssh_client)
130
131 def _delete_server(self, server):
132 self.compute_client.servers.delete(server)
133 self.delete_timeout(self.compute_client.servers, server.id)
134
135 def _check_content_of_written_file(self, ssh_client, expected):
136 actual = self._get_content(ssh_client)
137 self.assertEqual(expected, actual)
138
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900139 @test.services('compute', 'volume', 'image')
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900140 def test_volume_boot_pattern(self):
141 keypair = self.create_keypair()
Grishkin0f1e11c2014-05-04 20:44:52 +0400142 self.security_group = self._create_security_group_nova()
fujioka yuuichi636f8db2013-08-09 12:05:24 +0900143
144 # create an instance from volume
145 volume_origin = self._create_volume_from_image()
146 instance_1st = self._boot_instance_from_volume(volume_origin.id,
147 keypair)
148
149 # write content to volume on instance
150 ssh_client_for_instance_1st = self._ssh_to_server(instance_1st,
151 keypair)
152 text = self._write_text(ssh_client_for_instance_1st)
153
154 # delete instance
155 self._delete_server(instance_1st)
156
157 # create a 2nd instance from volume
158 instance_2nd = self._boot_instance_from_volume(volume_origin.id,
159 keypair)
160
161 # check the content of written file
162 ssh_client_for_instance_2nd = self._ssh_to_server(instance_2nd,
163 keypair)
164 self._check_content_of_written_file(ssh_client_for_instance_2nd, text)
165
166 # snapshot a volume
167 snapshot = self._create_snapshot_from_volume(volume_origin.id)
168
169 # create a 3rd instance from snapshot
170 volume = self._create_volume_from_snapshot(snapshot.id)
171 instance_from_snapshot = self._boot_instance_from_volume(volume.id,
172 keypair)
173
174 # check the content of written file
175 ssh_client = self._ssh_to_server(instance_from_snapshot, keypair)
176 self._check_content_of_written_file(ssh_client, text)
177
178 # NOTE(gfidente): ensure resources are in clean state for
179 # deletion operations to succeed
180 self._stop_instances([instance_2nd, instance_from_snapshot])
181 self._detach_volumes([volume_origin, volume])
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100182
183
184class TestVolumeBootPatternV2(TestVolumeBootPattern):
185 def _boot_instance_from_volume(self, vol_id, keypair):
186 bdms = [{'uuid': vol_id, 'source_type': 'volume',
187 'destination_type': 'volume', 'boot_index': 0,
188 'delete_on_termination': False}]
Grishkin0f1e11c2014-05-04 20:44:52 +0400189 security_groups = [self.security_group.name]
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100190 create_kwargs = {
191 'block_device_mapping_v2': bdms,
Grishkin0f1e11c2014-05-04 20:44:52 +0400192 'key_name': keypair.name,
193 'security_groups': security_groups
Nikola Dipanov7cff03f2014-03-12 14:06:25 +0100194 }
195 return self.create_server(image='', create_kwargs=create_kwargs)