Merge "Log each test case status in a task"
[yardstick.git] / api / resources / v2 / testsuites.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 import os
10 import errno
11 import logging
12
13 import yaml
14
15 from api import ApiResource
16 from yardstick.common.utils import result_handler
17 from yardstick.common import constants as consts
18 from yardstick.benchmark.core.testsuite import Testsuite
19 from yardstick.benchmark.core import Param
20
21 LOG = logging.getLogger(__name__)
22 LOG.setLevel(logging.DEBUG)
23
24
25 class V2Testsuites(ApiResource):
26
27     def get(self):
28         param = Param({})
29         testsuite_list = Testsuite().list_all(param)
30
31         data = {
32             'testsuites': testsuite_list
33         }
34
35         return result_handler(consts.API_SUCCESS, data)
36
37     def post(self):
38         return self._dispatch_post()
39
40     def create_suite(self, args):
41         try:
42             suite_name = args['name']
43         except KeyError:
44             return result_handler(consts.API_ERROR, 'name must be provided')
45
46         try:
47             testcases = args['testcases']
48         except KeyError:
49             return result_handler(consts.API_ERROR, 'testcases must be provided')
50
51         testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]
52
53         suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
54         suite_content = {
55             'schema': 'yardstick:suite:0.1',
56             'name': suite_name,
57             'test_cases_dir': 'tests/opnfv/test_cases/',
58             'test_cases': testcases
59         }
60
61         LOG.info('write test suite')
62         with open(suite, 'w') as f:
63             yaml.dump(suite_content, f, default_flow_style=False)
64
65         return result_handler(consts.API_SUCCESS, {'suite': suite_name})
66
67
68 class V2Testsuite(ApiResource):
69
70     def get(self, suite_name):
71         suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
72         try:
73             with open(suite_path) as f:
74                 data = f.read()
75         except IOError as e:
76             if e.errno == errno.ENOENT:
77                 return result_handler(consts.API_ERROR, 'suite does not exist')
78
79         return result_handler(consts.API_SUCCESS, {'testsuite': data})
80
81     def delete(self, suite_name):
82         suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
83         try:
84             os.remove(suite_path)
85         except IOError as e:
86             if e.errno == errno.ENOENT:
87                 return result_handler(consts.API_ERROR, 'suite does not exist')
88
89         return result_handler(consts.API_SUCCESS, {'testsuite': suite_name})