Add API(v2) to create 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
10 LOG = logging.getLogger(__name__)
11 LOG.setLevel(logging.DEBUG)
12
13
14 class V2Testsuites(ApiResource):
15
16     def post(self):
17         return self._dispatch_post()
18
19     def create_suite(self, args):
20         try:
21             suite_name = args['name']
22         except KeyError:
23             return result_handler(consts.API_ERROR, 'name must be provided')
24
25         try:
26             testcases = args['testcases']
27         except KeyError:
28             return result_handler(consts.API_ERROR, 'testcases must be provided')
29
30         testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]
31
32         suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
33         suite_content = {
34             'schema': 'yardstick:suite:0.1',
35             'name': suite_name,
36             'test_cases_dir': 'tests/opnfv/test_cases/',
37             'test_cases': testcases
38         }
39
40         LOG.info('write test suite')
41         with open(suite, 'w') as f:
42             yaml.dump(suite_content, f, default_flow_style=False)
43
44         return result_handler(consts.API_SUCCESS, {'suite': suite_name})