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