blob: 41b0fda897d82344ea4d8a3bba8811e83a1e96be [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):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090054 self.keypair = self.create_keypair()
Masayuki Igawaa6de1552013-06-18 17:08:24 +090055
56 def _create_security_group_rule(self):
57 sgs = self.compute_client.security_groups.list()
58 for sg in sgs:
59 if sg.name == 'default':
60 secgroup = sg
61
62 ruleset = {
63 # ssh
64 'ip_protocol': 'tcp',
65 'from_port': 22,
66 'to_port': 22,
67 'cidr': '0.0.0.0/0',
68 'group_id': None
69 }
70 sg_rule = self.compute_client.security_group_rules.create(secgroup.id,
71 **ruleset)
72 self.addCleanup(self.compute_client.security_group_rules.delete,
73 sg_rule.id)
74
fujioka yuuichia11994e2013-07-09 11:19:51 +090075 def _ssh_to_server(self, server_or_ip):
76 if isinstance(server_or_ip, basestring):
77 ip = server_or_ip
78 else:
79 network_name_for_ssh = self.config.compute.network_for_ssh
80 ip = server_or_ip.networks[network_name_for_ssh][0]
Masayuki Igawaa6de1552013-06-18 17:08:24 +090081 username = self.config.scenario.ssh_user
Masayuki Igawaa6de1552013-06-18 17:08:24 +090082 linux_client = RemoteClient(ip,
83 username,
84 pkey=self.keypair.private_key)
85
86 return linux_client.ssh_client
87
fujioka yuuichia11994e2013-07-09 11:19:51 +090088 def _write_timestamp(self, server_or_ip):
89 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +090090 ssh_client.exec_command('date > /tmp/timestamp; sync')
91 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
92
93 def _create_image(self, server):
94 snapshot_name = rand_name('scenario-snapshot-')
95 create_image_client = self.compute_client.servers.create_image
96 image_id = create_image_client(server, snapshot_name)
97 self.addCleanup(self.image_client.images.delete, image_id)
98 self._wait_for_server_status(server, 'ACTIVE')
99 self._wait_for_image_status(image_id, 'active')
100 snapshot_image = self.image_client.images.get(image_id)
101 self.assertEquals(snapshot_name, snapshot_image.name)
102 return image_id
103
fujioka yuuichia11994e2013-07-09 11:19:51 +0900104 def _check_timestamp(self, server_or_ip):
105 ssh_client = self._ssh_to_server(server_or_ip)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900106 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
107 self.assertEqual(self.timestamp, got_timestamp)
108
fujioka yuuichia11994e2013-07-09 11:19:51 +0900109 def _create_floating_ip(self):
110 floating_ip = self.compute_client.floating_ips.create()
111 self.addCleanup(floating_ip.delete)
112 return floating_ip
113
114 def _set_floating_ip_to_server(self, server, floating_ip):
115 server.add_floating_ip(floating_ip)
116
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900117 def test_snapshot_pattern(self):
118 # prepare for booting a instance
119 self._add_keypair()
120 self._create_security_group_rule()
121
122 # boot a instance and create a timestamp file in it
123 server = self._boot_image(self.config.compute.image_ref)
fujioka yuuichia11994e2013-07-09 11:19:51 +0900124 if self.config.compute.use_floatingip_for_ssh:
125 fip_for_server = self._create_floating_ip()
126 self._set_floating_ip_to_server(server, fip_for_server)
127 self._write_timestamp(fip_for_server.ip)
128 else:
129 self._write_timestamp(server)
Masayuki Igawaa6de1552013-06-18 17:08:24 +0900130
131 # snapshot the instance
132 snapshot_image_id = self._create_image(server)
133
134 # boot a second instance from the snapshot
135 server_from_snapshot = self._boot_image(snapshot_image_id)
136
137 # check the existence of the timestamp file in the second instance
fujioka yuuichia11994e2013-07-09 11:19:51 +0900138 if self.config.compute.use_floatingip_for_ssh:
139 fip_for_snapshot = self._create_floating_ip()
140 self._set_floating_ip_to_server(server_from_snapshot,
141 fip_for_snapshot)
142 self._check_timestamp(fip_for_snapshot.ip)
143 else:
144 self._check_timestamp(server_from_snapshot)