Merge "Open storperf testcase to huawei-pod2"
[yardstick.git] / api / resources / v2 / testcases.py
1 import logging
2 import errno
3 import os
4
5 from api import ApiResource
6 from yardstick.common.utils import result_handler
7 from yardstick.common import constants as consts
8 from yardstick.benchmark.core import Param
9 from yardstick.benchmark.core.testcase import Testcase
10
11 LOG = logging.getLogger(__name__)
12 LOG.setLevel(logging.DEBUG)
13
14
15 class V2Testcases(ApiResource):
16
17     def get(self):
18         param = Param({})
19         testcase_list = Testcase().list_all(param)
20         return result_handler(consts.API_SUCCESS, testcase_list)
21
22     def post(self):
23         return self._dispatch_post()
24
25     def upload_case(self, args):
26         try:
27             upload_file = args['file']
28         except KeyError:
29             return result_handler(consts.API_ERROR, 'file must be provided')
30
31         case_name = os.path.join(consts.TESTCASE_DIR, upload_file.filename)
32
33         LOG.info('save case file')
34         upload_file.save(case_name)
35
36         return result_handler(consts.API_SUCCESS, {'testcase': upload_file.filename})
37
38
39 class V2Testcase(ApiResource):
40
41     def get(self, case_name):
42         case_path = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(case_name))
43
44         try:
45             with open(case_path) as f:
46                 data = f.read()
47         except IOError as e:
48             if e.errno == errno.ENOENT:
49                 return result_handler(consts.API_ERROR, 'case does not exist')
50
51         return result_handler(consts.API_SUCCESS, {'testcase': data})
52
53     def delete(self, case_name):
54         case_path = os.path.join(consts.TESTCASE_DIR, '{}.yaml'.format(case_name))
55
56         try:
57             os.remove(case_path)
58         except IOError as e:
59             if e.errno == errno.ENOENT:
60                 return result_handler(consts.API_ERROR, 'case does not exist')
61
62         return result_handler(consts.API_SUCCESS, {'testcase': case_name})