blob: d41490a36e34858d6c5db6355a83ace8c7fbb1dc [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
Matthew Treinish6c072292014-01-29 19:15:52 +000016from tempest import config
Nachi Ueno95b41282014-01-15 06:54:21 -080017from tempest.openstack.common import log
Masayuki Igawaa6de1552013-06-18 17:08:24 +090018from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090019from tempest import test
Masayuki Igawaa6de1552013-06-18 17:08:24 +090020
Matthew Treinish6c072292014-01-29 19:15:52 +000021CONF = config.CONF
Masayuki Igawaa6de1552013-06-18 17:08:24 +090022
Nachi Ueno95b41282014-01-15 06:54:21 -080023LOG = log.getLogger(__name__)
24
25
Masayuki Igawaa6de1552013-06-18 17:08:24 +090026class TestSnapshotPattern(manager.OfficialClientTest):
27 """
28 This test is for snapshotting an instance and booting with it.
29 The following is the scenario outline:
30 * boot a instance and create a timestamp file in it
31 * snapshot the instance
32 * boot a second instance from the snapshot
33 * check the existence of the timestamp file in the second instance
34
35 """
36
Masayuki Igawaa6de1552013-06-18 17:08:24 +090037 def _boot_image(self, image_id):
Grishkin0f1e11c2014-05-04 20:44:52 +040038 security_groups = [self.security_group.name]
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090039 create_kwargs = {
Grishkin0f1e11c2014-05-04 20:44:52 +040040 'key_name': self.keypair.name,
41 'security_groups': security_groups
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090042 }
Giulio Fidente61cadca2013-09-24 18:33:37 +020043 return self.create_server(image=image_id, create_kwargs=create_kwargs)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090044
45 def _add_keypair(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090046 self.keypair = self.create_keypair()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090047
fujioka yuuichia11994e2013-07-09 11:19:51 +090048 def _ssh_to_server(self, server_or_ip):
Nachi Ueno95b41282014-01-15 06:54:21 -080049 try:
Elena Ezhova91db24e2014-02-28 20:47:10 +040050 return self.get_remote_client(server_or_ip)
Nachi Ueno95b41282014-01-15 06:54:21 -080051 except Exception:
Matthew Treinish7adb6952014-06-16 10:05:57 -040052 LOG.exception('Initializing SSH connection failed')
Nachi Ueno95b41282014-01-15 06:54:21 -080053 self._log_console_output()
Matthew Treinish7adb6952014-06-16 10:05:57 -040054 raise
Masayuki Igawaa6de1552013-06-18 17:08:24 +090055
fujioka yuuichia11994e2013-07-09 11:19:51 +090056 def _write_timestamp(self, server_or_ip):
57 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090058 ssh_client.exec_command('date > /tmp/timestamp; sync')
59 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
60
fujioka yuuichia11994e2013-07-09 11:19:51 +090061 def _check_timestamp(self, server_or_ip):
62 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090063 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
64 self.assertEqual(self.timestamp, got_timestamp)
65
fujioka yuuichia11994e2013-07-09 11:19:51 +090066 def _create_floating_ip(self):
67 floating_ip = self.compute_client.floating_ips.create()
68 self.addCleanup(floating_ip.delete)
69 return floating_ip
70
71 def _set_floating_ip_to_server(self, server, floating_ip):
72 server.add_floating_ip(floating_ip)
73
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090074 @test.services('compute', 'network', 'image')
Masayuki Igawaa6de1552013-06-18 17:08:24 +090075 def test_snapshot_pattern(self):
76 # prepare for booting a instance
77 self._add_keypair()
Grishkin0f1e11c2014-05-04 20:44:52 +040078 self.security_group = self._create_security_group_nova()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090079
80 # boot a instance and create a timestamp file in it
Matthew Treinish6c072292014-01-29 19:15:52 +000081 server = self._boot_image(CONF.compute.image_ref)
82 if CONF.compute.use_floatingip_for_ssh:
fujioka yuuichia11994e2013-07-09 11:19:51 +090083 fip_for_server = self._create_floating_ip()
84 self._set_floating_ip_to_server(server, fip_for_server)
85 self._write_timestamp(fip_for_server.ip)
86 else:
87 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090088
89 # snapshot the instance
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +090090 snapshot_image = self.create_server_snapshot(server=server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090091
92 # boot a second instance from the snapshot
Ken'ichi Ohmichia4912232013-08-26 14:03:25 +090093 server_from_snapshot = self._boot_image(snapshot_image.id)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090094
95 # check the existence of the timestamp file in the second instance
Matthew Treinish6c072292014-01-29 19:15:52 +000096 if CONF.compute.use_floatingip_for_ssh:
fujioka yuuichia11994e2013-07-09 11:19:51 +090097 fip_for_snapshot = self._create_floating_ip()
98 self._set_floating_ip_to_server(server_from_snapshot,
99 fip_for_snapshot)
100 self._check_timestamp(fip_for_snapshot.ip)
101 else:
102 self._check_timestamp(server_from_snapshot)