blob: 1cbb1978fa15a6e37e499ca7d7e40e83fe95acac [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
Gunnar Mills096cd562018-03-26 10:19:12 -05009import sys
10import os
George Keishing9fbdf792016-10-18 06:16:09 -050011import getopt
12import csv
George Keishing9fbdf792016-10-18 06:16:09 -050013import robot.errors
manasarm5dedfaf2018-02-07 14:54:54 +053014import re
15from datetime import datetime
George Keishing9fbdf792016-10-18 06:16:09 -050016from robot.api import ExecutionResult
17from robot.result.visitor import ResultVisitor
George Keishing74777bd2017-02-07 01:43:38 -060018from xml.etree import ElementTree
manasarm5dedfaf2018-02-07 14:54:54 +053019
20# Remove the python library path to restore with local project path later.
21save_path_0 = sys.path[0]
22del sys.path[0]
23sys.path.append(os.path.join(os.path.dirname(__file__), "../../../lib"))
24
25from gen_arg import *
26from gen_print import *
27from gen_valid import *
28
29# Restore sys.path[0].
30sys.path.insert(0, save_path_0)
31
32parser = argparse.ArgumentParser(
33 usage='%(prog)s [OPTIONS]',
34 description="%(prog)s uses a robot framework API to extract test result\
35 data from output.xml generated by robot tests. For more information on the\
36 Robot Framework API, see\
37 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html and\
38 https://gerrit.openbmc-project.xyz/#/c/8885/15/tools/oem/ibm/\
39 gen_csv_results.py",
40 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
41 prefix_chars='-+')
42
43parser.add_argument(
44 '--source',
45 '-s',
46 help='The output.xml robot test result file path.')
47
48parser.add_argument(
49 '--dest',
50 '-d',
51 help='The directory path where the generated .csv files will go.')
52
manasarme4f79c92018-02-22 13:02:46 +053053parser.add_argument(
54 '--version_id',
55 help='Driver version of openbmc firmware which was used during test,\
56 e.g. "v2.1-215-g6e7eacb".')
57
58parser.add_argument(
59 '--platform',
George Keishing19533d52018-04-09 03:08:03 -050060 help='OpenBMC platform which was used during test,\
manasarme4f79c92018-02-22 13:02:46 +053061 e.g. "Witherspoon".')
62
George Keishing19533d52018-04-09 03:08:03 -050063parser.add_argument(
64 '--level',
65 help='OpenBMC release level which was used during test,\
66 e.g. "OBMC920".')
67
manasarm5dedfaf2018-02-07 14:54:54 +053068# Populate stock_list with options we want.
69stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
70
71
72def exit_function(signal_number=0,
73 frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +053074 r"""
75 Execute whenever the program ends normally or with the signals that we
76 catch (i.e. TERM, INT).
77 """
78
79 dprint_executing()
80
81 dprint_var(signal_number)
82
83 qprint_pgm_footer()
84
85
86def signal_handler(signal_number,
87 frame):
manasarm5dedfaf2018-02-07 14:54:54 +053088 r"""
89 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
90 program would terminate immediately with return code 143 and without
91 calling the exit_function.
92 """
93
94 # Our convention is to set up exit_function with atexit.register() so
95 # there is no need to explicitly call exit_function from here.
96
97 dprint_executing()
98
99 # Calling exit prevents us from returning to the code that was running
100 # when the signal was received.
101 exit(0)
102
103
104def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530105 r"""
106 Validate program parameters, etc. Return True or False (i.e. pass/fail)
107 accordingly.
108 """
109
110 if not valid_file_path(source):
111 return False
112
113 if not valid_dir_path(dest):
114 return False
115
116 gen_post_validation(exit_function, signal_handler)
117
118 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500119
120
George Keishing19533d52018-04-09 03:08:03 -0500121def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform, level):
manasarm5dedfaf2018-02-07 14:54:54 +0530122 r"""
123 Parse the robot-generated output.xml file and extract various test
124 output data. Put the extracted information into a csv file in the "dest"
125 folder.
126
127 Description of argument(s):
manasarme4f79c92018-02-22 13:02:46 +0530128 xml_file_path The path to a Robot-generated output.xml file.
129 csv_dir_path The path to the directory that is to contain the .csv files
130 generated by this function.
131 version_id Version of the openbmc firmware
132 (e.g. "v2.1-215-g6e7eacb").
133 platform Platform of the openbmc system.
George Keishing19533d52018-04-09 03:08:03 -0500134 level Release level of the OpenBMC system (e.g. "OBMC920").
manasarm5dedfaf2018-02-07 14:54:54 +0530135 """
136
George Keishing74777bd2017-02-07 01:43:38 -0600137 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500138 result.configure(stat_config={'suite_stat_level': 2,
139 'tag_stat_combine': 'tagANDanother'})
140
141 stats = result.statistics
142 print "--------------------------------------"
143 print "Total Test Count:\t",\
144 stats.total.critical.passed + stats.total.critical.failed
145 print "Total Test Failed:\t", stats.total.critical.failed
146 print "Total Test Passed:\t", stats.total.critical.passed
147 print "Test Start Time:\t", result.suite.starttime
148 print "Test End Time:\t\t", result.suite.endtime
149 print "--------------------------------------"
150
151 # Use ResultVisitor object and save off the test data info
152 class TestResult(ResultVisitor):
153 def __init__(self):
154 self.testData = []
155
156 def visit_test(self, test):
157 self.testData += [test]
158
159 collectDataObj = TestResult()
160 result.visit(collectDataObj)
161
162 # Write the result statistics attributes to CSV file
163 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600164
165 # Default Test data
George Keishing9fbdf792016-10-18 06:16:09 -0500166 l_subsys = 'OPENBMC'
167 l_test_type = 'FTC'
George Keishing19533d52018-04-09 03:08:03 -0500168
George Keishing9fbdf792016-10-18 06:16:09 -0500169 l_pse_rel = 'OBMC910'
George Keishing19533d52018-04-09 03:08:03 -0500170 if level:
171 l_pse_rel = level
172
George Keishing74777bd2017-02-07 01:43:38 -0600173 l_env = 'HW'
174 l_proc = 'P9'
175 l_platform_type = ""
176 l_func_area = ""
177
Gunnar Mills096cd562018-03-26 10:19:12 -0500178 # System data from XML meta data
manasarme4f79c92018-02-22 13:02:46 +0530179 #l_system_info = get_system_details(xml_file_path)
180
181 # First let us try to collect information from keyboard input
182 # If keyboard input cannot give both information, then find from xml file.
183 if version_id and platform:
184 l_driver = version_id
185 l_platform_type = platform
186 print "BMC Version_id:", version_id
187 print "BMC Platform:", platform
George Keishing74777bd2017-02-07 01:43:38 -0600188 else:
manasarme4f79c92018-02-22 13:02:46 +0530189 # System data from XML meta data
190 l_system_info = get_system_details(xml_file_path)
191 l_driver = l_system_info[0]
192 l_platform_type = l_system_info[1]
193
194 # Driver version id and platform are mandatorily required for CSV file
195 # generation. If any one is not avaulable, exit CSV file generation process.
196 if l_driver and l_platform_type:
197 print "Driver and system info set."
198 else:
199 print "\
200 Both driver and system info need to be set. CSV file is not generated."
George Keishing74777bd2017-02-07 01:43:38 -0600201 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500202
203 # Default header
204 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600205 'test_result', 'test_name', 'pse_rel', 'driver',
206 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500207
208 l_csvlist.append(l_header)
209
210 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600211 l_base_dir = csv_dir_path
George Keishing9fbdf792016-10-18 06:16:09 -0500212 l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600213 # Example: 2017-02-20-08-47-22_Witherspoon.csv
214 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500215
216 print "Writing data into csv file:", l_csvfile
217
218 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600219 # Functional Area: Suite Name
220 # Test Name: Test Case Name
221 l_func_area = str(testcase.parent).split(' ', 1)[1]
222 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500223
224 # Test Result pass=0 fail=1
225 if testcase.status == 'PASS':
226 l_test_result = 0
227 else:
228 l_test_result = 1
229
George Keishing74777bd2017-02-07 01:43:38 -0600230 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
231 l_stime = xml_to_csv_time(testcase.starttime)
232 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500233 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600234 # test_result,test_name,pse_rel,driver,
235 # env,proc,platform_tyep,test_func_area,
236 l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
237 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
238 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500239 l_csvlist.append(l_data)
240
George Keishinga96e27c2016-12-04 23:05:04 -0600241 # Open the file and write to the CSV file
242 l_file = open(l_csvfile, "w")
243 l_writer = csv.writer(l_file, lineterminator='\n')
244 l_writer.writerows(l_csvlist)
245 l_file.close()
George Keishing9fbdf792016-10-18 06:16:09 -0500246
247
George Keishing74777bd2017-02-07 01:43:38 -0600248def xml_to_csv_time(xml_datetime):
249 r"""
250 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
251 and return it.
252
manasarm5dedfaf2018-02-07 14:54:54 +0530253 Description of argument(s):
254 datetime The date in the following format: %Y%m%d %H:%M:%S.%f
255 (This is the format typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600256
257 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
258 """
manasarm5dedfaf2018-02-07 14:54:54 +0530259
George Keishing74777bd2017-02-07 01:43:38 -0600260 # 20170206 05:05:19.342
261 l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
262 # 2017-02-06-05-05-19
263 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
264 return str(l_str)
265
266
267def get_system_details(xml_file_path):
268 r"""
269 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500270 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600271
manasarm5dedfaf2018-02-07 14:54:54 +0530272 Description of argument(s):
273 xml_file_path The relative or absolute path to the output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600274 """
manasarm5dedfaf2018-02-07 14:54:54 +0530275
manasarme4f79c92018-02-22 13:02:46 +0530276 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600277 bmc_platform = ""
278 with open(xml_file_path, 'rt') as output:
279 tree = ElementTree.parse(output)
280
281 for node in tree.iter('msg'):
282 # /etc/os-release output is logged in the XML as msg
283 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
284 if '${output} = VERSION_ID=' in node.text:
285 # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
manasarme4f79c92018-02-22 13:02:46 +0530286 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600287
288 # Platform is logged in the XML as msg.
289 # Example: ${bmc_model} = Witherspoon BMC
290 if '${bmc_model} = ' in node.text:
291 bmc_platform = node.text.split(" = ")[1]
292
manasarme4f79c92018-02-22 13:02:46 +0530293 print_vars(bmc_version_id, bmc_platform)
294 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600295
296
manasarm5dedfaf2018-02-07 14:54:54 +0530297def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500298
manasarm5dedfaf2018-02-07 14:54:54 +0530299 if not gen_get_options(parser, stock_list):
300 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500301
manasarm5dedfaf2018-02-07 14:54:54 +0530302 if not validate_parms():
303 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500304
manasarm5dedfaf2018-02-07 14:54:54 +0530305 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500306
George Keishing19533d52018-04-09 03:08:03 -0500307 parse_output_xml(source, dest, version_id, platform, level)
George Keishing9fbdf792016-10-18 06:16:09 -0500308
manasarm5dedfaf2018-02-07 14:54:54 +0530309 return True
310
311
312# Main
313
314if not main():
315 exit(1)