From 94f6b3f1dd697a9cf4950213435d1b823e9699ae Mon Sep 17 00:00:00 2001 From: Morgan Richomme Date: Mon, 12 Oct 2015 17:45:29 +0200 Subject: [PATCH] add functions in utils and fix vPing JIRA: FUNCTEST-36 Change-Id: I5bf315117edc25a6f92b3c4cc873e7626aaff5e0 Signed-off-by: Morgan Richomme --- testcases/Dashboard/dashboard_utils.py | 86 +++++++++++---------- testcases/Dashboard/vPing2Dashboard.py | 1 - testcases/functest_utils.py | 136 ++++++++++++++++++++++++--------- testcases/vPing/CI/libraries/vPing.py | 13 +++- 4 files changed, 157 insertions(+), 79 deletions(-) diff --git a/testcases/Dashboard/dashboard_utils.py b/testcases/Dashboard/dashboard_utils.py index 90562855..0af383f3 100644 --- a/testcases/Dashboard/dashboard_utils.py +++ b/testcases/Dashboard/dashboard_utils.py @@ -1,6 +1,5 @@ -#!/usr/bin/python -# -# Copyright (c) 2015 Orange +# copyrighi (c) 2015 Orange + # morgan.richomme@orange.com # # This program and the accompanying materials @@ -25,16 +24,16 @@ class TestCriteria: def __init__(self): self.project = '' self.testcase = '' - self.pod_id = -1 + self.pod_name = 'all' self.duration = 'all' self.version = 'all' self.installer = 'all' - def setCriteria(self, project, testcase, pod_id, + def setCriteria(self, project, testcase, pod_name, duration, version, installer): self.project = project self.testcase = testcase - self.pod_id = pod_id + self.pod_name = pod_name self.duration = duration self.version = version self.installer = installer @@ -44,17 +43,17 @@ class TestCriteria: return "" else: if(type(name) == int): - return "-" + str(name) + return "_" + str(name) else: - return "-" + name + return "_" + name def format(self): - pod_name = self.format_criteria(self.pod_id) + pod_name = self.format_criteria(self.pod_name) version_name = self.format_criteria(self.version) installer_name = self.format_criteria(self.installer) duration_name = self.format_criteria(self.duration) try: - fileName = "result-" + self.project + "-" + self.testcase + \ + fileName = "result_" + self.project + "_" + self.testcase + \ pod_name + version_name + installer_name + \ duration_name + ".json" except: @@ -78,9 +77,9 @@ def get_pods(db_url): for pod in pods: # cast int becase otherwise API retrieve 1.0 # TODO check format with API - pods_table.append(int(pod['_id'])) + pods_table.append(pod['name']) - pods_table.append(0) # 0 means all the pods here + pods_table.append('all') return pods_table except: print "Error retrieving the list of PODs" @@ -90,7 +89,7 @@ def get_pods(db_url): def get_versions(db_url): # retrieve the list of versions # TODO not supported in API yet - url = db_url + "/versions" + url = db_url + "/results" # Build headers headers = {'Content-Type': 'application/json'} @@ -99,14 +98,14 @@ def get_versions(db_url): # Get result as a json object versions_data = json.loads(db_data.text) # Get results - versions = versions_data['versions'] - + versions = versions_data['test_results'] versions_table = [] for version in versions: - versions_table.append(version['version']) + if (version['version'] is not None): + versions_table.append(version['version']) versions_table.append('all') - + versions_table = sorted(set(versions_table)) return versions_table except: print "Error retrieving the list of OPNFV versions" @@ -116,7 +115,7 @@ def get_versions(db_url): def get_installers(db_url): # retrieve the list of installers # TODO not supported in API yet - url = db_url + "/installers" + url = db_url + "/results" # Build headers headers = {'Content-Type': 'application/json'} @@ -125,15 +124,16 @@ def get_installers(db_url): # Get result as a json object installers_data = json.loads(db_data.text) # Get results - installers = installers_data['installers'] + installers = installers_data['test_results'] installers_table = [] for installer in installers: - installers_table.append(installer['installer']) + if (installer['installer'] is not None): + installers_table.append(installer['installer']) installers_table.append('all') - - return installers + installers_table = sorted(set(installers_table)) + return installers_table except: print "Error retrieving the list of OPNFV installers" return None @@ -180,33 +180,40 @@ def get_results(db_url, test_criteria): # - yardstick tests on any POD since 30 days # - Qtip tests on dell-test1 POD # - # params = {"pod_id":pod_id, "testcase":testcase} + # params = {"pod_name":pod, "testcase":testcase} # filter_date = days # data from now - days - # test_project = test_criteria.project + test_project = test_criteria.project testcase = test_criteria.testcase - # duration_frame = test_criteria.duration - # version = test_criteria.version - # installer_type = test_criteria.installer - pod_id = test_criteria.pod_id - - pod_criteria = "" - if (pod_id > 0): - pod_criteria = "&pod=" + str(pod_id) + period = test_criteria.duration + version = test_criteria.version + installer = test_criteria.installer + pod = test_criteria.pod_name # TODO complete params (installer type, testcase, version ) # need API to be up to date # we assume that criteria could be used at the API level # no need to processing on date for instance - params = {"pod_id": pod_id} + # params = {"pod_name": pod_name} # Build headers headers = {'Content-Type': 'application/json'} - url = db_url + "/results?case=" + testcase + pod_criteria + # build the request + # if criteria is all => remove criteria + url = db_url + "/results?project=" + test_project + "&case=" + testcase + + if (pod != "all"): + url += "&pod=" + pod + if (installer != "all"): + url += "&installer=" + installer + if (version != "all"): + url += "&version=" + version + url += "&period=" + str(period) # Send Request to Test DB - myData = requests.get(url, data=json.dumps(params), headers=headers) + myData = requests.get(url, headers=headers) + # Get result as a json object myNewData = json.loads(myData.text) @@ -217,15 +224,16 @@ def get_results(db_url, test_criteria): def generateJson(test_name, test_case, db_url): - # pod_id = 1 + # pod_id = "opnfv-jump-1' # test_version = 'Arno master' # test_installer = 'fuel' # test_retention = 30 pods = get_pods(db_url) - versions = ['ArnoR1', 'ArnoSR1', 'all'] # not available in the API yet - installers = ['fuel', 'foreman', 'all'] # not available in the API yet - test_durations = [90, 365, 'all'] # not available through the API yet + versions = get_versions(db_url) + installers = get_installers(db_url) + + test_durations = [90, 365, 0] # 0 means since the beginning # For all the PoDs for pod in pods: diff --git a/testcases/Dashboard/vPing2Dashboard.py b/testcases/Dashboard/vPing2Dashboard.py index f799e280..43008a62 100644 --- a/testcases/Dashboard/vPing2Dashboard.py +++ b/testcases/Dashboard/vPing2Dashboard.py @@ -49,7 +49,6 @@ TEST_DB = functest_yaml.get("results").get("test_db_url") def format_vPing_for_dashboard(criteria): - # Get results myDataResults = dashboard_utils.get_results(TEST_DB, criteria) diff --git a/testcases/functest_utils.py b/testcases/functest_utils.py index 0977ce72..31e41c96 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 @@ -363,15 +399,39 @@ 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 + print branch.name + return branch + + +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 diff --git a/testcases/vPing/CI/libraries/vPing.py b/testcases/vPing/CI/libraries/vPing.py index 5d68f222..65571c93 100644 --- a/testcases/vPing/CI/libraries/vPing.py +++ b/testcases/vPing/CI/libraries/vPing.py @@ -245,7 +245,18 @@ def cleanup(nova, neutron, network_dic): def push_results_to_db(payload): - # TODO move DB creds into config file + url = TEST_DB + "/results" + installer = functest_utils.get_installer_type(logger) + git_version = functest_utils.get_git_branch(args.repo_path) + # TODO pod_name hardcoded, info shall come from Jenkins + params = {"project_name": "functest", "case_name": "vPing", + "pod_name": "opnfv-jump-2", "installer": installer, + "version": git_version, "details": payload} + + headers = {'Content-Type': 'application/json'} + r = requests.post(url, data=json.dumps(params), headers=headers) + logger.debug(r) + url = TEST_DB + "/results" params = {"project_name": "functest", "case_name": "vPing", "pod_id": 1, "details": payload} -- 2.16.6