blob: 6e305c1b80a0443132f09e3d2ad512d4491095c7 [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):
47 name = rand_name('scenario-server-')
48 client = self.compute_client
49 flavor_id = self.config.compute.flavor_ref
50 LOG.debug("name:%s, image:%s" % (name, image_id))
51 server = client.servers.create(name=name,
52 image=image_id,
53 flavor=flavor_id,
54 key_name=self.keypair.name)
55 self.addCleanup(self.compute_client.servers.delete, server)
56 self.assertEqual(name, server.name)
57 self._wait_for_server_status(server, 'ACTIVE')
58 server = client.servers.get(server) # getting network information
59 LOG.debug("server:%s" % server)
60 return server
61
62 def _add_keypair(self):
63 name = rand_name('scenario-keypair-')
64 self.keypair = self.compute_client.keypairs.create(name=name)
65 self.addCleanup(self.compute_client.keypairs.delete, self.keypair)
66 self.assertEqual(name, self.keypair.name)
67
68 def _create_security_group_rule(self):
69 sgs = self.compute_client.security_groups.list()
70 for sg in sgs:
71 if sg.name == 'default':
72 secgroup = sg
73
74 ruleset = {
75 # ssh
76 'ip_protocol': 'tcp',
77 'from_port': 22,
78 'to_port': 22,
79 'cidr': '0.0.0.0/0',
80 'group_id': None
81 }
82 sg_rule = self.compute_client.security_group_rules.create(secgroup.id,
83 **ruleset)
84 self.addCleanup(self.compute_client.security_group_rules.delete,
85 sg_rule.id)
86
fujioka yuuichia11994e2013-07-09 11:19:51 +090087 def _ssh_to_server(self, server_or_ip):
88 if isinstance(server_or_ip, basestring):
89 ip = server_or_ip
90 else:
91 network_name_for_ssh = self.config.compute.network_for_ssh
92 ip = server_or_ip.networks[network_name_for_ssh][0]
Masayuki Igawaa6de1552013-06-18 17:08:24 +090093 username = self.config.scenario.ssh_user
Masayuki Igawaa6de1552013-06-18 17:08:24 +090094 linux_client = RemoteClient(ip,
95 username,
96 pkey=self.keypair.private_key)
97
98 return linux_client.ssh_client
99
fujioka yuuichia11994e2013-07-09 11:19:51 +0900100 def _write_timestamp(self, server_or_ip):
101 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900102 ssh_client.exec_command('date > /tmp/timestamp; sync')
103 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
104
105 def _create_image(self, server):
106 snapshot_name = rand_name('scenario-snapshot-')
107 create_image_client = self.compute_client.servers.create_image
108 image_id = create_image_client(server, snapshot_name)
109 self.addCleanup(self.image_client.images.delete, image_id)
110 self._wait_for_server_status(server, 'ACTIVE')
111 self._wait_for_image_status(image_id, 'active')
112 snapshot_image = self.image_client.images.get(image_id)
113 self.assertEquals(snapshot_name, snapshot_image.name)
114 return image_id
115
fujioka yuuichia11994e2013-07-09 11:19:51 +0900116 def _check_timestamp(self, server_or_ip):
117 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900118 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
119 self.assertEqual(self.timestamp, got_timestamp)
120
fujioka yuuichia11994e2013-07-09 11:19:51 +0900121 def _create_floating_ip(self):
122 floating_ip = self.compute_client.floating_ips.create()
123 self.addCleanup(floating_ip.delete)
124 return floating_ip
125
126 def _set_floating_ip_to_server(self, server, floating_ip):
127 server.add_floating_ip(floating_ip)
128
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900129 def test_snapshot_pattern(self):
130 # prepare for booting a instance
131 self._add_keypair()
132 self._create_security_group_rule()
133
134 # boot a instance and create a timestamp file in it
135 server = self._boot_image(self.config.compute.image_ref)
fujioka yuuichia11994e2013-07-09 11:19:51 +0900136 if self.config.compute.use_floatingip_for_ssh:
137 fip_for_server = self._create_floating_ip()
138 self._set_floating_ip_to_server(server, fip_for_server)
139 self._write_timestamp(fip_for_server.ip)
140 else:
141 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900142
143 # snapshot the instance
144 snapshot_image_id = self._create_image(server)
145
146 # boot a second instance from the snapshot
147 server_from_snapshot = self._boot_image(snapshot_image_id)
148
149 # check the existence of the timestamp file in the second instance
fujioka yuuichia11994e2013-07-09 11:19:51 +0900150 if self.config.compute.use_floatingip_for_ssh:
151 fip_for_snapshot = self._create_floating_ip()
152 self._set_floating_ip_to_server(server_from_snapshot,
153 fip_for_snapshot)
154 self._check_timestamp(fip_for_snapshot.ip)
155 else:
156 self._check_timestamp(server_from_snapshot)