diff options
-rw-r--r-- | functest/api/base.py | 5 | ||||
-rw-r--r-- | functest/api/resources/v1/tasks.py | 43 | ||||
-rw-r--r-- | functest/api/resources/v1/testcases.py | 19 | ||||
-rw-r--r-- | functest/api/urls.py | 6 |
4 files changed, 68 insertions, 5 deletions
diff --git a/functest/api/base.py b/functest/api/base.py index ffc567860..75f059b74 100644 --- a/functest/api/base.py +++ b/functest/api/base.py @@ -45,6 +45,11 @@ class ApiResource(Resource): return action, args + def _get_args(self): # pylint: disable=no-self-use + """ Convert the unicode to string for request.args """ + args = api_utils.change_to_str_in_dict(request.args) + return args + def _dispatch_post(self): """ Dispatch request """ action, args = self._post_args() diff --git a/functest/api/resources/v1/tasks.py b/functest/api/resources/v1/tasks.py index 7086e7075..e05db51be 100644 --- a/functest/api/resources/v1/tasks.py +++ b/functest/api/resources/v1/tasks.py @@ -11,9 +11,10 @@ Resources to retrieve the task results """ - +import errno import json import logging +import os import uuid from flask import jsonify @@ -21,13 +22,14 @@ from flask import jsonify from functest.api.base import ApiResource from functest.api.common import api_utils from functest.api.database.v1.handlers import TasksHandler +from functest.utils.constants import CONST LOGGER = logging.getLogger(__name__) -class V1Tasks(ApiResource): - """ V1Tasks Resource class""" +class V1Task(ApiResource): + """ V1Task Resource class""" def get(self, task_id): # pylint: disable=no-self-use """ GET the result of the task id """ @@ -56,3 +58,38 @@ class V1Tasks(ApiResource): result = {'status': status, 'result': json.loads(task.result)} return jsonify(result) + + +class V1TaskLog(ApiResource): + """ V1TaskLog Resource class""" + + def get(self, task_id): # pylint: disable=no-self-use + """ GET the log of the task id """ + try: + uuid.UUID(task_id) + except ValueError: + return api_utils.result_handler(status=1, data='Invalid task id') + + task_handler = TasksHandler() + try: + task = task_handler.get_task_by_taskid(task_id) + except ValueError: + return api_utils.result_handler(status=1, data='No such task id') + + task_log_dir = CONST.__getattribute__('dir_results') + + try: + with open(os.path.join(task_log_dir, + '{}.log'.format(task_id)), 'r') as log_file: + data = log_file.readlines() + except OSError as err: + if err.errno == errno.ENOENT: + return api_utils.result_handler( + status=1, data='Log file does not exist') + + return api_utils.result_handler( + status=1, data='Error with log file') + + return_data = {'data': data} + + return api_utils.result_handler(status=task.status, data=return_data) diff --git a/functest/api/resources/v1/testcases.py b/functest/api/resources/v1/testcases.py index f146c24ce..d708cf37b 100644 --- a/functest/api/resources/v1/testcases.py +++ b/functest/api/resources/v1/testcases.py @@ -11,10 +11,12 @@ Resources to handle testcase related requests """ -import os import logging +import os +import pkg_resources import uuid +import ConfigParser from flask import abort, jsonify from functest.api.base import ApiResource @@ -84,6 +86,7 @@ class V1Testcase(ApiResource): """ The built_in function to run a test case """ case_name = args.get('testcase') + self._update_logging_ini(args.get('task_id')) if not os.path.isfile(CONST.__getattribute__('env_active')): raise Exception("Functest environment is not ready.") @@ -113,3 +116,17 @@ class V1Testcase(ApiResource): } return {'result': result} + + def _update_logging_ini(self, task_id): # pylint: disable=no-self-use + """ Update the log file for each task""" + config = ConfigParser.RawConfigParser() + config.read( + pkg_resources.resource_filename('functest', 'ci/logging.ini')) + log_path = os.path.join(CONST.__getattribute__('dir_results'), + '{}.log'.format(task_id)) + config.set('handler_file', 'args', '("{}",)'.format(log_path)) + + with open( + pkg_resources.resource_filename( + 'functest', 'ci/logging.ini'), 'wb') as configfile: + config.write(configfile) diff --git a/functest/api/urls.py b/functest/api/urls.py index 59bf7d51c..0cc22f80f 100644 --- a/functest/api/urls.py +++ b/functest/api/urls.py @@ -63,5 +63,9 @@ URLPATTERNS = [ # GET /api/v1/functest/tasks/<task_id> # => GET the result of the task id - Url('/api/v1/functest/tasks/<task_id>', 'v1_tasks') + Url('/api/v1/functest/tasks/<task_id>', 'v1_task'), + + # GET /api/v1/functest/tasks/<task_id>/log + # => GET the log of the task + Url('/api/v1/functest/tasks/<task_id>/log', 'v1_task_log') ] |