Add API(v2) to get all test suite
[yardstick.git] / api / resources / v2 / testsuites.py
1 import os
2 import logging
3
4 import yaml
5
6 from api import ApiResource
7 from yardstick.common.utils import result_handler
8 from yardstick.common import constants as consts
9 from yardstick.benchmark.core.testsuite import Testsuite
10 from yardstick.benchmark.core import Param
11
12 LOG = logging.getLogger(__name__)
13 LOG.setLevel(logging.DEBUG)
14
15
16 class V2Testsuites(ApiResource):
17
18     def get(self):
19         param = Param({})
20         testsuite_list = Testsuite().list_all(param)
21
22         data = {
23             'testsuites': testsuite_list
24         }
25
26         return result_handler(consts.API_SUCCESS, data)
27
28     def post(self):
29         return self._dispatch_post()
30
31     def create_suite(self, args):
32         try:
33             suite_name = args['name']
34         except KeyError:
35             return result_handler(consts.API_ERROR, 'name must be provided')
36
37         try:
38             testcases = args['testcases']
39         except KeyError:
40             return result_handler(consts.API_ERROR, 'testcases must be provided')
41
42         testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]
43
44         suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
45         suite_content = {
46             'schema': 'yardstick:suite:0.1',
47             'name': suite_name,
48             'test_cases_dir': 'tests/opnfv/test_cases/',
49             'test_cases': testcases
50         }
51
52         LOG.info('write test suite')
53         with open(suite, 'w') as f:
54             yaml.dump(suite_content, f, default_flow_style=False)
55
56         return result_handler(consts.API_SUCCESS, {'suite': suite_name})