1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 """ Handler for yardstick command 'testcase' """
11 from __future__ import absolute_import
12 from __future__ import print_function
17 from yardstick.common.task_template import TaskTemplate
18 from yardstick.common import constants as consts
19 from yardstick.common.yaml_loader import yaml_load
21 LOG = logging.getLogger(__name__)
24 class Testcase(object):
27 Set of commands to discover and display test cases.
30 def list_all(self, args):
31 """List existing test cases"""
33 testcase_files = self._get_testcase_file_list()
34 testcase_list = [self._get_record(f) for f in testcase_files]
38 def _get_testcase_file_list(self):
40 testcase_files = sorted(os.listdir(consts.TESTCASE_DIR))
42 LOG.exception('Failed to list dir:\n%s\n', consts.TESTCASE_DIR)
47 def _get_record(self, testcase_file):
49 file_path = os.path.join(consts.TESTCASE_DIR, testcase_file)
50 with open(file_path) as f:
52 testcase_info = f.read()
54 LOG.exception('Failed to load test case:\n%s\n', testcase_file)
57 description, installer, deploy_scenarios = self._parse_testcase(
61 'Name': testcase_file.split(".")[0],
62 'Description': description,
63 'installer': installer,
64 'deploy_scenarios': deploy_scenarios
69 def _parse_testcase(self, testcase_info):
71 rendered_testcase = TaskTemplate.render(testcase_info)
72 testcase_cfg = yaml_load(rendered_testcase)
74 test_precondition = testcase_cfg.get('precondition', {})
75 installer_type = test_precondition.get('installer_type', 'all')
76 deploy_scenarios = test_precondition.get('deploy_scenarios', 'all')
78 description = self._get_description(testcase_cfg)
80 return description, installer_type, deploy_scenarios
82 def _get_description(self, testcase_cfg):
84 description_list = testcase_cfg['description'].split(';')
89 return description_list[1].replace(os.linesep, '').strip()
91 return description_list[0].replace(os.linesep, '').strip()
94 """Show details of a specific test case"""
95 testcase_name = args.casename[0]
96 testcase_path = os.path.join(consts.TESTCASE_DIR,
97 testcase_name + ".yaml")
98 with open(testcase_path) as f:
100 testcase_info = f.read()
102 LOG.exception('Failed to load test case:\n%s\n', testcase_path)