Create API and command to create a influxDB container
[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 import os
12 import yaml
13 import sys
14
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
19
20
21 class TestcaseCommands(object):
22     '''Testcase commands.
23
24        Set of commands to discover and display test cases.
25     '''
26     def __init__(self):
27         self.test_case_path = YARDSTICK_ROOT_PATH + 'tests/opnfv/test_cases/'
28         self.testcase_list = []
29
30     def do_list(self, args):
31         '''List existing test cases'''
32
33         try:
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})
38             raise e
39         testcase_files.sort()
40
41         for testcase_file in testcase_files:
42             record = self._get_record(testcase_file)
43             self.testcase_list.append(record)
44
45         self._format_print(self.testcase_list)
46         return True
47
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"
53         try:
54             with open(testcase_path) as f:
55                 try:
56                     testcase_info = f.read()
57                     print testcase_info
58
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})
63                     raise e
64         except IOError as ioerror:
65             sys.exit(ioerror)
66         return True
67
68     def _get_record(self, testcase_file):
69
70         try:
71             with open(self.test_case_path + testcase_file) as f:
72                 try:
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})
78                     raise e
79                 description, installer, deploy_scenarios = \
80                     self._parse_testcase(testcase_info)
81
82                 record = {'Name': testcase_file.split(".")[0],
83                           'Description': description,
84                           'installer': installer,
85                           'deploy_scenarios': deploy_scenarios}
86                 return record
87         except IOError as ioerror:
88             sys.exit(ioerror)
89
90     def _parse_testcase(self, testcase_info):
91
92         kw = {}
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')
101
102         description = testcase_info.split("\n")[2][1:].strip()
103         return description, installer_type, deploy_scenarios
104
105     def _format_print(self, testcase_list):
106         '''format output'''
107
108         print_hbar(88)
109         print("| %-21s | %-60s" % ("Testcase Name", "Description"))
110         print_hbar(88)
111         for testcase_record in testcase_list:
112             print "| %-16s | %-60s" % (testcase_record['Name'],
113                                        testcase_record['Description'])
114         print_hbar(88)