Merge "Fix adding right deb repo based on the distro we are running on"
[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 from api.database.v1.handlers import TasksHandler
26
27 LOG = logging.getLogger(__name__)
28 LOG.setLevel(logging.DEBUG)
29
30
31 class V1Testcase(ApiResource):
32
33     def get(self):
34         param = Param({})
35         testcase_list = Testcase().list_all(param)
36         return result_handler(consts.API_SUCCESS, testcase_list)
37
38
39 class V1CaseDocs(ApiResource):
40
41     def get(self, case_name):
42         docs_path = os.path.join(consts.DOCS_DIR, '{}.rst'.format(case_name))
43
44         if not os.path.exists(docs_path):
45             return result_handler(consts.API_ERROR, 'case not exists')
46
47         LOG.info('Reading %s', case_name)
48         with open(docs_path) as f:
49             content = f.read()
50
51         return result_handler(consts.API_SUCCESS, {'docs': content})
52
53
54 TestCaseActionModel = models.TestCaseActionModel
55 TestCaseActionArgsModel = models.TestCaseActionArgsModel
56 TestCaseActionArgsOptsModel = models.TestCaseActionArgsOptsModel
57 TestCaseActionArgsOptsTaskArgModel = models.TestCaseActionArgsOptsTaskArgModel
58
59
60 class V1ReleaseCase(ApiResource):
61
62     @swag_from(os.path.join(consts.REPOS_DIR,
63                             'api/swagger/docs/release_action.yaml'))
64     def post(self):
65         return self._dispatch_post()
66
67     def run_test_case(self, args):
68         try:
69             name = args['testcase']
70         except KeyError:
71             return result_handler(consts.API_ERROR, 'testcase must be provided')
72
73         testcase = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(name))
74
75         task_id = str(uuid.uuid4())
76
77         task_args = {
78             'inputfile': [testcase],
79             'task_id': task_id
80         }
81         task_args.update(args.get('opts', {}))
82
83         param = Param(task_args)
84         task_thread = TaskThread(Task().start, param, TasksHandler())
85         task_thread.start()
86
87         return result_handler(consts.API_SUCCESS, {'task_id': task_id})
88
89
90 class V1SampleCase(ApiResource):
91
92     def post(self):
93         return self._dispatch_post()
94
95     def run_test_case(self, args):
96         try:
97             name = args['testcase']
98         except KeyError:
99             return result_handler(consts.API_ERROR, 'testcase must be provided')
100
101         testcase = os.path.join(consts.SAMPLE_CASE_DIR, '{}.yaml'.format(name))
102
103         task_id = str(uuid.uuid4())
104
105         task_args = {
106             'inputfile': [testcase],
107             'task_id': task_id
108         }
109         task_args.update(args.get('opts', {}))
110
111         param = Param(task_args)
112         task_thread = TaskThread(Task().start, param, TasksHandler())
113         task_thread.start()
114
115         return result_handler(consts.API_SUCCESS, {'task_id': task_id})