blob: e8ce1bd4cc2f6f287622d11d5038f98daf74584a [file] [log] [blame]
Masayuki Igawaa6de1552013-06-18 17:08:24 +09001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 NEC Corporation
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Masayuki Igawaa6de1552013-06-18 17:08:24 +090018from tempest.common.utils.data_utils import rand_name
19from tempest.common.utils.linux.remote_client import RemoteClient
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040020from tempest.openstack.common import log as logging
Masayuki Igawaa6de1552013-06-18 17:08:24 +090021from tempest.scenario import manager
22
23
24LOG = logging.getLogger(__name__)
25
26
27class TestSnapshotPattern(manager.OfficialClientTest):
28 """
29 This test is for snapshotting an instance and booting with it.
30 The following is the scenario outline:
31 * boot a instance and create a timestamp file in it
32 * snapshot the instance
33 * boot a second instance from the snapshot
34 * check the existence of the timestamp file in the second instance
35
36 """
37
38 def _wait_for_server_status(self, server, status):
39 self.status_timeout(self.compute_client.servers,
40 server.id,
41 status)
42
43 def _wait_for_image_status(self, image_id, status):
44 self.status_timeout(self.image_client.images, image_id, status)
45
46 def _boot_image(self, image_id):
Ken'ichi Ohmichi61f272b2013-08-15 15:58:53 +090047 create_kwargs = {
48 'key_name': self.keypair.name
49 }
50 return self.create_server(self.compute_client, image=image_id,
51 create_kwargs=create_kwargs)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090052
53 def _add_keypair(self):
54 name = rand_name('scenario-keypair-')
55 self.keypair = self.compute_client.keypairs.create(name=name)
56 self.addCleanup(self.compute_client.keypairs.delete, self.keypair)
57 self.assertEqual(name, self.keypair.name)
58
59 def _create_security_group_rule(self):
60 sgs = self.compute_client.security_groups.list()
61 for sg in sgs:
62 if sg.name == 'default':
63 secgroup = sg
64
65 ruleset = {
66 # ssh
67 'ip_protocol': 'tcp',
68 'from_port': 22,
69 'to_port': 22,
70 'cidr': '0.0.0.0/0',
71 'group_id': None
72 }
73 sg_rule = self.compute_client.security_group_rules.create(secgroup.id,
74 **ruleset)
75 self.addCleanup(self.compute_client.security_group_rules.delete,
76 sg_rule.id)
77
fujioka yuuichia11994e2013-07-09 11:19:51 +090078 def _ssh_to_server(self, server_or_ip):
79 if isinstance(server_or_ip, basestring):
80 ip = server_or_ip
81 else:
82 network_name_for_ssh = self.config.compute.network_for_ssh
83 ip = server_or_ip.networks[network_name_for_ssh][0]
Masayuki Igawaa6de1552013-06-18 17:08:24 +090084 username = self.config.scenario.ssh_user
Masayuki Igawaa6de1552013-06-18 17:08:24 +090085 linux_client = RemoteClient(ip,
86 username,
87 pkey=self.keypair.private_key)
88
89 return linux_client.ssh_client
90
fujioka yuuichia11994e2013-07-09 11:19:51 +090091 def _write_timestamp(self, server_or_ip):
92 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090093 ssh_client.exec_command('date > /tmp/timestamp; sync')
94 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
95
96 def _create_image(self, server):
97 snapshot_name = rand_name('scenario-snapshot-')
98 create_image_client = self.compute_client.servers.create_image
99 image_id = create_image_client(server, snapshot_name)
100 self.addCleanup(self.image_client.images.delete, image_id)
101 self._wait_for_server_status(server, 'ACTIVE')
102 self._wait_for_image_status(image_id, 'active')
103 snapshot_image = self.image_client.images.get(image_id)
104 self.assertEquals(snapshot_name, snapshot_image.name)
105 return image_id
106
fujioka yuuichia11994e2013-07-09 11:19:51 +0900107 def _check_timestamp(self, server_or_ip):
108 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900109 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
110 self.assertEqual(self.timestamp, got_timestamp)
111
fujioka yuuichia11994e2013-07-09 11:19:51 +0900112 def _create_floating_ip(self):
113 floating_ip = self.compute_client.floating_ips.create()
114 self.addCleanup(floating_ip.delete)
115 return floating_ip
116
117 def _set_floating_ip_to_server(self, server, floating_ip):
118 server.add_floating_ip(floating_ip)
119
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900120 def test_snapshot_pattern(self):
121 # prepare for booting a instance
122 self._add_keypair()
123 self._create_security_group_rule()
124
125 # boot a instance and create a timestamp file in it
126 server = self._boot_image(self.config.compute.image_ref)
fujioka yuuichia11994e2013-07-09 11:19:51 +0900127 if self.config.compute.use_floatingip_for_ssh:
128 fip_for_server = self._create_floating_ip()
129 self._set_floating_ip_to_server(server, fip_for_server)
130 self._write_timestamp(fip_for_server.ip)
131 else:
132 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900133
134 # snapshot the instance
135 snapshot_image_id = self._create_image(server)
136
137 # boot a second instance from the snapshot
138 server_from_snapshot = self._boot_image(snapshot_image_id)
139
140 # check the existence of the timestamp file in the second instance
fujioka yuuichia11994e2013-07-09 11:19:51 +0900141 if self.config.compute.use_floatingip_for_ssh:
142 fip_for_snapshot = self._create_floating_ip()
143 self._set_floating_ip_to_server(server_from_snapshot,
144 fip_for_snapshot)
145 self._check_timestamp(fip_for_snapshot.ip)
146 else:
147 self._check_timestamp(server_from_snapshot)