Update orchestrator (cloudify) version from 3.2 to 3.3
[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 delete_neutron_port(neutron_client, port_id):
240     try:
241         neutron_client.delete_port(port_id)
242         return True
243     except:
244         print "Error:", sys.exc_info()[0]
245         return False
246
247
248 def get_network_id(neutron_client, network_name):
249     networks = neutron_client.list_networks()['networks']
250     id = ''
251     for n in networks:
252         if n['name'] == network_name:
253             id = n['id']
254             break
255     return id
256
257
258 def check_neutron_net(neutron_client, net_name):
259     for network in neutron_client.list_networks()['networks']:
260         if network['name'] == net_name:
261             for subnet in network['subnets']:
262                 return True
263     return False
264
265
266 def get_network_list(neutron_client):
267     network_list = neutron_client.list_networks()['networks']
268     if len(network_list) == 0:
269         return None
270     else:
271         return network_list
272
273
274 def get_router_list(neutron_client):
275     router_list = neutron_client.list_routers()['routers']
276     if len(router_list) == 0:
277         return None
278     else:
279         return router_list
280
281 def get_port_list(neutron_client):
282     port_list = neutron_client.list_ports()['ports']
283     if len(port_list) == 0:
284         return None
285     else:
286         return port_list
287
288
289
290 def get_external_net(neutron_client):
291     for network in neutron_client.list_networks()['networks']:
292         if network['router:external']:
293             return network['name']
294     return False
295
296
297 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
298     json_body = {"quota": {
299         "security_group": sg_quota,
300         "security_group_rule": sg_rule_quota
301     }}
302
303     try:
304         quota = neutron_client.update_quota(tenant_id=tenant_id,
305                                             body=json_body)
306         return True
307     except:
308         print "Error:", sys.exc_info()[0]
309         return False
310
311
312 def get_private_net(neutron_client):
313     # Checks if there is an existing private network
314     networks = neutron_client.list_networks()['networks']
315     if len(networks) == 0:
316         return None
317     for net in networks:
318         if net['router:external'] == False:
319             return net
320     return None
321
322 # ################ GLANCE #################
323 def get_images(nova_client):
324     try:
325         images = nova_client.images.list()
326         return images
327     except:
328         return None
329
330
331 def get_image_id(glance_client, image_name):
332     images = glance_client.images.list()
333     id = ''
334     for i in images:
335         if i.name == image_name:
336             id = i.id
337             break
338     return id
339
340
341 def create_glance_image(glance_client, image_name, file_path, public=True):
342     if not os.path.isfile(file_path):
343         print "Error: file "+file_path+" does not exist."
344         return False
345     try:
346         with open(file_path) as fimage:
347             image = glance_client.images.create(name=image_name,
348                                                 is_public=public,
349                                                 disk_format="qcow2",
350                                                 container_format="bare",
351                                                 data=fimage)
352         return image.id
353     except:
354         print "Error:", sys.exc_info()[0]
355         return False
356
357 def delete_glance_image(nova_client, image_id):
358     try:
359         nova_client.images.delete(image_id)
360         return True
361     except:
362         print "Error:", sys.exc_info()[0]
363         return False
364
365 # ################ CINDER #################
366 def get_volumes(cinder_client):
367     try:
368         volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
369         return volumes
370     except:
371         return None
372
373 def delete_volume(cinder_client, volume_id):
374     try:
375         cinder_client.volumes.delete(volume_id)
376         return True
377     except:
378         print "Error:", sys.exc_info()[0]
379         return False
380
381 # ################ CINDER #################
382 def get_security_groups(neutron_client):
383     try:
384         security_groups = neutron_client.list_security_groups()['security_groups']
385         return security_groups
386     except:
387         return None
388
389 def delete_security_group(neutron_client, secgroup_id):
390     try:
391         neutron_client.delete_security_group(secgroup_id)
392         return True
393     except:
394         print "Error:", sys.exc_info()[0]
395         return False
396
397
398 # ################ KEYSTONE #################
399 def get_tenants(keystone_client):
400     try:
401         tenants = keystone_client.tenants.list()
402         return tenants
403     except:
404         return None
405
406
407 def get_tenant_id(keystone_client, tenant_name):
408     tenants = keystone_client.tenants.list()
409     id = ''
410     for t in tenants:
411         if t.name == tenant_name:
412             id = t.id
413             break
414     return id
415
416 def get_users(keystone_client):
417     try:
418         users = keystone_client.users.list()
419         return users
420     except:
421         return None
422
423 def get_role_id(keystone_client, role_name):
424     roles = keystone_client.roles.list()
425     id = ''
426     for r in roles:
427         if r.name == role_name:
428             id = r.id
429             break
430     return id
431
432
433 def get_user_id(keystone_client, user_name):
434     users = keystone_client.users.list()
435     id = ''
436     for u in users:
437         if u.name == user_name:
438             id = u.id
439             break
440     return id
441
442
443 def create_tenant(keystone_client, tenant_name, tenant_description):
444     try:
445         tenant = keystone_client.tenants.create(tenant_name,
446                                                 tenant_description,
447                                                 enabled=True)
448         return tenant.id
449     except:
450         print "Error:", sys.exc_info()[0]
451         return False
452
453
454 def delete_tenant(keystone_client, tenant_id):
455     try:
456         tenant = keystone_client.tenants.delete(tenant_id)
457         return True
458     except:
459         print "Error:", sys.exc_info()[0]
460         return False
461
462
463 def create_user(keystone_client, user_name, user_password,
464                 user_email, tenant_id):
465     try:
466         user = keystone_client.users.create(user_name, user_password,
467                                             user_email, tenant_id,
468                                             enabled=True)
469         return user.id
470     except:
471         print "Error:", sys.exc_info()[0]
472         return False
473
474
475 def delete_user(keystone_client, user_id):
476     try:
477         tenant = keystone_client.users.delete(user_id)
478         return True
479     except:
480         print "Error:", sys.exc_info()[0]
481         return False
482
483
484 def add_role_user(keystone_client, user_id, role_id, tenant_id):
485     try:
486         keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
487         return True
488     except:
489         print "Error:", sys.exc_info()[0]
490         return False
491
492
493 # ################ UTILS #################
494 def check_internet_connectivity(url='http://www.opnfv.org/'):
495     """
496     Check if there is access to the internet
497     """
498     try:
499         urllib2.urlopen(url, timeout=5)
500         return True
501     except urllib2.URLError:
502         return False
503
504
505 def download_url(url, dest_path):
506     """
507     Download a file to a destination path given a URL
508     """
509     name = url.rsplit('/')[-1]
510     dest = dest_path + "/" + name
511     try:
512         response = urllib2.urlopen(url)
513     except (urllib2.HTTPError, urllib2.URLError):
514         return False
515
516     with open(dest, 'wb') as f:
517         shutil.copyfileobj(response, f)
518     return True
519
520
521 def execute_command(cmd, logger=None):
522     """
523     Execute Linux command
524     """
525     if logger:
526         logger.debug('Executing command : {}'.format(cmd))
527     output_file = "output.txt"
528     f = open(output_file, 'w+')
529     p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
530     f.close()
531     f = open(output_file, 'r')
532     result = f.read()
533     if result != "" and logger:
534         logger.debug(result)
535     if p == 0:
536         return True
537     else:
538         if logger:
539             logger.error("Error when executing command %s" % cmd)
540         exit(-1)
541
542
543 def get_git_branch(repo_path):
544     """
545     Get git branch name
546     """
547     repo = Repo(repo_path)
548     branch = repo.active_branch
549     return branch.name
550
551
552 def get_installer_type(logger=None):
553     """
554     Get installer type (fuel, foreman, apex, joid, compass)
555     """
556     try:
557         installer = os.environ['INSTALLER_TYPE']
558     except KeyError:
559         if logger:
560             logger.error("Impossible to retrieve the installer type")
561         installer = "Unkown"
562
563     return installer
564
565
566 def get_pod_name(logger=None):
567     """
568     Get PoD Name from env variable NODE_NAME
569     """
570     try:
571         return os.environ['NODE_NAME']
572     except KeyError:
573         if logger:
574             logger.error("Unable to retrieve the POD name from environment.Using pod name 'unknown-pod'")
575         return "unknown-pod"
576
577
578 def push_results_to_db(db_url, case_name, logger, pod_name, git_version, payload):
579     url = db_url + "/results"
580     installer = get_installer_type(logger)
581     params = {"project_name": "functest", "case_name": case_name,
582               "pod_name": pod_name, "installer": installer,
583               "version": git_version, "details": payload}
584
585     headers = {'Content-Type': 'application/json'}
586     try:
587         r = requests.post(url, data=json.dumps(params), headers=headers)
588         logger.debug(r)
589         return True
590     except:
591         print "Error:", sys.exc_info()[0]
592         return False