blob: e97f089b6ba600affa8d59d7b39de9ea729a671a [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
Matthew Treinishc49fcbe2015-02-05 23:37:34 -050018from tempest_lib import decorators
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -070020import testtools
Sean Daguefe8a6092013-07-27 08:15:55 -040021
Masayuki Igawa259c1132013-10-31 17:48:44 +090022from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000023from tempest import config
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
Matthew Treinish6c072292014-01-29 19:15:52 +000029CONF = config.CONF
30
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000031LOG = logging.getLogger(__name__)
32
33
Andrea Frittolic0651b22014-09-17 16:34:01 +010034class TestStampPattern(manager.ScenarioTest):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000035 """
36 This test is for snapshotting an instance/volume and attaching the volume
37 created from snapshot to the instance booted from snapshot.
38 The following is the scenario outline:
39 1. Boot an instance "instance1"
40 2. Create a volume "volume1"
41 3. Attach volume1 to instance1
42 4. Create a filesystem on volume1
43 5. Mount volume1
44 6. Create a file which timestamp is written in volume1
45 7. Unmount volume1
46 8. Detach volume1 from instance1
47 9. Get a snapshot "snapshot_from_volume" of volume1
48 10. Get a snapshot "snapshot_from_instance" of instance1
49 11. Boot an instance "instance2" from snapshot_from_instance
50 12. Create a volume "volume2" from snapshot_from_volume
51 13. Attach volume2 to instance2
52 14. Check the existence of a file which created at 6. in volume2
53 """
54
JordanPbce55532014-03-19 12:10:32 +010055 @classmethod
Andrea Frittoliac20b5e2014-09-15 13:31:14 +010056 def resource_setup(cls):
JordanPbce55532014-03-19 12:10:32 +010057 if not CONF.volume_feature_enabled.snapshot:
58 raise cls.skipException("Cinder volume snapshots are disabled")
Masayuki Igawa60ea6c52014-10-15 17:32:14 +090059 super(TestStampPattern, cls).resource_setup()
JordanPbce55532014-03-19 12:10:32 +010060
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000061 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
Andrea Frittolic0651b22014-09-17 16:34:01 +010062 self.snapshots_client.wait_for_snapshot_status(volume_snapshot['id'],
63 status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000064
65 def _boot_image(self, image_id):
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +000066 security_groups = [{'name': self.security_group['name']}]
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090067 create_kwargs = {
Andrea Frittolic0651b22014-09-17 16:34:01 +010068 'key_name': self.keypair['name'],
Grishkin0f1e11c2014-05-04 20:44:52 +040069 'security_groups': security_groups
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090070 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020071 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000072
73 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090074 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000075
Attila Fazekas70431ba2013-07-26 18:47:37 +020076 def _ssh_to_server(self, server_or_ip):
Elena Ezhova91db24e2014-02-28 20:47:10 +040077 return self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000078
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000079 def _create_volume_snapshot(self, volume):
Masayuki Igawa259c1132013-10-31 17:48:44 +090080 snapshot_name = data_utils.rand_name('scenario-snapshot-')
Andrea Frittolic0651b22014-09-17 16:34:01 +010081 _, snapshot = self.snapshots_client.create_snapshot(
82 volume['id'], display_name=snapshot_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000083
84 def cleaner():
Andrea Frittolic0651b22014-09-17 16:34:01 +010085 self.snapshots_client.delete_snapshot(snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000086 try:
Andrea Frittolic0651b22014-09-17 16:34:01 +010087 while self.snapshots_client.get_snapshot(snapshot['id']):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000088 time.sleep(1)
Masayuki Igawabfa07602015-01-20 18:47:17 +090089 except lib_exc.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000090 pass
91 self.addCleanup(cleaner)
92 self._wait_for_volume_status(volume, 'available')
Andrea Frittolic0651b22014-09-17 16:34:01 +010093 self.snapshots_client.wait_for_snapshot_status(snapshot['id'],
94 'available')
95 self.assertEqual(snapshot_name, snapshot['display_name'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000096 return snapshot
97
98 def _wait_for_volume_status(self, volume, status):
Andrea Frittolic0651b22014-09-17 16:34:01 +010099 self.volumes_client.wait_for_volume_status(volume['id'], status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000100
101 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900102 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000103
104 def _attach_volume(self, server, volume):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100105 # TODO(andreaf) we should use device from config instead if vdb
David Kranz3ebc7212015-02-10 12:19:19 -0500106 attached_volume = self.servers_client.attach_volume(
Andrea Frittolic0651b22014-09-17 16:34:01 +0100107 server['id'], volume['id'], device='/dev/vdb')
Andrea Frittolic0651b22014-09-17 16:34:01 +0100108 self.assertEqual(volume['id'], attached_volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000109 self._wait_for_volume_status(attached_volume, 'in-use')
110
111 def _detach_volume(self, server, volume):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100112 self.servers_client.detach_volume(server['id'], volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000113 self._wait_for_volume_status(volume, 'available')
114
Marc Solanasb15d8b62014-02-07 00:04:15 -0800115 def _wait_for_volume_available_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900116 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200117
118 def _func():
119 part = ssh.get_partitions()
120 LOG.debug("Partitions:%s" % part)
121 return 'vdb' in part
122
123 if not tempest.test.call_until_true(_func,
Matthew Treinish6c072292014-01-29 19:15:52 +0000124 CONF.compute.build_timeout,
125 CONF.compute.build_interval):
Attila Fazekas70431ba2013-07-26 18:47:37 +0200126 raise exceptions.TimeoutException
127
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000128 def _create_timestamp(self, server_or_ip):
129 ssh_client = self._ssh_to_server(server_or_ip)
130 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
131 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
132 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
133 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
134 ssh_client.exec_command('sudo umount /mnt')
135
136 def _check_timestamp(self, server_or_ip):
137 ssh_client = self._ssh_to_server(server_or_ip)
138 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
139 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
140 self.assertEqual(self.timestamp, got_timestamp)
141
Matthew Treinishc49fcbe2015-02-05 23:37:34 -0500142 @decorators.skip_because(bug="1205344")
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -0700143 @testtools.skipUnless(CONF.compute_feature_enabled.snapshot,
144 'Snapshotting is not available.')
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()
Andrea Frittolic0651b22014-09-17 16:34:01 +0100149 self.security_group = self._create_security_group()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000150
151 # boot an instance and create a timestamp file in it
152 volume = self._create_volume()
Matthew Treinish6c072292014-01-29 19:15:52 +0000153 server = self._boot_image(CONF.compute.image_ref)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000154
155 # create and add floating IP to server1
Matthew Treinish6c072292014-01-29 19:15:52 +0000156 if CONF.compute.use_floatingip_for_ssh:
Yair Friedae0e73d2014-11-24 11:56:26 +0200157 floating_ip_for_server = self.create_floating_ip(server)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100158 ip_for_server = floating_ip_for_server['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000159 else:
160 ip_for_server = server
161
162 self._attach_volume(server, volume)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800163 self._wait_for_volume_available_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000164 self._create_timestamp(ip_for_server)
165 self._detach_volume(server, volume)
166
167 # snapshot the volume
168 volume_snapshot = self._create_volume_snapshot(volume)
169
170 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900171 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000172
173 # create second volume from the snapshot(volume2)
174 volume_from_snapshot = self._create_volume(
Andrea Frittolic0651b22014-09-17 16:34:01 +0100175 snapshot_id=volume_snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000176
177 # boot second instance from the snapshot(instance2)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100178 server_from_snapshot = self._boot_image(snapshot_image['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000179
180 # create and add floating IP to server_from_snapshot
Matthew Treinish6c072292014-01-29 19:15:52 +0000181 if CONF.compute.use_floatingip_for_ssh:
Yair Friedae0e73d2014-11-24 11:56:26 +0200182 floating_ip_for_snapshot = self.create_floating_ip(
183 server_from_snapshot)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100184 ip_for_snapshot = floating_ip_for_snapshot['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000185 else:
186 ip_for_snapshot = server_from_snapshot
187
188 # attach volume2 to instance2
189 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800190 self._wait_for_volume_available_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000191
192 # check the existence of the timestamp file in the volume2
193 self._check_timestamp(ip_for_snapshot)