Merge "Fix Scale-up issue in HWLB"
[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
24 LOG = logging.getLogger(__name__)
25 LOG.setLevel(logging.DEBUG)
26
27
28 TestSuiteActionModel = models.TestSuiteActionModel
29 TestSuiteActionArgsModel = models.TestSuiteActionArgsModel
30 TestSuiteActionArgsOptsModel = models.TestSuiteActionArgsOptsModel
31 TestSuiteActionArgsOptsTaskArgModel = \
32     models.TestSuiteActionArgsOptsTaskArgModel
33
34
35 class V1Testsuite(ApiResource):
36
37     @swag_from(os.path.join(consts.REPOS_DIR,
38                             'api/swagger/docs/testsuites_action.yaml'))
39     def post(self):
40         return self._dispatch_post()
41
42     def run_test_suite(self, args):
43         try:
44             name = args['testsuite']
45         except KeyError:
46             return result_handler(consts.API_ERROR,
47                                   'testsuite must be provided')
48
49         testsuite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(name))
50
51         task_id = str(uuid.uuid4())
52
53         task_args = {
54             'inputfile': [testsuite],
55             'task_id': task_id,
56             'suite': True
57         }
58         task_args.update(args.get('opts', {}))
59
60         param = Param(task_args)
61         task_thread = TaskThread(Task().start, param)
62         task_thread.start()
63
64         return result_handler(consts.API_SUCCESS, {'task_id': task_id})