root / snf-tools / synnefo_tools / burnin / cyclades_common.py @ 8c67f82e
History | View | Annotate | Download (11.6 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
"""
|
35 |
Utility functions for Cyclades Tests
|
36 |
Cyclades require a lot helper functions and `common'
|
37 |
had grown too much.
|
38 |
|
39 |
"""
|
40 |
|
41 |
import time |
42 |
import base64 |
43 |
import socket |
44 |
import random |
45 |
import paramiko |
46 |
import tempfile |
47 |
import subprocess |
48 |
|
49 |
from synnefo_tools.burnin.common import BurninTests |
50 |
|
51 |
|
52 |
# Too many public methods. pylint: disable-msg=R0904
|
53 |
class CycladesTests(BurninTests): |
54 |
"""Extends the BurninTests class for Cyclades"""
|
55 |
def _ry_until_timeout_expires(self, opmsg, check_fun): |
56 |
"""Try to perform an action until timeout expires"""
|
57 |
assert callable(check_fun), "Not a function" |
58 |
|
59 |
action_timeout = self.action_timeout
|
60 |
action_warning = self.action_warning
|
61 |
if action_warning > action_timeout:
|
62 |
action_warning = action_timeout |
63 |
|
64 |
start_time = int(time.time())
|
65 |
end_time = start_time + action_warning |
66 |
while end_time > time.time():
|
67 |
try:
|
68 |
ret_value = check_fun() |
69 |
self.info("Operation `%s' finished in %s seconds", |
70 |
opmsg, int(time.time()) - start_time)
|
71 |
return ret_value
|
72 |
except Retry:
|
73 |
time.sleep(self.query_interval)
|
74 |
self.warning("Operation `%s' is taking too long after %s seconds", |
75 |
opmsg, int(time.time()) - start_time)
|
76 |
|
77 |
end_time = start_time + action_timeout |
78 |
while end_time > time.time():
|
79 |
try:
|
80 |
ret_value = check_fun() |
81 |
self.info("Operation `%s' finished in %s seconds", |
82 |
opmsg, int(time.time()) - start_time)
|
83 |
return ret_value
|
84 |
except Retry:
|
85 |
time.sleep(self.query_interval)
|
86 |
self.error("Operation `%s' timed out after %s seconds", |
87 |
opmsg, int(time.time()) - start_time)
|
88 |
self.fail("time out") |
89 |
|
90 |
def _get_list_of_servers(self, detail=False): |
91 |
"""Get (detailed) list of servers"""
|
92 |
if detail:
|
93 |
self.info("Getting detailed list of servers") |
94 |
else:
|
95 |
self.info("Getting simple list of servers") |
96 |
return self.clients.cyclades.list_servers(detail=detail) |
97 |
|
98 |
def _get_server_details(self, server): |
99 |
"""Get details for a server"""
|
100 |
self.info("Getting details for server %s with id %s", |
101 |
server['name'], server['id']) |
102 |
return self.clients.cyclades.get_server_details(server['id']) |
103 |
|
104 |
def _create_server(self, name, image, flavor, personality): |
105 |
"""Create a new server"""
|
106 |
self.info("Creating a server with name %s", name) |
107 |
self.info("Using image %s with id %s", image['name'], image['id']) |
108 |
self.info("Using flavor %s with id %s", flavor['name'], flavor['id']) |
109 |
server = self.clients.cyclades.create_server(
|
110 |
name, flavor['id'], image['id'], personality=personality) |
111 |
|
112 |
self.info("Server id: %s", server['id']) |
113 |
self.info("Server password: %s", server['adminPass']) |
114 |
|
115 |
self.assertEqual(server['name'], name) |
116 |
self.assertEqual(server['flavor']['id'], flavor['id']) |
117 |
self.assertEqual(server['image']['id'], image['id']) |
118 |
self.assertEqual(server['status'], "BUILD") |
119 |
|
120 |
return server
|
121 |
|
122 |
def _get_connection_username(self, server): |
123 |
"""Determine the username to use to connect to the server"""
|
124 |
users = server['metadata'].get("users", None) |
125 |
ret_user = None
|
126 |
if users is not None: |
127 |
user_list = users.split() |
128 |
if "root" in user_list: |
129 |
ret_user = "root"
|
130 |
else:
|
131 |
ret_user = random.choice(user_list) |
132 |
else:
|
133 |
# Return the login name for connections based on the server OS
|
134 |
self.info("Could not find `users' metadata in server. Let's guess") |
135 |
os_value = server['metadata'].get("os") |
136 |
if os_value in ("Ubuntu", "Kubuntu", "Fedora"): |
137 |
ret_user = "user"
|
138 |
elif os_value in ("windows", "windows_alpha1"): |
139 |
ret_user = "Administrator"
|
140 |
else:
|
141 |
ret_user = "root"
|
142 |
|
143 |
self.assertIsNotNone(ret_user)
|
144 |
self.info("User's login name: %s", ret_user) |
145 |
return ret_user
|
146 |
|
147 |
def _insist_on_server_transition(self, server, curr_status, new_status): |
148 |
"""Insist on server transiting from curr_status to new_status"""
|
149 |
def check_fun(): |
150 |
"""Check server status"""
|
151 |
srv = self.clients.cyclades.get_server_details(server['id']) |
152 |
if srv['status'] == curr_status: |
153 |
raise Retry()
|
154 |
elif srv['status'] == new_status: |
155 |
return
|
156 |
else:
|
157 |
msg = "Server %s went to unexpected status %s"
|
158 |
self.error(msg, server['name'], srv['status']) |
159 |
self.fail(msg % (server['name'], srv['status'])) |
160 |
opmsg = "Waiting for server %s to transit from %s to %s"
|
161 |
self.info(opmsg, server['name'], curr_status, new_status) |
162 |
opmsg = opmsg % (server['name'], curr_status, new_status)
|
163 |
self._try_until_timeout_expires(opmsg, check_fun)
|
164 |
|
165 |
def _insist_on_tcp_connection(self, family, host, port): |
166 |
"""Insist on tcp connection"""
|
167 |
def check_fun(): |
168 |
"""Get a connected socket from the specified family to host:port"""
|
169 |
sock = None
|
170 |
for res in socket.getaddrinfo(host, port, family, |
171 |
socket.SOCK_STREAM, 0,
|
172 |
socket.AI_PASSIVE): |
173 |
fam, socktype, proto, _, saddr = res |
174 |
try:
|
175 |
sock = socket.socket(fam, socktype, proto) |
176 |
except socket.error:
|
177 |
sock = None
|
178 |
continue
|
179 |
try:
|
180 |
sock.connect(saddr) |
181 |
except socket.error:
|
182 |
sock.close() |
183 |
sock = None
|
184 |
continue
|
185 |
if sock is None: |
186 |
raise Retry
|
187 |
return sock
|
188 |
familystr = {socket.AF_INET: "IPv4", socket.AF_INET6: "IPv6", |
189 |
socket.AF_UNSPEC: "Unspecified-IPv4/6"}
|
190 |
opmsg = "Connecting over %s to %s:%s"
|
191 |
self.info(opmsg, familystr.get(family, "Unknown"), host, port) |
192 |
opmsg = opmsg % (familystr.get(family, "Unknown"), host, port)
|
193 |
return self._try_until_timeout_expires(opmsg, check_fun) |
194 |
|
195 |
def _get_ip(self, server, version=4): |
196 |
"""Get the public IP of a server from the detailed server info"""
|
197 |
assert version in (4, 6) |
198 |
|
199 |
nics = server['attachments']
|
200 |
public_addrs = None
|
201 |
for nic in nics: |
202 |
net_id = nic['network_id']
|
203 |
if self.clients.cyclades.get_network_details(net_id)['public']: |
204 |
public_addrs = nic['ipv' + str(version)] |
205 |
|
206 |
self.assertIsNotNone(public_addrs)
|
207 |
msg = "Server's public IPv%s is %s"
|
208 |
self.info(msg, version, public_addrs)
|
209 |
return public_addrs
|
210 |
|
211 |
def _insist_on_ping(self, ip_addr, version=4): |
212 |
"""Test server responds to a single IPv4 of IPv6 ping"""
|
213 |
def check_fun(): |
214 |
"""Ping to server"""
|
215 |
cmd = ("ping%s -c 3 -w 20 %s" %
|
216 |
("6" if version == 6 else "", ip_addr)) |
217 |
ping = subprocess.Popen( |
218 |
cmd, shell=True, stdout=subprocess.PIPE,
|
219 |
stderr=subprocess.PIPE) |
220 |
ping.communicate() |
221 |
ret = ping.wait() |
222 |
if ret != 0: |
223 |
raise Retry
|
224 |
assert version in (4, 6) |
225 |
opmsg = "Sent IPv%s ping requests to %s"
|
226 |
self.info(opmsg, version, ip_addr)
|
227 |
opmsg = opmsg % (version, ip_addr) |
228 |
self._try_until_timeout_expires(opmsg, check_fun)
|
229 |
|
230 |
def _image_is(self, image, osfamily): |
231 |
"""Return true if the image is of `osfamily'"""
|
232 |
d_image = self.clients.cyclades.get_image_details(image['id']) |
233 |
return d_image['metadata']['osfamily'].lower().find(osfamily) >= 0 |
234 |
|
235 |
def _ssh_execute(self, hostip, username, password, command): |
236 |
"""Execute a command via ssh"""
|
237 |
ssh = paramiko.SSHClient() |
238 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
239 |
try:
|
240 |
ssh.connect(hostip, username=username, password=password) |
241 |
except socket.error as err: |
242 |
self.fail(err)
|
243 |
try:
|
244 |
_, stdout, _ = ssh.exec_command(command) |
245 |
except paramiko.SSHException as err: |
246 |
self.fail(err)
|
247 |
status = stdout.channel.recv_exit_status() |
248 |
output = stdout.readlines() |
249 |
ssh.close() |
250 |
return output, status
|
251 |
|
252 |
def _insist_get_hostname_over_ssh(self, hostip, username, password): |
253 |
"""Connect to server using ssh and get it's hostname"""
|
254 |
def check_fun(): |
255 |
"""Get hostname"""
|
256 |
try:
|
257 |
lines, status = self._ssh_execute(
|
258 |
hostip, username, password, "hostname")
|
259 |
self.assertEqual(status, 0) |
260 |
self.assertEqual(len(lines), 1) |
261 |
# Remove new line
|
262 |
return lines[0].strip('\n') |
263 |
except AssertionError: |
264 |
raise Retry()
|
265 |
opmsg = "Connecting to server using ssh and get it's hostname"
|
266 |
self.info(opmsg)
|
267 |
hostname = self._try_until_timeout_expires(opmsg, check_fun)
|
268 |
self.info("Server's hostname is %s", hostname) |
269 |
return hostname
|
270 |
|
271 |
# Too many arguments. pylint: disable-msg=R0913
|
272 |
def _check_file_through_ssh(self, hostip, username, password, |
273 |
remotepath, content): |
274 |
"""Fetch file from server and compare contents"""
|
275 |
self.info("Fetching file %s from remote server", remotepath) |
276 |
transport = paramiko.Transport((hostip, 22))
|
277 |
transport.connect(username=username, password=password) |
278 |
with tempfile.NamedTemporaryFile() as ftmp: |
279 |
sftp = paramiko.SFTPClient.from_transport(transport) |
280 |
sftp.get(remotepath, ftmp.name) |
281 |
sftp.close() |
282 |
transport.close() |
283 |
self.info("Comparing file contents") |
284 |
remote_content = base64.b64encode(ftmp.read()) |
285 |
self.assertEqual(content, remote_content)
|
286 |
|
287 |
|
288 |
class Retry(Exception): |
289 |
"""Retry the action
|
290 |
|
291 |
This is used by _try_unit_timeout_expires method.
|
292 |
|
293 |
"""
|