Merge "Yardstick API architecture improvement"
[yardstick.git] / api / resources / v1 / testcases.py
1 # ############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # ############################################################################
9
10 from __future__ import absolute_import
11 import uuid
12 import os
13 import logging
14
15 from flasgger.utils import swag_from
16
17 from yardstick.benchmark.core.testcase import Testcase
18 from yardstick.benchmark.core.task import Task
19 from yardstick.benchmark.core import Param
20 from yardstick.common import constants as consts
21 from yardstick.common.utils import result_handler
22 from api.utils.thread import TaskThread
23 from api import ApiResource
24 from api.swagger import models
25
26 LOG = logging.getLogger(__name__)
27 LOG.setLevel(logging.DEBUG)
28
29
30 class V1Testcase(ApiResource):
31
32     def get(self):
33         param = Param({})
34         testcase_list = Testcase().list_all(param)
35         return result_handler(consts.API_SUCCESS, testcase_list)
36
37
38 class V1CaseDocs(ApiResource):
39
40     def get(self, case_name):
41         docs_path = os.path.join(consts.DOCS_DIR, '{}.rst'.format(case_name))
42
43         if not os.path.exists(docs_path):
44             return result_handler(consts.API_ERROR, 'case not exists')
45
46         LOG.info('Reading %s', case_name)
47         with open(docs_path) as f:
48             content = f.read()
49
50         return result_handler(consts.API_SUCCESS, {'docs': content})
51
52
53 TestCaseActionModel = models.TestCaseActionModel
54 TestCaseActionArgsModel = models.TestCaseActionArgsModel
55 TestCaseActionArgsOptsModel = models.TestCaseActionArgsOptsModel
56 TestCaseActionArgsOptsTaskArgModel = models.TestCaseActionArgsOptsTaskArgModel
57
58
59 class V1ReleaseCase(ApiResource):
60
61     @swag_from(os.path.join(consts.REPOS_DIR,
62                             'api/swagger/docs/release_action.yaml'))
63     def post(self):
64         return self._dispatch_post()
65
66     def run_test_case(self, args):
67         try:
68             name = args['testcase']
69         except KeyError:
70             return result_handler(consts.API_ERROR, 'testcase must be provided')
71
72         testcase = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(name))
73
74         task_id = str(uuid.uuid4())
75
76         task_args = {
77             'inputfile': [testcase],
78             'task_id': task_id
79         }
80         task_args.update(args.get('opts', {}))
81
82         param = Param(task_args)
83         task_thread = TaskThread(Task().start, param)
84         task_thread.start()
85
86         return result_handler(consts.API_SUCCESS, {'task_id': task_id})
87
88
89 class V1SampleCase(ApiResource):
90
91     def post(self):
92         return self._dispatch_post()
93
94     def run_test_case(self, args):
95         try:
96             name = args['testcase']
97         except KeyError:
98             return result_handler(consts.API_ERROR, 'testcase must be provided')
99
100         testcase = os.path.join(consts.SAMPLE_CASE_DIR, '{}.yaml'.format(name))
101
102         task_id = str(uuid.uuid4())
103
104         task_args = {
105             'inputfile': [testcase],
106             'task_id': task_id
107         }
108         task_args.update(args.get('opts', {}))
109
110         param = Param(task_args)
111         task_thread = TaskThread(Task().start, param)
112         task_thread.start()
113
114         return result_handler(consts.API_SUCCESS, {'task_id': task_id})