Merge "Put vnf_test pass if more than 80% of vnf test result are OK"
[functest.git] / functest / api / resources / v1 / tasks.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
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
9
10 """
11 Resources to retrieve the task results
12 """
13
14 import errno
15 import json
16 import logging
17 import os
18 import uuid
19
20 from flask import jsonify
21
22 from functest.api.base import ApiResource
23 from functest.api.common import api_utils
24 from functest.api.database.v1.handlers import TasksHandler
25 from functest.utils.constants import CONST
26
27
28 LOGGER = logging.getLogger(__name__)
29
30
31 class V1Task(ApiResource):
32     """ V1Task Resource class"""
33
34     def get(self, task_id):  # pylint: disable=no-self-use
35         """ GET the result of the task id """
36         try:
37             uuid.UUID(task_id)
38         except ValueError:
39             return api_utils.result_handler(status=1, data='Invalid task id')
40
41         task_handler = TasksHandler()
42         try:
43             task = task_handler.get_task_by_taskid(task_id)
44         except ValueError:
45             return api_utils.result_handler(status=1, data='No such task id')
46
47         status = task.status
48         LOGGER.debug('Task status is: %s', status)
49
50         if status not in ['IN PROGRESS', 'FAIL', 'FINISHED']:
51             return api_utils.result_handler(status=1,
52                                             data='internal server error')
53         if status == 'IN PROGRESS':
54             result = {'status': status, 'result': ''}
55         elif status == 'FAIL':
56             result = {'status': status, 'error': task.error}
57         else:
58             result = {'status': status, 'result': json.loads(task.result)}
59
60         return jsonify(result)
61
62
63 class V1TaskLog(ApiResource):
64     """ V1TaskLog Resource class"""
65
66     def get(self, task_id):  # pylint: disable=no-self-use
67         """ GET the log of the task id """
68         try:
69             uuid.UUID(task_id)
70         except ValueError:
71             return api_utils.result_handler(status=1, data='Invalid task id')
72
73         task_handler = TasksHandler()
74         try:
75             task = task_handler.get_task_by_taskid(task_id)
76         except ValueError:
77             return api_utils.result_handler(status=1, data='No such task id')
78
79         task_log_dir = CONST.__getattribute__('dir_results')
80
81         try:
82             with open(os.path.join(task_log_dir,
83                                    '{}.log'.format(task_id)), 'r') as log_file:
84                 data = log_file.readlines()
85         except OSError as err:
86             if err.errno == errno.ENOENT:
87                 return api_utils.result_handler(
88                     status=1, data='Log file does not exist')
89
90             return api_utils.result_handler(
91                 status=1, data='Error with log file')
92
93         return_data = {'data': data}
94
95         return api_utils.result_handler(status=task.status, data=return_data)