Bugfix: logger ..
[functest.git] / testcases / functest_utils.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11
12 import os
13 import os.path
14 import urllib2
15 import subprocess
16 import sys
17 import requests
18 import json
19 import shutil
20 from git import Repo
21
22
23 # ############ CREDENTIALS OPENSTACK #############
24 def check_credentials():
25     """
26     Check if the OpenStack credentials (openrc) are sourced
27     """
28     env_vars = ['OS_AUTH_URL','OS_USERNAME','OS_PASSWORD','OS_TENANT_NAME']
29     return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
30
31
32 def get_credentials(service):
33     """Returns a creds dictionary filled with the following keys:
34     * username
35     * password/api_key (depending on the service)
36     * tenant_name/project_id (depending on the service)
37     * auth_url
38     :param service: a string indicating the name of the service
39                     requesting the credentials.
40     """
41     creds = {}
42     # Unfortunately, each of the OpenStack client will request slightly
43     # different entries in their credentials dict.
44     if service.lower() in ("nova", "cinder"):
45         password = "api_key"
46         tenant = "project_id"
47     else:
48         password = "password"
49         tenant = "tenant_name"
50
51     # The most common way to pass these info to the script is to do it through
52     # environment variables.
53     creds.update({
54         "username": os.environ.get('OS_USERNAME', "admin"),
55         password: os.environ.get("OS_PASSWORD", 'admin'),
56         "auth_url": os.environ.get("OS_AUTH_URL",
57                                    "http://192.168.20.71:5000/v2.0"),
58         tenant: os.environ.get("OS_TENANT_NAME", "admin"),
59     })
60
61     return creds
62
63
64 # ################ NOVA #################
65 def get_instances(nova_client):
66     try:
67         instances = nova_client.servers.list(search_opts={'all_tenants': 1})
68         return instances
69     except:
70         return None
71
72 def get_instance_status(nova_client, instance):
73     try:
74         instance = nova_client.servers.get(instance.id)
75         return instance.status
76     except:
77         return None
78
79 def get_instance_by_name(nova_client, instance_name):
80     try:
81         instance = nova_client.servers.find(name=instance_name)
82         return instance
83     except:
84         return None
85
86
87
88 def get_flavor_id(nova_client, flavor_name):
89     flavors = nova_client.flavors.list(detailed=True)
90     id = ''
91     for f in flavors:
92         if f.name == flavor_name:
93             id = f.id
94             break
95     return id
96
97
98 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
99     flavors = nova_client.flavors.list(detailed=True)
100     id = ''
101     for f in flavors:
102         if min_ram <= f.ram and f.ram <= max_ram:
103             id = f.id
104             break
105     return id
106
107
108 def delete_instance(nova_client, instance_id):
109     try:
110         nova_client.servers.force_delete(instance_id)
111         return True
112     except:
113         print "Error:", sys.exc_info()[0]
114         return False
115
116
117 def get_floating_ips(nova_client):
118     try:
119         floating_ips = nova_client.floating_ips.list()
120         return floating_ips
121     except:
122         return None
123
124 def delete_floating_ip(nova_client, floatingip_id):
125     try:
126         nova_client.floating_ips.delete(floatingip_id)
127         return True
128     except:
129         print "Error:", sys.exc_info()[0]
130         return None
131
132
133 # ################ NEUTRON #################
134 def create_neutron_net(neutron_client, name):
135     json_body = {'network': {'name': name,
136                              'admin_state_up': True}}
137     try:
138         network = neutron_client.create_network(body=json_body)
139         network_dict = network['network']
140         return network_dict['id']
141     except:
142         print "Error:", sys.exc_info()[0]
143         return False
144
145
146 def delete_neutron_net(neutron_client, network_id):
147     try:
148         neutron_client.delete_network(network_id)
149         return True
150     except:
151         print "Error:", sys.exc_info()[0]
152         return False
153
154
155 def create_neutron_subnet(neutron_client, name, cidr, net_id):
156     json_body = {'subnets': [{'name': name, 'cidr': cidr,
157                              'ip_version': 4, 'network_id': net_id}]}
158     try:
159         subnet = neutron_client.create_subnet(body=json_body)
160         return subnet['subnets'][0]['id']
161     except:
162         print "Error:", sys.exc_info()[0]
163         return False
164
165
166 def delete_neutron_subnet(neutron_client, subnet_id):
167     try:
168         neutron_client.delete_subnet(subnet_id)
169         return True
170     except:
171         print "Error:", sys.exc_info()[0]
172         return False
173
174
175 def create_neutron_router(neutron_client, name):
176     json_body = {'router': {'name': name, 'admin_state_up': True}}
177     try:
178         router = neutron_client.create_router(json_body)
179         return router['router']['id']
180     except:
181         print "Error:", sys.exc_info()[0]
182         return False
183
184
185 def delete_neutron_router(neutron_client, router_id):
186     json_body = {'router': {'id': router_id}}
187     try:
188         neutron_client.delete_router(router=router_id)
189         return True
190     except:
191         print "Error:", sys.exc_info()[0]
192         return False
193
194
195 def add_interface_router(neutron_client, router_id, subnet_id):
196     json_body = {"subnet_id": subnet_id}
197     try:
198         neutron_client.add_interface_router(router=router_id, body=json_body)
199         return True
200     except:
201         print "Error:", sys.exc_info()[0]
202         return False
203
204
205 def remove_interface_router(neutron_client, router_id, subnet_id):
206     json_body = {"subnet_id": subnet_id}
207     try:
208         neutron_client.remove_interface_router(router=router_id,
209                                                body=json_body)
210         return True
211     except:
212         print "Error:", sys.exc_info()[0]
213         return False
214
215 def remove_gateway_router(neutron_client, router_id):
216     try:
217         neutron_client.remove_gateway_router(router_id)
218         return True
219     except:
220         print "Error:", sys.exc_info()[0]
221         return False
222
223
224 def create_neutron_port(neutron_client, name, network_id, ip):
225     json_body = {'port': {
226                  'admin_state_up': True,
227                  'name': name,
228                  'network_id': network_id,
229                  'fixed_ips': [{"ip_address": ip}]
230                  }}
231     try:
232         port = neutron_client.create_port(body=json_body)
233         return port['port']['id']
234     except:
235         print "Error:", sys.exc_info()[0]
236         return False
237
238
239 def update_neutron_port(neutron_client, port_id, device_owner):
240     json_body = {'port': {
241                  'device_owner': device_owner,
242                  }}
243     try:
244         port = neutron_client.update_port(port=port_id,
245                                           body=json_body)
246         return port['port']['id']
247     except:
248         print "Error:", sys.exc_info()[0]
249         return False
250
251
252 def delete_neutron_port(neutron_client, port_id):
253     try:
254         neutron_client.delete_port(port_id)
255         return True
256     except:
257         print "Error:", sys.exc_info()[0]
258         return False
259
260
261 def get_network_id(neutron_client, network_name):
262     networks = neutron_client.list_networks()['networks']
263     id = ''
264     for n in networks:
265         if n['name'] == network_name:
266             id = n['id']
267             break
268     return id
269
270
271 def check_neutron_net(neutron_client, net_name):
272     for network in neutron_client.list_networks()['networks']:
273         if network['name'] == net_name:
274             for subnet in network['subnets']:
275                 return True
276     return False
277
278
279 def get_network_list(neutron_client):
280     network_list = neutron_client.list_networks()['networks']
281     if len(network_list) == 0:
282         return None
283     else:
284         return network_list
285
286
287 def get_router_list(neutron_client):
288     router_list = neutron_client.list_routers()['routers']
289     if len(router_list) == 0:
290         return None
291     else:
292         return router_list
293
294 def get_port_list(neutron_client):
295     port_list = neutron_client.list_ports()['ports']
296     if len(port_list) == 0:
297         return None
298     else:
299         return port_list
300
301
302
303 def get_external_net(neutron_client):
304     for network in neutron_client.list_networks()['networks']:
305         if network['router:external']:
306             return network['name']
307     return False
308
309
310 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
311     json_body = {"quota": {
312         "security_group": sg_quota,
313         "security_group_rule": sg_rule_quota
314     }}
315
316     try:
317         quota = neutron_client.update_quota(tenant_id=tenant_id,
318                                             body=json_body)
319         return True
320     except:
321         print "Error:", sys.exc_info()[0]
322         return False
323
324 def update_cinder_quota(cinder_client, tenant_id, vols_quota, snapshots_quota,gigabytes_quota):
325     quotas_values = {
326             "volumes": vols_quota,
327             "snapshots": snapshots_quota,
328             "gigabytes": gigabytes_quota
329     }
330
331     try:
332         quotas_default=cinder_client.quotas.update(tenant_id,**quotas_values)
333         return True
334     except:
335         print "Error:", sys.exc_info()[0]
336         return False
337
338 def get_private_net(neutron_client):
339     # Checks if there is an existing private network
340     networks = neutron_client.list_networks()['networks']
341     if len(networks) == 0:
342         return None
343     for net in networks:
344         if net['router:external'] == False:
345             return net
346     return None
347
348 # ################ GLANCE #################
349 def get_images(nova_client):
350     try:
351         images = nova_client.images.list()
352         return images
353     except:
354         return None
355
356
357 def get_image_id(glance_client, image_name):
358     images = glance_client.images.list()
359     id = ''
360     for i in images:
361         if i.name == image_name:
362             id = i.id
363             break
364     return id
365
366
367 def create_glance_image(glance_client, image_name, file_path, public=True):
368     if not os.path.isfile(file_path):
369         print "Error: file "+file_path+" does not exist."
370         return False
371     try:
372         with open(file_path) as fimage:
373             image = glance_client.images.create(name=image_name,
374                                                 is_public=public,
375                                                 disk_format="qcow2",
376                                                 container_format="bare",
377                                                 data=fimage)
378         return image.id
379     except:
380         print "Error:", sys.exc_info()[0]
381         return False
382
383 def delete_glance_image(nova_client, image_id):
384     try:
385         nova_client.images.delete(image_id)
386         return True
387     except:
388         print "Error:", sys.exc_info()[0]
389         return False
390
391 # ################ CINDER #################
392 def get_volumes(cinder_client):
393     try:
394         volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
395         return volumes
396     except:
397         return None
398
399 def delete_volume(cinder_client, volume_id):
400     try:
401         cinder_client.volumes.delete(volume_id)
402         return True
403     except:
404         print "Error:", sys.exc_info()[0]
405         return False
406
407 # ################ CINDER #################
408 def get_security_groups(neutron_client):
409     try:
410         security_groups = neutron_client.list_security_groups()['security_groups']
411         return security_groups
412     except:
413         return None
414
415 def delete_security_group(neutron_client, secgroup_id):
416     try:
417         neutron_client.delete_security_group(secgroup_id)
418         return True
419     except:
420         print "Error:", sys.exc_info()[0]
421         return False
422
423
424 # ################ KEYSTONE #################
425 def get_tenants(keystone_client):
426     try:
427         tenants = keystone_client.tenants.list()
428         return tenants
429     except:
430         return None
431
432
433 def get_tenant_id(keystone_client, tenant_name):
434     tenants = keystone_client.tenants.list()
435     id = ''
436     for t in tenants:
437         if t.name == tenant_name:
438             id = t.id
439             break
440     return id
441
442 def get_users(keystone_client):
443     try:
444         users = keystone_client.users.list()
445         return users
446     except:
447         return None
448
449 def get_role_id(keystone_client, role_name):
450     roles = keystone_client.roles.list()
451     id = ''
452     for r in roles:
453         if r.name == role_name:
454             id = r.id
455             break
456     return id
457
458
459 def get_user_id(keystone_client, user_name):
460     users = keystone_client.users.list()
461     id = ''
462     for u in users:
463         if u.name == user_name:
464             id = u.id
465             break
466     return id
467
468
469 def create_tenant(keystone_client, tenant_name, tenant_description):
470     try:
471         tenant = keystone_client.tenants.create(tenant_name,
472                                                 tenant_description,
473                                                 enabled=True)
474         return tenant.id
475     except:
476         print "Error:", sys.exc_info()[0]
477         return False
478
479
480 def delete_tenant(keystone_client, tenant_id):
481     try:
482         tenant = keystone_client.tenants.delete(tenant_id)
483         return True
484     except:
485         print "Error:", sys.exc_info()[0]
486         return False
487
488
489 def create_user(keystone_client, user_name, user_password,
490                 user_email, tenant_id):
491     try:
492         user = keystone_client.users.create(user_name, user_password,
493                                             user_email, tenant_id,
494                                             enabled=True)
495         return user.id
496     except:
497         print "Error:", sys.exc_info()[0]
498         return False
499
500
501 def delete_user(keystone_client, user_id):
502     try:
503         tenant = keystone_client.users.delete(user_id)
504         return True
505     except:
506         print "Error:", sys.exc_info()[0]
507         return False
508
509
510 def add_role_user(keystone_client, user_id, role_id, tenant_id):
511     try:
512         keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
513         return True
514     except:
515         print "Error:", sys.exc_info()[0]
516         return False
517
518
519 # ################ UTILS #################
520 def check_internet_connectivity(url='http://www.opnfv.org/'):
521     """
522     Check if there is access to the internet
523     """
524     try:
525         urllib2.urlopen(url, timeout=5)
526         return True
527     except urllib2.URLError:
528         return False
529
530
531 def download_url(url, dest_path):
532     """
533     Download a file to a destination path given a URL
534     """
535     name = url.rsplit('/')[-1]
536     dest = dest_path + "/" + name
537     try:
538         response = urllib2.urlopen(url)
539     except (urllib2.HTTPError, urllib2.URLError):
540         return False
541
542     with open(dest, 'wb') as f:
543         shutil.copyfileobj(response, f)
544     return True
545
546
547 def execute_command(cmd, logger=None):
548     """
549     Execute Linux command
550     """
551     if logger:
552         logger.debug('Executing command : {}'.format(cmd))
553     output_file = "output.txt"
554     f = open(output_file, 'w+')
555     p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
556     f.close()
557     f = open(output_file, 'r')
558     result = f.read()
559     if result != "" and logger:
560         logger.debug(result)
561     if p == 0:
562         return True
563     else:
564         if logger:
565             logger.error("Error when executing command %s" % cmd)
566         exit(-1)
567
568
569 def get_git_branch(repo_path):
570     """
571     Get git branch name
572     """
573     repo = Repo(repo_path)
574     branch = repo.active_branch
575     return branch.name
576
577
578 def get_installer_type(logger=None):
579     """
580     Get installer type (fuel, foreman, apex, joid, compass)
581     """
582     try:
583         installer = os.environ['INSTALLER_TYPE']
584     except KeyError:
585         if logger:
586             logger.error("Impossible to retrieve the installer type")
587         installer = "Unkown"
588
589     return installer
590
591
592 def get_pod_name(logger=None):
593     """
594     Get PoD Name from env variable NODE_NAME
595     """
596     try:
597         return os.environ['NODE_NAME']
598     except KeyError:
599         if logger:
600             logger.error("Unable to retrieve the POD name from environment.Using pod name 'unknown-pod'")
601         return "unknown-pod"
602
603
604 def push_results_to_db(db_url, case_name, logger, pod_name, git_version, payload):
605     url = db_url + "/results"
606     installer = get_installer_type(logger)
607     params = {"project_name": "functest", "case_name": case_name,
608               "pod_name": pod_name, "installer": installer,
609               "version": git_version, "details": payload}
610
611     headers = {'Content-Type': 'application/json'}
612     try:
613         r = requests.post(url, data=json.dumps(params), headers=headers)
614         logger.debug(r)
615         return True
616     except:
617         print "Error:", sys.exc_info()[0]
618         return False