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.benchmark.core import print_hbar
18 from yardstick.common.task_template import TaskTemplate
19 from yardstick.definitions import YARDSTICK_ROOT_PATH
22 class Testcase(object):
25 Set of commands to discover and display test cases.
29 self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/'
30 self.testcase_list = []
32 def list_all(self, args):
33 """List existing test cases"""
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})
43 for testcase_file in testcase_files:
44 record = self._get_record(testcase_file)
45 self.testcase_list.append(record)
47 self._format_print(self.testcase_list)
51 """Show details of a specific test case"""
52 testcase_name = args.casename[0]
53 testcase_path = self.test_case_path + testcase_name + ".yaml"
55 with open(testcase_path) as f:
57 testcase_info = f.read()
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})
65 except IOError as ioerror:
69 def _get_record(self, testcase_file):
72 with open(self.test_case_path + testcase_file) as f:
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})
80 description, installer, deploy_scenarios = \
81 self._parse_testcase(testcase_info)
83 record = {'Name': testcase_file.split(".")[0],
84 'Description': description,
85 'installer': installer,
86 'deploy_scenarios': deploy_scenarios}
88 except IOError as ioerror:
91 def _parse_testcase(self, testcase_info):
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')
103 description = testcase_info.split("\n")[2][1:].strip()
104 return description, installer_type, deploy_scenarios
106 def _format_print(self, testcase_list):
110 print("| %-21s | %-60s" % ("Testcase Name", "Description"))
112 for testcase_record in testcase_list:
113 print("| %-16s | %-60s" % (testcase_record['Name'],
114 testcase_record['Description']))