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
20 from flask import jsonify
22 from functest.api.base import ApiResource
23 from functest.api.common import api_utils, thread
24 from functest.cli.commands.cli_testcase import Testcase
25 from functest.api.database.v1.handlers import TasksHandler
26 from functest.utils.constants import CONST
27 import functest.utils.functest_utils as ft_utils
29 LOGGER = logging.getLogger(__name__)
32 class V1Testcases(ApiResource):
33 """ V1Testcases Resource class"""
35 def get(self): # pylint: disable=no-self-use
36 """ GET all testcases """
37 testcases_list = Testcase().list()
38 result = {'testcases': testcases_list.split('\n')[:-1]}
39 return jsonify(result)
42 class V1Testcase(ApiResource):
43 """ V1Testcase Resource class"""
45 def get(self, testcase_name): # pylint: disable=no-self-use
46 """ GET the info of one testcase"""
47 testcase = Testcase().show(testcase_name)
49 return api_utils.result_handler(
51 data="The test case '%s' does not exist or is not supported"
54 testcase_info = api_utils.change_obj_to_dict(testcase)
55 dependency_dict = api_utils.change_obj_to_dict(
56 testcase_info.get('dependency'))
57 testcase_info.pop('name')
58 testcase_info.pop('dependency')
59 result = {'testcase': testcase_name}
60 result.update(testcase_info)
61 result.update({'dependency': dependency_dict})
62 return jsonify(result)
65 """ Used to handle post request """
66 return self._dispatch_post()
68 def run_test_case(self, args):
69 """ Run a testcase """
71 case_name = args['testcase']
73 return api_utils.result_handler(
74 status=1, data='testcase name must be provided')
76 testcase = Testcase().show(case_name)
78 return api_utils.result_handler(
80 data="The test case '%s' does not exist or is not supported"
83 task_id = str(uuid.uuid4())
85 task_args = {'testcase': case_name, 'task_id': task_id}
87 task_args.update(args.get('opts', {}))
89 task_thread = thread.TaskThread(self._run, task_args, TasksHandler())
92 results = {'testcase': case_name, 'task_id': task_id}
93 return jsonify(results)
95 def _run(self, args): # pylint: disable=no-self-use
96 """ The built_in function to run a test case """
98 case_name = args.get('testcase')
99 self._update_logging_ini(args.get('task_id'))
101 if not os.path.isfile(CONST.__getattribute__('env_active')):
102 raise Exception("Functest environment is not ready.")
105 cmd = "run_tests -t {}".format(case_name)
106 runner = ft_utils.execute_command(cmd)
107 except Exception: # pylint: disable=broad-except
109 LOGGER.exception("Running test case %s failed!", case_name)
110 if runner == os.EX_OK:
116 'installer': CONST.__getattribute__('INSTALLER_TYPE'),
117 'scenario': CONST.__getattribute__('DEPLOY_SCENARIO'),
118 'build_tag': CONST.__getattribute__('BUILD_TAG'),
119 'ci_loop': CONST.__getattribute__('CI_LOOP')
122 'task_id': args.get('task_id'),
123 'case_name': case_name,
124 'env_info': env_info,
128 return {'result': result}
130 def _update_logging_ini(self, task_id): # pylint: disable=no-self-use
131 """ Update the log file for each task"""
132 config = ConfigParser.RawConfigParser()
134 pkg_resources.resource_filename('functest', 'ci/logging.ini'))
135 log_path = os.path.join(CONST.__getattribute__('dir_results'),
136 '{}.log'.format(task_id))
137 config.set('handler_file', 'args', '("{}",)'.format(log_path))
140 pkg_resources.resource_filename(
141 'functest', 'ci/logging.ini'), 'wb') as configfile:
142 config.write(configfile)