diff options
Diffstat (limited to 'testcases/functest_utils.py')
-rw-r--r-- | testcases/functest_utils.py | 137 |
1 files changed, 98 insertions, 39 deletions
diff --git a/testcases/functest_utils.py b/testcases/functest_utils.py index c2e4bc6d5..59e89a5af 100644 --- a/testcases/functest_utils.py +++ b/testcases/functest_utils.py @@ -8,28 +8,35 @@ # http://www.apache.org/licenses/LICENSE-2.0 # -import re, json, os, urllib2, shutil, subprocess, sys -############# CREDENTIALS OPENSTACK ############# +import os +import urllib2 +import subprocess +import sys +from git import Repo + + +# ############ CREDENTIALS OPENSTACK ############# def check_credentials(): """ Check if the OpenStack credentials (openrc) are sourced """ - #TODO: there must be a short way to do this, doing if os.environ["something"] == "" throws an error + # TODO: there must be a short way to do this + # doing if os.environ["something"] == "" throws an error try: - os.environ['OS_AUTH_URL'] + os.environ['OS_AUTH_URL'] except KeyError: return False try: - os.environ['OS_USERNAME'] + os.environ['OS_USERNAME'] except KeyError: return False try: - os.environ['OS_PASSWORD'] + os.environ['OS_PASSWORD'] except KeyError: return False try: - os.environ['OS_TENANT_NAME'] + os.environ['OS_TENANT_NAME'] except KeyError: return False return True @@ -44,7 +51,6 @@ def get_credentials(service): :param service: a string indicating the name of the service requesting the credentials. """ - #TODO: get credentials from the openrc file creds = {} # Unfortunately, each of the OpenStack client will request slightly # different entries in their credentials dict. @@ -58,17 +64,18 @@ def get_credentials(service): # The most common way to pass these info to the script is to do it through # environment variables. creds.update({ - "username": os.environ.get('OS_USERNAME', "admin"), # add your cloud username details - password: os.environ.get("OS_PASSWORD", 'admin'), # add password - "auth_url": os.environ.get("OS_AUTH_URL","http://192.168.20.71:5000/v2.0"), # Auth URL + "username": os.environ.get('OS_USERNAME', "admin"), + password: os.environ.get("OS_PASSWORD", 'admin'), + "auth_url": os.environ.get("OS_AUTH_URL", + "http://192.168.20.71:5000/v2.0"), tenant: os.environ.get("OS_TENANT_NAME", "admin"), }) return creds -################# NOVA ################# -def get_instance_status(nova_client,instance): +# ################ NOVA ################# +def get_instance_status(nova_client, instance): try: instance = nova_client.servers.get(instance.id) return instance.status @@ -104,10 +111,10 @@ def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram): return id -################# NEUTRON ################# +# ################ NEUTRON ################# def create_neutron_net(neutron_client, name): json_body = {'network': {'name': name, - 'admin_state_up': True}} + 'admin_state_up': True}} try: network = neutron_client.create_network(body=json_body) network_dict = network['network'] @@ -116,6 +123,7 @@ def create_neutron_net(neutron_client, name): print "Error:", sys.exc_info()[0] return False + def delete_neutron_net(neutron_client, network_id): try: neutron_client.delete_network(network_id) @@ -124,9 +132,10 @@ def delete_neutron_net(neutron_client, network_id): print "Error:", sys.exc_info()[0] return False + def create_neutron_subnet(neutron_client, name, cidr, net_id): json_body = {'subnets': [{'name': name, 'cidr': cidr, - 'ip_version': 4, 'network_id': net_id}]} + 'ip_version': 4, 'network_id': net_id}]} try: subnet = neutron_client.create_subnet(body=json_body) return subnet['subnets'][0]['id'] @@ -134,6 +143,7 @@ def create_neutron_subnet(neutron_client, name, cidr, net_id): print "Error:", sys.exc_info()[0] return False + def delete_neutron_subnet(neutron_client, subnet_id): try: neutron_client.delete_subnet(subnet_id) @@ -142,6 +152,7 @@ def delete_neutron_subnet(neutron_client, subnet_id): print "Error:", sys.exc_info()[0] return False + def create_neutron_router(neutron_client, name): json_body = {'router': {'name': name, 'admin_state_up': True}} try: @@ -151,6 +162,7 @@ def create_neutron_router(neutron_client, name): print "Error:", sys.exc_info()[0] return False + def delete_neutron_router(neutron_client, router_id): json_body = {'router': {'id': router_id}} try: @@ -174,19 +186,21 @@ def add_interface_router(neutron_client, router_id, subnet_id): def remove_interface_router(neutron_client, router_id, subnet_id): json_body = {"subnet_id": subnet_id} try: - neutron_client.remove_interface_router(router=router_id, body=json_body) + neutron_client.remove_interface_router(router=router_id, + body=json_body) return True except: print "Error:", sys.exc_info()[0] return False + def create_neutron_port(neutron_client, name, network_id, ip): json_body = {'port': { - 'admin_state_up': True, - 'name': name, - 'network_id': network_id, - 'fixed_ips': [{"ip_address": ip}] - }} + 'admin_state_up': True, + 'name': name, + 'network_id': network_id, + 'fixed_ips': [{"ip_address": ip}] + }} try: port = neutron_client.create_port(body=json_body) return port['port']['id'] @@ -197,33 +211,37 @@ def create_neutron_port(neutron_client, name, network_id, ip): def get_network_id(neutron_client, network_name): networks = neutron_client.list_networks()['networks'] - id = '' + id = '' for n in networks: if n['name'] == network_name: id = n['id'] break return id + def check_neutron_net(neutron_client, net_name): for network in neutron_client.list_networks()['networks']: - if network['name'] == net_name : + if network['name'] == net_name: for subnet in network['subnets']: return True return False + def get_network_list(neutron_client): network_list = neutron_client.list_networks()['networks'] - if len(network_list) == 0 : + if len(network_list) == 0: return None - else : + else: return network_list + def get_external_net(neutron_client): for network in neutron_client.list_networks()['networks']: if network['router:external']: return network['name'] return False + def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota): json_body = {"quota": { "security_group": sg_quota, @@ -231,13 +249,15 @@ def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota): }} try: - quota = neutron_client.update_quota(tenant_id=tenant_id, body=json_body) + quota = neutron_client.update_quota(tenant_id=tenant_id, + body=json_body) return True except: print "Error:", sys.exc_info()[0] return False -################# GLANCE ################# +# ################ GLANCE ################# + def get_image_id(glance_client, image_name): images = glance_client.images.list() @@ -248,17 +268,21 @@ def get_image_id(glance_client, image_name): break return id + def create_glance_image(glance_client, image_name, file_path, is_public=True): try: with open(file_path) as fimage: - image = glance_client.images.create(name=image_name, is_public=is_public, disk_format="qcow2", - container_format="bare", data=fimage) + image = glance_client.images.create(name=image_name, + is_public=is_public, + disk_format="qcow2", + container_format="bare", + data=fimage) return image.id except: return False -################# KEYSTONE ################# +# ################ KEYSTONE ################# def get_tenant_id(keystone_client, tenant_name): tenants = keystone_client.tenants.list() id = '' @@ -268,6 +292,7 @@ def get_tenant_id(keystone_client, tenant_name): break return id + def get_role_id(keystone_client, role_name): roles = keystone_client.roles.list() id = '' @@ -277,6 +302,7 @@ def get_role_id(keystone_client, role_name): break return id + def get_user_id(keystone_client, user_name): users = keystone_client.users.list() id = '' @@ -286,14 +312,18 @@ def get_user_id(keystone_client, user_name): break return id + def create_tenant(keystone_client, tenant_name, tenant_description): try: - tenant = keystone_client.tenants.create(tenant_name, tenant_description, enabled=True) + tenant = keystone_client.tenants.create(tenant_name, + tenant_description, + enabled=True) return tenant.id except: print "Error:", sys.exc_info()[0] return False + def delete_tenant(keystone_client, tenant_id): try: tenant = keystone_client.tenants.delete(tenant_id) @@ -302,14 +332,19 @@ def delete_tenant(keystone_client, tenant_id): print "Error:", sys.exc_info()[0] return False -def create_user(keystone_client, user_name, user_password, user_email, tenant_id): + +def create_user(keystone_client, user_name, user_password, + user_email, tenant_id): try: - user = keystone_client.users.create(user_name, user_password, user_email, tenant_id, enabled=True) + user = keystone_client.users.create(user_name, user_password, + user_email, tenant_id, + enabled=True) return user.id except: print "Error:", sys.exc_info()[0] return False + def delete_user(keystone_client, user_id): try: tenant = keystone_client.users.delete(user_id) @@ -318,6 +353,7 @@ def delete_user(keystone_client, user_id): print "Error:", sys.exc_info()[0] return False + def add_role_user(keystone_client, user_id, role_id, tenant_id): try: keystone_client.roles.add_user_role(user_id, role_id, tenant_id) @@ -327,7 +363,7 @@ def add_role_user(keystone_client, user_id, role_id, tenant_id): return False -################# UTILS ################# +# ################ UTILS ################# def check_internet_connectivity(url='http://www.opnfv.org/'): """ Check if there is access to the internet @@ -335,7 +371,7 @@ def check_internet_connectivity(url='http://www.opnfv.org/'): try: urllib2.urlopen(url, timeout=5) return True - except urllib.URLError: + except urllib2.URLError: return False @@ -363,15 +399,38 @@ def execute_command(cmd, logger=None): logger.debug('Executing command : {}'.format(cmd)) output_file = "output.txt" f = open(output_file, 'w+') - p = subprocess.call(cmd,shell=True, stdout=f, stderr=subprocess.STDOUT) + p = subprocess.call(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT) f.close() f = open(output_file, 'r') result = f.read() if result != "" and logger: logger.debug(result) - if p == 0 : + if p == 0: return True else: if logger: - logger.error("Error when executing command %s" %cmd) + logger.error("Error when executing command %s" % cmd) exit(-1) + + +def get_git_branch(repo_path): + """ + Get git branch name + """ + repo = Repo(repo_path) + branch = repo.active_branch + return branch.name + + +def get_installer_type(logger=None): + """ + Get installer type (fuel, foreman, apex, joid, compass) + """ + try: + installer = os.environ['INSTALLER_TYPE'] + except KeyError: + if logger: + logger.error("Impossible to retrieve the installer type") + installer = "Unkown" + + return installer |