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