blob: 2578da8eb302f2528add131985c080f0f383e474 [file] [log] [blame]
manasarm5dedfaf2018-02-07 14:54:54 +05301#!/usr/bin/env python
2
George Keishing9fbdf792016-10-18 06:16:09 -05003r"""
manasarm5dedfaf2018-02-07 14:54:54 +05304Use robot framework API to extract test result data from output.xml generated
5by robot tests. For more information on the Robot Framework API, see
6http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html
George Keishing9fbdf792016-10-18 06:16:09 -05007"""
manasarm5dedfaf2018-02-07 14:54:54 +05308
9import sys, os
George Keishing9fbdf792016-10-18 06:16:09 -050010import getopt
11import csv
George Keishing9fbdf792016-10-18 06:16:09 -050012import robot.errors
manasarm5dedfaf2018-02-07 14:54:54 +053013import re
14from datetime import datetime
George Keishing9fbdf792016-10-18 06:16:09 -050015from robot.api import ExecutionResult
16from robot.result.visitor import ResultVisitor
George Keishing74777bd2017-02-07 01:43:38 -060017from xml.etree import ElementTree
manasarm5dedfaf2018-02-07 14:54:54 +053018
19# Remove the python library path to restore with local project path later.
20save_path_0 = sys.path[0]
21del sys.path[0]
22sys.path.append(os.path.join(os.path.dirname(__file__), "../../../lib"))
23
24from gen_arg import *
25from gen_print import *
26from gen_valid import *
27
28# Restore sys.path[0].
29sys.path.insert(0, save_path_0)
30
31parser = argparse.ArgumentParser(
32 usage='%(prog)s [OPTIONS]',
33 description="%(prog)s uses a robot framework API to extract test result\
34 data from output.xml generated by robot tests. For more information on the\
35 Robot Framework API, see\
36 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html and\
37 https://gerrit.openbmc-project.xyz/#/c/8885/15/tools/oem/ibm/\
38 gen_csv_results.py",
39 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
40 prefix_chars='-+')
41
42parser.add_argument(
43 '--source',
44 '-s',
45 help='The output.xml robot test result file path.')
46
47parser.add_argument(
48 '--dest',
49 '-d',
50 help='The directory path where the generated .csv files will go.')
51
manasarme4f79c92018-02-22 13:02:46 +053052parser.add_argument(
53 '--version_id',
54 help='Driver version of openbmc firmware which was used during test,\
55 e.g. "v2.1-215-g6e7eacb".')
56
57parser.add_argument(
58 '--platform',
59 help='Openbmc platform which was used during test,\
60 e.g. "Witherspoon".')
61
manasarm5dedfaf2018-02-07 14:54:54 +053062# Populate stock_list with options we want.
63stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
64
65
66def exit_function(signal_number=0,
67 frame=None):
68
69 r"""
70 Execute whenever the program ends normally or with the signals that we
71 catch (i.e. TERM, INT).
72 """
73
74 dprint_executing()
75
76 dprint_var(signal_number)
77
78 qprint_pgm_footer()
79
80
81def signal_handler(signal_number,
82 frame):
83
84 r"""
85 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
86 program would terminate immediately with return code 143 and without
87 calling the exit_function.
88 """
89
90 # Our convention is to set up exit_function with atexit.register() so
91 # there is no need to explicitly call exit_function from here.
92
93 dprint_executing()
94
95 # Calling exit prevents us from returning to the code that was running
96 # when the signal was received.
97 exit(0)
98
99
100def validate_parms():
101
102 r"""
103 Validate program parameters, etc. Return True or False (i.e. pass/fail)
104 accordingly.
105 """
106
107 if not valid_file_path(source):
108 return False
109
110 if not valid_dir_path(dest):
111 return False
112
113 gen_post_validation(exit_function, signal_handler)
114
115 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500116
117
manasarme4f79c92018-02-22 13:02:46 +0530118def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform):
manasarm5dedfaf2018-02-07 14:54:54 +0530119
120 r"""
121 Parse the robot-generated output.xml file and extract various test
122 output data. Put the extracted information into a csv file in the "dest"
123 folder.
124
125 Description of argument(s):
manasarme4f79c92018-02-22 13:02:46 +0530126 xml_file_path The path to a Robot-generated output.xml file.
127 csv_dir_path The path to the directory that is to contain the .csv files
128 generated by this function.
129 version_id Version of the openbmc firmware
130 (e.g. "v2.1-215-g6e7eacb").
131 platform Platform of the openbmc system.
manasarm5dedfaf2018-02-07 14:54:54 +0530132 """
133
George Keishing74777bd2017-02-07 01:43:38 -0600134 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500135 result.configure(stat_config={'suite_stat_level': 2,
136 'tag_stat_combine': 'tagANDanother'})
137
138 stats = result.statistics
139 print "--------------------------------------"
140 print "Total Test Count:\t",\
141 stats.total.critical.passed + stats.total.critical.failed
142 print "Total Test Failed:\t", stats.total.critical.failed
143 print "Total Test Passed:\t", stats.total.critical.passed
144 print "Test Start Time:\t", result.suite.starttime
145 print "Test End Time:\t\t", result.suite.endtime
146 print "--------------------------------------"
147
148 # Use ResultVisitor object and save off the test data info
149 class TestResult(ResultVisitor):
150 def __init__(self):
151 self.testData = []
152
153 def visit_test(self, test):
154 self.testData += [test]
155
156 collectDataObj = TestResult()
157 result.visit(collectDataObj)
158
159 # Write the result statistics attributes to CSV file
160 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600161
162 # Default Test data
George Keishing9fbdf792016-10-18 06:16:09 -0500163 l_subsys = 'OPENBMC'
164 l_test_type = 'FTC'
165 l_pse_rel = 'OBMC910'
George Keishing74777bd2017-02-07 01:43:38 -0600166 l_env = 'HW'
167 l_proc = 'P9'
168 l_platform_type = ""
169 l_func_area = ""
170
manasarme4f79c92018-02-22 13:02:46 +0530171 ## System data from XML meta data
172 #l_system_info = get_system_details(xml_file_path)
173
174 # First let us try to collect information from keyboard input
175 # If keyboard input cannot give both information, then find from xml file.
176 if version_id and platform:
177 l_driver = version_id
178 l_platform_type = platform
179 print "BMC Version_id:", version_id
180 print "BMC Platform:", platform
George Keishing74777bd2017-02-07 01:43:38 -0600181 else:
manasarme4f79c92018-02-22 13:02:46 +0530182 # System data from XML meta data
183 l_system_info = get_system_details(xml_file_path)
184 l_driver = l_system_info[0]
185 l_platform_type = l_system_info[1]
186
187 # Driver version id and platform are mandatorily required for CSV file
188 # generation. If any one is not avaulable, exit CSV file generation process.
189 if l_driver and l_platform_type:
190 print "Driver and system info set."
191 else:
192 print "\
193 Both driver and system info need to be set. CSV file is not generated."
George Keishing74777bd2017-02-07 01:43:38 -0600194 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500195
196 # Default header
197 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600198 'test_result', 'test_name', 'pse_rel', 'driver',
199 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500200
201 l_csvlist.append(l_header)
202
203 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600204 l_base_dir = csv_dir_path
George Keishing9fbdf792016-10-18 06:16:09 -0500205 l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600206 # Example: 2017-02-20-08-47-22_Witherspoon.csv
207 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500208
209 print "Writing data into csv file:", l_csvfile
210
211 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600212 # Functional Area: Suite Name
213 # Test Name: Test Case Name
214 l_func_area = str(testcase.parent).split(' ', 1)[1]
215 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500216
217 # Test Result pass=0 fail=1
218 if testcase.status == 'PASS':
219 l_test_result = 0
220 else:
221 l_test_result = 1
222
George Keishing74777bd2017-02-07 01:43:38 -0600223 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
224 l_stime = xml_to_csv_time(testcase.starttime)
225 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500226 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600227 # test_result,test_name,pse_rel,driver,
228 # env,proc,platform_tyep,test_func_area,
229 l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
230 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
231 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500232 l_csvlist.append(l_data)
233
George Keishinga96e27c2016-12-04 23:05:04 -0600234 # Open the file and write to the CSV file
235 l_file = open(l_csvfile, "w")
236 l_writer = csv.writer(l_file, lineterminator='\n')
237 l_writer.writerows(l_csvlist)
238 l_file.close()
George Keishing9fbdf792016-10-18 06:16:09 -0500239
240
George Keishing74777bd2017-02-07 01:43:38 -0600241def xml_to_csv_time(xml_datetime):
manasarm5dedfaf2018-02-07 14:54:54 +0530242
George Keishing74777bd2017-02-07 01:43:38 -0600243 r"""
244 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
245 and return it.
246
manasarm5dedfaf2018-02-07 14:54:54 +0530247 Description of argument(s):
248 datetime The date in the following format: %Y%m%d %H:%M:%S.%f
249 (This is the format typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600250
251 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
252 """
manasarm5dedfaf2018-02-07 14:54:54 +0530253
George Keishing74777bd2017-02-07 01:43:38 -0600254 # 20170206 05:05:19.342
255 l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
256 # 2017-02-06-05-05-19
257 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
258 return str(l_str)
259
260
261def get_system_details(xml_file_path):
manasarm5dedfaf2018-02-07 14:54:54 +0530262
George Keishing74777bd2017-02-07 01:43:38 -0600263 r"""
264 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500265 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600266
manasarm5dedfaf2018-02-07 14:54:54 +0530267 Description of argument(s):
268 xml_file_path The relative or absolute path to the output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600269 """
manasarm5dedfaf2018-02-07 14:54:54 +0530270
manasarme4f79c92018-02-22 13:02:46 +0530271 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600272 bmc_platform = ""
273 with open(xml_file_path, 'rt') as output:
274 tree = ElementTree.parse(output)
275
276 for node in tree.iter('msg'):
277 # /etc/os-release output is logged in the XML as msg
278 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
279 if '${output} = VERSION_ID=' in node.text:
280 # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
manasarme4f79c92018-02-22 13:02:46 +0530281 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600282
283 # Platform is logged in the XML as msg.
284 # Example: ${bmc_model} = Witherspoon BMC
285 if '${bmc_model} = ' in node.text:
286 bmc_platform = node.text.split(" = ")[1]
287
manasarme4f79c92018-02-22 13:02:46 +0530288 print_vars(bmc_version_id, bmc_platform)
289 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600290
291
manasarm5dedfaf2018-02-07 14:54:54 +0530292def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500293
manasarm5dedfaf2018-02-07 14:54:54 +0530294 if not gen_get_options(parser, stock_list):
295 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500296
manasarm5dedfaf2018-02-07 14:54:54 +0530297 if not validate_parms():
298 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500299
manasarm5dedfaf2018-02-07 14:54:54 +0530300 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500301
manasarme4f79c92018-02-22 13:02:46 +0530302 parse_output_xml(source, dest, version_id, platform)
George Keishing9fbdf792016-10-18 06:16:09 -0500303
manasarm5dedfaf2018-02-07 14:54:54 +0530304 return True
305
306
307# Main
308
309if not main():
310 exit(1)