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