diff options
Diffstat (limited to 'functest/utils')
-rw-r--r-- | functest/utils/decorators.py | 23 | ||||
-rw-r--r-- | functest/utils/functest_logger.py | 7 | ||||
-rw-r--r-- | functest/utils/functest_utils.py | 33 | ||||
-rw-r--r-- | functest/utils/openstack_utils.py | 18 |
4 files changed, 51 insertions, 30 deletions
diff --git a/functest/utils/decorators.py b/functest/utils/decorators.py index 276235d9..46ffe35d 100644 --- a/functest/utils/decorators.py +++ b/functest/utils/decorators.py @@ -1,37 +1,42 @@ #!/usr/bin/env python +# pylint: disable=missing-docstring + import errno -import mock import os -import requests.sessions import urlparse +import mock +import requests.sessions + def can_dump_request_to_file(method): def dump_preparedrequest(request, **kwargs): + # pylint: disable=unused-argument parseresult = urlparse.urlparse(request.url) if parseresult.scheme == "file": try: - os.makedirs(parseresult.path) - except OSError as e: - if e.errno != errno.EEXIST: + dirname = os.path.dirname(parseresult.path) + os.makedirs(dirname) + except OSError as ex: + if ex.errno != errno.EEXIST: raise - with open(os.path.join(parseresult.path, 'dump.txt'), 'a') as f: + with open(parseresult.path, 'a') as dumpfile: headers = "" for key in request.headers: headers += key + " " + request.headers[key] + "\n" message = "{} {}\n{}\n{}\n\n\n".format( request.method, request.url, headers, request.body) - f.write(message) + dumpfile.write(message) return mock.Mock() def patch_request(method, url, **kwargs): with requests.sessions.Session() as session: parseresult = urlparse.urlparse(url) if parseresult.scheme == "file": - with mock.patch.object( - session, 'send', side_effect=dump_preparedrequest): + with mock.patch.object(session, 'send', + side_effect=dump_preparedrequest): return session.request(method=method, url=url, **kwargs) else: return session.request(method=method, url=url, **kwargs) diff --git a/functest/utils/functest_logger.py b/functest/utils/functest_logger.py index 555e9c28..ba52829f 100644 --- a/functest/utils/functest_logger.py +++ b/functest/utils/functest_logger.py @@ -41,6 +41,13 @@ ignore = ["paramiko", class Logger(object): + instance = None + + def __new__(cls, logger_name): + if cls.instance is None: + cls.instance = object.__new__(cls) + return cls.instance + def __init__(self, logger_name): self.setup_logging() self.logger = logging.getLogger(logger_name) diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py index 7cc5029d..7d993cbf 100644 --- a/functest/utils/functest_utils.py +++ b/functest/utils/functest_utils.py @@ -192,12 +192,12 @@ def logger_test_results(project, case_name, status, details): @decorators.can_dump_request_to_file def push_results_to_db(project, case_name, - start_date, stop_date, criteria, details): + start_date, stop_date, result, details): """ POST results to the Result target DB """ # Retrieve params from CI and conf - url = get_db_url() + "/results" + url = get_db_url() try: installer = os.environ['INSTALLER_TYPE'] @@ -213,7 +213,7 @@ def push_results_to_db(project, case_name, params = {"project_name": project, "case_name": case_name, "pod_name": pod_name, "installer": installer, - "version": version, "scenario": scenario, "criteria": criteria, + "version": version, "scenario": scenario, "criteria": result, "build_tag": build_tag, "start_date": test_start, "stop_date": test_stop, "details": details} @@ -248,7 +248,7 @@ def push_results_to_db(project, case_name, 'pod': pod_name, 'v': version, 's': scenario, - 'c': criteria, + 'c': result, 't': build_tag, 'd': details, 'error': e @@ -337,7 +337,7 @@ def get_dict_by_test(testname): for dic_tier in testcases_yaml.get("tiers"): for dic_testcase in dic_tier['testcases']: - if dic_testcase['name'] == testname: + if dic_testcase['case_name'] == testname: return dic_testcase logger.error('Project %s is not defined in testcases.yaml' % testname) @@ -379,23 +379,14 @@ def get_functest_config(parameter): return get_parameter_from_yaml(parameter, yaml_) -def check_success_rate(case_name, success_rate): - success_rate = float(success_rate) +def check_success_rate(case_name, result): + # It should be removed as TestCase tests criteria + # and result. + logger.warning('check_success_rate will be removed soon') criteria = get_criteria_by_test(case_name) - - def get_criteria_value(op): - return float(criteria.split(op)[1].rstrip('%')) - - status = 'FAIL' - ops = ['==', '>='] - for op in ops: - if op in criteria: - c_value = get_criteria_value(op) - if eval("%s %s %s" % (success_rate, op, c_value)): - status = 'PASS' - break - - return status + if type(criteria) == int and result >= criteria: + return 'PASS' + return 'FAIL' def merge_dicts(dict1, dict2): diff --git a/functest/utils/openstack_utils.py b/functest/utils/openstack_utils.py index 4663f7ba..929a761e 100644 --- a/functest/utils/openstack_utils.py +++ b/functest/utils/openstack_utils.py @@ -1394,6 +1394,15 @@ def create_tenant(keystone_client, tenant_name, tenant_description): return None +def get_or_create_tenant(keystone_client, tenant_name, tenant_description): + tenant_id = get_tenant_id(keystone_client, tenant_name) + if not tenant_id: + tenant_id = create_tenant(keystone_client, tenant_name, + tenant_description) + + return tenant_id + + def create_user(keystone_client, user_name, user_password, user_email, tenant_id): try: @@ -1417,6 +1426,15 @@ def create_user(keystone_client, user_name, user_password, return None +def get_or_create_user(keystone_client, user_name, user_password, + tenant_id, user_email=None): + user_id = get_user_id(keystone_client, user_name) + if not user_id: + user_id = create_user(keystone_client, user_name, user_password, + user_email, tenant_id) + return user_id + + def add_role_user(keystone_client, user_id, role_id, tenant_id): try: if is_keystone_v3(): |