diff options
author | Linda Wang <wangwulin@huawei.com> | 2017-08-18 06:09:56 +0000 |
---|---|---|
committer | Linda Wang <wangwulin@huawei.com> | 2017-08-21 02:47:42 +0000 |
commit | 26efacba9304071956d16a9a50a18e8f243518bb (patch) | |
tree | 38c8cdb8c363f1f9592be904b6e805567c3be66d /functest/api/common | |
parent | 58665932c2c910cedac2a135e5b84a5ec4b5946e (diff) |
Create API to run a test case
Two APIs are created here:
1. Run a test case
2. Get the result of the task id
JIRA: FUNCTEST-853
Change-Id: I12950933b143b82fb6aeb186ea1b35ddd16e6097
Signed-off-by: Linda Wang <wangwulin@huawei.com>
Diffstat (limited to 'functest/api/common')
-rw-r--r-- | functest/api/common/api_utils.py | 10 | ||||
-rw-r--r-- | functest/api/common/error.py | 24 | ||||
-rw-r--r-- | functest/api/common/thread.py | 52 |
3 files changed, 62 insertions, 24 deletions
diff --git a/functest/api/common/api_utils.py b/functest/api/common/api_utils.py index f518e777..d85acf92 100644 --- a/functest/api/common/api_utils.py +++ b/functest/api/common/api_utils.py @@ -18,6 +18,7 @@ import os import sys from oslo_utils import importutils +from flask import jsonify import six import functest @@ -89,3 +90,12 @@ def change_obj_to_dict(obj): for key, value in vars(obj).items(): dic.update({key: value}) return dic + + +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/common/error.py b/functest/api/common/error.py deleted file mode 100644 index d0045225..00000000 --- a/functest/api/common/error.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/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/common/thread.py b/functest/api/common/thread.py new file mode 100644 index 00000000..fb60aaac --- /dev/null +++ b/functest/api/common/thread.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 + +""" +Used to handle multi-thread tasks +""" + +import logging +import threading + +from oslo_serialization import jsonutils + + +LOGGER = logging.getLogger(__name__) + + +class TaskThread(threading.Thread): + """ Task Thread Class """ + + def __init__(self, target, args, handler): + super(TaskThread, self).__init__(target=target, args=args) + self.target = target + self.args = args + self.handler = handler + + def run(self): + """ Override the function run: run testcase and update database """ + update_data = {'task_id': self.args.get('task_id'), + 'status': 'IN PROGRESS'} + self.handler.insert(update_data) + + LOGGER.info('Starting running test case') + + try: + data = self.target(self.args) + except Exception as err: # pylint: disable=broad-except + LOGGER.exception('Task Failed') + update_data = {'status': 'FAIL', 'error': str(err)} + self.handler.update_attr(self.args.get('task_id'), update_data) + else: + LOGGER.info('Task Finished') + LOGGER.debug('Result: %s', data) + new_data = {'status': 'FINISHED', + 'result': jsonutils.dumps(data.get('result', {}))} + + self.handler.update_attr(self.args.get('task_id'), new_data) |