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
13 from yardstick.common.utils import cliargs
19 class TestcaseCommands(object):
22 Set of commands to discover and display test cases.
25 self.test_case_path = 'tests/opnfv/test_cases/'
26 self.testcase_list = []
28 def do_list(self, args):
29 '''List existing test cases'''
32 testcase_files = os.listdir(self.test_case_path)
33 except Exception as e:
34 print(("Failed to list dir:\n%(path)s\n%(err)s\n")
35 % {"path": self.test_case_path, "err": e})
39 for testcase_file in testcase_files:
40 record = self._get_record(testcase_file)
41 self.testcase_list.append(record)
43 self._format_print(self.testcase_list)
46 @cliargs("casename", type=str, help="test case name", nargs=1)
47 def do_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"
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'])