blob: 038d25140f3dc89c43d092ca078a69686371eb13 [file] [log] [blame]
Yuiko Takadaebcf6af2013-07-09 05:10:55 +00001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 NEC Corporation
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Sean Daguefe8a6092013-07-27 08:15:55 -040018import time
19
Attila Fazekas70431ba2013-07-26 18:47:37 +020020from cinderclient import exceptions as cinder_exceptions
Sean Daguefe8a6092013-07-27 08:15:55 -040021import testtools
22
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000023from tempest.common.utils.data_utils import rand_name
24from tempest.common.utils.linux.remote_client import RemoteClient
Attila Fazekas70431ba2013-07-26 18:47:37 +020025from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040026from tempest.openstack.common import log as logging
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000027from tempest.scenario import manager
Attila Fazekas70431ba2013-07-26 18:47:37 +020028import tempest.test
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000029
30LOG = logging.getLogger(__name__)
31
32
33class TestStampPattern(manager.OfficialClientTest):
34 """
35 This test is for snapshotting an instance/volume and attaching the volume
36 created from snapshot to the instance booted from snapshot.
37 The following is the scenario outline:
38 1. Boot an instance "instance1"
39 2. Create a volume "volume1"
40 3. Attach volume1 to instance1
41 4. Create a filesystem on volume1
42 5. Mount volume1
43 6. Create a file which timestamp is written in volume1
44 7. Unmount volume1
45 8. Detach volume1 from instance1
46 9. Get a snapshot "snapshot_from_volume" of volume1
47 10. Get a snapshot "snapshot_from_instance" of instance1
48 11. Boot an instance "instance2" from snapshot_from_instance
49 12. Create a volume "volume2" from snapshot_from_volume
50 13. Attach volume2 to instance2
51 14. Check the existence of a file which created at 6. in volume2
52 """
53
54 def _wait_for_server_status(self, server, status):
55 self.status_timeout(self.compute_client.servers,
56 server.id,
57 status)
58
59 def _wait_for_image_status(self, image_id, status):
60 self.status_timeout(self.image_client.images, image_id, status)
61
62 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
63 self.status_timeout(self.volume_client.volume_snapshots,
64 volume_snapshot.id, status)
65
66 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090067 create_kwargs = {
68 'key_name': self.keypair.name
69 }
70 return self.create_server(self.compute_client, image=image_id,
71 create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000072
73 def _add_keypair(self):
74 name = rand_name('scenario-keypair-')
75 self.keypair = self.compute_client.keypairs.create(name=name)
76 self.addCleanup(self.compute_client.keypairs.delete, self.keypair)
77 self.assertEqual(name, self.keypair.name)
78
79 def _create_floating_ip(self):
80 floating_ip = self.compute_client.floating_ips.create()
81 self.addCleanup(floating_ip.delete)
82 return floating_ip
83
84 def _add_floating_ip(self, server, floating_ip):
85 server.add_floating_ip(floating_ip)
86
87 def _create_security_group_rule(self):
88 sgs = self.compute_client.security_groups.list()
89 for sg in sgs:
90 if sg.name == 'default':
91 secgroup = sg
92
93 ruleset = {
94 # ssh
95 'ip_protocol': 'tcp',
96 'from_port': 22,
97 'to_port': 22,
98 'cidr': '0.0.0.0/0',
99 'group_id': None
100 }
101 sg_rule = self.compute_client.security_group_rules.create(secgroup.id,
102 **ruleset)
103 self.addCleanup(self.compute_client.security_group_rules.delete,
104 sg_rule.id)
105
Attila Fazekas70431ba2013-07-26 18:47:37 +0200106 def _remote_client_to_server(self, server_or_ip):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000107 if isinstance(server_or_ip, basestring):
108 ip = server_or_ip
109 else:
110 network_name_for_ssh = self.config.compute.network_for_ssh
111 ip = server_or_ip.networks[network_name_for_ssh][0]
112 username = self.config.scenario.ssh_user
113 linux_client = RemoteClient(ip,
114 username,
115 pkey=self.keypair.private_key)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200116 return linux_client
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000117
Attila Fazekas70431ba2013-07-26 18:47:37 +0200118 def _ssh_to_server(self, server_or_ip):
119 linux_client = self._remote_client_to_server(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000120 return linux_client.ssh_client
121
122 def _create_image(self, server):
123 snapshot_name = rand_name('scenario-snapshot-')
124 create_image_client = self.compute_client.servers.create_image
125 image_id = create_image_client(server, snapshot_name)
126 self.addCleanup(self.image_client.images.delete, image_id)
127 self._wait_for_server_status(server, 'ACTIVE')
128 self._wait_for_image_status(image_id, 'active')
129 snapshot_image = self.image_client.images.get(image_id)
130 self.assertEquals(snapshot_name, snapshot_image.name)
131 return image_id
132
133 def _create_volume_snapshot(self, volume):
134 snapshot_name = rand_name('scenario-snapshot-')
135 volume_snapshots = self.volume_client.volume_snapshots
136 snapshot = volume_snapshots.create(
137 volume.id, display_name=snapshot_name)
138
139 def cleaner():
140 volume_snapshots.delete(snapshot)
141 try:
142 while volume_snapshots.get(snapshot.id):
143 time.sleep(1)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200144 except cinder_exceptions.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000145 pass
146 self.addCleanup(cleaner)
147 self._wait_for_volume_status(volume, 'available')
148 self._wait_for_volume_snapshot_status(snapshot, 'available')
149 self.assertEquals(snapshot_name, snapshot.display_name)
150 return snapshot
151
152 def _wait_for_volume_status(self, volume, status):
153 self.status_timeout(
154 self.volume_client.volumes, volume.id, status)
155
156 def _create_volume(self, snapshot_id=None):
157 name = rand_name('scenario-volume-')
158 LOG.debug("volume display-name:%s" % name)
159 volume = self.volume_client.volumes.create(size=1,
160 display_name=name,
161 snapshot_id=snapshot_id)
162 LOG.debug("volume created:%s" % volume.display_name)
163
164 def cleaner():
165 self._wait_for_volume_status(volume, 'available')
166 self.volume_client.volumes.delete(volume)
167 self.addCleanup(cleaner)
168 self._wait_for_volume_status(volume, 'available')
169 self.assertEqual(name, volume.display_name)
170 return volume
171
172 def _attach_volume(self, server, volume):
173 attach_volume_client = self.compute_client.volumes.create_server_volume
174 attached_volume = attach_volume_client(server.id,
175 volume.id,
176 '/dev/vdb')
177 self.assertEqual(volume.id, attached_volume.id)
178 self._wait_for_volume_status(attached_volume, 'in-use')
179
180 def _detach_volume(self, server, volume):
181 detach_volume_client = self.compute_client.volumes.delete_server_volume
182 detach_volume_client(server.id, volume.id)
183 self._wait_for_volume_status(volume, 'available')
184
Attila Fazekas70431ba2013-07-26 18:47:37 +0200185 def _wait_for_volume_availible_on_the_system(self, server_or_ip):
186 ssh = self._remote_client_to_server(server_or_ip)
187 conf = self.config
188
189 def _func():
190 part = ssh.get_partitions()
191 LOG.debug("Partitions:%s" % part)
192 return 'vdb' in part
193
194 if not tempest.test.call_until_true(_func,
195 conf.compute.build_timeout,
196 conf.compute.build_interval):
197 raise exceptions.TimeoutException
198
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000199 def _create_timestamp(self, server_or_ip):
200 ssh_client = self._ssh_to_server(server_or_ip)
201 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
202 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
203 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
204 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
205 ssh_client.exec_command('sudo umount /mnt')
206
207 def _check_timestamp(self, server_or_ip):
208 ssh_client = self._ssh_to_server(server_or_ip)
209 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
210 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
211 self.assertEqual(self.timestamp, got_timestamp)
212
Sean Daguefe8a6092013-07-27 08:15:55 -0400213 @testtools.skip("Until Bug #1205344 is fixed")
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000214 def test_stamp_pattern(self):
215 # prepare for booting a instance
216 self._add_keypair()
217 self._create_security_group_rule()
218
219 # boot an instance and create a timestamp file in it
220 volume = self._create_volume()
221 server = self._boot_image(self.config.compute.image_ref)
222
223 # create and add floating IP to server1
224 if self.config.compute.use_floatingip_for_ssh:
225 floating_ip_for_server = self._create_floating_ip()
226 self._add_floating_ip(server, floating_ip_for_server)
227 ip_for_server = floating_ip_for_server.ip
228 else:
229 ip_for_server = server
230
231 self._attach_volume(server, volume)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200232 self._wait_for_volume_availible_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000233 self._create_timestamp(ip_for_server)
234 self._detach_volume(server, volume)
235
236 # snapshot the volume
237 volume_snapshot = self._create_volume_snapshot(volume)
238
239 # snapshot the instance
240 snapshot_image_id = self._create_image(server)
241
242 # create second volume from the snapshot(volume2)
243 volume_from_snapshot = self._create_volume(
244 snapshot_id=volume_snapshot.id)
245
246 # boot second instance from the snapshot(instance2)
247 server_from_snapshot = self._boot_image(snapshot_image_id)
248
249 # create and add floating IP to server_from_snapshot
250 if self.config.compute.use_floatingip_for_ssh:
251 floating_ip_for_snapshot = self._create_floating_ip()
252 self._add_floating_ip(server_from_snapshot,
253 floating_ip_for_snapshot)
254 ip_for_snapshot = floating_ip_for_snapshot.ip
255 else:
256 ip_for_snapshot = server_from_snapshot
257
258 # attach volume2 to instance2
259 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200260 self._wait_for_volume_availible_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000261
262 # check the existence of the timestamp file in the volume2
263 self._check_timestamp(ip_for_snapshot)