summaryrefslogtreecommitdiffstats
path: root/api/resources/v2/testsuites.py
blob: 56ad473759603bc4fc5a9531ed3114629e0065bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os
import errno
import logging

import yaml

from api import ApiResource
from yardstick.common.utils import result_handler
from yardstick.common import constants as consts
from yardstick.benchmark.core.testsuite import Testsuite
from yardstick.benchmark.core import Param

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


class V2Testsuites(ApiResource):

    def get(self):
        param = Param({})
        testsuite_list = Testsuite().list_all(param)

        data = {
            'testsuites': testsuite_list
        }

        return result_handler(consts.API_SUCCESS, data)

    def post(self):
        return self._dispatch_post()

    def create_suite(self, args):
        try:
            suite_name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        try:
            testcases = args['testcases']
        except KeyError:
            return result_handler(consts.API_ERROR, 'testcases must be provided')

        testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]

        suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        suite_content = {
            'schema': 'yardstick:suite:0.1',
            'name': suite_name,
            'test_cases_dir': 'tests/opnfv/test_cases/',
            'test_cases': testcases
        }

        LOG.info('write test suite')
        with open(suite, 'w') as f:
            yaml.dump(suite_content, f, default_flow_style=False)

        return result_handler(consts.API_SUCCESS, {'suite': suite_name})


class V2Testsuite(ApiResource):

    def get(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        try:
            with open(suite_path) as f:
                data = f.read()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': data})

    def delete(self, suite_name):
        suite_path = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
        try:
            os.remove(suite_path)
        except IOError as e:
            if e.errno == errno.ENOENT:
                return result_handler(consts.API_ERROR, 'suite does not exist')

        return result_handler(consts.API_SUCCESS, {'testsuite': suite_name})