Merge "add "yardstick testcase list" command function"
[yardstick.git] / yardstick / cmd / commands / testcase.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 """ Handler for yardstick command 'testcase' """
11 from yardstick.cmd import print_hbar
12 from yardstick.common.task_template import TaskTemplate
13 import os
14 import yaml
15 import sys
16
17
18 class TestcaseCommands(object):
19     '''Testcase commands.
20
21        Set of commands to discover and display test cases.
22     '''
23     def __init__(self):
24         self.test_case_path = 'tests/opnfv/test_cases/'
25         self.testcase_list = []
26
27     def do_list(self, args):
28         '''List existing test cases'''
29
30         try:
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})
35             raise e
36         testcase_files.sort()
37
38         for testcase_file in testcase_files:
39             record = self._get_record(testcase_file)
40             self.testcase_list.append(record)
41
42         self._format_print(self.testcase_list)
43         return True
44
45     def _get_record(self, testcase_file):
46
47         try:
48             with open(self.test_case_path + testcase_file) as f:
49                 try:
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})
55                     raise e
56                 description, installer, deploy_scenarios = \
57                     self._parse_testcase(testcase_info)
58
59                 record = {'Name': testcase_file.split(".")[0],
60                           'Description': description,
61                           'installer': installer,
62                           'deploy_scenarios': deploy_scenarios}
63                 return record
64         except IOError as ioerror:
65             sys.exit(ioerror)
66
67     def _parse_testcase(self, testcase_info):
68
69         kw = {}
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')
78
79         description = testcase_info.split("\n")[2][1:].strip()
80         return description, installer_type, deploy_scenarios
81
82     def _format_print(self, testcase_list):
83         '''format output'''
84
85         print_hbar(88)
86         print("| %-21s | %-60s" % ("Testcase Name", "Description"))
87         print_hbar(88)
88         for testcase_record in testcase_list:
89             print "| %-16s | %-60s" % (testcase_record['Name'],
90                                        testcase_record['Description'])
91         print_hbar(88)