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.cmd import print_hbar
16 from yardstick.common.task_template import TaskTemplate
17 from yardstick.common.utils import cliargs
18 from yardstick.definitions import YARDSTICK_ROOT_PATH
21 class TestcaseCommands(object):
24 Set of commands to discover and display test cases.
27 self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/'
28 self.testcase_list = []
30 def do_list(self, args):
31 '''List existing test cases'''
34 testcase_files = os.listdir(self.test_case_path)
35 except Exception as e:
36 print(("Failed to list dir:\n%(path)s\n%(err)s\n")
37 % {"path": self.test_case_path, "err": e})
41 for testcase_file in testcase_files:
42 record = self._get_record(testcase_file)
43 self.testcase_list.append(record)
45 self._format_print(self.testcase_list)
48 @cliargs("casename", type=str, help="test case name", nargs=1)
49 def do_show(self, args):
50 '''Show details of a specific test case'''
51 testcase_name = args.casename[0]
52 testcase_path = self.test_case_path + testcase_name + ".yaml"
54 with open(testcase_path) as f:
56 testcase_info = f.read()
59 except Exception as e:
60 print(("Failed to load test cases:"
61 "\n%(testcase_file)s\n%(err)s\n")
62 % {"testcase_file": testcase_path, "err": e})
64 except IOError as ioerror:
68 def _get_record(self, testcase_file):
71 with open(self.test_case_path + testcase_file) as f:
73 testcase_info = f.read()
74 except Exception as e:
75 print(("Failed to load test cases:"
76 "\n%(testcase_file)s\n%(err)s\n")
77 % {"testcase_file": testcase_file, "err": e})
79 description, installer, deploy_scenarios = \
80 self._parse_testcase(testcase_info)
82 record = {'Name': testcase_file.split(".")[0],
83 'Description': description,
84 'installer': installer,
85 'deploy_scenarios': deploy_scenarios}
87 except IOError as ioerror:
90 def _parse_testcase(self, testcase_info):
93 rendered_testcase = TaskTemplate.render(testcase_info, **kw)
94 testcase_cfg = yaml.load(rendered_testcase)
95 test_precondition = testcase_cfg.get('precondition', None)
96 installer_type = 'all'
97 deploy_scenarios = 'all'
98 if test_precondition is not None:
99 installer_type = test_precondition.get('installer_type', 'all')
100 deploy_scenarios = test_precondition.get('deploy_scenarios', 'all')
102 description = testcase_info.split("\n")[2][1:].strip()
103 return description, installer_type, deploy_scenarios
105 def _format_print(self, testcase_list):
109 print("| %-21s | %-60s" % ("Testcase Name", "Description"))
111 for testcase_record in testcase_list:
112 print "| %-16s | %-60s" % (testcase_record['Name'],
113 testcase_record['Description'])