add .gitignore
[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 update_neutron_net(neutron_client, network_id, shared=False):
147     json_body = {'network': {'shared': shared}}
148     try:
149         neutron_client.update_network(network_id, body=json_body)
150         return True
151     except:
152         print "Error:", sys.exc_info()[0]
153         return False
154
155
156 def delete_neutron_net(neutron_client, network_id):
157     try:
158         neutron_client.delete_network(network_id)
159         return True
160     except:
161         print "Error:", sys.exc_info()[0]
162         return False
163
164
165 def create_neutron_subnet(neutron_client, name, cidr, net_id):
166     json_body = {'subnets': [{'name': name, 'cidr': cidr,
167                              'ip_version': 4, 'network_id': net_id}]}
168     try:
169         subnet = neutron_client.create_subnet(body=json_body)
170         return subnet['subnets'][0]['id']
171     except:
172         print "Error:", sys.exc_info()[0]
173         return False
174
175
176 def delete_neutron_subnet(neutron_client, subnet_id):
177     try:
178         neutron_client.delete_subnet(subnet_id)
179         return True
180     except:
181         print "Error:", sys.exc_info()[0]
182         return False
183
184
185 def create_neutron_router(neutron_client, name):
186     json_body = {'router': {'name': name, 'admin_state_up': True}}
187     try:
188         router = neutron_client.create_router(json_body)
189         return router['router']['id']
190     except:
191         print "Error:", sys.exc_info()[0]
192         return False
193
194
195 def delete_neutron_router(neutron_client, router_id):
196     json_body = {'router': {'id': router_id}}
197     try:
198         neutron_client.delete_router(router=router_id)
199         return True
200     except:
201         print "Error:", sys.exc_info()[0]
202         return False
203
204
205 def add_interface_router(neutron_client, router_id, subnet_id):
206     json_body = {"subnet_id": subnet_id}
207     try:
208         neutron_client.add_interface_router(router=router_id, body=json_body)
209         return True
210     except:
211         print "Error:", sys.exc_info()[0]
212         return False
213
214
215 def remove_interface_router(neutron_client, router_id, subnet_id):
216     json_body = {"subnet_id": subnet_id}
217     try:
218         neutron_client.remove_interface_router(router=router_id,
219                                                body=json_body)
220         return True
221     except:
222         print "Error:", sys.exc_info()[0]
223         return False
224
225 def remove_gateway_router(neutron_client, router_id):
226     try:
227         neutron_client.remove_gateway_router(router_id)
228         return True
229     except:
230         print "Error:", sys.exc_info()[0]
231         return False
232
233
234 def create_neutron_port(neutron_client, name, network_id, ip):
235     json_body = {'port': {
236                  'admin_state_up': True,
237                  'name': name,
238                  'network_id': network_id,
239                  'fixed_ips': [{"ip_address": ip}]
240                  }}
241     try:
242         port = neutron_client.create_port(body=json_body)
243         return port['port']['id']
244     except:
245         print "Error:", sys.exc_info()[0]
246         return False
247
248
249 def update_neutron_port(neutron_client, port_id, device_owner):
250     json_body = {'port': {
251                  'device_owner': device_owner,
252                  }}
253     try:
254         port = neutron_client.update_port(port=port_id,
255                                           body=json_body)
256         return port['port']['id']
257     except:
258         print "Error:", sys.exc_info()[0]
259         return False
260
261
262 def delete_neutron_port(neutron_client, port_id):
263     try:
264         neutron_client.delete_port(port_id)
265         return True
266     except:
267         print "Error:", sys.exc_info()[0]
268         return False
269
270
271 def get_network_id(neutron_client, network_name):
272     networks = neutron_client.list_networks()['networks']
273     id = ''
274     for n in networks:
275         if n['name'] == network_name:
276             id = n['id']
277             break
278     return id
279
280
281 def check_neutron_net(neutron_client, net_name):
282     for network in neutron_client.list_networks()['networks']:
283         if network['name'] == net_name:
284             for subnet in network['subnets']:
285                 return True
286     return False
287
288
289 def get_network_list(neutron_client):
290     network_list = neutron_client.list_networks()['networks']
291     if len(network_list) == 0:
292         return None
293     else:
294         return network_list
295
296
297 def get_router_list(neutron_client):
298     router_list = neutron_client.list_routers()['routers']
299     if len(router_list) == 0:
300         return None
301     else:
302         return router_list
303
304 def get_port_list(neutron_client):
305     port_list = neutron_client.list_ports()['ports']
306     if len(port_list) == 0:
307         return None
308     else:
309         return port_list
310
311
312
313 def get_external_net(neutron_client):
314     for network in neutron_client.list_networks()['networks']:
315         if network['router:external']:
316             return network['name']
317     return False
318
319
320 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
321     json_body = {"quota": {
322         "security_group": sg_quota,
323         "security_group_rule": sg_rule_quota
324     }}
325
326     try:
327         quota = neutron_client.update_quota(tenant_id=tenant_id,
328                                             body=json_body)
329         return True
330     except:
331         print "Error:", sys.exc_info()[0]
332         return False
333
334 def update_cinder_quota(cinder_client, tenant_id, vols_quota, snapshots_quota,gigabytes_quota):
335     quotas_values = {
336             "volumes": vols_quota,
337             "snapshots": snapshots_quota,
338             "gigabytes": gigabytes_quota
339     }
340
341     try:
342         quotas_default=cinder_client.quotas.update(tenant_id,**quotas_values)
343         return True
344     except:
345         print "Error:", sys.exc_info()[0]
346         return False
347
348 def get_private_net(neutron_client):
349     # Checks if there is an existing shared private network
350     networks = neutron_client.list_networks()['networks']
351     if len(networks) == 0:
352         return None
353     for net in networks:
354         if (net['router:external'] == False) and (net['shared'] == True):
355             return net
356     return None
357
358 # ################ GLANCE #################
359 def get_images(nova_client):
360     try:
361         images = nova_client.images.list()
362         return images
363     except:
364         return None
365
366
367 def get_image_id(glance_client, image_name):
368     images = glance_client.images.list()
369     id = ''
370     for i in images:
371         if i.name == image_name:
372             id = i.id
373             break
374     return id
375
376
377 def create_glance_image(glance_client, image_name, file_path, public=True):
378     if not os.path.isfile(file_path):
379         print "Error: file "+file_path+" does not exist."
380         return False
381     try:
382         with open(file_path) as fimage:
383             image = glance_client.images.create(name=image_name,
384                                                 is_public=public,
385                                                 disk_format="qcow2",
386                                                 container_format="bare",
387                                                 data=fimage)
388         return image.id
389     except:
390         print "Error:", sys.exc_info()[0]
391         return False
392
393 def delete_glance_image(nova_client, image_id):
394     try:
395         nova_client.images.delete(image_id)
396         return True
397     except:
398         print "Error:", sys.exc_info()[0]
399         return False
400
401 # ################ CINDER #################
402 def get_volumes(cinder_client):
403     try:
404         volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
405         return volumes
406     except:
407         return None
408
409 def delete_volume(cinder_client, volume_id):
410     try:
411         cinder_client.volumes.delete(volume_id)
412         return True
413     except:
414         print "Error:", sys.exc_info()[0]
415         return False
416
417 # ################ CINDER #################
418 def get_security_groups(neutron_client):
419     try:
420         security_groups = neutron_client.list_security_groups()['security_groups']
421         return security_groups
422     except:
423         return None
424
425 def delete_security_group(neutron_client, secgroup_id):
426     try:
427         neutron_client.delete_security_group(secgroup_id)
428         return True
429     except:
430         print "Error:", sys.exc_info()[0]
431         return False
432
433
434 # ################ KEYSTONE #################
435 def get_tenants(keystone_client):
436     try:
437         tenants = keystone_client.tenants.list()
438         return tenants
439     except:
440         return None
441
442
443 def get_tenant_id(keystone_client, tenant_name):
444     tenants = keystone_client.tenants.list()
445     id = ''
446     for t in tenants:
447         if t.name == tenant_name:
448             id = t.id
449             break
450     return id
451
452 def get_users(keystone_client):
453     try:
454         users = keystone_client.users.list()
455         return users
456     except:
457         return None
458
459 def get_role_id(keystone_client, role_name):
460     roles = keystone_client.roles.list()
461     id = ''
462     for r in roles:
463         if r.name == role_name:
464             id = r.id
465             break
466     return id
467
468
469 def get_user_id(keystone_client, user_name):
470     users = keystone_client.users.list()
471     id = ''
472     for u in users:
473         if u.name == user_name:
474             id = u.id
475             break
476     return id
477
478
479 def create_tenant(keystone_client, tenant_name, tenant_description):
480     try:
481         tenant = keystone_client.tenants.create(tenant_name,
482                                                 tenant_description,
483                                                 enabled=True)
484         return tenant.id
485     except:
486         print "Error:", sys.exc_info()[0]
487         return False
488
489
490 def delete_tenant(keystone_client, tenant_id):
491     try:
492         tenant = keystone_client.tenants.delete(tenant_id)
493         return True
494     except:
495         print "Error:", sys.exc_info()[0]
496         return False
497
498
499 def create_user(keystone_client, user_name, user_password,
500                 user_email, tenant_id):
501     try:
502         user = keystone_client.users.create(user_name, user_password,
503                                             user_email, tenant_id,
504                                             enabled=True)
505         return user.id
506     except:
507         print "Error:", sys.exc_info()[0]
508         return False
509
510
511 def delete_user(keystone_client, user_id):
512     try:
513         tenant = keystone_client.users.delete(user_id)
514         return True
515     except:
516         print "Error:", sys.exc_info()[0]
517         return False
518
519
520 def add_role_user(keystone_client, user_id, role_id, tenant_id):
521     try:
522         keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
523         return True
524     except:
525         print "Error:", sys.exc_info()[0]
526         return False
527
528
529 # ################ UTILS #################
530 def check_internet_connectivity(url='http://www.opnfv.org/'):
531     """
532     Check if there is access to the internet
533     """
534     try:
535         urllib2.urlopen(url, timeout=5)
536         return True
537     except urllib2.URLError:
538         return False
539
540
541 def download_url(url, dest_path):
542     """
543     Download a file to a destination path given a URL
544     """
545     name = url.rsplit('/')[-1]
546     dest = dest_path + "/" + name
547     try:
548         response = urllib2.urlopen(url)
549     except (urllib2.HTTPError, urllib2.URLError):
550         return False
551
552     with open(dest, 'wb') as f:
553         shutil.copyfileobj(response, f)
554     return True
555
556
557 def execute_command(cmd, logger=None):
558     """
559     Execute Linux command
560     """
561     if logger:
562         logger.debug('Executing command : {}'.format(cmd))
563     output_file = "output.txt"
564     f = open(output_file, 'w+')
565     p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
566     f.close()
567     f = open(output_file, 'r')
568     result = f.read()
569     if result != "" and logger:
570         logger.debug(result)
571     if p == 0:
572         return True
573     else:
574         if logger:
575             logger.error("Error when executing command %s" % cmd)
576         exit(-1)
577
578
579 def get_git_branch(repo_path):
580     """
581     Get git branch name
582     """
583     repo = Repo(repo_path)
584     branch = repo.active_branch
585     return branch.name
586
587
588 def get_installer_type(logger=None):
589     """
590     Get installer type (fuel, foreman, apex, joid, compass)
591     """
592     try:
593         installer = os.environ['INSTALLER_TYPE']
594     except KeyError:
595         if logger:
596             logger.error("Impossible to retrieve the installer type")
597         installer = "Unkown"
598
599     return installer
600
601
602 def get_pod_name(logger=None):
603     """
604     Get PoD Name from env variable NODE_NAME
605     """
606     try:
607         return os.environ['NODE_NAME']
608     except KeyError:
609         if logger:
610             logger.error("Unable to retrieve the POD name from environment.Using pod name 'unknown-pod'")
611         return "unknown-pod"
612
613
614 def push_results_to_db(db_url, case_name, logger, pod_name, git_version, payload):
615     url = db_url + "/results"
616     installer = get_installer_type(logger)
617     params = {"project_name": "functest", "case_name": case_name,
618               "pod_name": pod_name, "installer": installer,
619               "version": git_version, "details": payload}
620
621     headers = {'Content-Type': 'application/json'}
622     try:
623         r = requests.post(url, data=json.dumps(params), headers=headers)
624         logger.debug(r)
625         return True
626     except:
627         print "Error:", sys.exc_info()[0]
628         return False