blob: ee2c737476263d586d2646534567905d6d373260 [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
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -070018import testtools
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
Andrea Frittolic0651b22014-09-17 16:34:01 +010032class TestStampPattern(manager.ScenarioTest):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000033 """
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
Andrea Frittoliac20b5e2014-09-15 13:31:14 +010054 def resource_setup(cls):
JordanPbce55532014-03-19 12:10:32 +010055 if not CONF.volume_feature_enabled.snapshot:
56 raise cls.skipException("Cinder volume snapshots are disabled")
Masayuki Igawa60ea6c52014-10-15 17:32:14 +090057 super(TestStampPattern, cls).resource_setup()
JordanPbce55532014-03-19 12:10:32 +010058
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000059 def _wait_for_volume_snapshot_status(self, volume_snapshot, status):
Andrea Frittolic0651b22014-09-17 16:34:01 +010060 self.snapshots_client.wait_for_snapshot_status(volume_snapshot['id'],
61 status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000062
63 def _boot_image(self, image_id):
Andrea Frittolic0651b22014-09-17 16:34:01 +010064 security_groups = [self.security_group]
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090065 create_kwargs = {
Andrea Frittolic0651b22014-09-17 16:34:01 +010066 'key_name': self.keypair['name'],
Grishkin0f1e11c2014-05-04 20:44:52 +040067 'security_groups': security_groups
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090068 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020069 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000070
71 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090072 self.keypair = self.create_keypair()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000073
Attila Fazekas70431ba2013-07-26 18:47:37 +020074 def _ssh_to_server(self, server_or_ip):
Elena Ezhova91db24e2014-02-28 20:47:10 +040075 return self.get_remote_client(server_or_ip)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000076
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-')
Andrea Frittolic0651b22014-09-17 16:34:01 +010079 _, snapshot = self.snapshots_client.create_snapshot(
80 volume['id'], display_name=snapshot_name)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000081
82 def cleaner():
Andrea Frittolic0651b22014-09-17 16:34:01 +010083 self.snapshots_client.delete_snapshot(snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000084 try:
Andrea Frittolic0651b22014-09-17 16:34:01 +010085 while self.snapshots_client.get_snapshot(snapshot['id']):
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000086 time.sleep(1)
Andrea Frittolic0651b22014-09-17 16:34:01 +010087 except exceptions.NotFound:
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000088 pass
89 self.addCleanup(cleaner)
90 self._wait_for_volume_status(volume, 'available')
Andrea Frittolic0651b22014-09-17 16:34:01 +010091 self.snapshots_client.wait_for_snapshot_status(snapshot['id'],
92 'available')
93 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):
Andrea Frittolic0651b22014-09-17 16:34:01 +010097 self.volumes_client.wait_for_volume_status(volume['id'], status)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +000098
99 def _create_volume(self, snapshot_id=None):
Ken'ichi Ohmichi70672df2013-08-19 18:35:19 +0900100 return self.create_volume(snapshot_id=snapshot_id)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000101
102 def _attach_volume(self, server, volume):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100103 # TODO(andreaf) we should use device from config instead if vdb
104 _, attached_volume = self.servers_client.attach_volume(
105 server['id'], volume['id'], device='/dev/vdb')
Andrea Frittolic0651b22014-09-17 16:34:01 +0100106 self.assertEqual(volume['id'], attached_volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000107 self._wait_for_volume_status(attached_volume, 'in-use')
108
109 def _detach_volume(self, server, volume):
Andrea Frittolic0651b22014-09-17 16:34:01 +0100110 self.servers_client.detach_volume(server['id'], volume['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000111 self._wait_for_volume_status(volume, 'available')
112
Marc Solanasb15d8b62014-02-07 00:04:15 -0800113 def _wait_for_volume_available_on_the_system(self, server_or_ip):
Ken'ichi Ohmichib3aa9122013-08-22 23:27:25 +0900114 ssh = self.get_remote_client(server_or_ip)
Attila Fazekas70431ba2013-07-26 18:47:37 +0200115
116 def _func():
117 part = ssh.get_partitions()
118 LOG.debug("Partitions:%s" % part)
119 return 'vdb' in part
120
121 if not tempest.test.call_until_true(_func,
Matthew Treinish6c072292014-01-29 19:15:52 +0000122 CONF.compute.build_timeout,
123 CONF.compute.build_interval):
Attila Fazekas70431ba2013-07-26 18:47:37 +0200124 raise exceptions.TimeoutException
125
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000126 def _create_timestamp(self, server_or_ip):
127 ssh_client = self._ssh_to_server(server_or_ip)
128 ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/vdb')
129 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
130 ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"')
131 self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
132 ssh_client.exec_command('sudo umount /mnt')
133
134 def _check_timestamp(self, server_or_ip):
135 ssh_client = self._ssh_to_server(server_or_ip)
136 ssh_client.exec_command('sudo mount /dev/vdb /mnt')
137 got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp')
138 self.assertEqual(self.timestamp, got_timestamp)
139
Giulio Fidente386ac8f2013-10-07 14:29:27 +0200140 @tempest.test.skip_because(bug="1205344")
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -0700141 @testtools.skipUnless(CONF.compute_feature_enabled.snapshot,
142 'Snapshotting is not available.')
Matthew Treinish2153ec02013-09-09 20:57:30 +0000143 @tempest.test.services('compute', 'network', 'volume', 'image')
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000144 def test_stamp_pattern(self):
145 # prepare for booting a instance
146 self._add_keypair()
Andrea Frittolic0651b22014-09-17 16:34:01 +0100147 self.security_group = self._create_security_group()
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000148
149 # boot an instance and create a timestamp file in it
150 volume = self._create_volume()
Matthew Treinish6c072292014-01-29 19:15:52 +0000151 server = self._boot_image(CONF.compute.image_ref)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000152
153 # create and add floating IP to server1
Matthew Treinish6c072292014-01-29 19:15:52 +0000154 if CONF.compute.use_floatingip_for_ssh:
Yair Friedae0e73d2014-11-24 11:56:26 +0200155 floating_ip_for_server = self.create_floating_ip(server)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100156 ip_for_server = floating_ip_for_server['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000157 else:
158 ip_for_server = server
159
160 self._attach_volume(server, volume)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800161 self._wait_for_volume_available_on_the_system(ip_for_server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000162 self._create_timestamp(ip_for_server)
163 self._detach_volume(server, volume)
164
165 # snapshot the volume
166 volume_snapshot = self._create_volume_snapshot(volume)
167
168 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +0900169 snapshot_image = self.create_server_snapshot(server=server)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000170
171 # create second volume from the snapshot(volume2)
172 volume_from_snapshot = self._create_volume(
Andrea Frittolic0651b22014-09-17 16:34:01 +0100173 snapshot_id=volume_snapshot['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000174
175 # boot second instance from the snapshot(instance2)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100176 server_from_snapshot = self._boot_image(snapshot_image['id'])
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000177
178 # create and add floating IP to server_from_snapshot
Matthew Treinish6c072292014-01-29 19:15:52 +0000179 if CONF.compute.use_floatingip_for_ssh:
Yair Friedae0e73d2014-11-24 11:56:26 +0200180 floating_ip_for_snapshot = self.create_floating_ip(
181 server_from_snapshot)
Andrea Frittolic0651b22014-09-17 16:34:01 +0100182 ip_for_snapshot = floating_ip_for_snapshot['ip']
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000183 else:
184 ip_for_snapshot = server_from_snapshot
185
186 # attach volume2 to instance2
187 self._attach_volume(server_from_snapshot, volume_from_snapshot)
Marc Solanasb15d8b62014-02-07 00:04:15 -0800188 self._wait_for_volume_available_on_the_system(ip_for_snapshot)
Yuiko Takadaebcf6af2013-07-09 05:10:55 +0000189
190 # check the existence of the timestamp file in the volume2
191 self._check_timestamp(ip_for_snapshot)