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
25 # ----------------------------------------------------------
29 # -----------------------------------------------------------
32 #*********************************************
34 #*********************************************
35 def check_credentials():
37 Check if the OpenStack credentials (openrc) are sourced
39 env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
40 return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
42 def get_credentials(service):
43 """Returns a creds dictionary filled with the following keys:
45 * password/api_key (depending on the service)
46 * tenant_name/project_id (depending on the service)
48 :param service: a string indicating the name of the service
49 requesting the credentials.
52 # Unfortunately, each of the OpenStack client will request slightly
53 # different entries in their credentials dict.
54 if service.lower() in ("nova", "cinder"):
59 tenant = "tenant_name"
61 # The most common way to pass these info to the script is to do it through
62 # environment variables.
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"),
74 #*********************************************
76 #*********************************************
77 def get_instances(nova_client):
79 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
82 print "Error [get_instances(nova_client)]:", e
86 def get_instance_status(nova_client, instance):
88 instance = nova_client.servers.get(instance.id)
89 return instance.status
91 #print "Error [get_instance_status(nova_client, '%s')]:" % \
96 def get_instance_by_name(nova_client, instance_name):
98 instance = nova_client.servers.find(name=instance_name)
101 print "Error [get_instance_by_name(nova_client, '%s')]:" % \
106 def get_flavor_id(nova_client, flavor_name):
107 flavors = nova_client.flavors.list(detailed=True)
110 if f.name == flavor_name:
116 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
117 flavors = nova_client.flavors.list(detailed=True)
120 if min_ram <= f.ram and f.ram <= max_ram:
126 def get_floating_ips(nova_client):
128 floating_ips = nova_client.floating_ips.list()
131 print "Error [get_floating_ips(nova_client)]:", e
135 def create_flavor(nova_client, flavor_name, ram, disk, vcpus):
137 flavor = nova_client.flavors.create(flavor_name,ram,vcpus,disk)
139 print "Error [create_flavor(nova_client, '%s', '%s', '%s', "\
140 "'%s')]:" %(flavor_name,ram, disk, vcpus), e
145 def create_floating_ip(neutron_client):
146 extnet_id = get_external_net_id(neutron_client)
147 props = {'floating_network_id': extnet_id}
149 ip_json = neutron_client.create_floatingip({'floatingip': props})
150 floating_ip = ip_json['floatingip']['floating_ip_address']
152 print "Error [create_floating_ip(neutron_client)]:", e
157 def add_floating_ip(nova_client, server_id, floatingip_id):
159 nova_client.servers.add_floating_ip(server_id,floatingip_id)
162 print "Error [add_floating_ip(nova_client, '%s', '%s')]:" % \
163 (server_id, floatingip_id), e
167 def delete_instance(nova_client, instance_id):
169 nova_client.servers.force_delete(instance_id)
172 print "Error [delete_instance(nova_client, '%s')]:" % instance_id, e
176 def delete_floating_ip(nova_client, floatingip_id):
178 nova_client.floating_ips.delete(floatingip_id)
181 print "Error [delete_floating_ip(nova_client, '%s')]:" % floatingip_id, e
187 #*********************************************
189 #*********************************************
190 def get_network_list(neutron_client):
191 network_list = neutron_client.list_networks()['networks']
192 if len(network_list) == 0:
198 def get_router_list(neutron_client):
199 router_list = neutron_client.list_routers()['routers']
200 if len(router_list) == 0:
206 def get_port_list(neutron_client):
207 port_list = neutron_client.list_ports()['ports']
208 if len(port_list) == 0:
214 def get_network_id(neutron_client, network_name):
215 networks = neutron_client.list_networks()['networks']
218 if n['name'] == network_name:
224 def get_private_net(neutron_client):
225 # Checks if there is an existing shared private network
226 networks = neutron_client.list_networks()['networks']
227 if len(networks) == 0:
230 if (net['router:external'] is False) and (net['shared'] is True):
235 def get_external_net(neutron_client):
236 for network in neutron_client.list_networks()['networks']:
237 if network['router:external']:
238 return network['name']
242 def get_external_net_id(neutron_client):
243 for network in neutron_client.list_networks()['networks']:
244 if network['router:external']:
249 def check_neutron_net(neutron_client, net_name):
250 for network in neutron_client.list_networks()['networks']:
251 if network['name'] == net_name:
252 for subnet in network['subnets']:
257 def create_neutron_net(neutron_client, name):
258 json_body = {'network': {'name': name,
259 'admin_state_up': True}}
261 network = neutron_client.create_network(body=json_body)
262 network_dict = network['network']
263 return network_dict['id']
265 print "Error [create_neutron_net(neutron_client, '%s')]:" % name, e
269 def create_neutron_subnet(neutron_client, name, cidr, net_id):
270 json_body = {'subnets': [{'name': name, 'cidr': cidr,
271 'ip_version': 4, 'network_id': net_id}]}
273 subnet = neutron_client.create_subnet(body=json_body)
274 return subnet['subnets'][0]['id']
276 print "Error [create_neutron_subnet(neutron_client, '%s', '%s', "\
277 "'%s')]:" %(name,cidr, net_id), e
281 def create_neutron_router(neutron_client, name):
282 json_body = {'router': {'name': name, 'admin_state_up': True}}
284 router = neutron_client.create_router(json_body)
285 return router['router']['id']
287 print "Error [create_neutron_router(neutron_client, '%s')]:" % name, e
291 def create_neutron_port(neutron_client, name, network_id, ip):
292 json_body = {'port': {
293 'admin_state_up': True,
295 'network_id': network_id,
296 'fixed_ips': [{"ip_address": ip}]
299 port = neutron_client.create_port(body=json_body)
300 return port['port']['id']
302 print "Error [create_neutron_port(neutron_client, '%s', '%s', "\
303 "'%s')]:" %(name,network_id, ip), e
307 def update_neutron_net(neutron_client, network_id, shared=False):
308 json_body = {'network': {'shared': shared}}
310 neutron_client.update_network(network_id, body=json_body)
313 print "Error [update_neutron_net(neutron_client, '%s', '%s')]:" % \
314 (network_id,str(shared)), e
318 def update_neutron_port(neutron_client, port_id, device_owner):
319 json_body = {'port': {
320 'device_owner': device_owner,
323 port = neutron_client.update_port(port=port_id,
325 return port['port']['id']
327 print "Error [update_neutron_port(neutron_client, '%s', '%s')]:" % \
328 (port_id,device_owner), e
332 def add_interface_router(neutron_client, router_id, subnet_id):
333 json_body = {"subnet_id": subnet_id}
335 neutron_client.add_interface_router(router=router_id, body=json_body)
338 print "Error [add_interface_router(neutron_client, '%s', '%s')]:" % \
339 (router_id,subnet_id), e
342 def add_gateway_router(neutron_client, router_id):
343 ext_net_id = get_external_net_id(neutron_client)
344 router_dict = {'network_id': ext_net_id}
346 neutron_client.add_gateway_router(router_id,router_dict)
349 print "Error [add_gateway_router(neutron_client, '%s')]:" % router_id, e
353 def delete_neutron_net(neutron_client, network_id):
355 neutron_client.delete_network(network_id)
358 print "Error [delete_neutron_net(neutron_client, '%s')]:" % network_id, e
362 def delete_neutron_subnet(neutron_client, subnet_id):
364 neutron_client.delete_subnet(subnet_id)
367 print "Error [delete_neutron_subnet(neutron_client, '%s')]:" % subnet_id, e
371 def delete_neutron_router(neutron_client, router_id):
372 json_body = {'router': {'id': router_id}}
374 neutron_client.delete_router(router=router_id)
377 print "Error [delete_neutron_router(neutron_client, '%s')]:" % \
382 def delete_neutron_port(neutron_client, port_id):
384 neutron_client.delete_port(port_id)
387 print "Error [delete_neutron_port(neutron_client, '%s')]:" % port_id, e
391 def remove_interface_router(neutron_client, router_id, subnet_id):
392 json_body = {"subnet_id": subnet_id}
394 neutron_client.remove_interface_router(router=router_id,
398 print "Error [remove_interface_router(neutron_client, '%s', '%s')]:" % \
399 (router_id,subnet_id), e
403 def remove_gateway_router(neutron_client, router_id):
405 neutron_client.remove_gateway_router(router_id)
408 print "Error [remove_gateway_router(neutron_client, '%s')]:" % router_id, e
413 #*********************************************
415 #*********************************************
416 def get_security_groups(neutron_client):
418 security_groups = neutron_client.list_security_groups()[
420 return security_groups
422 print "Error [get_security_groups(neutron_client)]:", e
426 def create_security_group(neutron_client, sg_name, sg_description):
427 json_body= {'security_group' : { 'name' : sg_name, \
428 'description' : sg_description }}
430 secgroup = neutron_client.create_security_group(json_body)
431 return secgroup['security_group']
433 print "Error [create_security_group(neutron_client, '%s', '%s')]:" % \
434 (sg_name,sg_description), e
438 def create_secgroup_rule(neutron_client, sg_id, direction, protocol,
439 port_range_min = None, port_range_max = None):
440 if port_range_min == None and port_range_max == None:
441 json_body = { 'security_group_rule' : \
442 { 'direction' : direction, \
443 'security_group_id' : sg_id, \
444 'protocol' : protocol } }
445 elif port_range_min != None and port_range_max != None:
446 json_body = { 'security_group_rule' : \
447 { 'direction' : direction, \
448 'security_group_id' : sg_id, \
449 'port_range_min': port_range_min, \
450 'port_range_max' : port_range_max, \
451 'protocol' : protocol } }
453 print "Error [create_secgroup_rule(neutron_client, '%s', '%s', "\
454 "'%s', '%s', '%s', '%s')]:" %(neutron_client, sg_id, direction, \
455 port_range_min, port_range_max, protocol),\
456 " Invalid values for port_range_min, port_range_max"
459 neutron_client.create_security_group_rule(json_body)
462 print "Error [create_secgroup_rule(neutron_client, '%s', '%s', "\
463 "'%s', '%s', '%s', '%s')]:" %(neutron_client, sg_id, direction, \
464 port_range_min, port_range_max, protocol), e
468 def add_secgroup_to_instance(nova_client, instance_id, secgroup_id):
470 nova_client.servers.add_security_group(instance_id, secgroup_id)
473 print "Error [add_secgroup_to_instance(nova_client, '%s', '%s')]: " % \
474 (instance_id, secgroup_id), e
478 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
479 json_body = {"quota": {
480 "security_group": sg_quota,
481 "security_group_rule": sg_rule_quota
485 quota = neutron_client.update_quota(tenant_id=tenant_id,
489 print "Error [update_sg_quota(neutron_client, '%s', '%s', "\
490 "'%s')]:" %(tenant_id, sg_quota, sg_rule_quota), e
494 def delete_security_group(neutron_client, secgroup_id):
496 neutron_client.delete_security_group(secgroup_id)
499 print "Error [delete_security_group(neutron_client, '%s')]:" % secgroup_id, e
505 #*********************************************
507 #*********************************************
508 def get_images(nova_client):
510 images = nova_client.images.list()
513 print "Error [get_images]:", e
517 def get_image_id(glance_client, image_name):
518 images = glance_client.images.list()
521 if i.name == image_name:
527 def create_glance_image(glance_client, image_name, file_path, public=True):
528 if not os.path.isfile(file_path):
529 print "Error: file " + file_path + " does not exist."
532 with open(file_path) as fimage:
533 image = glance_client.images.create(name=image_name,
536 container_format="bare",
540 print "Error [create_glance_image(glance_client, '%s', '%s', "\
541 "'%s')]:" %(image_name, file_path, str(public)), e
545 def delete_glance_image(nova_client, image_id):
547 nova_client.images.delete(image_id)
550 print "Error [delete_glance_image(nova_client, '%s')]:" % image_id, e
555 #*********************************************
557 #*********************************************
558 def get_volumes(cinder_client):
560 volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
563 print "Error [get_volumes(cinder_client)]:", e
567 def list_volume_types(cinder_client, public=True, private=True):
569 volume_types = cinder_client.volume_types.list()
571 volume_types = [vt for vt in volume_types if not vt.is_public]
573 volume_types = [vt for vt in volume_types if vt.is_public]
576 print "Error [list_volume_types(cinder_client)]:", e
580 def create_volume_type(cinder_client, name):
582 volume_type = cinder_client.volume_types.create(name)
585 print "Error [create_volume_type(cinder_client, '%s')]:" % name, e
589 def update_cinder_quota(cinder_client, tenant_id, vols_quota,
590 snapshots_quota, gigabytes_quota):
591 quotas_values = {"volumes": vols_quota,
592 "snapshots": snapshots_quota,
593 "gigabytes": gigabytes_quota}
596 quotas_default = cinder_client.quotas.update(tenant_id,
600 print "Error [update_cinder_quota(cinder_client, '%s', '%s', '%s'" \
601 "'%s')]:" %(tenant_id, vols_quota, snapshots_quota, gigabytes_quota), e
605 def delete_volume(cinder_client, volume_id, forced=False):
609 cinder_client.volumes.detach(volume_id)
611 print "Error:", sys.exc_info()[0]
612 cinder_client.volumes.force_delete(volume_id)
614 cinder_client.volumes.delete(volume_id)
617 print "Error [delete_volume(cinder_client, '%s', '%s')]:" % \
618 (volume_id, str(forced)), e
622 def delete_volume_type(cinder_client, volume_type):
624 cinder_client.volume_types.delete(volume_type)
627 print "Error [delete_volume_type(cinder_client, '%s')]:" % volume_type, e
632 #*********************************************
634 #*********************************************
635 def get_tenants(keystone_client):
637 tenants = keystone_client.tenants.list()
640 print "Error [get_tenants(keystone_client)]:", e
644 def get_users(keystone_client):
646 users = keystone_client.users.list()
649 print "Error [get_users(keystone_client)]:", e
653 def get_tenant_id(keystone_client, tenant_name):
654 tenants = keystone_client.tenants.list()
657 if t.name == tenant_name:
663 def get_user_id(keystone_client, user_name):
664 users = keystone_client.users.list()
667 if u.name == user_name:
673 def get_role_id(keystone_client, role_name):
674 roles = keystone_client.roles.list()
677 if r.name == role_name:
683 def create_tenant(keystone_client, tenant_name, tenant_description):
685 tenant = keystone_client.tenants.create(tenant_name,
690 print "Error [create_tenant(cinder_client, '%s', '%s')]:" % \
691 (tenant_name, tenant_description), e
695 def create_user(keystone_client, user_name, user_password,
696 user_email, tenant_id):
698 user = keystone_client.users.create(user_name, user_password,
699 user_email, tenant_id,
703 print "Error [create_user(keystone_client, '%s', '%s', '%s'" \
704 "'%s')]:" %(user_name, user_password, user_email, tenant_id), e
708 def add_role_user(keystone_client, user_id, role_id, tenant_id):
710 keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
713 print "Error [add_role_user(keystone_client, '%s', '%s'" \
714 "'%s')]:" %(user_id, role_id, tenant_id), e
718 def delete_tenant(keystone_client, tenant_id):
720 tenant = keystone_client.tenants.delete(tenant_id)
723 print "Error [delete_tenant(keystone_client, '%s')]:" % tenant_id, e
727 def delete_user(keystone_client, user_id):
729 tenant = keystone_client.users.delete(user_id)
732 print "Error [delete_user(keystone_client, '%s')]:" % user_id, e
741 # ----------------------------------------------------------
745 # -----------------------------------------------------------
746 def check_internet_connectivity(url='http://www.opnfv.org/'):
748 Check if there is access to the internet
751 urllib2.urlopen(url, timeout=5)
753 except urllib2.URLError:
757 def download_url(url, dest_path):
759 Download a file to a destination path given a URL
761 name = url.rsplit('/')[-1]
762 dest = dest_path + "/" + name
764 response = urllib2.urlopen(url)
765 except (urllib2.HTTPError, urllib2.URLError):
768 with open(dest, 'wb') as f:
769 shutil.copyfileobj(response, f)
775 # ----------------------------------------------------------
779 # -----------------------------------------------------------
780 def get_git_branch(repo_path):
784 repo = Repo(repo_path)
785 branch = repo.active_branch
789 def get_installer_type(logger=None):
791 Get installer type (fuel, apex, joid, compass)
794 installer = os.environ['INSTALLER_TYPE']
797 logger.error("Impossible to retrieve the installer type")
798 installer = "Unknown_installer"
803 def get_scenario(logger=None):
808 scenario = os.environ['DEPLOY_SCENARIO']
811 logger.error("Impossible to retrieve the scenario")
812 scenario = "Unknown_scenario"
817 def get_pod_name(logger=None):
819 Get PoD Name from env variable NODE_NAME
822 return os.environ['NODE_NAME']
826 "Unable to retrieve the POD name from environment.Using pod name 'unknown-pod'")
830 def push_results_to_db(db_url, case_name, logger, pod_name,
833 POST results to the Result target DB
835 url = db_url + "/results"
836 installer = get_installer_type(logger)
837 params = {"project_name": "functest", "case_name": case_name,
838 "pod_name": pod_name, "installer": installer,
839 "version": version, "details": payload}
841 headers = {'Content-Type': 'application/json'}
843 r = requests.post(url, data=json.dumps(params), headers=headers)
848 print "Error [push_results_to_db('%s', '%s', '%s', '%s', '%s')]:" \
849 % (db_url, case_name, pod_name, version, payload), e
853 def get_resolvconf_ns():
855 Get nameservers from current resolv.conf
858 rconf = open("/etc/resolv.conf", "r")
859 line = rconf.readline()
861 ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
862 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
864 result = sock.connect_ex((ip.group(),53))
866 nameservers.append(ip.group())
867 line = rconf.readline()
871 def getTestEnv(test, functest_yaml):
873 Get the config of the testcase based on functest_config.yaml
875 - test = test project e.g; ovno
876 - test = testcase e.g. functest/odl
877 look for the / to see if it is a test project or a testcase
880 TEST_ENV = functest_yaml.get("test-dependencies")
882 if test.find("/") < 0:
883 config_test = TEST_ENV[test]
885 test_split = test.split("/")
886 testproject = test_split[0]
887 testcase = test_split[1]
888 config_test = TEST_ENV[testproject][testcase]
890 # if not defined in dependencies => no dependencies
893 print "Error [getTestEnv]:", e
898 def get_ci_envvars():
900 Get the CI env variables
903 "installer": os.environ.get('INSTALLER_TYPE'),
904 "scenario": os.environ.get('DEPLOY_SCENARIO')}
908 def isTestRunnable(test, functest_yaml):
910 Return True if the test is runnable in the current scenario
912 # By default we assume that all the tests are always runnable...
914 # Retrieve CI environment
915 ci_env = get_ci_envvars()
916 # Retrieve test environement from config file
917 test_env = getTestEnv(test, functest_yaml)
919 # if test_env not empty => dependencies to be checked
920 if test_env is not None and len(test_env) > 0:
921 # possible criteria = ["installer", "scenario"]
922 # consider test criteria from config file
923 # compare towards CI env through CI en variable
924 for criteria in test_env:
925 if re.search(test_env[criteria], ci_env[criteria]) is None:
926 # print "Test "+ test + " cannot be run on the environment"
931 def generateTestcaseList(functest_yaml):
933 Generate a test file with the runnable test according to
938 testcase_list = functest_yaml.get("test-dependencies")
939 projects = testcase_list.keys()
941 for project in projects:
942 testcases = testcase_list[project]
943 # 1 or 2 levels for testcases project[/case]l
944 # if only project name without controller or scenario
945 # => shall be runnable on any controller/scenario
946 if testcases is None:
947 test_list += project + " "
949 for testcase in testcases:
950 if testcase == "installer" or testcase == "scenario":
952 if isTestRunnable(project, functest_yaml):
953 test_list += project + " "
955 # project/testcase (2 levels)
956 thetest = project + "/" + testcase
957 if isTestRunnable(thetest, functest_yaml):
958 test_list += testcase + " "
960 # sort the list to execute the test in the right order
961 test_order_list = functest_yaml.get("test_exec_priority")
962 test_sorted_list = ""
963 for test in test_order_list:
964 if test_order_list[test] in test_list:
965 test_sorted_list += test_order_list[test] + " "
967 # create a file that could be consumed by run-test.sh
968 # this method is used only for CI
969 # so it can be run only in container
970 # reuse default conf directory to store the list of runnable tests
971 file = open("/home/opnfv/functest/conf/testcase-list.txt", 'w')
972 file.write(test_sorted_list)
975 return test_sorted_list
978 def execute_command(cmd, logger=None, exit_on_error=True):
980 Execute Linux command
981 prints stdout to a file and depending on if there
982 is a logger defined, it will print it or not.
985 logger.debug('Executing command : {}'.format(cmd))
986 output_file = "output.txt"
987 f = open(output_file, 'w+')
988 p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
990 f = open(output_file, 'r')
992 if result != "" and logger:
998 logger.error("Error when executing command %s" % cmd)