blob: 5235871b3acc37a3e4e4159e80117fd1089efc19 [file] [log] [blame]
Yuiko Takadaebcf6af2013-07-09 05:10:55 +00001# Copyright 2013 NEC Corporation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Sean Daguefe8a6092013-07-27 08:15:55 -040016import time
17
Attila Fazekas70431ba2013-07-26 18:47:37 +020018from cinderclient import exceptions as cinder_exceptions
Sean Daguefe8a6092013-07-27 08:15:55 -040019
Masayuki Igawa259c1132013-10-31 17:48:44 +090020from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000021from tempest import config
Attila Fazekas70431ba2013-07-26 18:47:37 +020022from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040023from tempest.openstack.common import log as logging
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000024from tempest.scenario import manager
Attila Fazekas70431ba2013-07-26 18:47:37 +020025import tempest.test
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000026
Matthew Treinish6c072292014-01-29 19:15:52 +000027CONF = config.CONF
28
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000029LOG = 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
JordanPbce55532014-03-19 12:10:32 +010053 @classmethod
54 def setUpClass(cls):
55 super(TestStampPattern, cls).setUpClass()
56
57 if not CONF.volume_feature_enabled.snapshot:
58 raise cls.skipException("Cinder volume snapshots are disabled")
59
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000060 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
61 self.status_timeout(self.volume_client.volume_snapshots,
62 volume_snapshot.id, status)
63
64 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090065 create_kwargs = {
66 'key_name': self.keypair.name
67 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020068 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000069
70 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090071 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000072
73 def _create_floating_ip(self):
74 floating_ip = self.compute_client.floating_ips.create()
75 self.addCleanup(floating_ip.delete)
76 return floating_ip
77
78 def _add_floating_ip(self, server, floating_ip):
79 server.add_floating_ip(floating_ip)
80
Attila Fazekas70431ba2013-07-26 18:47:37 +020081 def _ssh_to_server(self, server_or_ip):
Elena Ezhova91db24e2014-02-28 20:47:10 +040082 return self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000083
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000084 def _create_volume_snapshot(self, volume):
Masayuki Igawa259c1132013-10-31 17:48:44 +090085 snapshot_name = data_utils.rand_name('scenario-snapshot-')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000086 volume_snapshots = self.volume_client.volume_snapshots
87 snapshot = volume_snapshots.create(
88 volume.id, display_name=snapshot_name)
89
90 def cleaner():
91 volume_snapshots.delete(snapshot)
92 try:
93 while volume_snapshots.get(snapshot.id):
94 time.sleep(1)
Attila Fazekas70431ba2013-07-26 18:47:37 +020095 except cinder_exceptions.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000096 pass
97 self.addCleanup(cleaner)
98 self._wait_for_volume_status(volume, 'available')
99 self._wait_for_volume_snapshot_status(snapshot, 'available')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700100 self.assertEqual(snapshot_name, snapshot.display_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000101 return snapshot
102
103 def _wait_for_volume_status(self, volume, status):
104 self.status_timeout(
105 self.volume_client.volumes, volume.id, status)
106
107 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900108 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000109
110 def _attach_volume(self, server, volume):
111 attach_volume_client = self.compute_client.volumes.create_server_volume
112 attached_volume = attach_volume_client(server.id,
113 volume.id,
114 '/dev/vdb')
115 self.assertEqual(volume.id, attached_volume.id)
116 self._wait_for_volume_status(attached_volume, 'in-use')
117
118 def _detach_volume(self, server, volume):
119 detach_volume_client = self.compute_client.volumes.delete_server_volume
120 detach_volume_client(server.id, volume.id)
121 self._wait_for_volume_status(volume, 'available')
122
Marc Solanasb15d8b62014-02-07 00:04:15 -0800123 def _wait_for_volume_available_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900124 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200125
126 def _func():
127 part = ssh.get_partitions()
128 LOG.debug("Partitions:%s" % part)
129 return 'vdb' in part
130
131 if not tempest.test.call_until_true(_func,
Matthew Treinish6c072292014-01-29 19:15:52 +0000132 CONF.compute.build_timeout,
133 CONF.compute.build_interval):
Attila Fazekas70431ba2013-07-26 18:47:37 +0200134 raise exceptions.TimeoutException
135
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000136 def _create_timestamp(self, server_or_ip):
137 ssh_client = self._ssh_to_server(server_or_ip)
138 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
139 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
140 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
141 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
142 ssh_client.exec_command('sudo umount /mnt')
143
144 def _check_timestamp(self, server_or_ip):
145 ssh_client = self._ssh_to_server(server_or_ip)
146 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
147 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
148 self.assertEqual(self.timestamp, got_timestamp)
149
Giulio Fidente386ac8f2013-10-07 14:29:27 +0200150 @tempest.test.skip_because(bug="1205344")
Matthew Treinish2153ec02013-09-09 20:57:30 +0000151 @tempest.test.services('compute', 'network', 'volume', 'image')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000152 def test_stamp_pattern(self):
153 # prepare for booting a instance
154 self._add_keypair()
Yair Friedeb69f3f2013-10-10 13:18:16 +0300155 self._create_loginable_secgroup_rule_nova()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000156
157 # boot an instance and create a timestamp file in it
158 volume = self._create_volume()
Matthew Treinish6c072292014-01-29 19:15:52 +0000159 server = self._boot_image(CONF.compute.image_ref)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000160
161 # create and add floating IP to server1
Matthew Treinish6c072292014-01-29 19:15:52 +0000162 if CONF.compute.use_floatingip_for_ssh:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000163 floating_ip_for_server = self._create_floating_ip()
164 self._add_floating_ip(server, floating_ip_for_server)
165 ip_for_server = floating_ip_for_server.ip
166 else:
167 ip_for_server = server
168
169 self._attach_volume(server, volume)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800170 self._wait_for_volume_available_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000171 self._create_timestamp(ip_for_server)
172 self._detach_volume(server, volume)
173
174 # snapshot the volume
175 volume_snapshot = self._create_volume_snapshot(volume)
176
177 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900178 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000179
180 # create second volume from the snapshot(volume2)
181 volume_from_snapshot = self._create_volume(
182 snapshot_id=volume_snapshot.id)
183
184 # boot second instance from the snapshot(instance2)
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900185 server_from_snapshot = self._boot_image(snapshot_image.id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000186
187 # create and add floating IP to server_from_snapshot
Matthew Treinish6c072292014-01-29 19:15:52 +0000188 if CONF.compute.use_floatingip_for_ssh:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000189 floating_ip_for_snapshot = self._create_floating_ip()
190 self._add_floating_ip(server_from_snapshot,
191 floating_ip_for_snapshot)
192 ip_for_snapshot = floating_ip_for_snapshot.ip
193 else:
194 ip_for_snapshot = server_from_snapshot
195
196 # attach volume2 to instance2
197 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800198 self._wait_for_volume_available_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000199
200 # check the existence of the timestamp file in the volume2
201 self._check_timestamp(ip_for_snapshot)