Merge "Bugfix: don't use jsonutils.load, use loads()"
[yardstick.git] / api / resources / testsuites_action.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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
10 """Yardstick test suite api action"""
11
12 from __future__ import absolute_import
13 import uuid
14 import os
15 import logging
16 import yaml
17
18 from api.utils import common as common_utils
19 from yardstick.common import constants as consts
20 from yardstick.common.task_template import TaskTemplate
21
22 logger = logging.getLogger(__name__)
23
24
25 def runTestSuite(args):
26     try:
27         opts = args.get('opts', {})
28         testsuite = args['testsuite']
29     except KeyError:
30         return common_utils.error_handler('Lack of testsuite argument')
31
32     if 'suite' not in opts:
33         opts['suite'] = 'true'
34
35     testsuite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(testsuite))
36
37     task_id = str(uuid.uuid4())
38
39     command_list = ['task', 'start']
40     command_list = common_utils.get_command_list(command_list, opts, testsuite)
41     logger.debug('The command_list is: %s', command_list)
42
43     logger.debug('Start to execute command list')
44     task_dic = {
45         'task_id': task_id,
46         'details': _get_cases_from_suite_file(testsuite)
47     }
48     common_utils.exec_command_task(command_list, task_dic)
49
50     return common_utils.result_handler('success', task_id)
51
52
53 def _get_cases_from_suite_file(testsuite):
54     def get_name(full_name):
55         return os.path.splitext(full_name)[0]
56
57     with open(testsuite) as f:
58         contents = TaskTemplate.render(f.read())
59
60     suite_dic = yaml.safe_load(contents)
61     testcases = (get_name(c['file_name']) for c in suite_dic['test_cases'])
62     return ','.join(testcases)