Add jenkins build tag for result api
[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 import json
12 import os
13 import os.path
14 import re
15 import requests
16 import shutil
17 import socket
18 import subprocess
19 import sys
20 import urllib2
21 from git import Repo
22
23
24 # ----------------------------------------------------------
25 #
26 #               OPENSTACK UTILS
27 #
28 # -----------------------------------------------------------
29
30
31 #*********************************************
32 #   CREDENTIALS
33 #*********************************************
34 def check_credentials():
35     """
36     Check if the OpenStack credentials (openrc) are sourced
37     """
38     env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
39     return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
40
41
42 def get_credentials(service):
43     """Returns a creds dictionary filled with the following keys:
44     * username
45     * password/api_key (depending on the service)
46     * tenant_name/project_id (depending on the service)
47     * auth_url
48     :param service: a string indicating the name of the service
49                     requesting the credentials.
50     """
51     creds = {}
52     # Unfortunately, each of the OpenStack client will request slightly
53     # different entries in their credentials dict.
54     if service.lower() in ("nova", "cinder"):
55         password = "api_key"
56         tenant = "project_id"
57     else:
58         password = "password"
59         tenant = "tenant_name"
60
61     # The most common way to pass these info to the script is to do it through
62     # environment variables.
63     creds.update({
64         "username": os.environ.get('OS_USERNAME', "admin"),
65         password: os.environ.get("OS_PASSWORD", 'admin'),
66         "auth_url": os.environ.get("OS_AUTH_URL",
67                                    "http://192.168.20.71:5000/v2.0"),
68         tenant: os.environ.get("OS_TENANT_NAME", "admin"),
69     })
70     return creds
71
72
73 #*********************************************
74 #   NOVA
75 #*********************************************
76 def get_instances(nova_client):
77     try:
78         instances = nova_client.servers.list(search_opts={'all_tenants': 1})
79         return instances
80     except Exception, e:
81         print "Error [get_instances(nova_client)]:", e
82         return None
83
84
85 def get_instance_status(nova_client, instance):
86     try:
87         instance = nova_client.servers.get(instance.id)
88         return instance.status
89     except Exception, e:
90         # print "Error [get_instance_status(nova_client, '%s')]:" % \
91         #    str(instance), e
92         return None
93
94
95 def get_instance_by_name(nova_client, instance_name):
96     try:
97         instance = nova_client.servers.find(name=instance_name)
98         return instance
99     except Exception, e:
100         print "Error [get_instance_by_name(nova_client, '%s')]:" % \
101             instance_name, e
102         return None
103
104
105 def get_flavor_id(nova_client, flavor_name):
106     flavors = nova_client.flavors.list(detailed=True)
107     id = ''
108     for f in flavors:
109         if f.name == flavor_name:
110             id = f.id
111             break
112     return id
113
114
115 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
116     flavors = nova_client.flavors.list(detailed=True)
117     id = ''
118     for f in flavors:
119         if min_ram <= f.ram and f.ram <= max_ram:
120             id = f.id
121             break
122     return id
123
124
125 def get_floating_ips(nova_client):
126     try:
127         floating_ips = nova_client.floating_ips.list()
128         return floating_ips
129     except Exception, e:
130         print "Error [get_floating_ips(nova_client)]:", e
131         return None
132
133
134 def create_flavor(nova_client, flavor_name, ram, disk, vcpus):
135     try:
136         flavor = nova_client.flavors.create(flavor_name, ram, vcpus, disk)
137     except Exception, e:
138         print "Error [create_flavor(nova_client, '%s', '%s', '%s', "\
139             "'%s')]:" % (flavor_name, ram, disk, vcpus), e
140         return None
141     return flavor.id
142
143
144 def create_floating_ip(neutron_client):
145     extnet_id = get_external_net_id(neutron_client)
146     props = {'floating_network_id': extnet_id}
147     try:
148         ip_json = neutron_client.create_floatingip({'floatingip': props})
149         fip_addr = ip_json['floatingip']['floating_ip_address']
150         fip_id = ip_json['floatingip']['id']
151     except Exception, e:
152         print "Error [create_floating_ip(neutron_client)]:", e
153         return None
154     return {'fip_addr': fip_addr, 'fip_id': fip_id}
155
156
157 def add_floating_ip(nova_client, server_id, floatingip_id):
158     try:
159         nova_client.servers.add_floating_ip(server_id, floatingip_id)
160         return True
161     except Exception, e:
162         print "Error [add_floating_ip(nova_client, '%s', '%s')]:" % \
163             (server_id, floatingip_id), e
164         return False
165
166
167 def delete_instance(nova_client, instance_id):
168     try:
169         nova_client.servers.force_delete(instance_id)
170         return True
171     except Exception, e:
172         print "Error [delete_instance(nova_client, '%s')]:" % instance_id, e
173         return False
174
175
176 def delete_floating_ip(nova_client, floatingip_id):
177     try:
178         nova_client.floating_ips.delete(floatingip_id)
179         return True
180     except Exception, e:
181         print "Error [delete_floating_ip(nova_client, '%s')]:" % floatingip_id, e
182         return False
183
184
185 #*********************************************
186 #   NEUTRON
187 #*********************************************
188 def get_network_list(neutron_client):
189     network_list = neutron_client.list_networks()['networks']
190     if len(network_list) == 0:
191         return None
192     else:
193         return network_list
194
195
196 def get_router_list(neutron_client):
197     router_list = neutron_client.list_routers()['routers']
198     if len(router_list) == 0:
199         return None
200     else:
201         return router_list
202
203
204 def get_port_list(neutron_client):
205     port_list = neutron_client.list_ports()['ports']
206     if len(port_list) == 0:
207         return None
208     else:
209         return port_list
210
211
212 def get_network_id(neutron_client, network_name):
213     networks = neutron_client.list_networks()['networks']
214     id = ''
215     for n in networks:
216         if n['name'] == network_name:
217             id = n['id']
218             break
219     return id
220
221
222 def get_subnet_id(neutron_client, subnet_name):
223     subnets = neutron_client.list_subnets()['subnets']
224     id = ''
225     for s in subnets:
226         if s['name'] == subnet_name:
227             id = s['id']
228             break
229     return id
230
231
232 def get_router_id(neutron_client, router_name):
233     routers = neutron_client.list_routers()['routers']
234     id = ''
235     for r in routers:
236         if r['name'] == router_name:
237             id = r['id']
238             break
239     return id
240
241
242 def get_private_net(neutron_client):
243     # Checks if there is an existing shared private network
244     networks = neutron_client.list_networks()['networks']
245     if len(networks) == 0:
246         return None
247     for net in networks:
248         if (net['router:external'] is False) and (net['shared'] is True):
249             return net
250     return None
251
252
253 def get_external_net(neutron_client):
254     for network in neutron_client.list_networks()['networks']:
255         if network['router:external']:
256             return network['name']
257     return False
258
259
260 def get_external_net_id(neutron_client):
261     for network in neutron_client.list_networks()['networks']:
262         if network['router:external']:
263             return network['id']
264     return False
265
266
267 def check_neutron_net(neutron_client, net_name):
268     for network in neutron_client.list_networks()['networks']:
269         if network['name'] == net_name:
270             for subnet in network['subnets']:
271                 return True
272     return False
273
274
275 def create_neutron_net(neutron_client, name):
276     json_body = {'network': {'name': name,
277                              'admin_state_up': True}}
278     try:
279         network = neutron_client.create_network(body=json_body)
280         network_dict = network['network']
281         return network_dict['id']
282     except Exception, e:
283         print "Error [create_neutron_net(neutron_client, '%s')]:" % name, e
284         return False
285
286
287 def create_neutron_subnet(neutron_client, name, cidr, net_id):
288     json_body = {'subnets': [{'name': name, 'cidr': cidr,
289                               'ip_version': 4, 'network_id': net_id}]}
290     try:
291         subnet = neutron_client.create_subnet(body=json_body)
292         return subnet['subnets'][0]['id']
293     except Exception, e:
294         print "Error [create_neutron_subnet(neutron_client, '%s', '%s', "\
295             "'%s')]:" % (name, cidr, net_id), e
296         return False
297
298
299 def create_neutron_router(neutron_client, name):
300     json_body = {'router': {'name': name, 'admin_state_up': True}}
301     try:
302         router = neutron_client.create_router(json_body)
303         return router['router']['id']
304     except Exception, e:
305         print "Error [create_neutron_router(neutron_client, '%s')]:" % name, e
306         return False
307
308
309 def create_neutron_port(neutron_client, name, network_id, ip):
310     json_body = {'port': {
311                  'admin_state_up': True,
312                  'name': name,
313                  'network_id': network_id,
314                  'fixed_ips': [{"ip_address": ip}]
315                  }}
316     try:
317         port = neutron_client.create_port(body=json_body)
318         return port['port']['id']
319     except Exception, e:
320         print "Error [create_neutron_port(neutron_client, '%s', '%s', "\
321             "'%s')]:" % (name, network_id, ip), e
322         return False
323
324
325 def update_neutron_net(neutron_client, network_id, shared=False):
326     json_body = {'network': {'shared': shared}}
327     try:
328         neutron_client.update_network(network_id, body=json_body)
329         return True
330     except Exception, e:
331         print "Error [update_neutron_net(neutron_client, '%s', '%s')]:" % \
332             (network_id, str(shared)), e
333         return False
334
335
336 def update_neutron_port(neutron_client, port_id, device_owner):
337     json_body = {'port': {
338                  'device_owner': device_owner,
339                  }}
340     try:
341         port = neutron_client.update_port(port=port_id,
342                                           body=json_body)
343         return port['port']['id']
344     except Exception, e:
345         print "Error [update_neutron_port(neutron_client, '%s', '%s')]:" % \
346             (port_id, device_owner), e
347         return False
348
349
350 def add_interface_router(neutron_client, router_id, subnet_id):
351     json_body = {"subnet_id": subnet_id}
352     try:
353         neutron_client.add_interface_router(router=router_id, body=json_body)
354         return True
355     except Exception, e:
356         print "Error [add_interface_router(neutron_client, '%s', '%s')]:" % \
357             (router_id, subnet_id), e
358         return False
359
360
361 def add_gateway_router(neutron_client, router_id):
362     ext_net_id = get_external_net_id(neutron_client)
363     router_dict = {'network_id': ext_net_id}
364     try:
365         neutron_client.add_gateway_router(router_id, router_dict)
366         return True
367     except Exception, e:
368         print "Error [add_gateway_router(neutron_client, '%s')]:" % router_id, e
369         return False
370
371
372 def delete_neutron_net(neutron_client, network_id):
373     try:
374         neutron_client.delete_network(network_id)
375         return True
376     except Exception, e:
377         print "Error [delete_neutron_net(neutron_client, '%s')]:" % network_id, e
378         return False
379
380
381 def delete_neutron_subnet(neutron_client, subnet_id):
382     try:
383         neutron_client.delete_subnet(subnet_id)
384         return True
385     except Exception, e:
386         print "Error [delete_neutron_subnet(neutron_client, '%s')]:" % subnet_id, e
387         return False
388
389
390 def delete_neutron_router(neutron_client, router_id):
391     json_body = {'router': {'id': router_id}}
392     try:
393         neutron_client.delete_router(router=router_id)
394         return True
395     except Exception, e:
396         print "Error [delete_neutron_router(neutron_client, '%s')]:" % \
397             router_id, e
398         return False
399
400
401 def delete_neutron_port(neutron_client, port_id):
402     try:
403         neutron_client.delete_port(port_id)
404         return True
405     except Exception, e:
406         print "Error [delete_neutron_port(neutron_client, '%s')]:" % port_id, e
407         return False
408
409
410 def remove_interface_router(neutron_client, router_id, subnet_id):
411     json_body = {"subnet_id": subnet_id}
412     try:
413         neutron_client.remove_interface_router(router=router_id,
414                                                body=json_body)
415         return True
416     except Exception, e:
417         print "Error [remove_interface_router(neutron_client, '%s', '%s')]:" % \
418             (router_id, subnet_id), e
419         return False
420
421
422 def remove_gateway_router(neutron_client, router_id):
423     try:
424         neutron_client.remove_gateway_router(router_id)
425         return True
426     except Exception, e:
427         print "Error [remove_gateway_router(neutron_client, '%s')]:" % router_id, e
428         return False
429
430
431 #*********************************************
432 #   SEC GROUPS
433 #*********************************************
434 def get_security_groups(neutron_client):
435     try:
436         security_groups = neutron_client.list_security_groups()[
437             'security_groups']
438         return security_groups
439     except Exception, e:
440         print "Error [get_security_groups(neutron_client)]:", e
441         return None
442
443
444 def get_security_group_id(neutron_client, sg_name):
445     security_groups = get_security_groups(neutron_client)
446     id = ''
447     for sg in security_groups:
448         if sg['name'] == sg_name:
449             id = sg['id']
450             break
451     return id
452
453
454 def create_security_group(neutron_client, sg_name, sg_description):
455     json_body = {'security_group': {'name': sg_name,
456                                     'description': sg_description}}
457     try:
458         secgroup = neutron_client.create_security_group(json_body)
459         return secgroup['security_group']
460     except Exception, e:
461         print "Error [create_security_group(neutron_client, '%s', '%s')]:" % \
462             (sg_name, sg_description), e
463         return False
464
465
466 def create_secgroup_rule(neutron_client, sg_id, direction, protocol,
467                          port_range_min=None, port_range_max=None):
468     if port_range_min is None and port_range_max is None:
469         json_body = {'security_group_rule': {'direction': direction,
470                                              'security_group_id': sg_id,
471                                              'protocol': protocol}}
472     elif port_range_min is not None and port_range_max is not None:
473         json_body = {'security_group_rule': {'direction': direction,
474                                              'security_group_id': sg_id,
475                                              'port_range_min': port_range_min,
476                                              'port_range_max': port_range_max,
477                                              'protocol': protocol}}
478     else:
479         print "Error [create_secgroup_rule(neutron_client, '%s', '%s', "\
480               "'%s', '%s', '%s', '%s')]:" % (neutron_client, sg_id, direction,
481                                              port_range_min, port_range_max, protocol),\
482               " Invalid values for port_range_min, port_range_max"
483         return False
484     try:
485         neutron_client.create_security_group_rule(json_body)
486         return True
487     except Exception, e:
488         print "Error [create_secgroup_rule(neutron_client, '%s', '%s', "\
489             "'%s', '%s', '%s', '%s')]:" % (neutron_client, sg_id, direction,
490                                            port_range_min, port_range_max,
491                                            protocol), e
492         return False
493
494
495 def add_secgroup_to_instance(nova_client, instance_id, secgroup_id):
496     try:
497         nova_client.servers.add_security_group(instance_id, secgroup_id)
498         return True
499     except Exception, e:
500         print "Error [add_secgroup_to_instance(nova_client, '%s', '%s')]: " % \
501             (instance_id, secgroup_id), e
502         return False
503
504
505 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
506     json_body = {"quota": {
507         "security_group": sg_quota,
508         "security_group_rule": sg_rule_quota
509     }}
510
511     try:
512         quota = neutron_client.update_quota(tenant_id=tenant_id,
513                                             body=json_body)
514         return True
515     except Exception, e:
516         print "Error [update_sg_quota(neutron_client, '%s', '%s', "\
517             "'%s')]:" % (tenant_id, sg_quota, sg_rule_quota), e
518         return False
519
520
521 def delete_security_group(neutron_client, secgroup_id):
522     try:
523         neutron_client.delete_security_group(secgroup_id)
524         return True
525     except Exception, e:
526         print "Error [delete_security_group(neutron_client, '%s')]:" % secgroup_id, e
527         return False
528
529
530 #*********************************************
531 #   GLANCE
532 #*********************************************
533 def get_images(nova_client):
534     try:
535         images = nova_client.images.list()
536         return images
537     except Exception, e:
538         print "Error [get_images]:", e
539         return None
540
541
542 def get_image_id(glance_client, image_name):
543     images = glance_client.images.list()
544     id = ''
545     for i in images:
546         if i.name == image_name:
547             id = i.id
548             break
549     return id
550
551
552 def create_glance_image(glance_client, image_name, file_path, public=True):
553     if not os.path.isfile(file_path):
554         print "Error: file " + file_path + " does not exist."
555         return False
556     try:
557         with open(file_path) as fimage:
558             image = glance_client.images.create(name=image_name,
559                                                 is_public=public,
560                                                 disk_format="qcow2",
561                                                 container_format="bare",
562                                                 data=fimage)
563         return image.id
564     except Exception, e:
565         print "Error [create_glance_image(glance_client, '%s', '%s', "\
566             "'%s')]:" % (image_name, file_path, str(public)), e
567         return False
568
569
570 def delete_glance_image(nova_client, image_id):
571     try:
572         nova_client.images.delete(image_id)
573         return True
574     except Exception, e:
575         print "Error [delete_glance_image(nova_client, '%s')]:" % image_id, e
576         return False
577
578
579 #*********************************************
580 #   CINDER
581 #*********************************************
582 def get_volumes(cinder_client):
583     try:
584         volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
585         return volumes
586     except Exception, e:
587         print "Error [get_volumes(cinder_client)]:", e
588         return None
589
590
591 def list_volume_types(cinder_client, public=True, private=True):
592     try:
593         volume_types = cinder_client.volume_types.list()
594         if not public:
595             volume_types = [vt for vt in volume_types if not vt.is_public]
596         if not private:
597             volume_types = [vt for vt in volume_types if vt.is_public]
598         return volume_types
599     except Exception, e:
600         print "Error [list_volume_types(cinder_client)]:", e
601         return None
602
603
604 def create_volume_type(cinder_client, name):
605     try:
606         volume_type = cinder_client.volume_types.create(name)
607         return volume_type
608     except Exception, e:
609         print "Error [create_volume_type(cinder_client, '%s')]:" % name, e
610         return None
611
612
613 def update_cinder_quota(cinder_client, tenant_id, vols_quota,
614                         snapshots_quota, gigabytes_quota):
615     quotas_values = {"volumes": vols_quota,
616                      "snapshots": snapshots_quota,
617                      "gigabytes": gigabytes_quota}
618
619     try:
620         quotas_default = cinder_client.quotas.update(tenant_id,
621                                                      **quotas_values)
622         return True
623     except Exception, e:
624         print "Error [update_cinder_quota(cinder_client, '%s', '%s', '%s'" \
625             "'%s')]:" % (tenant_id, vols_quota,
626                          snapshots_quota, gigabytes_quota), e
627         return False
628
629
630 def delete_volume(cinder_client, volume_id, forced=False):
631     try:
632         if forced:
633             try:
634                 cinder_client.volumes.detach(volume_id)
635             except:
636                 print "Error:", sys.exc_info()[0]
637             cinder_client.volumes.force_delete(volume_id)
638         else:
639             cinder_client.volumes.delete(volume_id)
640         return True
641     except Exception, e:
642         print "Error [delete_volume(cinder_client, '%s', '%s')]:" % \
643             (volume_id, str(forced)), e
644         return False
645
646
647 def delete_volume_type(cinder_client, volume_type):
648     try:
649         cinder_client.volume_types.delete(volume_type)
650         return True
651     except Exception, e:
652         print "Error [delete_volume_type(cinder_client, '%s')]:" % volume_type, e
653         return False
654
655
656 #*********************************************
657 #   KEYSTONE
658 #*********************************************
659 def get_tenants(keystone_client):
660     try:
661         tenants = keystone_client.tenants.list()
662         return tenants
663     except Exception, e:
664         print "Error [get_tenants(keystone_client)]:", e
665         return None
666
667
668 def get_users(keystone_client):
669     try:
670         users = keystone_client.users.list()
671         return users
672     except Exception, e:
673         print "Error [get_users(keystone_client)]:", e
674         return None
675
676
677 def get_tenant_id(keystone_client, tenant_name):
678     tenants = keystone_client.tenants.list()
679     id = ''
680     for t in tenants:
681         if t.name == tenant_name:
682             id = t.id
683             break
684     return id
685
686
687 def get_user_id(keystone_client, user_name):
688     users = keystone_client.users.list()
689     id = ''
690     for u in users:
691         if u.name == user_name:
692             id = u.id
693             break
694     return id
695
696
697 def get_role_id(keystone_client, role_name):
698     roles = keystone_client.roles.list()
699     id = ''
700     for r in roles:
701         if r.name == role_name:
702             id = r.id
703             break
704     return id
705
706
707 def create_tenant(keystone_client, tenant_name, tenant_description):
708     try:
709         tenant = keystone_client.tenants.create(tenant_name,
710                                                 tenant_description,
711                                                 enabled=True)
712         return tenant.id
713     except Exception, e:
714         print "Error [create_tenant(cinder_client, '%s', '%s')]:" % \
715             (tenant_name, tenant_description), e
716         return False
717
718
719 def create_user(keystone_client, user_name, user_password,
720                 user_email, tenant_id):
721     try:
722         user = keystone_client.users.create(user_name, user_password,
723                                             user_email, tenant_id,
724                                             enabled=True)
725         return user.id
726     except Exception, e:
727         print "Error [create_user(keystone_client, '%s', '%s', '%s'" \
728             "'%s')]:" % (user_name, user_password, user_email, tenant_id), e
729         return False
730
731
732 def add_role_user(keystone_client, user_id, role_id, tenant_id):
733     try:
734         keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
735         return True
736     except Exception, e:
737         print "Error [add_role_user(keystone_client, '%s', '%s'" \
738             "'%s')]:" % (user_id, role_id, tenant_id), e
739         return False
740
741
742 def delete_tenant(keystone_client, tenant_id):
743     try:
744         tenant = keystone_client.tenants.delete(tenant_id)
745         return True
746     except Exception, e:
747         print "Error [delete_tenant(keystone_client, '%s')]:" % tenant_id, e
748         return False
749
750
751 def delete_user(keystone_client, user_id):
752     try:
753         tenant = keystone_client.users.delete(user_id)
754         return True
755     except Exception, e:
756         print "Error [delete_user(keystone_client, '%s')]:" % user_id, e
757         return False
758
759
760 # ----------------------------------------------------------
761 #
762 #               INTERNET UTILS
763 #
764 # -----------------------------------------------------------
765 def check_internet_connectivity(url='http://www.opnfv.org/'):
766     """
767     Check if there is access to the internet
768     """
769     try:
770         urllib2.urlopen(url, timeout=5)
771         return True
772     except urllib2.URLError:
773         return False
774
775
776 def download_url(url, dest_path):
777     """
778     Download a file to a destination path given a URL
779     """
780     name = url.rsplit('/')[-1]
781     dest = dest_path + "/" + name
782     try:
783         response = urllib2.urlopen(url)
784     except (urllib2.HTTPError, urllib2.URLError):
785         return False
786
787     with open(dest, 'wb') as f:
788         shutil.copyfileobj(response, f)
789     return True
790
791
792 # ----------------------------------------------------------
793 #
794 #               CI UTILS
795 #
796 # -----------------------------------------------------------
797 def get_git_branch(repo_path):
798     """
799     Get git branch name
800     """
801     repo = Repo(repo_path)
802     branch = repo.active_branch
803     return branch.name
804
805
806 def get_installer_type(logger=None):
807     """
808     Get installer type (fuel, apex, joid, compass)
809     """
810     try:
811         installer = os.environ['INSTALLER_TYPE']
812     except KeyError:
813         if logger:
814             logger.error("Impossible to retrieve the installer type")
815         installer = "Unknown_installer"
816
817     return installer
818
819
820 def get_scenario(logger=None):
821     """
822     Get scenario
823     """
824     try:
825         scenario = os.environ['DEPLOY_SCENARIO']
826     except KeyError:
827         if logger:
828             logger.error("Impossible to retrieve the scenario")
829         scenario = "Unknown_scenario"
830
831     return scenario
832
833
834 def get_pod_name(logger=None):
835     """
836     Get PoD Name from env variable NODE_NAME
837     """
838     try:
839         return os.environ['NODE_NAME']
840     except KeyError:
841         if logger:
842             logger.error(
843                 "Unable to retrieve the POD name from environment.Using pod name 'unknown-pod'")
844         return "unknown-pod"
845
846
847 def get_build_tag(logger=None):
848     """
849     Get build tag of jenkins jobs
850     """
851     try:
852         build_tag = os.environ['BUILD_TAG']
853     except KeyError:
854         if logger:
855             logger.error("Impossible to retrieve the build tag")
856         build_tag = "unknown_build_tag"
857
858     return build_tag
859
860
861 def push_results_to_db(db_url, project, case_name, logger, pod_name,
862                        version, build_tag="unknown_build_tag", payload):
863     """
864     POST results to the Result target DB
865     """
866     url = db_url + "/results"
867     installer = get_installer_type(logger)
868     params = {"project_name": project, "case_name": case_name,
869               "pod_name": pod_name, "installer": installer,
870               "version": version, "build_tag": build_tag, "details": payload}
871
872     headers = {'Content-Type': 'application/json'}
873     try:
874         r = requests.post(url, data=json.dumps(params), headers=headers)
875         if logger:
876             logger.debug(r)
877         return True
878     except Exception, e:
879         print "Error [push_results_to_db('%s', '%s', '%s', '%s', '%s', '%s', '%s')]:" \
880             % (db_url, project, case_name, pod_name, version, build_tag, payload), e
881         return False
882
883
884 def get_resolvconf_ns():
885     """
886     Get nameservers from current resolv.conf
887     """
888     nameservers = []
889     rconf = open("/etc/resolv.conf", "r")
890     line = rconf.readline()
891     while line:
892         ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
893         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
894         if ip:
895             result = sock.connect_ex((ip.group(), 53))
896             if result == 0:
897                 nameservers.append(ip.group())
898         line = rconf.readline()
899     return nameservers
900
901
902 def getTestEnv(test, functest_yaml):
903     """
904     Get the config of the testcase based on functest_config.yaml
905       2 options
906         - test = test project e.g; ovno
907         - test = testcase e.g. functest/odl
908        look for the / to see if it is a test project or a testcase
909     """
910     try:
911         TEST_ENV = functest_yaml.get("test-dependencies")
912
913         if test.find("/") < 0:
914             config_test = TEST_ENV[test]
915         else:
916             test_split = test.split("/")
917             testproject = test_split[0]
918             testcase = test_split[1]
919             config_test = TEST_ENV[testproject][testcase]
920     except KeyError:
921         # if not defined in dependencies => no dependencies
922         config_test = ""
923     except Exception, e:
924         print "Error [getTestEnv]:", e
925
926     return config_test
927
928
929 def get_ci_envvars():
930     """
931     Get the CI env variables
932     """
933     ci_env_var = {
934         "installer": os.environ.get('INSTALLER_TYPE'),
935         "scenario": os.environ.get('DEPLOY_SCENARIO')}
936     return ci_env_var
937
938
939 def isTestRunnable(test, functest_yaml):
940     """
941     Return True if the test is runnable in the current scenario
942     """
943     # By default we assume that all the tests are always runnable...
944     is_runnable = True
945     # Retrieve CI environment
946     ci_env = get_ci_envvars()
947     # Retrieve test environement from config file
948     test_env = getTestEnv(test, functest_yaml)
949
950     # if test_env not empty => dependencies to be checked
951     if test_env is not None and len(test_env) > 0:
952         # possible criteria = ["installer", "scenario"]
953         # consider test criteria from config file
954         # compare towards CI env through CI en variable
955         for criteria in test_env:
956             if re.search(test_env[criteria], ci_env[criteria]) is None:
957                 # print "Test "+ test + " cannot be run on the environment"
958                 is_runnable = False
959     return is_runnable
960
961
962 def generateTestcaseList(functest_yaml):
963     """
964     Generate a test file with the runnable test according to
965     the current scenario
966     """
967     test_list = ""
968     # get testcases
969     testcase_list = functest_yaml.get("test-dependencies")
970     projects = testcase_list.keys()
971
972     for project in projects:
973         testcases = testcase_list[project]
974         # 1 or 2 levels for testcases project[/case]l
975         # if only project name without controller or scenario
976         # => shall be runnable on any controller/scenario
977         if testcases is None:
978             test_list += project + " "
979         else:
980             for testcase in testcases:
981                 if testcase == "installer" or testcase == "scenario":
982                     # project (1 level)
983                     if isTestRunnable(project, functest_yaml):
984                         test_list += project + " "
985                 else:
986                     # project/testcase (2 levels)
987                     thetest = project + "/" + testcase
988                     if isTestRunnable(thetest, functest_yaml):
989                         test_list += testcase + " "
990
991     # sort the list to execute the test in the right order
992     test_order_list = functest_yaml.get("test_exec_priority")
993     test_sorted_list = ""
994     for test in test_order_list:
995         if test_order_list[test] in test_list:
996             test_sorted_list += test_order_list[test] + " "
997
998     # create a file that could be consumed by run-test.sh
999     # this method is used only for CI
1000     # so it can be run only in container
1001     # reuse default conf directory to store the list of runnable tests
1002     file = open("/home/opnfv/functest/conf/testcase-list.txt", 'w')
1003     file.write(test_sorted_list)
1004     file.close()
1005
1006     return test_sorted_list
1007
1008
1009 def execute_command(cmd, logger=None, exit_on_error=True):
1010     """
1011     Execute Linux command
1012         prints stdout to a file and depending on if there
1013         is a logger defined, it will print it or not.
1014     """
1015     if logger:
1016         logger.debug('Executing command : {}'.format(cmd))
1017     output_file = "output.txt"
1018     f = open(output_file, 'w+')
1019     p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
1020     f.close()
1021     f = open(output_file, 'r')
1022     result = f.read()
1023     if result != "" and logger:
1024         logger.debug(result)
1025     if p == 0:
1026         return True
1027     else:
1028         if logger:
1029             logger.error("Error when executing command %s" % cmd)
1030         if exit_on_error:
1031             exit(-1)
1032         return False