cf5a28dbd86e8414bfaddf8c6202802aaaded915
[functest.git] / testcases / OpenStack / vPing / vping_util.py
1 import os
2 import pprint
3 import re
4 import sys
5 import time
6
7 import paramiko
8 from scp import SCPClient
9
10 import functest.utils.functest_utils as ft_utils
11 import functest.utils.openstack_utils as os_utils
12 FUNCTEST_REPO = ft_utils.FUNCTEST_REPO
13
14 NAME_VM_1 = ft_utils.get_functest_config('vping.vm_name_1')
15 NAME_VM_2 = ft_utils.get_functest_config('vping.vm_name_2')
16
17 VM_BOOT_TIMEOUT = 180
18 VM_DELETE_TIMEOUT = 100
19 PING_TIMEOUT = ft_utils.get_functest_config('vping.ping_timeout')
20
21 GLANCE_IMAGE_NAME = ft_utils.get_functest_config('vping.image_name')
22 GLANCE_IMAGE_FILENAME = \
23     ft_utils.get_functest_config('general.openstack.image_file_name')
24 GLANCE_IMAGE_FORMAT = \
25     ft_utils.get_functest_config('general.openstack.image_disk_format')
26 GLANCE_IMAGE_PATH = \
27     ft_utils.get_functest_config('general.directories.dir_functest_data') + \
28     "/" + GLANCE_IMAGE_FILENAME
29
30
31 FLAVOR = ft_utils.get_functest_config('vping.vm_flavor')
32
33 # NEUTRON Private Network parameters
34 PRIVATE_NET_NAME = \
35     ft_utils.get_functest_config('vping.vping_private_net_name')
36 PRIVATE_SUBNET_NAME = \
37     ft_utils.get_functest_config('vping.vping_private_subnet_name')
38 PRIVATE_SUBNET_CIDR = \
39     ft_utils.get_functest_config('vping.vping_private_subnet_cidr')
40 ROUTER_NAME = ft_utils.get_functest_config('vping.vping_router_name')
41
42 SECGROUP_NAME = ft_utils.get_functest_config('vping.vping_sg_name')
43 SECGROUP_DESCR = ft_utils.get_functest_config('vping.vping_sg_descr')
44
45
46 neutron_client = None
47 glance_client = None
48 nova_client = None
49 logger = None
50
51 pp = pprint.PrettyPrinter(indent=4)
52
53
54 def pMsg(value):
55     """pretty printing"""
56     pp.pprint(value)
57
58
59 def check_repo_exist():
60     if not os.path.exists(FUNCTEST_REPO):
61         logger.error("Functest repository not found '%s'" % FUNCTEST_REPO)
62         exit(-1)
63
64
65 def get_vmname_1():
66     return NAME_VM_1
67
68
69 def get_vmname_2():
70     return NAME_VM_2
71
72
73 def init(vping_logger):
74     global nova_client
75     nova_client = os_utils.get_nova_client()
76     global neutron_client
77     neutron_client = os_utils.get_neutron_client()
78     global glance_client
79     glance_client = os_utils.get_glance_client()
80     global logger
81     logger = vping_logger
82
83
84 def waitVmActive(nova, vm):
85
86     # sleep and wait for VM status change
87     sleep_time = 3
88     count = VM_BOOT_TIMEOUT / sleep_time
89     while True:
90         status = os_utils.get_instance_status(nova, vm)
91         logger.debug("Status: %s" % status)
92         if status == "ACTIVE":
93             return True
94         if status == "ERROR" or status == "error":
95             return False
96         if count == 0:
97             logger.debug("Booting a VM timed out...")
98             return False
99         count -= 1
100         time.sleep(sleep_time)
101     return False
102
103
104 def create_security_group():
105     sg_id = os_utils.get_security_group_id(neutron_client,
106                                            SECGROUP_NAME)
107     if sg_id != '':
108         logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
109     else:
110         logger.info("Creating security group  '%s'..." % SECGROUP_NAME)
111         SECGROUP = os_utils.create_security_group(neutron_client,
112                                                   SECGROUP_NAME,
113                                                   SECGROUP_DESCR)
114         if not SECGROUP:
115             logger.error("Failed to create the security group...")
116             return False
117
118         sg_id = SECGROUP['id']
119
120         logger.debug("Security group '%s' with ID=%s created successfully."
121                      % (SECGROUP['name'], sg_id))
122
123         logger.debug("Adding ICMP rules in security group '%s'..."
124                      % SECGROUP_NAME)
125         if not os_utils.create_secgroup_rule(neutron_client, sg_id,
126                                              'ingress', 'icmp'):
127             logger.error("Failed to create the security group rule...")
128             return False
129
130         logger.debug("Adding SSH rules in security group '%s'..."
131                      % SECGROUP_NAME)
132         if not os_utils.create_secgroup_rule(neutron_client, sg_id,
133                                              'ingress', 'tcp',
134                                              '22', '22'):
135             logger.error("Failed to create the security group rule...")
136             return False
137
138         if not os_utils.create_secgroup_rule(
139                 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
140             logger.error("Failed to create the security group rule...")
141             return False
142     return sg_id
143
144
145 def create_image():
146     _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
147                                                GLANCE_IMAGE_PATH,
148                                                GLANCE_IMAGE_FORMAT)
149     if not image_id:
150         exit(-1)
151
152     return image_id
153
154
155 def get_flavor():
156     EXIT_CODE = -1
157
158     # Check if the given flavor exists
159     try:
160         flavor = nova_client.flavors.find(name=FLAVOR)
161         logger.info("Using existing Flavor '%s'..." % FLAVOR)
162         return flavor
163     except:
164         logger.error("Flavor '%s' not found." % FLAVOR)
165         logger.info("Available flavors are: ")
166         pMsg(nova_client.flavor.list())
167         exit(EXIT_CODE)
168
169
170 def create_network_full():
171     EXIT_CODE = -1
172
173     network_dic = os_utils.create_network_full(neutron_client,
174                                                PRIVATE_NET_NAME,
175                                                PRIVATE_SUBNET_NAME,
176                                                ROUTER_NAME,
177                                                PRIVATE_SUBNET_CIDR)
178
179     if not network_dic:
180         logger.error(
181             "There has been a problem when creating the neutron network")
182         exit(EXIT_CODE)
183     network_id = network_dic["net_id"]
184     return network_id
185
186
187 def delete_exist_vms():
188     servers = nova_client.servers.list()
189     for server in servers:
190         if server.name == NAME_VM_1 or server.name == NAME_VM_2:
191             logger.info("Instance %s found. Deleting..." % server.name)
192             server.delete()
193
194
195 def is_userdata(case):
196     return case == 'vping_userdata'
197
198
199 def is_ssh(case):
200     return case == 'vping_ssh'
201
202
203 def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id):
204     EXIT_CODE = -1
205
206     config = dict()
207     config['name'] = name
208     config['flavor'] = flavor
209     config['image'] = image_id
210     config['nics'] = [{"net-id": network_id}]
211     if is_userdata(case):
212         config['config_drive'] = True
213         if name == NAME_VM_2:
214             u = ("#!/bin/sh\n\n"
215                  "while true; do\n"
216                  " ping -c 1 %s 2>&1 >/dev/null\n"
217                  " RES=$?\n"
218                  " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
219                  "  echo 'vPing OK'\n"
220                  "  break\n"
221                  " else\n"
222                  "  echo 'vPing KO'\n"
223                  " fi\n"
224                  " sleep 1\n"
225                  "done\n" % test_ip)
226             config['userdata'] = u
227
228     logger.info("Creating instance '%s'..." % name)
229     logger.debug("Configuration: %s" % config)
230     vm = nova_client.servers.create(**config)
231
232     # wait until VM status is active
233     if not waitVmActive(nova_client, vm):
234
235         logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
236             name, os_utils.get_instance_status(nova_client, vm)))
237         exit(EXIT_CODE)
238     else:
239         logger.info("Instance '%s' is ACTIVE." % name)
240
241     add_secgroup(name, vm.id, sg_id)
242
243     return vm
244
245
246 def get_test_ip(vm):
247     test_ip = vm.networks.get(PRIVATE_NET_NAME)[0]
248     logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
249     return test_ip
250
251
252 def add_secgroup(vmname, vm_id, sg_id):
253     logger.info("Adding '%s' to security group '%s'..." %
254                 (vmname, SECGROUP_NAME))
255     os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id)
256
257
258 def add_float_ip(vm):
259     EXIT_CODE = -1
260
261     logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2)
262     floatip_dic = os_utils.create_floating_ip(neutron_client)
263     floatip = floatip_dic['fip_addr']
264
265     if floatip is None:
266         logger.error("Cannot create floating IP.")
267         exit(EXIT_CODE)
268     logger.info("Floating IP created: '%s'" % floatip)
269
270     logger.info("Associating floating ip: '%s' to VM '%s' "
271                 % (floatip, NAME_VM_2))
272     if not os_utils.add_floating_ip(nova_client, vm.id, floatip):
273         logger.error("Cannot associate floating IP to VM.")
274         exit(EXIT_CODE)
275
276     return floatip
277
278
279 def establish_ssh(vm, floatip):
280     EXIT_CODE = -1
281
282     logger.info("Trying to establish SSH connection to %s..." % floatip)
283     username = 'cirros'
284     password = 'cubswin:)'
285     ssh = paramiko.SSHClient()
286     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
287
288     timeout = 50
289     nolease = False
290     got_ip = False
291     discover_count = 0
292     cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0]
293     while timeout > 0:
294         try:
295             ssh.connect(floatip, username=username,
296                         password=password, timeout=2)
297             logger.debug("SSH connection established to %s." % floatip)
298             break
299         except:
300             logger.debug("Waiting for %s..." % floatip)
301             time.sleep(6)
302             timeout -= 1
303
304         console_log = vm.get_console_output()
305
306         # print each "Sending discover" captured on the console log
307         if (len(re.findall("Sending discover", console_log)) >
308                 discover_count and not got_ip):
309             discover_count += 1
310             logger.debug("Console-log '%s': Sending discover..."
311                          % NAME_VM_2)
312
313         # check if eth0 got an ip,the line looks like this:
314         # "inet addr:192.168."....
315         # if the dhcp agent fails to assing ip, this line will not appear
316         if "inet addr:" + cidr_first_octet in console_log and not got_ip:
317             got_ip = True
318             logger.debug("The instance '%s' succeeded to get the IP "
319                          "from the dhcp agent." % NAME_VM_2)
320
321         # if dhcp doesnt work,it shows "No lease, failing".The test will fail
322         if "No lease, failing" in console_log and not nolease and not got_ip:
323             nolease = True
324             logger.debug("Console-log '%s': No lease, failing..."
325                          % NAME_VM_2)
326             logger.info("The instance failed to get an IP from the "
327                         "DHCP agent. The test will probably timeout...")
328
329     if timeout == 0:  # 300 sec timeout (5 min)
330         logger.error("Cannot establish connection to IP '%s'. Aborting"
331                      % floatip)
332         exit(EXIT_CODE)
333     return ssh
334
335
336 def transfer_ping_script(ssh, floatip):
337     EXIT_CODE = -1
338
339     logger.info("Trying to transfer ping.sh to %s..." % floatip)
340     scp = SCPClient(ssh.get_transport())
341
342     ping_script = FUNCTEST_REPO + "/testcases/OpenStack/vPing/ping.sh"
343     try:
344         scp.put(ping_script, "~/")
345     except:
346         logger.error("Cannot SCP the file '%s' to VM '%s'"
347                      % (ping_script, floatip))
348         exit(EXIT_CODE)
349
350     cmd = 'chmod 755 ~/ping.sh'
351     (stdin, stdout, stderr) = ssh.exec_command(cmd)
352     for line in stdout.readlines():
353         print line
354
355
356 def do_vping_ssh(ssh, test_ip):
357     logger.info("Waiting for ping...")
358
359     sec = 0
360     cmd = '~/ping.sh ' + test_ip
361     flag = False
362
363     while True:
364         time.sleep(1)
365         (stdin, stdout, stderr) = ssh.exec_command(cmd)
366         output = stdout.readlines()
367
368         for line in output:
369             if "vPing OK" in line:
370                 logger.info("vPing detected!")
371                 EXIT_CODE = 0
372                 flag = True
373                 break
374
375             elif sec == PING_TIMEOUT:
376                 logger.info("Timeout reached.")
377                 flag = True
378                 break
379         if flag:
380             break
381         logger.debug("Pinging %s. Waiting for response..." % test_ip)
382         sec += 1
383     return EXIT_CODE, time.time()
384
385
386 def do_vping_userdata(vm, test_ip):
387     logger.info("Waiting for ping...")
388     EXIT_CODE = -1
389     sec = 0
390     metadata_tries = 0
391
392     while True:
393         time.sleep(1)
394         console_log = vm.get_console_output()
395         if "vPing OK" in console_log:
396             logger.info("vPing detected!")
397             EXIT_CODE = 0
398             break
399         elif ("failed to read iid from metadata" in console_log or
400               metadata_tries > 5):
401             EXIT_CODE = -2
402             break
403         elif sec == PING_TIMEOUT:
404             logger.info("Timeout reached.")
405             break
406         elif sec % 10 == 0:
407             if "request failed" in console_log:
408                 logger.debug("It seems userdata is not supported in "
409                              "nova boot. Waiting a bit...")
410                 metadata_tries += 1
411             else:
412                 logger.debug("Pinging %s. Waiting for response..." % test_ip)
413         sec += 1
414
415     return EXIT_CODE, time.time()
416
417
418 def do_vping(case, vm, test_ip):
419     if is_userdata(case):
420         return do_vping_userdata(vm, test_ip)
421     else:
422         floatip = add_float_ip(vm)
423         ssh = establish_ssh(vm, floatip)
424         transfer_ping_script(ssh, floatip)
425         return do_vping_ssh(ssh, test_ip)
426
427
428 def check_result(code, start_time, stop_time):
429     test_status = "FAIL"
430     if code == 0:
431         logger.info("vPing OK")
432         duration = round(stop_time - start_time, 1)
433         logger.info("vPing duration:'%s'" % duration)
434         test_status = "PASS"
435     elif code == -2:
436         duration = 0
437         logger.info("Userdata is not supported in nova boot. Aborting test...")
438     else:
439         duration = 0
440         logger.error("vPing FAILED")
441
442     details = {'timestart': start_time,
443                'duration': duration,
444                'status': test_status}
445
446     return details
447
448
449 def push_result(report, case, start_time, stop_time, details):
450     if report:
451         try:
452             logger.debug("Pushing vPing %s results into DB..." % case)
453             ft_utils.push_results_to_db('functest',
454                                         case,
455                                         start_time,
456                                         stop_time,
457                                         details['status'],
458                                         details=details)
459         except:
460             logger.error("Error pushing results into Database '%s'"
461                          % sys.exc_info()[0])