blob: dc32edc147247121093c192403bb0c29bd25457f [file] [log] [blame]
Masayuki Igawaa6de1552013-06-18 17:08:24 +09001# 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
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -070016import testtools
17
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
Nachi Ueno95b41282014-01-15 06:54:21 -080019from tempest.openstack.common import log
Masayuki Igawaa6de1552013-06-18 17:08:24 +090020from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
Masayuki Igawaa6de1552013-06-18 17:08:24 +090022
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
Masayuki Igawaa6de1552013-06-18 17:08:24 +090024
Nachi Ueno95b41282014-01-15 06:54:21 -080025LOG = log.getLogger(__name__)
26
27
nithya-ganesan882595e2014-07-29 18:51:07 +000028class TestSnapshotPattern(manager.ScenarioTest):
Masayuki Igawaa6de1552013-06-18 17:08:24 +090029 """
30 This test is for snapshotting an instance and booting with it.
31 The following is the scenario outline:
32 * boot a instance and create a timestamp file in it
33 * snapshot the instance
34 * boot a second instance from the snapshot
35 * check the existence of the timestamp file in the second instance
36
37 """
38
Masayuki Igawaa6de1552013-06-18 17:08:24 +090039 def _boot_image(self, image_id):
nithya-ganesan882595e2014-07-29 18:51:07 +000040 security_groups = [self.security_group]
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090041 create_kwargs = {
nithya-ganesan882595e2014-07-29 18:51:07 +000042 'key_name': self.keypair['name'],
Grishkin0f1e11c2014-05-04 20:44:52 +040043 'security_groups': security_groups
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090044 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020045 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090046
47 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090048 self.keypair = self.create_keypair()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090049
fujioka yuuichia11994e2013-07-09 11:19:51 +090050 def _ssh_to_server(self, server_or_ip):
Nachi Ueno95b41282014-01-15 06:54:21 -080051 try:
Elena Ezhova91db24e2014-02-28 20:47:10 +040052 return self.get_remote_client(server_or_ip)
Nachi Ueno95b41282014-01-15 06:54:21 -080053 except Exception:
Matthew Treinish7adb6952014-06-16 10:05:57 -040054 LOG.exception('Initializing SSH connection failed')
Nachi Ueno95b41282014-01-15 06:54:21 -080055 self._log_console_output()
Matthew Treinish7adb6952014-06-16 10:05:57 -040056 raise
Masayuki Igawaa6de1552013-06-18 17:08:24 +090057
fujioka yuuichia11994e2013-07-09 11:19:51 +090058 def _write_timestamp(self, server_or_ip):
59 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090060 ssh_client.exec_command('date > /tmp/timestamp; sync')
61 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
62
fujioka yuuichia11994e2013-07-09 11:19:51 +090063 def _check_timestamp(self, server_or_ip):
64 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090065 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
66 self.assertEqual(self.timestamp, got_timestamp)
67
fujioka yuuichia11994e2013-07-09 11:19:51 +090068 def _create_floating_ip(self):
nithya-ganesan882595e2014-07-29 18:51:07 +000069 _, floating_ip = self.floating_ips_client.create_floating_ip()
70 self.addCleanup(self.delete_wrapper,
71 self.floating_ips_client.delete_floating_ip,
72 floating_ip['id'])
fujioka yuuichia11994e2013-07-09 11:19:51 +090073 return floating_ip
74
75 def _set_floating_ip_to_server(self, server, floating_ip):
nithya-ganesan882595e2014-07-29 18:51:07 +000076 self.floating_ips_client.associate_floating_ip_to_server(
77 floating_ip['ip'], server['id'])
fujioka yuuichia11994e2013-07-09 11:19:51 +090078
Adam Gandelmanfbc95ac2014-06-19 17:33:43 -070079 @testtools.skipUnless(CONF.compute_feature_enabled.snapshot,
80 'Snapshotting is not available.')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090081 @test.services('compute', 'network', 'image')
Masayuki Igawaa6de1552013-06-18 17:08:24 +090082 def test_snapshot_pattern(self):
83 # prepare for booting a instance
84 self._add_keypair()
Yair Fried1fc32a12014-08-04 09:11:30 +030085 self.security_group = self._create_security_group()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090086
87 # boot a instance and create a timestamp file in it
Matthew Treinish6c072292014-01-29 19:15:52 +000088 server = self._boot_image(CONF.compute.image_ref)
89 if CONF.compute.use_floatingip_for_ssh:
fujioka yuuichia11994e2013-07-09 11:19:51 +090090 fip_for_server = self._create_floating_ip()
91 self._set_floating_ip_to_server(server, fip_for_server)
nithya-ganesan882595e2014-07-29 18:51:07 +000092 self._write_timestamp(fip_for_server['ip'])
fujioka yuuichia11994e2013-07-09 11:19:51 +090093 else:
94 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090095
96 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +090097 snapshot_image = self.create_server_snapshot(server=server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090098
99 # boot a second instance from the snapshot
nithya-ganesan882595e2014-07-29 18:51:07 +0000100 server_from_snapshot = self._boot_image(snapshot_image['id'])
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900101
102 # check the existence of the timestamp file in the second instance
Matthew Treinish6c072292014-01-29 19:15:52 +0000103 if CONF.compute.use_floatingip_for_ssh:
fujioka yuuichia11994e2013-07-09 11:19:51 +0900104 fip_for_snapshot = self._create_floating_ip()
105 self._set_floating_ip_to_server(server_from_snapshot,
106 fip_for_snapshot)
nithya-ganesan882595e2014-07-29 18:51:07 +0000107 self._check_timestamp(fip_for_snapshot['ip'])
fujioka yuuichia11994e2013-07-09 11:19:51 +0900108 else:
109 self._check_timestamp(server_from_snapshot)