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