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