Merge "Open storperf testcase to huawei-pod2"
[yardstick.git] / yardstick / benchmark / core / testcase.py
1 ##############################################################################
2 # Copyright (c) 2015 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 """ Handler for yardstick command 'testcase' """
11 from __future__ import absolute_import
12 from __future__ import print_function
13
14 import os
15 import yaml
16 import logging
17
18 from yardstick.common.task_template import TaskTemplate
19 from yardstick.common import constants as consts
20
21 LOG = logging.getLogger(__name__)
22
23
24 class Testcase(object):
25     """Testcase commands.
26
27        Set of commands to discover and display test cases.
28     """
29
30     def list_all(self, args):
31         """List existing test cases"""
32
33         testcase_files = self._get_testcase_file_list()
34         testcase_list = [self._get_record(f) for f in testcase_files]
35
36         return testcase_list
37
38     def _get_testcase_file_list(self):
39         try:
40             testcase_files = sorted(os.listdir(consts.TESTCASE_DIR))
41         except OSError:
42             LOG.exception('Failed to list dir:\n%s\n', consts.TESTCASE_DIR)
43             raise
44
45         return testcase_files
46
47     def _get_record(self, testcase_file):
48
49         file_path = os.path.join(consts.TESTCASE_DIR, testcase_file)
50         with open(file_path) as f:
51             try:
52                 testcase_info = f.read()
53             except IOError:
54                 LOG.exception('Failed to load test case:\n%s\n', testcase_file)
55                 raise
56
57         description, installer, deploy_scenarios = self._parse_testcase(
58             testcase_info)
59
60         record = {
61             'Name': testcase_file.split(".")[0],
62             'Description': description,
63             'installer': installer,
64             'deploy_scenarios': deploy_scenarios
65         }
66
67         return record
68
69     def _parse_testcase(self, testcase_info):
70
71         rendered_testcase = TaskTemplate.render(testcase_info)
72         testcase_cfg = yaml.load(rendered_testcase)
73
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')
77
78         description = self._get_description(testcase_cfg)
79
80         return description, installer_type, deploy_scenarios
81
82     def _get_description(self, testcase_cfg):
83         try:
84             description_list = testcase_cfg['description'].split(';')
85         except KeyError:
86             return ''
87         else:
88             try:
89                 return description_list[1].replace(os.linesep, '').strip()
90             except IndexError:
91                 return description_list[0].replace(os.linesep, '').strip()
92
93     def show(self, args):
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:
99             try:
100                 testcase_info = f.read()
101             except IOError:
102                 LOG.exception('Failed to load test case:\n%s\n', testcase_path)
103                 raise
104
105             print(testcase_info)
106         return True