diff options
Diffstat (limited to 'functest')
35 files changed, 828 insertions, 364 deletions
diff --git a/functest/api/__init__.py b/functest/api/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/api/__init__.py diff --git a/functest/api/base.py b/functest/api/base.py new file mode 100644 index 00000000..efeab824 --- /dev/null +++ b/functest/api/base.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +The base class to dispatch request + +""" + +import logging + +from flask import request +from flask_restful import Resource + +from functest.api.common import api_utils, error + + +LOGGER = logging.getLogger(__name__) + + +class ApiResource(Resource): + """ API Resource class""" + + def __init__(self): + super(ApiResource, self).__init__() + + def _post_args(self): # pylint: disable=no-self-use + # pylint: disable=maybe-no-member + """ Return action and args after parsing request """ + + data = request.json if request.json else {} + params = api_utils.change_to_str_in_dict(data) + action = params.get('action', request.form.get('action', '')) + args = params.get('args', {}) + try: + args['file'] = request.files['file'] + except KeyError: + pass + LOGGER.debug('Input args are: action: %s, args: %s', action, args) + + return action, args + + def _dispatch_post(self): + """ Dispatch request """ + action, args = self._post_args() + return self._dispatch(args, action) + + def _dispatch(self, args, action): + """ + Dynamically load the classes with reflection and + obtain corresponding methods + """ + try: + return getattr(self, action)(args) + except AttributeError: + error.result_handler(status=1, data='No such action') + + +# Import modules from package "functest.api.resources" +# and append them into sys.modules +api_utils.import_modules_from_package("functest.api.resources") diff --git a/functest/api/common/__init__.py b/functest/api/common/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/api/common/__init__.py diff --git a/functest/api/common/api_utils.py b/functest/api/common/api_utils.py new file mode 100644 index 00000000..f518e777 --- /dev/null +++ b/functest/api/common/api_utils.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Utils for functest restapi + +""" + +import collections +import logging +import os +import sys +from oslo_utils import importutils + +import six + +import functest + +LOGGER = logging.getLogger(__name__) + + +def change_to_str_in_dict(obj): + """ + Return a dict with key and value both in string if they are in Unicode + """ + if isinstance(obj, collections.Mapping): + return {str(k): change_to_str_in_dict(v) for k, v in obj.items()} + elif isinstance(obj, list): + return [change_to_str_in_dict(ele) for ele in obj] + elif isinstance(obj, six.text_type): + return str(obj) + return obj + + +def itersubclasses(cls, _seen=None): + """ Generator over all subclasses of a given class in depth first order """ + + if not isinstance(cls, type): + raise TypeError("itersubclasses must be called with " + "new-style classes, not %.100r" % cls) + _seen = _seen or set() + try: + subs = cls.__subclasses__() + except TypeError: # fails only when cls is type + subs = cls.__subclasses__(cls) + for sub in subs: + if sub not in _seen: + _seen.add(sub) + yield sub + for itersub in itersubclasses(sub, _seen): + yield itersub + + +def import_modules_from_package(package): + """ + Import modules from package and append into sys.modules + :param: package - Full package name. For example: functest.api.resources + """ + path = [os.path.dirname(functest.__file__), ".."] + package.split(".") + path = os.path.join(*path) + for root, _, files in os.walk(path): + for filename in files: + if filename.startswith("__") or not filename.endswith(".py"): + continue + new_package = ".".join(root.split(os.sep)).split("....")[1] + module_name = "%s.%s" % (new_package, filename[:-3]) + try: + try_append_module(module_name, sys.modules) + except ImportError: + LOGGER.exception("unable to import %s", module_name) + + +def try_append_module(name, modules): + """ Append the module into specified module system """ + + if name not in modules: + modules[name] = importutils.import_module(name) + + +def change_obj_to_dict(obj): + """ Transfer the object into dict """ + dic = {} + for key, value in vars(obj).items(): + dic.update({key: value}) + return dic diff --git a/functest/api/common/error.py b/functest/api/common/error.py new file mode 100644 index 00000000..d0045225 --- /dev/null +++ b/functest/api/common/error.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Used to handle results + +""" + +from flask import jsonify + + +def result_handler(status, data): + """ Return the json format of result in dict """ + result = { + 'status': status, + 'result': data + } + return jsonify(result) diff --git a/functest/api/resources/__init__.py b/functest/api/resources/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/api/resources/__init__.py diff --git a/functest/api/resources/v1/__init__.py b/functest/api/resources/v1/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/api/resources/v1/__init__.py diff --git a/functest/api/resources/v1/creds.py b/functest/api/resources/v1/creds.py new file mode 100644 index 00000000..e402d7e3 --- /dev/null +++ b/functest/api/resources/v1/creds.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Resources to handle openstack related requests +""" + +from flask import jsonify + +from functest.api.base import ApiResource +from functest.cli.commands.cli_os import OpenStack +from functest.utils import openstack_utils as os_utils +from functest.utils.constants import CONST + + +class V1Creds(ApiResource): + """ V1Creds Resource class""" + + def get(self): # pylint: disable=no-self-use + """ Get credentials """ + os_utils.source_credentials(CONST.__getattribute__('openstack_creds')) + credentials_show = OpenStack.show_credentials() + return jsonify(credentials_show) diff --git a/functest/api/resources/v1/envs.py b/functest/api/resources/v1/envs.py new file mode 100644 index 00000000..35bffb04 --- /dev/null +++ b/functest/api/resources/v1/envs.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Resources to handle environment related requests +""" + +from flask import jsonify + +from functest.api.base import ApiResource +from functest.cli.commands.cli_env import Env +import functest.utils.functest_utils as ft_utils + + +class V1Envs(ApiResource): + """ V1Envs Resource class""" + + def get(self): # pylint: disable=no-self-use + """ Get environment """ + environment_show = Env().show() + return jsonify(environment_show) + + def post(self): + """ Used to handle post request """ + return self._dispatch_post() + + def prepare(self, args): # pylint: disable=no-self-use, unused-argument + """ Prepare environment """ + ft_utils.execute_command("prepare_env start") diff --git a/functest/api/resources/v1/testcases.py b/functest/api/resources/v1/testcases.py new file mode 100644 index 00000000..c3b8217a --- /dev/null +++ b/functest/api/resources/v1/testcases.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Resources to handle testcase related requests +""" + +from flask import abort, jsonify + +from functest.api.base import ApiResource +from functest.api.common import api_utils +from functest.cli.commands.cli_testcase import Testcase + + +class V1Testcases(ApiResource): + """ V1Testcases Resource class""" + + def get(self): # pylint: disable=no-self-use + """ GET all testcases """ + testcases_list = Testcase().list() + result = {'testcases': testcases_list.split('\n')[:-1]} + return jsonify(result) + + +class V1Testcase(ApiResource): + """ V1Testcase Resource class""" + + def get(self, testcase_name): # pylint: disable=no-self-use + """ GET the info of one testcase""" + testcase = Testcase().show(testcase_name) + if not testcase: + abort(404, "The test case '%s' does not exist or is not supported" + % testcase_name) + testcase_info = api_utils.change_obj_to_dict(testcase) + dependency_dict = api_utils.change_obj_to_dict( + testcase_info.get('dependency')) + testcase_info.pop('name') + testcase_info.pop('dependency') + result = {'testcase': testcase_name} + result.update(testcase_info) + result.update({'dependency': dependency_dict}) + return jsonify(result) diff --git a/functest/api/resources/v1/tiers.py b/functest/api/resources/v1/tiers.py new file mode 100644 index 00000000..71a98bea --- /dev/null +++ b/functest/api/resources/v1/tiers.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Resources to handle tier related requests +""" + +import re + +from flask import abort, jsonify + +from functest.api.base import ApiResource +from functest.cli.commands.cli_tier import Tier + + +class V1Tiers(ApiResource): + """ V1Tiers Resource class """ + + def get(self): + # pylint: disable=no-self-use + """ GET all tiers """ + tiers_list = Tier().list() + data = re.split("[\n\t]", tiers_list) + data = [i.strip() for i in data if i != ''] + data_dict = dict() + for i in range(len(data) / 2): + one_data = {data[i * 2]: data[i * 2 + 1]} + if i == 0: + data_dict = one_data + else: + data_dict.update(one_data) + result = {'tiers': data_dict} + return jsonify(result) + + +class V1Tier(ApiResource): + """ V1Tier Resource class """ + + def get(self, tier_name): # pylint: disable=no-self-use + """ GET the info of one tier """ + testcases = Tier().gettests(tier_name) + if not testcases: + abort(404, "The tier with name '%s' does not exist." % tier_name) + tier_info = Tier().show(tier_name) + tier_info.__dict__.pop('name') + tier_info.__dict__.pop('tests_array') + result = {'tier': tier_name, 'testcases': testcases} + result.update(tier_info.__dict__) + return jsonify(result) + + +class V1TestcasesinTier(ApiResource): + """ V1TestcasesinTier Resource class """ + + def get(self, tier_name): # pylint: disable=no-self-use + """ GET all testcases within given tier """ + testcases = Tier().gettests(tier_name) + if not testcases: + abort(404, "The tier with name '%s' does not exist." % tier_name) + result = {'tier': tier_name, 'testcases': testcases} + return jsonify(result) diff --git a/functest/api/server.py b/functest/api/server.py new file mode 100644 index 00000000..e246333e --- /dev/null +++ b/functest/api/server.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Used to launch Functest RestApi + +""" + +import logging +import socket +from urlparse import urljoin +import pkg_resources + +from flask import Flask +from flask_restful import Api + +from functest.api.base import ApiResource +from functest.api.urls import URLPATTERNS +from functest.api.common import api_utils + + +LOGGER = logging.getLogger(__name__) + + +def get_resource(resource_name): + """ Obtain the required resource according to resource name """ + name = ''.join(resource_name.split('_')) + return next((r for r in api_utils.itersubclasses(ApiResource) + if r.__name__.lower() == name)) + + +def get_endpoint(url): + """ Obtain the endpoint of url """ + address = socket.gethostbyname(socket.gethostname()) + return urljoin('http://{}:5000'.format(address), url) + + +def api_add_resource(api): + """ + The resource has multiple URLs and you can pass multiple URLs to the + add_resource() method on the Api object. Each one will be routed to + your Resource + """ + for url_pattern in URLPATTERNS: + try: + api.add_resource( + get_resource(url_pattern.target), url_pattern.url, + endpoint=get_endpoint(url_pattern.url)) + except StopIteration: + LOGGER.error('url resource not found: %s', url_pattern.url) + + +def main(): + """Entry point""" + logging.config.fileConfig(pkg_resources.resource_filename( + 'functest', 'ci/logging.ini')) + LOGGER.info('Starting Functest server') + app = Flask(__name__) + api = Api(app) + api_add_resource(api) + app.run(host='0.0.0.0') diff --git a/functest/api/urls.py b/functest/api/urls.py new file mode 100644 index 00000000..ca45b4be --- /dev/null +++ b/functest/api/urls.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +Define multiple URLs +""" + + +class Url(object): # pylint: disable=too-few-public-methods + """ Url Class """ + + def __init__(self, url, target): + super(Url, self).__init__() + self.url = url + self.target = target + + +URLPATTERNS = [ + # GET /api/v1/functest/envs => GET environment + Url('/api/v1/functest/envs', 'v1_envs'), + + # POST /api/v1/functest/envs/action , {"action":"prepare"} + # => Prepare environment + Url('/api/v1/functest/envs/action', 'v1_envs'), + + # GET /api/v1/functest/openstack/credentials => GET credentials + Url('/api/v1/functest/openstack/credentials', 'v1_creds'), + + # GET /api/v1/functest/testcases => GET all testcases + Url('/api/v1/functest/testcases', 'v1_test_cases'), + + # GET /api/v1/functest/testcases/<testcase_name> + # => GET the info of one testcase + Url('/api/v1/functest/testcases/<testcase_name>', 'v1_testcase'), + + # GET /api/v1/functest/testcases => GET all tiers + Url('/api/v1/functest/tiers', 'v1_tiers'), + + # GET /api/v1/functest/tiers/<tier_name> + # => GET the info of one tier + Url('/api/v1/functest/tiers/<tier_name>', 'v1_tier'), + + # GET /api/v1/functest/tiers/<tier_name>/testcases + # => GET all testcases within given tier + Url('/api/v1/functest/tiers/<tier_name>/testcases', 'v1_testcases_in_tier') +] diff --git a/functest/ci/config_functest.yaml b/functest/ci/config_functest.yaml index 22084263..829c51f7 100644 --- a/functest/ci/config_functest.yaml +++ b/functest/ci/config_functest.yaml @@ -7,7 +7,6 @@ general: dir_repo_releng: /home/opnfv/repos/releng repo_vims_test: /home/opnfv/repos/vnfs/vims-test repo_onos: /home/opnfv/repos/onos - repo_netready: /home/opnfv/repos/netready repo_barometer: /home/opnfv/repos/barometer repo_doctor: /home/opnfv/repos/doctor repo_odl_test: /src/odl_test @@ -206,3 +205,4 @@ energy_recorder: api_url: http://energy.opnfv.fr/resources api_user: "" api_password: "" + diff --git a/functest/ci/logging.ini b/functest/ci/logging.ini index 210c8f5f..f1ab7241 100644 --- a/functest/ci/logging.ini +++ b/functest/ci/logging.ini @@ -1,5 +1,5 @@ [loggers] -keys=root,functest,ci,cli,core,energy,opnfv_tests,utils +keys=root,functest,api,ci,cli,core,energy,opnfv_tests,utils [handlers] keys=console,wconsole,file,null @@ -16,6 +16,11 @@ level=NOTSET handlers=file qualname=functest +[logger_api] +level=NOTSET +handlers=wconsole +qualname=functest.api + [logger_ci] level=NOTSET handlers=console diff --git a/functest/ci/run_tests.py b/functest/ci/run_tests.py index b95e1008..e26f4305 100644 --- a/functest/ci/run_tests.py +++ b/functest/ci/run_tests.py @@ -1,12 +1,11 @@ #!/usr/bin/env python -# -# Author: Jose Lausuch (jose.lausuch@ericsson.com) + +# Copyright (c) 2016 Ericsson AB and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 -# import argparse import enum @@ -17,6 +16,7 @@ import os import pkg_resources import re import sys +import textwrap import prettytable @@ -66,17 +66,14 @@ class RunTestsParser(object): class Runner(object): def __init__(self): - self.executed_test_cases = [] + self.executed_test_cases = {} self.overall_result = Result.EX_OK self.clean_flag = True self.report_flag = False - - @staticmethod - def print_separator(str, count=45): - line = "" - for i in range(0, count - 1): - line += str - logger.info("%s" % line) + self._tiers = tb.TierBuilder( + CONST.__getattribute__('INSTALLER_TYPE'), + CONST.__getattribute__('DEPLOY_SCENARIO'), + pkg_resources.resource_filename('functest', 'ci/testcases.yaml')) @staticmethod def source_rc_file(): @@ -109,21 +106,11 @@ class Runner(object): logger.exception("Cannot get {}'s config options".format(testname)) return None - def run_test(self, test, tier_name, testcases=None): + def run_test(self, test): if not test.is_enabled(): raise TestNotEnabled( "The test case {} is not enabled".format(test.get_name())) - logger.info("\n") # blank line - self.print_separator("=") logger.info("Running test case '%s'...", test.get_name()) - self.print_separator("=") - logger.debug("\n%s" % test) - self.source_rc_file() - - flags = " -t %s" % test.get_name() - if self.report_flag: - flags += " -r" - result = testcase.TestCase.EX_RUN_ERROR run_dict = self.get_run_dict(test.get_name()) if run_dict: @@ -132,7 +119,7 @@ class Runner(object): cls = getattr(module, run_dict['class']) test_dict = ft_utils.get_dict_by_test(test.get_name()) test_case = cls(**test_dict) - self.executed_test_cases.append(test_case) + self.executed_test_cases[test.get_name()] = test_case if self.clean_flag: if test_case.create_snapshot() != test_case.EX_OK: return result @@ -156,7 +143,6 @@ class Runner(object): run_dict['class'])) else: raise Exception("Cannot import the class for the test case.") - return result def run_tier(self, tier): @@ -165,68 +151,60 @@ class Runner(object): if tests is None or len(tests) == 0: logger.info("There are no supported test cases in this tier " "for the given scenario") - return 0 - logger.info("\n\n") # blank line - self.print_separator("#") - logger.info("Running tier '%s'" % tier_name) - self.print_separator("#") - logger.debug("\n%s" % tier) - for test in tests: - result = self.run_test(test, tier_name) - if result != testcase.TestCase.EX_OK: - logger.error("The test case '%s' failed.", test.get_name()) - self.overall_result = Result.EX_ERROR - if test.is_blocking(): - raise BlockingTestFailed( - "The test case {} failed and is blocking".format( - test.get_name())) + self.overall_result = Result.EX_ERROR + else: + logger.info("Running tier '%s'" % tier_name) + for test in tests: + result = self.run_test(test) + if result != testcase.TestCase.EX_OK: + logger.error("The test case '%s' failed.", test.get_name()) + self.overall_result = Result.EX_ERROR + if test.is_blocking(): + raise BlockingTestFailed( + "The test case {} failed and is blocking".format( + test.get_name())) + return self.overall_result - def run_all(self, tiers): - summary = "" + def run_all(self): tiers_to_run = [] - - for tier in tiers.get_tiers(): + msg = prettytable.PrettyTable( + header_style='upper', padding_width=5, + field_names=['tiers', 'order', 'CI Loop', 'description', + 'testcases']) + for tier in self._tiers.get_tiers(): if (len(tier.get_tests()) != 0 and re.search(CONST.__getattribute__('CI_LOOP'), tier.get_ci_loop()) is not None): tiers_to_run.append(tier) - summary += ("\n - %s:\n\t %s" - % (tier.get_name(), - tier.get_test_names())) - - logger.info("Tests to be executed:%s" % summary) + msg.add_row([tier.get_name(), tier.get_order(), + tier.get_ci_loop(), + textwrap.fill(tier.description, width=40), + textwrap.fill(' '.join([str(x.get_name( + )) for x in tier.get_tests()]), width=40)]) + logger.info("TESTS TO BE EXECUTED:\n\n%s\n", msg) for tier in tiers_to_run: self.run_tier(tier) def main(self, **kwargs): - _tiers = tb.TierBuilder( - CONST.__getattribute__('INSTALLER_TYPE'), - CONST.__getattribute__('DEPLOY_SCENARIO'), - pkg_resources.resource_filename('functest', 'ci/testcases.yaml')) - if kwargs['noclean']: self.clean_flag = False - if kwargs['report']: self.report_flag = True - try: if kwargs['test']: self.source_rc_file() logger.debug("Test args: %s", kwargs['test']) - if _tiers.get_tier(kwargs['test']): - self.run_tier(_tiers.get_tier(kwargs['test'])) - elif _tiers.get_test(kwargs['test']): + if self._tiers.get_tier(kwargs['test']): + self.run_tier(self._tiers.get_tier(kwargs['test'])) + elif self._tiers.get_test(kwargs['test']): result = self.run_test( - _tiers.get_test(kwargs['test']), - _tiers.get_tier_name(kwargs['test']), - kwargs['test']) + self._tiers.get_test(kwargs['test'])) if result != testcase.TestCase.EX_OK: logger.error("The test case '%s' failed.", kwargs['test']) self.overall_result = Result.EX_ERROR elif kwargs['test'] == "all": - self.run_all(_tiers) + self.run_all() else: logger.error("Unknown test case or tier '%s', " "or not supported by " @@ -234,39 +212,45 @@ class Runner(object): % (kwargs['test'], CONST.__getattribute__('DEPLOY_SCENARIO'))) logger.debug("Available tiers are:\n\n%s", - _tiers) + self._tiers) return Result.EX_ERROR else: - self.run_all(_tiers) + self.run_all() except BlockingTestFailed: pass except Exception: logger.exception("Failures when running testcase(s)") self.overall_result = Result.EX_ERROR + if not self._tiers.get_test(kwargs['test']): + self.summary(self._tiers.get_tier(kwargs['test'])) + logger.info("Execution exit value: %s" % self.overall_result) + return self.overall_result + def summary(self, tier=None): msg = prettytable.PrettyTable( header_style='upper', padding_width=5, field_names=['env var', 'value']) for env_var in ['INSTALLER_TYPE', 'DEPLOY_SCENARIO', 'BUILD_TAG', 'CI_LOOP']: msg.add_row([env_var, CONST.__getattribute__(env_var)]) - logger.info("Deployment description: \n\n%s\n", msg) - - if len(self.executed_test_cases) > 1: - msg = prettytable.PrettyTable( - header_style='upper', padding_width=5, - field_names=['test case', 'project', 'tier', - 'duration', 'result']) - for test_case in self.executed_test_cases: + logger.info("Deployment description:\n\n%s\n", msg) + msg = prettytable.PrettyTable( + header_style='upper', padding_width=5, + field_names=['test case', 'project', 'tier', + 'duration', 'result']) + tiers = [tier] if tier else self._tiers.get_tiers() + for tier in tiers: + for test in tier.get_tests(): + test_case = self.executed_test_cases[test.get_name()] result = 'PASS' if(test_case.is_successful( - ) == test_case.EX_OK) else 'FAIL' + ) == test_case.EX_OK) else 'FAIL' msg.add_row([test_case.case_name, test_case.project_name, - _tiers.get_tier_name(test_case.case_name), + self._tiers.get_tier_name(test_case.case_name), test_case.get_duration(), result]) - logger.info("FUNCTEST REPORT: \n\n%s\n", msg) - - logger.info("Execution exit value: %s" % self.overall_result) - return self.overall_result + for test in tier.get_skipped_test(): + msg.add_row([test.get_name(), test.get_project(), + tier.get_name(), "00:00", "SKIP"]) + logger.info("FUNCTEST REPORT:\n\n%s\n", msg) def main(): diff --git a/functest/ci/testcases.yaml b/functest/ci/testcases.yaml index d93e9ebe..719d00e1 100644 --- a/functest/ci/testcases.yaml +++ b/functest/ci/testcases.yaml @@ -149,7 +149,7 @@ tiers: case_name: odl project_name: functest criteria: 100 - blocking: true + blocking: false description: >- Test Suite for the OpenDaylight SDN Controller. It integrates some test suites from upstream using @@ -265,7 +265,7 @@ tiers: module: 'functest.core.feature' class: 'BashFeature' args: - cmd: 'cd /home/opnfv/repos/promise/promise/test/functest && python ./run_tests.py' + cmd: 'run_promise_tests.py' - case_name: doctor-notification @@ -317,23 +317,6 @@ tiers: cmd: '. /home/opnfv/functest/conf/stackrc && security_scan --config /usr/local/etc/securityscanning/config.ini' - - case_name: copper - enabled: false - project_name: copper - criteria: 100 - blocking: false - description: >- - Test suite for policy management based on OpenStack Congress - dependencies: - installer: 'apex' - scenario: '^((?!fdio).)*$' - run: - module: 'functest.core.feature' - class: 'BashFeature' - args: - cmd: 'cd /src/copper/tests && bash run.sh && cd -' - - - case_name: multisite enabled: false project_name: multisite @@ -382,6 +365,7 @@ tiers: - case_name: parser-basics + enabled: false project_name: parser criteria: 100 blocking: false @@ -414,23 +398,6 @@ tiers: cmd: 'cd /src/domino && ./tests/run_multinode.sh' - - case_name: gluon_vping - enabled: false - project_name: netready - criteria: 100 - blocking: false - description: >- - Test suite from Netready project. - dependencies: - installer: 'apex' - scenario: 'gluon' - run: - module: 'functest.core.feature' - class: 'BashFeature' - args: - cmd: 'cd /home/opnfv/repos/netready/test/functest && python ./gluon-test-suite.py' - - - case_name: barometercollectd enabled: false project_name: barometer diff --git a/functest/ci/tier_builder.py b/functest/ci/tier_builder.py index f8038468..d2722dc2 100644 --- a/functest/ci/tier_builder.py +++ b/functest/ci/tier_builder.py @@ -1,11 +1,11 @@ #!/usr/bin/env python + +# Copyright (c) 2016 Ericsson AB and others. # -# jose.lausuch@ericsson.com # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 -# import tier_handler as th import yaml @@ -52,11 +52,14 @@ class TierBuilder(object): dependency=dep, criteria=dic_testcase['criteria'], blocking=dic_testcase['blocking'], - description=dic_testcase['description']) + description=dic_testcase['description'], + project=dic_testcase['project_name']) if (testcase.is_compatible(self.ci_installer, self.ci_scenario) and testcase.is_enabled()): tier.add_test(testcase) + else: + tier.skip_test(testcase) self.tier_objects.append(tier) diff --git a/functest/ci/tier_handler.py b/functest/ci/tier_handler.py index 4f2f14ec..dd3e77ce 100644 --- a/functest/ci/tier_handler.py +++ b/functest/ci/tier_handler.py @@ -1,14 +1,18 @@ #!/usr/bin/env python + +# Copyright (c) 2016 Ericsson AB and others. # -# jose.lausuch@ericsson.com # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 -# import re +import textwrap + +import prettytable + LINE_LENGTH = 72 @@ -32,6 +36,7 @@ class Tier(object): def __init__(self, name, order, ci_loop, description=""): self.tests_array = [] + self.skipped_tests_array = [] self.name = name self.order = order self.ci_loop = ci_loop @@ -40,12 +45,18 @@ class Tier(object): def add_test(self, testcase): self.tests_array.append(testcase) + def skip_test(self, testcase): + self.skipped_tests_array.append(testcase) + def get_tests(self): array_tests = [] for test in self.tests_array: array_tests.append(test) return array_tests + def get_skipped_test(self): + return self.skipped_tests_array + def get_test_names(self): array_tests = [] for test in self.tests_array: @@ -75,31 +86,16 @@ class Tier(object): return self.ci_loop def __str__(self): - lines = split_text(self.description, LINE_LENGTH - 6) - - out = "" - out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) - out += ("| Tier: " + self.name.ljust(LINE_LENGTH - 10) + "|\n") - out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) - out += ("| Order: " + str(self.order).ljust(LINE_LENGTH - 10) + "|\n") - out += ("| CI Loop: " + str(self.ci_loop).ljust(LINE_LENGTH - 12) + - "|\n") - out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n") - for line in lines: - out += ("| " + line.ljust(LINE_LENGTH - 7) + " |\n") - out += ("| Test cases:".ljust(LINE_LENGTH - 1) + "|\n") - tests = self.get_test_names() - if len(tests) > 0: - for i in range(len(tests)): - out += ("| - %s |\n" % tests[i].ljust(LINE_LENGTH - 9)) - else: - out += ("| (There are no supported test cases " - .ljust(LINE_LENGTH - 1) + "|\n") - out += ("| in this tier for the given scenario) " - .ljust(LINE_LENGTH - 1) + "|\n") - out += ("|".ljust(LINE_LENGTH - 1) + "|\n") - out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2))) - return out + msg = prettytable.PrettyTable( + header_style='upper', padding_width=5, + field_names=['tiers', 'order', 'CI Loop', 'description', + 'testcases']) + msg.add_row( + [self.name, self.order, self.ci_loop, + textwrap.fill(self.description, width=40), + textwrap.fill(' '.join([str(x.get_name( + )) for x in self.get_tests()]), width=40)]) + return msg.get_string() class TestCase(object): @@ -109,13 +105,15 @@ class TestCase(object): dependency, criteria, blocking, - description=""): + description="", + project=""): self.name = name self.enabled = enabled self.dependency = dependency self.criteria = criteria self.blocking = blocking self.description = description + self.project = project @staticmethod def is_none(item): @@ -147,26 +145,16 @@ class TestCase(object): def is_blocking(self): return self.blocking + def get_project(self): + return self.project + def __str__(self): - lines = split_text(self.description, LINE_LENGTH - 6) - - out = "" - out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) - out += ("| Testcase: " + self.name.ljust(LINE_LENGTH - 14) + "|\n") - out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) - out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n") - for line in lines: - out += ("| " + line.ljust(LINE_LENGTH - 7) + " |\n") - out += ("| Criteria: " + - str(self.criteria).ljust(LINE_LENGTH - 14) + "|\n") - out += ("| Dependencies:".ljust(LINE_LENGTH - 1) + "|\n") - installer = self.dependency.get_installer() - scenario = self.dependency.get_scenario() - out += ("| - Installer:" + installer.ljust(LINE_LENGTH - 17) + "|\n") - out += ("| - Scenario :" + scenario.ljust(LINE_LENGTH - 17) + "|\n") - out += ("|".ljust(LINE_LENGTH - 1) + "|\n") - out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2))) - return out + msg = prettytable.PrettyTable( + header_style='upper', padding_width=5, + field_names=['test case', 'description', 'criteria', 'dependency']) + msg.add_row([self.name, textwrap.fill(self.description, width=40), + self.criteria, self.dependency]) + return msg.get_string() class Dependency(object): @@ -182,6 +170,7 @@ class Dependency(object): return self.scenario def __str__(self): - return ("Dependency info:\n" - " installer: " + self.installer + "\n" - " scenario: " + self.scenario + "\n") + delimitator = "\n" if self.get_installer( + ) and self.get_scenario() else "" + return "{}{}{}".format(self.get_installer(), delimitator, + self.get_scenario()) diff --git a/functest/cli/commands/cli_env.py b/functest/cli/commands/cli_env.py index 99d36996..72a870b5 100644 --- a/functest/cli/commands/cli_env.py +++ b/functest/cli/commands/cli_env.py @@ -16,7 +16,7 @@ from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils -class CliEnv(object): +class Env(object): def __init__(self): pass @@ -56,17 +56,14 @@ class CliEnv(object): if self.status(verbose=False) == 0: STATUS = "ready" - msg = prettytable.PrettyTable( - header_style='upper', padding_width=5, - field_names=['Functest Environment', 'value']) - msg.add_row(['INSTALLER', installer_info]) - msg.add_row(['SCENARIO', scenario]) - msg.add_row(['POD', node]) - if build_tag: - msg.add_row(['BUILD TAG', build_tag]) - msg.add_row(['DEBUG FLAG', is_debug]) - msg.add_row(['STATUS', STATUS]) - click.echo(msg.get_string()) + env_info = {'INSTALLER': installer_info, + 'SCENARIO': scenario, + 'POD': node, + 'DEBUG FLAG': is_debug, + 'BUILD_TAG': build_tag, + 'STATUS': STATUS} + + return env_info def status(self, verbose=True): ret_val = 0 @@ -78,3 +75,19 @@ class CliEnv(object): click.echo("Functest environment ready to run tests.\n") return ret_val + + +class CliEnv(Env): + + def __init__(self): + super(CliEnv, self).__init__() + + def show(self): + env_info = super(CliEnv, self).show() + msg = prettytable.PrettyTable( + header_style='upper', padding_width=5, + field_names=['Functest Environment', 'value']) + for key, value in env_info.iteritems(): + if key is not None: + msg.add_row([key, value]) + click.echo(msg.get_string()) diff --git a/functest/cli/commands/cli_os.py b/functest/cli/commands/cli_os.py index f4ec1661..e97ab080 100644 --- a/functest/cli/commands/cli_os.py +++ b/functest/cli/commands/cli_os.py @@ -18,7 +18,7 @@ import functest.utils.openstack_clean as os_clean import functest.utils.openstack_snapshot as os_snapshot -class CliOpenStack(object): +class OpenStack(object): def __init__(self): self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL') @@ -43,9 +43,11 @@ class CliOpenStack(object): @staticmethod def show_credentials(): + dic_credentials = {} for key, value in os.environ.items(): if key.startswith('OS_'): - click.echo("{}={}".format(key, value)) + dic_credentials.update({key: value}) + return dic_credentials def check(self): self.ping_endpoint() @@ -88,3 +90,16 @@ class CliOpenStack(object): "'functest openstack snapshot-create'") return os_clean.main() + + +class CliOpenStack(OpenStack): + + def __init__(self): + super(CliOpenStack, self).__init__() + + @staticmethod + def show_credentials(): + dic_credentials = OpenStack.show_credentials() + for key, value in dic_credentials.items(): + if key.startswith('OS_'): + click.echo("{}={}".format(key, value)) diff --git a/functest/cli/commands/cli_testcase.py b/functest/cli/commands/cli_testcase.py index cb3d4739..65dd9ab7 100644 --- a/functest/cli/commands/cli_testcase.py +++ b/functest/cli/commands/cli_testcase.py @@ -20,7 +20,7 @@ import functest.utils.functest_utils as ft_utils import functest.utils.functest_vacation as vacation -class CliTestcase(object): +class Testcase(object): def __init__(self): self.tiers = tb.TierBuilder( @@ -33,15 +33,11 @@ class CliTestcase(object): for tier in self.tiers.get_tiers(): for test in tier.get_tests(): summary += (" %s\n" % test.get_name()) - click.echo(summary) + return summary def show(self, testname): description = self.tiers.get_test(testname) - if description is None: - click.echo("The test case '%s' does not exist or is not supported." - % testname) - - click.echo(description) + return description @staticmethod def run(testname, noclean=False, report=False): @@ -62,3 +58,20 @@ class CliTestcase(object): for test in tests: cmd = "run_tests {}-t {}".format(flags, test) ft_utils.execute_command(cmd) + + +class CliTestcase(Testcase): + + def __init__(self): + super(CliTestcase, self).__init__() + + def list(self): + click.echo(super(CliTestcase, self).list()) + + def show(self, testname): + testcase_show = super(CliTestcase, self).show(testname) + if testcase_show: + click.echo(testcase_show) + else: + click.echo("The test case '%s' does not exist or is not supported." + % testname) diff --git a/functest/cli/commands/cli_tier.py b/functest/cli/commands/cli_tier.py index 9b2e60ba..995354bb 100644 --- a/functest/cli/commands/cli_tier.py +++ b/functest/cli/commands/cli_tier.py @@ -19,7 +19,7 @@ from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils -class CliTier(object): +class Tier(object): def __init__(self): self.tiers = tb.TierBuilder( @@ -34,26 +34,23 @@ class CliTier(object): % (tier.get_order(), tier.get_name(), tier.get_test_names())) - click.echo(summary) + return summary def show(self, tiername): tier = self.tiers.get_tier(tiername) if tier is None: - tier_names = self.tiers.get_tier_names() - click.echo("The tier with name '%s' does not exist. " - "Available tiers are:\n %s\n" % (tiername, tier_names)) + return None else: - click.echo(self.tiers.get_tier(tiername)) + tier_info = self.tiers.get_tier(tiername) + return tier_info def gettests(self, tiername): tier = self.tiers.get_tier(tiername) if tier is None: - tier_names = self.tiers.get_tier_names() - click.echo("The tier with name '%s' does not exist. " - "Available tiers are:\n %s\n" % (tiername, tier_names)) + return None else: tests = tier.get_test_names() - click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests)) + return tests @staticmethod def run(tiername, noclean=False, report=False): @@ -70,3 +67,30 @@ class CliTier(object): else: cmd = "run_tests {}-t {}".format(flags, tiername) ft_utils.execute_command(cmd) + + +class CliTier(Tier): + + def __init__(self): + super(CliTier, self).__init__() + + def list(self): + click.echo(super(CliTier, self).list()) + + def show(self, tiername): + tier_info = super(CliTier, self).show(tiername) + if tier_info: + click.echo(tier_info) + else: + tier_names = self.tiers.get_tier_names() + click.echo("The tier with name '%s' does not exist. " + "Available tiers are:\n %s\n" % (tiername, tier_names)) + + def gettests(self, tiername): + tests = super(CliTier, self).gettests(tiername) + if tests: + click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests)) + else: + tier_names = self.tiers.get_tier_names() + click.echo("The tier with name '%s' does not exist. " + "Available tiers are:\n %s\n" % (tiername, tier_names)) diff --git a/functest/energy/energy.py b/functest/energy/energy.py index 580bc6b0..c410e84f 100644 --- a/functest/energy/energy.py +++ b/functest/energy/energy.py @@ -16,6 +16,7 @@ import urllib from functools import wraps import requests +import urllib3 import functest.utils.functest_utils as ft_utils @@ -76,7 +77,7 @@ class EnergyRecorder(object): INITIAL_STEP = "running" # Default connection timeout - CONNECTION_TIMOUT = 1 + CONNECTION_TIMOUT = urllib3.Timeout(connect=1, read=3) @staticmethod def load_config(): @@ -104,13 +105,13 @@ class EnergyRecorder(object): "API recorder at: " + energy_recorder_uri + uri_comp) # Creds - user = ft_utils.get_functest_config( + creds_usr = ft_utils.get_functest_config( "energy_recorder.api_user") - password = ft_utils.get_functest_config( + creds_pass = ft_utils.get_functest_config( "energy_recorder.api_password") - if user != "" and password != "": - energy_recorder_api_auth = (user, password) + if creds_usr != "" and creds_pass != "": + energy_recorder_api_auth = (creds_usr, creds_pass) else: energy_recorder_api_auth = None diff --git a/functest/opnfv_tests/openstack/refstack_client/refstack_client.py b/functest/opnfv_tests/openstack/refstack_client/refstack_client.py index 921d69b4..86053ccf 100644 --- a/functest/opnfv_tests/openstack/refstack_client/refstack_client.py +++ b/functest/opnfv_tests/openstack/refstack_client/refstack_client.py @@ -107,13 +107,13 @@ class RefstackClient(testcase.OSGCTestCase): num_failures = match[1] LOGGER.info("".join(match)) success_testcases = [] - for match in re.findall(r"\{0\}(.*?)[. ]*ok", output): + for match in re.findall(r"\{0\} (.*?)[. ]*ok", output): success_testcases.append(match) failed_testcases = [] - for match in re.findall(r"\{0\}(.*?)[. ]*FAILED", output): + for match in re.findall(r"\{0\} (.*?)[. ]*FAILED", output): failed_testcases.append(match) skipped_testcases = [] - for match in re.findall(r"\{0\}(.*?)[. ]*SKIPPED:", output): + for match in re.findall(r"\{0\} (.*?)[. ]*SKIPPED:", output): skipped_testcases.append(match) num_executed = int(num_tests) - int(num_skipped) diff --git a/functest/opnfv_tests/openstack/tempest/conf_utils.py b/functest/opnfv_tests/openstack/tempest/conf_utils.py index 2313ec04..7c88fc5d 100644 --- a/functest/opnfv_tests/openstack/tempest/conf_utils.py +++ b/functest/opnfv_tests/openstack/tempest/conf_utils.py @@ -336,7 +336,7 @@ def configure_tempest_update_params(tempest_conf_file, 'into tempest.conf file') with open(TEMPEST_CONF_YAML) as f: conf_yaml = yaml.safe_load(f) - if not conf_yaml: + if conf_yaml: sections = config.sections() for section in conf_yaml: if section not in sections: diff --git a/functest/opnfv_tests/openstack/tempest/tempest.py b/functest/opnfv_tests/openstack/tempest/tempest.py index 4993c74a..b00bc6af 100644 --- a/functest/opnfv_tests/openstack/tempest/tempest.py +++ b/functest/opnfv_tests/openstack/tempest/tempest.py @@ -196,13 +196,13 @@ class TempestCommon(testcase.OSGCTestCase): output = logfile.read() success_testcases = [] - for match in re.findall('(.*?)[. ]*success ', output): + for match in re.findall('.*\{0\} (.*?)[. ]*success ', output): success_testcases.append(match) failed_testcases = [] - for match in re.findall('(.*?)[. ]*fail ', output): + for match in re.findall('.*\{0\} (.*?)[. ]*fail ', output): failed_testcases.append(match) skipped_testcases = [] - for match in re.findall('(.*?)[. ]*skip:', output): + for match in re.findall('.*\{0\} (.*?)[. ]*skip:', output): skipped_testcases.append(match) self.details = {"tests": int(num_tests), diff --git a/functest/tests/unit/ci/test_run_tests.py b/functest/tests/unit/ci/test_run_tests.py index fb8cb391..7495c40e 100644 --- a/functest/tests/unit/ci/test_run_tests.py +++ b/functest/tests/unit/ci/test_run_tests.py @@ -54,11 +54,6 @@ class RunTestsTesting(unittest.TestCase): self.run_tests_parser = run_tests.RunTestsParser() - @mock.patch('functest.ci.run_tests.logger.info') - def test_print_separator(self, mock_logger_info): - self.runner.print_separator(self.sep) - mock_logger_info.assert_called_once_with(self.sep * 44) - @mock.patch('functest.ci.run_tests.logger.error') def test_source_rc_file_missing_file(self, mock_logger_error): with mock.patch('functest.ci.run_tests.os.path.isfile', @@ -120,8 +115,7 @@ class RunTestsTesting(unittest.TestCase): args = {'get_name.return_value': 'test_name', 'needs_clean.return_value': False} mock_test.configure_mock(**args) - with mock.patch('functest.ci.run_tests.Runner.print_separator'),\ - mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ + with mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ mock.patch('functest.ci.run_tests.Runner.get_run_dict', return_value=None), \ self.assertRaises(Exception) as context: @@ -129,7 +123,6 @@ class RunTestsTesting(unittest.TestCase): msg = "Cannot import the class for the test case." self.assertTrue(msg in context) - @mock.patch('functest.ci.run_tests.Runner.print_separator') @mock.patch('functest.ci.run_tests.Runner.source_rc_file') @mock.patch('importlib.import_module', name="module", return_value=mock.Mock(test_class=mock.Mock( @@ -145,123 +138,107 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.Runner.get_run_dict', return_value=test_run_dict): self.runner.clean_flag = True - self.runner.run_test(mock_test, 'tier_name') + self.runner.run_test(mock_test) self.assertEqual(self.runner.overall_result, run_tests.Result.EX_OK) - @mock.patch('functest.ci.run_tests.logger.info') - def test_run_tier_default(self, mock_logger_info): - with mock.patch('functest.ci.run_tests.Runner.print_separator'), \ - mock.patch( - 'functest.ci.run_tests.Runner.run_test', - return_value=TestCase.EX_OK) as mock_method: - self.runner.run_tier(self.tier) - mock_method.assert_any_call(mock.ANY, 'test_tier') - self.assertTrue(mock_logger_info.called) + @mock.patch('functest.ci.run_tests.Runner.run_test', + return_value=TestCase.EX_OK) + def test_run_tier_default(self, *mock_methods): + self.assertEqual(self.runner.run_tier(self.tier), + run_tests.Result.EX_OK) + mock_methods[0].assert_called_with(mock.ANY) @mock.patch('functest.ci.run_tests.logger.info') def test_run_tier_missing_test(self, mock_logger_info): - with mock.patch('functest.ci.run_tests.Runner.print_separator'): - self.tier.get_tests.return_value = None - self.assertEqual(self.runner.run_tier(self.tier), 0) - self.assertTrue(mock_logger_info.called) + self.tier.get_tests.return_value = None + self.assertEqual(self.runner.run_tier(self.tier), + run_tests.Result.EX_ERROR) + self.assertTrue(mock_logger_info.called) @mock.patch('functest.ci.run_tests.logger.info') - def test_run_all_default(self, mock_logger_info): - with mock.patch( - 'functest.ci.run_tests.Runner.run_tier') as mock_method: - CONST.__setattr__('CI_LOOP', 'test_ci_loop') - self.runner.run_all(self.tiers) - mock_method.assert_any_call(self.tier) - self.assertTrue(mock_logger_info.called) + @mock.patch('functest.ci.run_tests.Runner.run_tier') + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_run_all_default(self, *mock_methods): + CONST.__setattr__('CI_LOOP', 'test_ci_loop') + self.runner.run_all() + mock_methods[1].assert_not_called() + self.assertTrue(mock_methods[2].called) @mock.patch('functest.ci.run_tests.logger.info') - def test_run_all_missing_tier(self, mock_logger_info): + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_run_all_missing_tier(self, *mock_methods): CONST.__setattr__('CI_LOOP', 'loop_re_not_available') - self.runner.run_all(self.tiers) - self.assertTrue(mock_logger_info.called) + self.runner.run_all() + self.assertTrue(mock_methods[1].called) - def test_main_failed(self): + @mock.patch('functest.ci.run_tests.Runner.source_rc_file', + side_effect=Exception) + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_main_failed(self, *mock_methods): kwargs = {'test': 'test_name', 'noclean': True, 'report': True} - mock_obj = mock.Mock() args = {'get_tier.return_value': False, 'get_test.return_value': False} - mock_obj.configure_mock(**args) - with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file', - side_effect=Exception): - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_ERROR) - with mock.patch('functest.ci.run_tests.tb.TierBuilder', - return_value=mock_obj), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file', - side_effect=Exception): - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_ERROR) - - def test_main_tier(self, *args): + self.runner._tiers = mock.Mock() + self.runner._tiers.configure_mock(**args) + self.assertEqual(self.runner.main(**kwargs), + run_tests.Result.EX_ERROR) + mock_methods[1].assert_called_once_with() + + @mock.patch('functest.ci.run_tests.Runner.source_rc_file') + @mock.patch('functest.ci.run_tests.Runner.run_test', + return_value=TestCase.EX_OK) + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_main_tier(self, *mock_methods): mock_tier = mock.Mock() - args = {'get_name.return_value': 'tier_name'} + args = {'get_name.return_value': 'tier_name', + 'get_tests.return_value': ['test_name']} mock_tier.configure_mock(**args) kwargs = {'test': 'tier_name', 'noclean': True, 'report': True} - mock_obj = mock.Mock() args = {'get_tier.return_value': mock_tier, 'get_test.return_value': None} - mock_obj.configure_mock(**args) - with mock.patch('functest.ci.run_tests.tb.TierBuilder', - return_value=mock_obj), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ - mock.patch('functest.ci.run_tests.Runner.run_tier') as m: - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_OK) - self.assertTrue(m.called) - - def test_main_test(self, *args): + self.runner._tiers = mock.Mock() + self.runner._tiers.configure_mock(**args) + self.assertEqual(self.runner.main(**kwargs), + run_tests.Result.EX_OK) + mock_methods[1].assert_called_once_with('test_name') + + @mock.patch('functest.ci.run_tests.Runner.source_rc_file') + @mock.patch('functest.ci.run_tests.Runner.run_test', + return_value=TestCase.EX_OK) + def test_main_test(self, *mock_methods): kwargs = {'test': 'test_name', 'noclean': True, 'report': True} - mock_test = mock.Mock() - args = {'get_name.return_value': 'test_name', - 'needs_clean.return_value': True} - mock_test.configure_mock(**args) - mock_obj = mock.Mock() args = {'get_tier.return_value': None, - 'get_test.return_value': mock_test} - mock_obj.configure_mock(**args) - with mock.patch('functest.ci.run_tests.tb.TierBuilder', - return_value=mock_obj), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ - mock.patch('functest.ci.run_tests.Runner.run_test', - return_value=TestCase.EX_OK) as m: - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_OK) - self.assertTrue(m.called) - - def test_main_all_tier(self, *args): + 'get_test.return_value': 'test_name'} + self.runner._tiers = mock.Mock() + self.runner._tiers.configure_mock(**args) + self.assertEqual(self.runner.main(**kwargs), + run_tests.Result.EX_OK) + mock_methods[0].assert_called_once_with('test_name') + + @mock.patch('functest.ci.run_tests.Runner.source_rc_file') + @mock.patch('functest.ci.run_tests.Runner.run_all') + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_main_all_tier(self, *mock_methods): kwargs = {'test': 'all', 'noclean': True, 'report': True} - mock_obj = mock.Mock() args = {'get_tier.return_value': None, 'get_test.return_value': None} - mock_obj.configure_mock(**args) - with mock.patch('functest.ci.run_tests.tb.TierBuilder', - return_value=mock_obj), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ - mock.patch('functest.ci.run_tests.Runner.run_all') as m: - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_OK) - self.assertTrue(m.called) - - def test_main_any_tier_test_ko(self, *args): + self.runner._tiers = mock.Mock() + self.runner._tiers.configure_mock(**args) + self.assertEqual(self.runner.main(**kwargs), + run_tests.Result.EX_OK) + mock_methods[1].assert_called_once_with() + + @mock.patch('functest.ci.run_tests.Runner.source_rc_file') + @mock.patch('functest.ci.run_tests.Runner.summary') + def test_main_any_tier_test_ko(self, *mock_methods): kwargs = {'test': 'any', 'noclean': True, 'report': True} - mock_obj = mock.Mock() args = {'get_tier.return_value': None, 'get_test.return_value': None} - mock_obj.configure_mock(**args) - with mock.patch('functest.ci.run_tests.tb.TierBuilder', - return_value=mock_obj), \ - mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ - mock.patch('functest.ci.run_tests.logger.debug') as m: - self.assertEqual(self.runner.main(**kwargs), - run_tests.Result.EX_ERROR) - self.assertTrue(m.called) + self.runner._tiers = mock.Mock() + self.runner._tiers.configure_mock(**args) + self.assertEqual(self.runner.main(**kwargs), + run_tests.Result.EX_ERROR) if __name__ == "__main__": diff --git a/functest/tests/unit/ci/test_tier_builder.py b/functest/tests/unit/ci/test_tier_builder.py index ab75e15b..700c6e91 100644 --- a/functest/tests/unit/ci/test_tier_builder.py +++ b/functest/tests/unit/ci/test_tier_builder.py @@ -24,7 +24,8 @@ class TierBuilderTesting(unittest.TestCase): 'case_name': 'test_name', 'criteria': 'test_criteria', 'blocking': 'test_blocking', - 'description': 'test_desc'} + 'description': 'test_desc', + 'project_name': 'project_name'} self.dic_tier = {'name': 'test_tier', 'order': 'test_order', diff --git a/functest/tests/unit/openstack/refstack_client/test_refstack_client.py b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py index e4e3364d..c5601075 100644 --- a/functest/tests/unit/openstack/refstack_client/test_refstack_client.py +++ b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py @@ -79,9 +79,9 @@ class OSRefstackClientTesting(unittest.TestCase): ''') self.details = {"tests": 3, "failures": 1, - "success": [' tempest.api.compute [18.464988s]'], - "errors": [' tempest.api.volume [0.230334s]'], - "skipped": [' tempest.api.network [1.265828s]']} + "success": ['tempest.api.compute [18.464988s]'], + "errors": ['tempest.api.volume [0.230334s]'], + "skipped": ['tempest.api.network [1.265828s]']} with mock.patch('__builtin__.open', mock.mock_open(read_data=log_file)): self.refstackclient.parse_refstack_result() diff --git a/functest/tests/unit/openstack/tempest/test_conf_utils.py b/functest/tests/unit/openstack/tempest/test_conf_utils.py index 79ef9915..a807ae46 100644 --- a/functest/tests/unit/openstack/tempest/test_conf_utils.py +++ b/functest/tests/unit/openstack/tempest/test_conf_utils.py @@ -262,10 +262,9 @@ class OSTempestConfUtilsTesting(unittest.TestCase): mock.patch('__builtin__.open', mock.mock_open()), \ mock.patch('functest.opnfv_tests.openstack.tempest.' 'conf_utils.backup_tempest_config'), \ - mock.patch('functest.utils.functest_utils.yaml.safe_load') \ - as mock_yaml: + mock.patch('functest.utils.functest_utils.yaml.safe_load', + return_value={'validation': {'ssh_timeout': 300}}): CONST.__setattr__('OS_ENDPOINT_TYPE', None) - mock_yaml.return_value = mock.Mock() conf_utils.\ configure_tempest_update_params('test_conf_file', IMAGE_ID=image_id, diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 98c7d6e9..b4cc5b73 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -133,24 +133,20 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_scenario(), self.scenario) - @mock.patch('functest.utils.functest_utils.get_build_tag') - def test_get_version_daily_job(self, mock_get_build_tag): - mock_get_build_tag.return_value = self.build_tag + def test_get_version_daily_job(self): + CONST.__setattr__('BUILD_TAG', self.build_tag) self.assertEqual(functest_utils.get_version(), self.version) - @mock.patch('functest.utils.functest_utils.get_build_tag') - def test_get_version_weekly_job(self, mock_get_build_tag): - mock_get_build_tag.return_value = self.build_tag_week + def test_get_version_weekly_job(self): + CONST.__setattr__('BUILD_TAG', self.build_tag_week) self.assertEqual(functest_utils.get_version(), self.version) - @mock.patch('functest.utils.functest_utils.get_build_tag') - def test_get_version_with_dummy_build_tag(self, mock_get_build_tag): - mock_get_build_tag.return_value = 'whatever' + def test_get_version_with_dummy_build_tag(self): + CONST.__setattr__('BUILD_TAG', 'whatever') self.assertEqual(functest_utils.get_version(), 'unknown') - @mock.patch('functest.utils.functest_utils.get_build_tag') - def test_get_version_unknown(self, mock_get_build_tag): - mock_get_build_tag.return_value = "unknown_build_tag" + def test_get_version_unknown(self): + CONST.__setattr__('BUILD_TAG', 'unknown_build_tag') self.assertEqual(functest_utils.get_version(), "unknown") @mock.patch('functest.utils.functest_utils.logger.info') @@ -173,33 +169,15 @@ class FunctestUtilsTesting(unittest.TestCase): self.node_name) @mock.patch('functest.utils.functest_utils.logger.info') - def test_get_build_tag_failed(self, mock_logger_info): - with mock.patch.dict(os.environ, - {}, - clear=True): - self.assertEqual(functest_utils.get_build_tag(), - "none") - mock_logger_info.assert_called_once_with("Impossible to retrieve" - " the build tag") - - def test_get_build_tag_default(self): - with mock.patch.dict(os.environ, - {'BUILD_TAG': self.build_tag}, - clear=True): - self.assertEqual(functest_utils.get_build_tag(), - self.build_tag) - - @mock.patch('functest.utils.functest_utils.logger.info') def test_logger_test_results(self, mock_logger_info): CONST.__setattr__('results_test_db_url', self.db_url) + CONST.__setattr__('BUILD_TAG', self.build_tag) with mock.patch('functest.utils.functest_utils.get_pod_name', return_value=self.node_name), \ mock.patch('functest.utils.functest_utils.get_scenario', return_value=self.scenario), \ mock.patch('functest.utils.functest_utils.get_version', - return_value=self.version), \ - mock.patch('functest.utils.functest_utils.get_build_tag', - return_value=self.build_tag): + return_value=self.version): functest_utils.logger_test_results(self.project, self.case_name, self.status, self.details) mock_logger_info.assert_called_once_with( diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index cabd18e5..3bd7e3dd 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -13,6 +13,7 @@ import unittest import mock from functest.utils import openstack_utils +from functest.utils.constants import CONST class OSUtilsTesting(unittest.TestCase): @@ -187,11 +188,18 @@ class OSUtilsTesting(unittest.TestCase): mock_obj.configure_mock(**attrs) self.role = mock_obj + mock_obj = mock.Mock() + attrs = {'id': 'domain_id', + 'name': 'test_domain'} + mock_obj.configure_mock(**attrs) + self.domain = mock_obj + self.keystone_client = mock.Mock() attrs = {'projects.list.return_value': [self.tenant], 'tenants.list.return_value': [self.tenant], 'users.list.return_value': [self.user], 'roles.list.return_value': [self.role], + 'domains.list.return_value': [self.domain], 'projects.create.return_value': self.tenant, 'tenants.create.return_value': self.tenant, 'users.create.return_value': self.user, @@ -1650,9 +1658,16 @@ class OSUtilsTesting(unittest.TestCase): 'test_role'), 'role_id') + def test_get_domain_id_default(self): + self.assertEqual(openstack_utils. + get_domain_id(self.keystone_client, + 'test_domain'), + 'domain_id') + def test_create_tenant_default(self): with mock.patch('functest.utils.openstack_utils.' 'is_keystone_v3', return_value=True): + CONST.__setattr__('OS_PROJECT_DOMAIN_NAME', 'Default') self.assertEqual(openstack_utils. create_tenant(self.keystone_client, 'test_tenant', diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py index 91781bd2..bf68e43a 100644 --- a/functest/utils/functest_utils.py +++ b/functest/utils/functest_utils.py @@ -107,7 +107,9 @@ def get_version(): # jenkins-functest-fuel-baremetal-weekly-master-8 # use regex to match branch info rule = "(dai|week)ly-(.+?)-[0-9]*" - build_tag = get_build_tag() + build_tag = CONST.__getattribute__('BUILD_TAG') + if not build_tag: + build_tag = 'none' m = re.search(rule, build_tag) if m: return m.group(2) @@ -128,19 +130,6 @@ def get_pod_name(): return "unknown-pod" -def get_build_tag(): - """ - Get build tag of jenkins jobs - """ - try: - build_tag = os.environ['BUILD_TAG'] - except KeyError: - logger.info("Impossible to retrieve the build tag") - build_tag = "none" - - return build_tag - - def logger_test_results(project, case_name, status, details): """ Format test case results for the logger @@ -148,7 +137,7 @@ def logger_test_results(project, case_name, status, details): pod_name = get_pod_name() scenario = get_scenario() version = get_version() - build_tag = get_build_tag() + build_tag = CONST.__getattribute__('BUILD_TAG') db_url = CONST.__getattribute__("results_test_db_url") logger.info( diff --git a/functest/utils/openstack_utils.py b/functest/utils/openstack_utils.py index 1bdfa253..335f14cd 100644 --- a/functest/utils/openstack_utils.py +++ b/functest/utils/openstack_utils.py @@ -22,6 +22,7 @@ from heatclient import client as heatclient from novaclient import client as novaclient from keystoneclient import client as keystoneclient from neutronclient.neutron import client as neutronclient +from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils @@ -1376,13 +1377,25 @@ def get_role_id(keystone_client, role_name): return id +def get_domain_id(keystone_client, domain_name): + domains = keystone_client.domains.list() + id = '' + for d in domains: + if d.name == domain_name: + id = d.id + break + return id + + def create_tenant(keystone_client, tenant_name, tenant_description): try: if is_keystone_v3(): + domain_name = CONST.__getattribute__('OS_PROJECT_DOMAIN_NAME') + domain_id = get_domain_id(keystone_client, domain_name) tenant = keystone_client.projects.create( name=tenant_name, description=tenant_description, - domain="default", + domain=domain_id, enabled=True) else: tenant = keystone_client.tenants.create(tenant_name, |