d292ad2d74227b64e72f9fc8e2e114e33ed8ae48
[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 import os
12 import yaml
13 import sys
14
15 from yardstick.benchmark.core import print_hbar
16 from yardstick.common.task_template import TaskTemplate
17 from yardstick.definitions import YARDSTICK_ROOT_PATH
18
19
20 class Testcase(object):
21     '''Testcase commands.
22
23        Set of commands to discover and display test cases.
24     '''
25     def __init__(self):
26         self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/'
27         self.testcase_list = []
28
29     def list_all(self, args):
30         '''List existing test cases'''
31
32         try:
33             testcase_files = os.listdir(self.test_case_path)
34         except Exception as e:
35             print(("Failed to list dir:\n%(path)s\n%(err)s\n")
36                   % {"path": self.test_case_path, "err": e})
37             raise e
38         testcase_files.sort()
39
40         for testcase_file in testcase_files:
41             record = self._get_record(testcase_file)
42             self.testcase_list.append(record)
43
44         self._format_print(self.testcase_list)
45         return True
46
47     def show(self, args):
48         '''Show details of a specific test case'''
49         testcase_name = args.casename[0]
50         testcase_path = self.test_case_path + testcase_name + ".yaml"
51         try:
52             with open(testcase_path) as f:
53                 try:
54                     testcase_info = f.read()
55                     print testcase_info
56
57                 except Exception as e:
58                     print(("Failed to load test cases:"
59                            "\n%(testcase_file)s\n%(err)s\n")
60                           % {"testcase_file": testcase_path, "err": e})
61                     raise e
62         except IOError as ioerror:
63             sys.exit(ioerror)
64         return True
65
66     def _get_record(self, testcase_file):
67
68         try:
69             with open(self.test_case_path + testcase_file) as f:
70                 try:
71                     testcase_info = f.read()
72                 except Exception as e:
73                     print(("Failed to load test cases:"
74                            "\n%(testcase_file)s\n%(err)s\n")
75                           % {"testcase_file": testcase_file, "err": e})
76                     raise e
77                 description, installer, deploy_scenarios = \
78                     self._parse_testcase(testcase_info)
79
80                 record = {'Name': testcase_file.split(".")[0],
81                           'Description': description,
82                           'installer': installer,
83                           'deploy_scenarios': deploy_scenarios}
84                 return record
85         except IOError as ioerror:
86             sys.exit(ioerror)
87
88     def _parse_testcase(self, testcase_info):
89
90         kw = {}
91         rendered_testcase = TaskTemplate.render(testcase_info, **kw)
92         testcase_cfg = yaml.load(rendered_testcase)
93         test_precondition = testcase_cfg.get('precondition', None)
94         installer_type = 'all'
95         deploy_scenarios = 'all'
96         if test_precondition is not None:
97             installer_type = test_precondition.get('installer_type', 'all')
98             deploy_scenarios = test_precondition.get('deploy_scenarios', 'all')
99
100         description = testcase_info.split("\n")[2][1:].strip()
101         return description, installer_type, deploy_scenarios
102
103     def _format_print(self, testcase_list):
104         '''format output'''
105
106         print_hbar(88)
107         print("| %-21s | %-60s" % ("Testcase Name", "Description"))
108         print_hbar(88)
109         for testcase_record in testcase_list:
110             print "| %-16s | %-60s" % (testcase_record['Name'],
111                                        testcase_record['Description'])
112         print_hbar(88)