Merge "Improve NSB Standalone XML generation"
[yardstick.git] / api / resources / v1 / testsuites.py
1 ##############################################################################
2 # Copyright (c) 2016 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 from __future__ import absolute_import
10 import uuid
11 import os
12 import logging
13
14 from flasgger.utils import swag_from
15
16 from api import ApiResource
17 from api.utils.thread import TaskThread
18 from yardstick.common import constants as consts
19 from yardstick.common.utils import result_handler
20 from yardstick.benchmark.core import Param
21 from yardstick.benchmark.core.task import Task
22 from api.swagger import models
23 from api.database.v1.handlers import TasksHandler
24
25 LOG = logging.getLogger(__name__)
26 LOG.setLevel(logging.DEBUG)
27
28
29 TestSuiteActionModel = models.TestSuiteActionModel
30 TestSuiteActionArgsModel = models.TestSuiteActionArgsModel
31 TestSuiteActionArgsOptsModel = models.TestSuiteActionArgsOptsModel
32 TestSuiteActionArgsOptsTaskArgModel = \
33     models.TestSuiteActionArgsOptsTaskArgModel
34
35
36 class V1Testsuite(ApiResource):
37
38     @swag_from(os.path.join(consts.REPOS_DIR,
39                             'api/swagger/docs/testsuites_action.yaml'))
40     def post(self):
41         return self._dispatch_post()
42
43     def run_test_suite(self, args):
44         try:
45             name = args['testsuite']
46         except KeyError:
47             return result_handler(consts.API_ERROR,
48                                   'testsuite must be provided')
49
50         testsuite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(name))
51
52         task_id = str(uuid.uuid4())
53
54         task_args = {
55             'inputfile': [testsuite],
56             'task_id': task_id,
57             'suite': True
58         }
59         task_args.update(args.get('opts', {}))
60
61         param = Param(task_args)
62         task_thread = TaskThread(Task().start, param, TasksHandler())
63         task_thread.start()
64
65         return result_handler(consts.API_SUCCESS, {'task_id': task_id})