3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 Resources to handle testcase related requests
22 from flask import jsonify
23 from flasgger.utils import swag_from
25 from functest.api.base import ApiResource
26 from functest.api.common import api_utils, thread
27 from functest.cli.commands.cli_testcase import Testcase
28 from functest.api.database.v1.handlers import TasksHandler
29 from functest.utils.constants import CONST
30 import functest.utils.functest_utils as ft_utils
32 LOGGER = logging.getLogger(__name__)
34 ADDRESS = socket.gethostbyname(socket.gethostname())
35 ENDPOINT_TESTCASES = ('http://{}:5000/api/v1/functest/testcases'
39 class V1Testcases(ApiResource):
40 """ V1Testcases Resource class"""
42 @swag_from(pkg_resources.resource_filename(
43 'functest', 'api/swagger/testcases.yaml'))
44 def get(self): # pylint: disable=no-self-use
45 """ GET all testcases """
46 testcases_list = Testcase().list()
47 result = {'testcases': re.split(' |\n ', testcases_list)[1:]}
48 return jsonify(result)
51 class V1Testcase(ApiResource):
52 """ V1Testcase Resource class"""
55 pkg_resources.resource_filename('functest',
56 'api/swagger/testcase.yaml'),
57 endpoint='{0}/<testcase_name>'.format(ENDPOINT_TESTCASES))
58 def get(self, testcase_name): # pylint: disable=no-self-use
59 """ GET the info of one testcase"""
60 testcase = Testcase().show(testcase_name)
62 return api_utils.result_handler(
64 data="The test case '%s' does not exist or is not supported"
67 testcase_info = api_utils.change_obj_to_dict(testcase)
68 dependency_dict = api_utils.change_obj_to_dict(
69 testcase_info.get('dependency'))
70 testcase_info.pop('name')
71 testcase_info.pop('dependency')
72 result = {'testcase': testcase_name}
73 result.update(testcase_info)
74 result.update({'dependency': dependency_dict})
75 return jsonify(result)
78 pkg_resources.resource_filename('functest',
79 'api/swagger/testcase_run.yaml'),
80 endpoint='{0}/action'.format(ENDPOINT_TESTCASES))
82 """ Used to handle post request """
83 return self._dispatch_post()
85 def run_test_case(self, args):
86 """ Run a testcase """
88 case_name = args['testcase']
90 return api_utils.result_handler(
91 status=1, data='testcase name must be provided')
93 testcase = Testcase().show(case_name)
95 return api_utils.result_handler(
97 data="The test case '%s' does not exist or is not supported"
100 task_id = str(uuid.uuid4())
102 task_args = {'testcase': case_name, 'task_id': task_id}
104 task_args.update(args.get('opts', {}))
106 task_thread = thread.TaskThread(self._run, task_args, TasksHandler())
109 result = {'testcase': case_name, 'task_id': task_id}
110 return jsonify({'result': result})
112 def _run(self, args): # pylint: disable=no-self-use
113 """ The built_in function to run a test case """
115 case_name = args.get('testcase')
116 self._update_logging_ini(args.get('task_id'))
119 cmd = "run_tests -t {}".format(case_name)
120 runner = ft_utils.execute_command(cmd)
121 except Exception: # pylint: disable=broad-except
123 LOGGER.exception("Running test case %s failed!", case_name)
124 if runner == os.EX_OK:
130 'installer': CONST.__getattribute__('INSTALLER_TYPE'),
131 'scenario': CONST.__getattribute__('DEPLOY_SCENARIO'),
132 'build_tag': CONST.__getattribute__('BUILD_TAG'),
133 'ci_loop': CONST.__getattribute__('CI_LOOP')
136 'task_id': args.get('task_id'),
137 'testcase': case_name,
138 'env_info': env_info,
142 return {'result': result}
144 def _update_logging_ini(self, task_id): # pylint: disable=no-self-use
145 """ Update the log file for each task"""
146 config = ConfigParser.RawConfigParser()
148 pkg_resources.resource_filename('functest', 'ci/logging.ini'))
149 log_path = os.path.join(CONST.__getattribute__('dir_results'),
150 '{}.log'.format(task_id))
151 config.set('handler_file', 'args', '("{}",)'.format(log_path))
154 pkg_resources.resource_filename(
155 'functest', 'ci/logging.ini'), 'wb') as configfile:
156 config.write(configfile)