Update release note for Colorado 2.0
[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 from yardstick.common.utils import cliargs
14 import os
15 import yaml
16 import sys
17
18
19 class TestcaseCommands(object):
20     '''Testcase commands.
21
22        Set of commands to discover and display test cases.
23     '''
24     def __init__(self):
25         self.test_case_path = 'tests/opnfv/test_cases/'
26         self.testcase_list = []
27
28     def do_list(self, args):
29         '''List existing test cases'''
30
31         try:
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})
36             raise e
37         testcase_files.sort()
38
39         for testcase_file in testcase_files:
40             record = self._get_record(testcase_file)
41             self.testcase_list.append(record)
42
43         self._format_print(self.testcase_list)
44         return True
45
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"
51         try:
52             with open(testcase_path) as f:
53                 try:
54                     testcase_info = f.read()
55                     print testcase_info
56
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})
61                     raise e
62         except IOError as ioerror:
63             sys.exit(ioerror)
64         return True
65
66     def _get_record(self, testcase_file):
67
68         try:
69             with open(self.test_case_path + testcase_file) as f:
70                 try:
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})
76                     raise e
77                 description, installer, deploy_scenarios = \
78                     self._parse_testcase(testcase_info)
79
80                 record = {'Name': testcase_file.split(".")[0],
81                           'Description': description,
82                           'installer': installer,
83                           'deploy_scenarios': deploy_scenarios}
84                 return record
85         except IOError as ioerror:
86             sys.exit(ioerror)
87
88     def _parse_testcase(self, testcase_info):
89
90         kw = {}
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')
99
100         description = testcase_info.split("\n")[2][1:].strip()
101         return description, installer_type, deploy_scenarios
102
103     def _format_print(self, testcase_list):
104         '''format output'''
105
106         print_hbar(88)
107         print("| %-21s | %-60s" % ("Testcase Name", "Description"))
108         print_hbar(88)
109         for testcase_record in testcase_list:
110             print "| %-16s | %-60s" % (testcase_record['Name'],
111                                        testcase_record['Description'])
112         print_hbar(88)