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' """
15 from yardstick.benchmark.core import print_hbar
16 from yardstick.common.task_template import TaskTemplate
17 from yardstick.definitions import YARDSTICK_ROOT_PATH
20 class Testcase(object):
23 Set of commands to discover and display test cases.
26 self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/'
27 self.testcase_list = []
29 def list_all(self, args):
30 '''List existing test cases'''
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})
40 for testcase_file in testcase_files:
41 record = self._get_record(testcase_file)
42 self.testcase_list.append(record)
44 self._format_print(self.testcase_list)
48 '''Show details of a specific test case'''
49 testcase_name = args.casename[0]
50 testcase_path = self.test_case_path + testcase_name + ".yaml"
52 with open(testcase_path) as f:
54 testcase_info = f.read()
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})
62 except IOError as ioerror:
66 def _get_record(self, testcase_file):
69 with open(self.test_case_path + testcase_file) as f:
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})
77 description, installer, deploy_scenarios = \
78 self._parse_testcase(testcase_info)
80 record = {'Name': testcase_file.split(".")[0],
81 'Description': description,
82 'installer': installer,
83 'deploy_scenarios': deploy_scenarios}
85 except IOError as ioerror:
88 def _parse_testcase(self, testcase_info):
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')
100 description = testcase_info.split("\n")[2][1:].strip()
101 return description, installer_type, deploy_scenarios
103 def _format_print(self, testcase_list):
107 print("| %-21s | %-60s" % ("Testcase Name", "Description"))
109 for testcase_record in testcase_list:
110 print "| %-16s | %-60s" % (testcase_record['Name'],
111 testcase_record['Description'])