blob: a8cedc78bffb93a68df8651f8c58c81334e74163 [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 -040021
Masayuki Igawa259c1132013-10-31 17:48:44 +090022from tempest.common.utils import data_utils
Attila Fazekas70431ba2013-07-26 18:47:37 +020023from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040024from tempest.openstack.common import log as logging
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000025from tempest.scenario import manager
Attila Fazekas70431ba2013-07-26 18:47:37 +020026import tempest.test
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000027
28LOG = logging.getLogger(__name__)
29
30
31class TestStampPattern(manager.OfficialClientTest):
32 """
33 This test is for snapshotting an instance/volume and attaching the volume
34 created from snapshot to the instance booted from snapshot.
35 The following is the scenario outline:
36 1. Boot an instance "instance1"
37 2. Create a volume "volume1"
38 3. Attach volume1 to instance1
39 4. Create a filesystem on volume1
40 5. Mount volume1
41 6. Create a file which timestamp is written in volume1
42 7. Unmount volume1
43 8. Detach volume1 from instance1
44 9. Get a snapshot "snapshot_from_volume" of volume1
45 10. Get a snapshot "snapshot_from_instance" of instance1
46 11. Boot an instance "instance2" from snapshot_from_instance
47 12. Create a volume "volume2" from snapshot_from_volume
48 13. Attach volume2 to instance2
49 14. Check the existence of a file which created at 6. in volume2
50 """
51
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000052 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
53 self.status_timeout(self.volume_client.volume_snapshots,
54 volume_snapshot.id, status)
55
56 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090057 create_kwargs = {
58 'key_name': self.keypair.name
59 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020060 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000061
62 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090063 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000064
65 def _create_floating_ip(self):
66 floating_ip = self.compute_client.floating_ips.create()
67 self.addCleanup(floating_ip.delete)
68 return floating_ip
69
70 def _add_floating_ip(self, server, floating_ip):
71 server.add_floating_ip(floating_ip)
72
Attila Fazekas70431ba2013-07-26 18:47:37 +020073 def _ssh_to_server(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +090074 linux_client = self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000075 return linux_client.ssh_client
76
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000077 def _create_volume_snapshot(self, volume):
Masayuki Igawa259c1132013-10-31 17:48:44 +090078 snapshot_name = data_utils.rand_name('scenario-snapshot-')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000079 volume_snapshots = self.volume_client.volume_snapshots
80 snapshot = volume_snapshots.create(
81 volume.id, display_name=snapshot_name)
82
83 def cleaner():
84 volume_snapshots.delete(snapshot)
85 try:
86 while volume_snapshots.get(snapshot.id):
87 time.sleep(1)
Attila Fazekas70431ba2013-07-26 18:47:37 +020088 except cinder_exceptions.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000089 pass
90 self.addCleanup(cleaner)
91 self._wait_for_volume_status(volume, 'available')
92 self._wait_for_volume_snapshot_status(snapshot, 'available')
Chang Bo Guofc77e932013-09-16 17:38:26 -070093 self.assertEqual(snapshot_name, snapshot.display_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000094 return snapshot
95
96 def _wait_for_volume_status(self, volume, status):
97 self.status_timeout(
98 self.volume_client.volumes, volume.id, status)
99
100 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900101 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000102
103 def _attach_volume(self, server, volume):
104 attach_volume_client = self.compute_client.volumes.create_server_volume
105 attached_volume = attach_volume_client(server.id,
106 volume.id,
107 '/dev/vdb')
108 self.assertEqual(volume.id, attached_volume.id)
109 self._wait_for_volume_status(attached_volume, 'in-use')
110
111 def _detach_volume(self, server, volume):
112 detach_volume_client = self.compute_client.volumes.delete_server_volume
113 detach_volume_client(server.id, volume.id)
114 self._wait_for_volume_status(volume, 'available')
115
Attila Fazekas70431ba2013-07-26 18:47:37 +0200116 def _wait_for_volume_availible_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900117 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200118 conf = self.config
119
120 def _func():
121 part = ssh.get_partitions()
122 LOG.debug("Partitions:%s" % part)
123 return 'vdb' in part
124
125 if not tempest.test.call_until_true(_func,
126 conf.compute.build_timeout,
127 conf.compute.build_interval):
128 raise exceptions.TimeoutException
129
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000130 def _create_timestamp(self, server_or_ip):
131 ssh_client = self._ssh_to_server(server_or_ip)
132 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
133 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
134 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
135 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
136 ssh_client.exec_command('sudo umount /mnt')
137
138 def _check_timestamp(self, server_or_ip):
139 ssh_client = self._ssh_to_server(server_or_ip)
140 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
141 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
142 self.assertEqual(self.timestamp, got_timestamp)
143
Giulio Fidente386ac8f2013-10-07 14:29:27 +0200144 @tempest.test.skip_because(bug="1205344")
Matthew Treinish2153ec02013-09-09 20:57:30 +0000145 @tempest.test.services('compute', 'network', 'volume', 'image')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000146 def test_stamp_pattern(self):
147 # prepare for booting a instance
148 self._add_keypair()
Ken'ichi Ohmichi3c1f5192013-08-19 19:02:15 +0900149 self.create_loginable_secgroup_rule()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000150
151 # boot an instance and create a timestamp file in it
152 volume = self._create_volume()
153 server = self._boot_image(self.config.compute.image_ref)
154
155 # create and add floating IP to server1
156 if self.config.compute.use_floatingip_for_ssh:
157 floating_ip_for_server = self._create_floating_ip()
158 self._add_floating_ip(server, floating_ip_for_server)
159 ip_for_server = floating_ip_for_server.ip
160 else:
161 ip_for_server = server
162
163 self._attach_volume(server, volume)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200164 self._wait_for_volume_availible_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000165 self._create_timestamp(ip_for_server)
166 self._detach_volume(server, volume)
167
168 # snapshot the volume
169 volume_snapshot = self._create_volume_snapshot(volume)
170
171 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900172 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000173
174 # create second volume from the snapshot(volume2)
175 volume_from_snapshot = self._create_volume(
176 snapshot_id=volume_snapshot.id)
177
178 # boot second instance from the snapshot(instance2)
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900179 server_from_snapshot = self._boot_image(snapshot_image.id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000180
181 # create and add floating IP to server_from_snapshot
182 if self.config.compute.use_floatingip_for_ssh:
183 floating_ip_for_snapshot = self._create_floating_ip()
184 self._add_floating_ip(server_from_snapshot,
185 floating_ip_for_snapshot)
186 ip_for_snapshot = floating_ip_for_snapshot.ip
187 else:
188 ip_for_snapshot = server_from_snapshot
189
190 # attach volume2 to instance2
191 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200192 self._wait_for_volume_availible_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000193
194 # check the existence of the timestamp file in the volume2
195 self._check_timestamp(ip_for_snapshot)