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 yardstick.cmd import print_hbar
12 from yardstick.common.task_template import TaskTemplate
18 class TestcaseCommands(object):
21 Set of commands to discover and display test cases.
24 self.test_case_path = 'tests/opnfv/test_cases/'
25 self.testcase_list = []
27 def do_list(self, args):
28 '''List existing test cases'''
31 testcase_files = os.listdir(self.test_case_path)
32 except Exception as e:
33 print(("Failed to list dir:\n%(path)s\n%(err)s\n")
34 % {"path": self.test_case_path, "err": e})
38 for testcase_file in testcase_files:
39 record = self._get_record(testcase_file)
40 self.testcase_list.append(record)
42 self._format_print(self.testcase_list)
45 def _get_record(self, testcase_file):
48 with open(self.test_case_path + testcase_file) as f:
50 testcase_info = f.read()
51 except Exception as e:
52 print(("Failed to load test cases:"
53 "\n%(testcase_file)s\n%(err)s\n")
54 % {"testcase_file": testcase_file, "err": e})
56 description, installer, deploy_scenarios = \
57 self._parse_testcase(testcase_info)
59 record = {'Name': testcase_file.split(".")[0],
60 'Description': description,
61 'installer': installer,
62 'deploy_scenarios': deploy_scenarios}
64 except IOError as ioerror:
67 def _parse_testcase(self, testcase_info):
70 rendered_testcase = TaskTemplate.render(testcase_info, **kw)
71 testcase_cfg = yaml.load(rendered_testcase)
72 test_precondition = testcase_cfg.get('precondition', None)
73 installer_type = 'all'
74 deploy_scenarios = 'all'
75 if test_precondition is not None:
76 installer_type = test_precondition.get('installer_type', 'all')
77 deploy_scenarios = test_precondition.get('deploy_scenarios', 'all')
79 description = testcase_info.split("\n")[2][1:].strip()
80 return description, installer_type, deploy_scenarios
82 def _format_print(self, testcase_list):
86 print("| %-21s | %-60s" % ("Testcase Name", "Description"))
88 for testcase_record in testcase_list:
89 print "| %-16s | %-60s" % (testcase_record['Name'],
90 testcase_record['Description'])