blob: 77254210d1d8b1fbe256a5f0fb2eded79a110568 [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
18import logging
19
20from tempest.common.utils.data_utils import rand_name
21from tempest.common.utils.linux.remote_client import RemoteClient
22from tempest.scenario import manager
23
24
25LOG = logging.getLogger(__name__)
26
27
28class TestSnapshotPattern(manager.OfficialClientTest):
29 """
30 This test is for snapshotting an instance and booting with it.
31 The following is the scenario outline:
32 * boot a instance and create a timestamp file in it
33 * snapshot the instance
34 * boot a second instance from the snapshot
35 * check the existence of the timestamp file in the second instance
36
37 """
38
39 def _wait_for_server_status(self, server, status):
40 self.status_timeout(self.compute_client.servers,
41 server.id,
42 status)
43
44 def _wait_for_image_status(self, image_id, status):
45 self.status_timeout(self.image_client.images, image_id, status)
46
47 def _boot_image(self, image_id):
48 name = rand_name('scenario-server-')
49 client = self.compute_client
50 flavor_id = self.config.compute.flavor_ref
51 LOG.debug("name:%s, image:%s" % (name, image_id))
52 server = client.servers.create(name=name,
53 image=image_id,
54 flavor=flavor_id,
55 key_name=self.keypair.name)
56 self.addCleanup(self.compute_client.servers.delete, server)
57 self.assertEqual(name, server.name)
58 self._wait_for_server_status(server, 'ACTIVE')
59 server = client.servers.get(server) # getting network information
60 LOG.debug("server:%s" % server)
61 return server
62
63 def _add_keypair(self):
64 name = rand_name('scenario-keypair-')
65 self.keypair = self.compute_client.keypairs.create(name=name)
66 self.addCleanup(self.compute_client.keypairs.delete, self.keypair)
67 self.assertEqual(name, self.keypair.name)
68
69 def _create_security_group_rule(self):
70 sgs = self.compute_client.security_groups.list()
71 for sg in sgs:
72 if sg.name == 'default':
73 secgroup = sg
74
75 ruleset = {
76 # ssh
77 'ip_protocol': 'tcp',
78 'from_port': 22,
79 'to_port': 22,
80 'cidr': '0.0.0.0/0',
81 'group_id': None
82 }
83 sg_rule = self.compute_client.security_group_rules.create(secgroup.id,
84 **ruleset)
85 self.addCleanup(self.compute_client.security_group_rules.delete,
86 sg_rule.id)
87
88 def _ssh_to_server(self, server):
89 username = self.config.scenario.ssh_user
90 ip = server.networks[self.config.compute.network_for_ssh][0]
91 linux_client = RemoteClient(ip,
92 username,
93 pkey=self.keypair.private_key)
94
95 return linux_client.ssh_client
96
97 def _write_timestamp(self, server):
98 ssh_client = self._ssh_to_server(server)
99 ssh_client.exec_command('date > /tmp/timestamp; sync')
100 self.timestamp = ssh_client.exec_command('cat /tmp/timestamp')
101
102 def _create_image(self, server):
103 snapshot_name = rand_name('scenario-snapshot-')
104 create_image_client = self.compute_client.servers.create_image
105 image_id = create_image_client(server, snapshot_name)
106 self.addCleanup(self.image_client.images.delete, image_id)
107 self._wait_for_server_status(server, 'ACTIVE')
108 self._wait_for_image_status(image_id, 'active')
109 snapshot_image = self.image_client.images.get(image_id)
110 self.assertEquals(snapshot_name, snapshot_image.name)
111 return image_id
112
113 def _check_timestamp(self, server):
114 ssh_client = self._ssh_to_server(server)
115 got_timestamp = ssh_client.exec_command('cat /tmp/timestamp')
116 self.assertEqual(self.timestamp, got_timestamp)
117
118 def test_snapshot_pattern(self):
119 # prepare for booting a instance
120 self._add_keypair()
121 self._create_security_group_rule()
122
123 # boot a instance and create a timestamp file in it
124 server = self._boot_image(self.config.compute.image_ref)
125 self._write_timestamp(server)
126
127 # snapshot the instance
128 snapshot_image_id = self._create_image(server)
129
130 # boot a second instance from the snapshot
131 server_from_snapshot = self._boot_image(snapshot_image_id)
132
133 # check the existence of the timestamp file in the second instance
134 self._check_timestamp(server_from_snapshot)