blob: 91e4657c3a9ee23a3ce54376ba12eee5a47e739a [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000018from oslo_log import log as logging
Matthew Treinishc49fcbe2015-02-05 23:37:34 -050019from tempest_lib import decorators
Masayuki Igawabfa07602015-01-20 18:47:17 +090020from tempest_lib import exceptions as lib_exc
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -070021import testtools
Sean Daguefe8a6092013-07-27 08:15:55 -040022
Fei Long Wangd39431f2015-05-14 11:30:48 +120023from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000024from tempest import config
Attila Fazekas70431ba2013-07-26 18:47:37 +020025from tempest import exceptions
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000026from tempest.scenario import manager
Chris Hoge7579c1a2015-02-26 14:12:15 -080027from tempest import test
Attila Fazekas70431ba2013-07-26 18:47:37 +020028import tempest.test
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000029
Matthew Treinish6c072292014-01-29 19:15:52 +000030CONF = config.CONF
31
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000032LOG = logging.getLogger(__name__)
33
34
Andrea Frittolic0651b22014-09-17 16:34:01 +010035class TestStampPattern(manager.ScenarioTest):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000036 """
37 This test is for snapshotting an instance/volume and attaching the volume
38 created from snapshot to the instance booted from snapshot.
39 The following is the scenario outline:
40 1. Boot an instance "instance1"
41 2. Create a volume "volume1"
42 3. Attach volume1 to instance1
43 4. Create a filesystem on volume1
44 5. Mount volume1
45 6. Create a file which timestamp is written in volume1
46 7. Unmount volume1
47 8. Detach volume1 from instance1
48 9. Get a snapshot "snapshot_from_volume" of volume1
49 10. Get a snapshot "snapshot_from_instance" of instance1
50 11. Boot an instance "instance2" from snapshot_from_instance
51 12. Create a volume "volume2" from snapshot_from_volume
52 13. Attach volume2 to instance2
53 14. Check the existence of a file which created at 6. in volume2
54 """
55
JordanPbce55532014-03-19 12:10:32 +010056 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +000057 def skip_checks(cls):
58 super(TestStampPattern, cls).skip_checks()
JordanPbce55532014-03-19 12:10:32 +010059 if not CONF.volume_feature_enabled.snapshot:
60 raise cls.skipException("Cinder volume snapshots are disabled")
61
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000062 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
Andrea Frittolic0651b22014-09-17 16:34:01 +010063 self.snapshots_client.wait_for_snapshot_status(volume_snapshot['id'],
64 status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000065
66 def _boot_image(self, image_id):
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +000067 security_groups = [{'name': self.security_group['name']}]
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090068 create_kwargs = {
Andrea Frittolic0651b22014-09-17 16:34:01 +010069 'key_name': self.keypair['name'],
Grishkin0f1e11c2014-05-04 20:44:52 +040070 'security_groups': security_groups
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090071 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020072 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000073
74 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090075 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000076
Attila Fazekas70431ba2013-07-26 18:47:37 +020077 def _ssh_to_server(self, server_or_ip):
Elena Ezhova91db24e2014-02-28 20:47:10 +040078 return self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000079
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000080 def _create_volume_snapshot(self, volume):
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +000081 snapshot_name = data_utils.rand_name('scenario-snapshot')
Andrea Frittolic0651b22014-09-17 16:34:01 +010082 _, snapshot = self.snapshots_client.create_snapshot(
John Warrenff7faf62015-08-17 16:59:06 +000083 volume['id'], display_name=snapshot_name)['snapshot']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000084
85 def cleaner():
Andrea Frittolic0651b22014-09-17 16:34:01 +010086 self.snapshots_client.delete_snapshot(snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000087 try:
John Warrenff7faf62015-08-17 16:59:06 +000088 while self.snapshots_client.show_snapshot(
89 snapshot['id'])['snapshot']:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000090 time.sleep(1)
Masayuki Igawabfa07602015-01-20 18:47:17 +090091 except lib_exc.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000092 pass
93 self.addCleanup(cleaner)
94 self._wait_for_volume_status(volume, 'available')
Andrea Frittolic0651b22014-09-17 16:34:01 +010095 self.snapshots_client.wait_for_snapshot_status(snapshot['id'],
96 'available')
97 self.assertEqual(snapshot_name, snapshot['display_name'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000098 return snapshot
99
100 def _wait_for_volume_status(self, volume, status):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100101 self.volumes_client.wait_for_volume_status(volume['id'], status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000102
103 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900104 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000105
106 def _attach_volume(self, server, volume):
David Kranz3ebc7212015-02-10 12:19:19 -0500107 attached_volume = self.servers_client.attach_volume(
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +0000108 server['id'], volumeId=volume['id'], device='/dev/%s'
ghanshyam0f825252015-08-25 16:02:50 +0900109 % CONF.compute.volume_device_name)['volumeAttachment']
Andrea Frittolic0651b22014-09-17 16:34:01 +0100110 self.assertEqual(volume['id'], attached_volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000111 self._wait_for_volume_status(attached_volume, 'in-use')
112
113 def _detach_volume(self, server, volume):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100114 self.servers_client.detach_volume(server['id'], volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000115 self._wait_for_volume_status(volume, 'available')
116
Marc Solanasb15d8b62014-02-07 00:04:15 -0800117 def _wait_for_volume_available_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900118 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200119
120 def _func():
121 part = ssh.get_partitions()
122 LOG.debug("Partitions:%s" % part)
Alexander Gubanove0634ab2015-05-25 10:28:25 +0300123 return CONF.compute.volume_device_name in part
Attila Fazekas70431ba2013-07-26 18:47:37 +0200124
125 if not tempest.test.call_until_true(_func,
Matthew Treinish6c072292014-01-29 19:15:52 +0000126 CONF.compute.build_timeout,
127 CONF.compute.build_interval):
Attila Fazekas70431ba2013-07-26 18:47:37 +0200128 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)
Alexander Gubanove0634ab2015-05-25 10:28:25 +0300132 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/%s'
133 % CONF.compute.volume_device_name)
134 ssh_client.exec_command('sudo mount /dev/%s /mnt'
135 % CONF.compute.volume_device_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000136 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)
Alexander Gubanove0634ab2015-05-25 10:28:25 +0300142 ssh_client.exec_command('sudo mount /dev/%s /mnt'
143 % CONF.compute.volume_device_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000144 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
145 self.assertEqual(self.timestamp, got_timestamp)
146
Matthew Treinishc49fcbe2015-02-05 23:37:34 -0500147 @decorators.skip_because(bug="1205344")
Chris Hoge7579c1a2015-02-26 14:12:15 -0800148 @test.idempotent_id('10fd234a-515c-41e5-b092-8323060598c5')
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -0700149 @testtools.skipUnless(CONF.compute_feature_enabled.snapshot,
150 'Snapshotting is not available.')
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):
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +0900153 # prepare for booting an instance
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000154 self._add_keypair()
Andrea Frittolic0651b22014-09-17 16:34:01 +0100155 self.security_group = self._create_security_group()
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:
Yair Friedae0e73d2014-11-24 11:56:26 +0200163 floating_ip_for_server = self.create_floating_ip(server)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100164 ip_for_server = floating_ip_for_server['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000165 else:
166 ip_for_server = server
167
168 self._attach_volume(server, volume)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800169 self._wait_for_volume_available_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000170 self._create_timestamp(ip_for_server)
171 self._detach_volume(server, volume)
172
173 # snapshot the volume
174 volume_snapshot = self._create_volume_snapshot(volume)
175
176 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900177 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000178
179 # create second volume from the snapshot(volume2)
180 volume_from_snapshot = self._create_volume(
Andrea Frittolic0651b22014-09-17 16:34:01 +0100181 snapshot_id=volume_snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000182
183 # boot second instance from the snapshot(instance2)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100184 server_from_snapshot = self._boot_image(snapshot_image['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000185
186 # create and add floating IP to server_from_snapshot
Matthew Treinish6c072292014-01-29 19:15:52 +0000187 if CONF.compute.use_floatingip_for_ssh:
Yair Friedae0e73d2014-11-24 11:56:26 +0200188 floating_ip_for_snapshot = self.create_floating_ip(
189 server_from_snapshot)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100190 ip_for_snapshot = floating_ip_for_snapshot['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000191 else:
192 ip_for_snapshot = server_from_snapshot
193
194 # attach volume2 to instance2
195 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800196 self._wait_for_volume_available_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000197
198 # check the existence of the timestamp file in the volume2
199 self._check_timestamp(ip_for_snapshot)