blob: c5a4aaf06eae9dd01b89d770230e1d328ab2352b [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
Attila Fazekas70431ba2013-07-26 18:47:37 +020024from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040025from tempest.openstack.common import log as logging
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000026from tempest.scenario import manager
Attila Fazekas70431ba2013-07-26 18:47:37 +020027import tempest.test
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000028
29LOG = logging.getLogger(__name__)
30
31
32class TestStampPattern(manager.OfficialClientTest):
33 """
34 This test is for snapshotting an instance/volume and attaching the volume
35 created from snapshot to the instance booted from snapshot.
36 The following is the scenario outline:
37 1. Boot an instance "instance1"
38 2. Create a volume "volume1"
39 3. Attach volume1 to instance1
40 4. Create a filesystem on volume1
41 5. Mount volume1
42 6. Create a file which timestamp is written in volume1
43 7. Unmount volume1
44 8. Detach volume1 from instance1
45 9. Get a snapshot "snapshot_from_volume" of volume1
46 10. Get a snapshot "snapshot_from_instance" of instance1
47 11. Boot an instance "instance2" from snapshot_from_instance
48 12. Create a volume "volume2" from snapshot_from_volume
49 13. Attach volume2 to instance2
50 14. Check the existence of a file which created at 6. in volume2
51 """
52
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000053 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
54 self.status_timeout(self.volume_client.volume_snapshots,
55 volume_snapshot.id, status)
56
57 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090058 create_kwargs = {
59 'key_name': self.keypair.name
60 }
61 return self.create_server(self.compute_client, image=image_id,
62 create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000063
64 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090065 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000066
67 def _create_floating_ip(self):
68 floating_ip = self.compute_client.floating_ips.create()
69 self.addCleanup(floating_ip.delete)
70 return floating_ip
71
72 def _add_floating_ip(self, server, floating_ip):
73 server.add_floating_ip(floating_ip)
74
Attila Fazekas70431ba2013-07-26 18:47:37 +020075 def _ssh_to_server(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +090076 linux_client = self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000077 return linux_client.ssh_client
78
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000079 def _create_volume_snapshot(self, volume):
80 snapshot_name = rand_name('scenario-snapshot-')
81 volume_snapshots = self.volume_client.volume_snapshots
82 snapshot = volume_snapshots.create(
83 volume.id, display_name=snapshot_name)
84
85 def cleaner():
86 volume_snapshots.delete(snapshot)
87 try:
88 while volume_snapshots.get(snapshot.id):
89 time.sleep(1)
Attila Fazekas70431ba2013-07-26 18:47:37 +020090 except cinder_exceptions.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000091 pass
92 self.addCleanup(cleaner)
93 self._wait_for_volume_status(volume, 'available')
94 self._wait_for_volume_snapshot_status(snapshot, 'available')
95 self.assertEquals(snapshot_name, snapshot.display_name)
96 return snapshot
97
98 def _wait_for_volume_status(self, volume, status):
99 self.status_timeout(
100 self.volume_client.volumes, volume.id, status)
101
102 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900103 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000104
105 def _attach_volume(self, server, volume):
106 attach_volume_client = self.compute_client.volumes.create_server_volume
107 attached_volume = attach_volume_client(server.id,
108 volume.id,
109 '/dev/vdb')
110 self.assertEqual(volume.id, attached_volume.id)
111 self._wait_for_volume_status(attached_volume, 'in-use')
112
113 def _detach_volume(self, server, volume):
114 detach_volume_client = self.compute_client.volumes.delete_server_volume
115 detach_volume_client(server.id, volume.id)
116 self._wait_for_volume_status(volume, 'available')
117
Attila Fazekas70431ba2013-07-26 18:47:37 +0200118 def _wait_for_volume_availible_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900119 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200120 conf = self.config
121
122 def _func():
123 part = ssh.get_partitions()
124 LOG.debug("Partitions:%s" % part)
125 return 'vdb' in part
126
127 if not tempest.test.call_until_true(_func,
128 conf.compute.build_timeout,
129 conf.compute.build_interval):
130 raise exceptions.TimeoutException
131
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000132 def _create_timestamp(self, server_or_ip):
133 ssh_client = self._ssh_to_server(server_or_ip)
134 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
135 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
136 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
137 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
138 ssh_client.exec_command('sudo umount /mnt')
139
140 def _check_timestamp(self, server_or_ip):
141 ssh_client = self._ssh_to_server(server_or_ip)
142 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
143 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
144 self.assertEqual(self.timestamp, got_timestamp)
145
Giulio Fidente6cc3ade2013-08-23 17:21:03 +0200146 @testtools.skip("Skipped until the Bug #1205344 is resolved.")
Matthew Treinish2153ec02013-09-09 20:57:30 +0000147 @tempest.test.services('compute', 'network', 'volume', 'image')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000148 def test_stamp_pattern(self):
149 # prepare for booting a instance
150 self._add_keypair()
Ken'ichi Ohmichi3c1f5192013-08-19 19:02:15 +0900151 self.create_loginable_secgroup_rule()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000152
153 # boot an instance and create a timestamp file in it
154 volume = self._create_volume()
155 server = self._boot_image(self.config.compute.image_ref)
156
157 # create and add floating IP to server1
158 if self.config.compute.use_floatingip_for_ssh:
159 floating_ip_for_server = self._create_floating_ip()
160 self._add_floating_ip(server, floating_ip_for_server)
161 ip_for_server = floating_ip_for_server.ip
162 else:
163 ip_for_server = server
164
165 self._attach_volume(server, volume)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200166 self._wait_for_volume_availible_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000167 self._create_timestamp(ip_for_server)
168 self._detach_volume(server, volume)
169
170 # snapshot the volume
171 volume_snapshot = self._create_volume_snapshot(volume)
172
173 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900174 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000175
176 # create second volume from the snapshot(volume2)
177 volume_from_snapshot = self._create_volume(
178 snapshot_id=volume_snapshot.id)
179
180 # boot second instance from the snapshot(instance2)
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900181 server_from_snapshot = self._boot_image(snapshot_image.id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000182
183 # create and add floating IP to server_from_snapshot
184 if self.config.compute.use_floatingip_for_ssh:
185 floating_ip_for_snapshot = self._create_floating_ip()
186 self._add_floating_ip(server_from_snapshot,
187 floating_ip_for_snapshot)
188 ip_for_snapshot = floating_ip_for_snapshot.ip
189 else:
190 ip_for_snapshot = server_from_snapshot
191
192 # attach volume2 to instance2
193 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200194 self._wait_for_volume_availible_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000195
196 # check the existence of the timestamp file in the volume2
197 self._check_timestamp(ip_for_snapshot)