blob: 5ff864296cea3f515f71626bbfe4d145b80bb4bc [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
Nachi Ueno95b41282014-01-15 06:54:21 -080016from tempest.openstack.common import log
Masayuki Igawaa6de1552013-06-18 17:08:24 +090017from tempest.scenario import manager
Matthew Treinish2153ec02013-09-09 20:57:30 +000018from tempest.test import services
Masayuki Igawaa6de1552013-06-18 17:08:24 +090019
20
Nachi Ueno95b41282014-01-15 06:54:21 -080021LOG = log.getLogger(__name__)
22
23
Masayuki Igawaa6de1552013-06-18 17:08:24 +090024class TestSnapshotPattern(manager.OfficialClientTest):
25 """
26 This test is for snapshotting an instance and booting with it.
27 The following is the scenario outline:
28 * boot a instance and create a timestamp file in it
29 * snapshot the instance
30 * boot a second instance from the snapshot
31 * check the existence of the timestamp file in the second instance
32
33 """
34
Masayuki Igawaa6de1552013-06-18 17:08:24 +090035 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090036 create_kwargs = {
37 'key_name': self.keypair.name
38 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020039 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090040
41 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090042 self.keypair = self.create_keypair()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090043
fujioka yuuichia11994e2013-07-09 11:19:51 +090044 def _ssh_to_server(self, server_or_ip):
Nachi Ueno95b41282014-01-15 06:54:21 -080045 try:
46 linux_client = self.get_remote_client(server_or_ip)
47 except Exception:
48 LOG.exception()
49 self._log_console_output()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090050 return linux_client.ssh_client
51
fujioka yuuichia11994e2013-07-09 11:19:51 +090052 def _write_timestamp(self, server_or_ip):
53 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090054 ssh_client.exec_command('date > /tmp/timestamp; sync')
55 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
56
fujioka yuuichia11994e2013-07-09 11:19:51 +090057 def _check_timestamp(self, server_or_ip):
58 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090059 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
60 self.assertEqual(self.timestamp, got_timestamp)
61
fujioka yuuichia11994e2013-07-09 11:19:51 +090062 def _create_floating_ip(self):
63 floating_ip = self.compute_client.floating_ips.create()
64 self.addCleanup(floating_ip.delete)
65 return floating_ip
66
67 def _set_floating_ip_to_server(self, server, floating_ip):
68 server.add_floating_ip(floating_ip)
69
Matthew Treinish2153ec02013-09-09 20:57:30 +000070 @services('compute', 'network', 'image')
Masayuki Igawaa6de1552013-06-18 17:08:24 +090071 def test_snapshot_pattern(self):
72 # prepare for booting a instance
73 self._add_keypair()
Yair Friedeb69f3f2013-10-10 13:18:16 +030074 self._create_loginable_secgroup_rule_nova()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090075
76 # boot a instance and create a timestamp file in it
77 server = self._boot_image(self.config.compute.image_ref)
fujioka yuuichia11994e2013-07-09 11:19:51 +090078 if self.config.compute.use_floatingip_for_ssh:
79 fip_for_server = self._create_floating_ip()
80 self._set_floating_ip_to_server(server, fip_for_server)
81 self._write_timestamp(fip_for_server.ip)
82 else:
83 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090084
85 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +090086 snapshot_image = self.create_server_snapshot(server=server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090087
88 # boot a second instance from the snapshot
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +090089 server_from_snapshot = self._boot_image(snapshot_image.id)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090090
91 # check the existence of the timestamp file in the second instance
fujioka yuuichia11994e2013-07-09 11:19:51 +090092 if self.config.compute.use_floatingip_for_ssh:
93 fip_for_snapshot = self._create_floating_ip()
94 self._set_floating_ip_to_server(server_from_snapshot,
95 fip_for_snapshot)
96 self._check_timestamp(fip_for_snapshot.ip)
97 else:
98 self._check_timestamp(server_from_snapshot)