blob: 342fe6999a35cc49b94ceb059b05d679316fdef2 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
manasarm5dedfaf2018-02-07 14:54:54 +05302
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
Steven Sombar3e82e3b2019-03-21 10:33:52 -050015import stat
George Keishing937fe3c2019-10-29 11:20:34 -050016import datetime
George Keishing9fbdf792016-10-18 06:16:09 -050017from robot.api import ExecutionResult
18from robot.result.visitor import ResultVisitor
George Keishing74777bd2017-02-07 01:43:38 -060019from xml.etree import ElementTree
manasarm5dedfaf2018-02-07 14:54:54 +053020
21# Remove the python library path to restore with local project path later.
22save_path_0 = sys.path[0]
23del sys.path[0]
George Keishingc2a6f092019-02-20 12:26:54 -060024sys.path.append(os.path.join(os.path.dirname(__file__), "../../lib"))
manasarm5dedfaf2018-02-07 14:54:54 +053025
26from gen_arg import *
27from gen_print import *
28from gen_valid import *
29
30# Restore sys.path[0].
31sys.path.insert(0, save_path_0)
32
Steven Sombara5e32f22019-03-01 13:33:15 -060033
34this_program = sys.argv[0]
35info = " For more information: " + this_program + ' -h'
36if len(sys.argv) == 1:
George Keishingbfa859a2020-04-02 00:38:24 -050037 print(info)
Steven Sombara5e32f22019-03-01 13:33:15 -060038 sys.exit(1)
39
40
manasarm5dedfaf2018-02-07 14:54:54 +053041parser = argparse.ArgumentParser(
Steven Sombara5e32f22019-03-01 13:33:15 -060042 usage=info,
manasarm5dedfaf2018-02-07 14:54:54 +053043 description="%(prog)s uses a robot framework API to extract test result\
44 data from output.xml generated by robot tests. For more information on the\
45 Robot Framework API, see\
George Keishing11b7af92018-06-11 10:39:01 -050046 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html",
manasarm5dedfaf2018-02-07 14:54:54 +053047 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
48 prefix_chars='-+')
49
50parser.add_argument(
51 '--source',
52 '-s',
Steven Sombara5e32f22019-03-01 13:33:15 -060053 help='The output.xml robot test result file path. This parameter is \
54 required.')
manasarm5dedfaf2018-02-07 14:54:54 +053055
56parser.add_argument(
57 '--dest',
58 '-d',
Steven Sombara5e32f22019-03-01 13:33:15 -060059 help='The directory path where the generated .csv files will go. This \
60 parameter is required.')
manasarm5dedfaf2018-02-07 14:54:54 +053061
manasarme4f79c92018-02-22 13:02:46 +053062parser.add_argument(
63 '--version_id',
64 help='Driver version of openbmc firmware which was used during test,\
Steven Sombara5e32f22019-03-01 13:33:15 -060065 e.g. "v2.1-215-g6e7eacb". This parameter is required.')
manasarme4f79c92018-02-22 13:02:46 +053066
67parser.add_argument(
68 '--platform',
George Keishing19533d52018-04-09 03:08:03 -050069 help='OpenBMC platform which was used during test,\
Steven Sombara5e32f22019-03-01 13:33:15 -060070 e.g. "Witherspoon". This parameter is required.')
manasarme4f79c92018-02-22 13:02:46 +053071
George Keishing19533d52018-04-09 03:08:03 -050072parser.add_argument(
73 '--level',
74 help='OpenBMC release level which was used during test,\
Steven Sombara5e32f22019-03-01 13:33:15 -060075 e.g. "Master", "OBMC920". This parameter is required.')
76
77parser.add_argument(
78 '--test_phase',
79 help='Name of testing phase, e.g. "DVT", "SVT", etc.\
80 This parameter is optional.',
81 default="FVT")
82
83parser.add_argument(
Vijaye3a98372019-10-30 02:04:22 -050084 '--subsystem',
85 help='Name of the subsystem, e.g. "OPENBMC" etc.\
86 This parameter is optional.',
87 default="OPENBMC")
88
89parser.add_argument(
Steven Sombara5e32f22019-03-01 13:33:15 -060090 '--processor',
91 help='Name of processor, e.g. "P9". This parameter is optional.',
92 default="OPENPOWER")
93
George Keishing19533d52018-04-09 03:08:03 -050094
manasarm5dedfaf2018-02-07 14:54:54 +053095# Populate stock_list with options we want.
96stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
97
98
99def exit_function(signal_number=0,
100 frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +0530101 r"""
102 Execute whenever the program ends normally or with the signals that we
103 catch (i.e. TERM, INT).
104 """
105
106 dprint_executing()
107
108 dprint_var(signal_number)
109
110 qprint_pgm_footer()
111
112
113def signal_handler(signal_number,
114 frame):
manasarm5dedfaf2018-02-07 14:54:54 +0530115 r"""
116 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
117 program would terminate immediately with return code 143 and without
118 calling the exit_function.
119 """
120
121 # Our convention is to set up exit_function with atexit.register() so
122 # there is no need to explicitly call exit_function from here.
123
124 dprint_executing()
125
126 # Calling exit prevents us from returning to the code that was running
127 # when the signal was received.
128 exit(0)
129
130
131def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530132 r"""
133 Validate program parameters, etc. Return True or False (i.e. pass/fail)
134 accordingly.
135 """
136
137 if not valid_file_path(source):
138 return False
139
140 if not valid_dir_path(dest):
141 return False
142
143 gen_post_validation(exit_function, signal_handler)
144
145 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500146
147
Steven Sombara5e32f22019-03-01 13:33:15 -0600148def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform, level,
149 test_phase, processor):
manasarm5dedfaf2018-02-07 14:54:54 +0530150 r"""
151 Parse the robot-generated output.xml file and extract various test
152 output data. Put the extracted information into a csv file in the "dest"
153 folder.
154
155 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500156 xml_file_path The path to a Robot-generated output.xml
157 file.
158 csv_dir_path The path to the directory that is to
159 contain the .csv files generated by
160 this function.
161 version_id Version of the openbmc firmware
162 (e.g. "v2.1-215-g6e7eacb").
163 platform Platform of the openbmc system.
164 level Release level of the OpenBMC system
George Keishing360db632018-09-06 12:01:28 -0500165 (e.g. "Master").
manasarm5dedfaf2018-02-07 14:54:54 +0530166 """
167
Peter D Phan8e13ade2021-09-16 06:15:55 -0500168 # Initialize tallies
169 total_critical_tc = 0
170 total_critical_passed = 0
171 total_critical_failed = 0
172 total_non_critical_tc = 0
173 total_non_critical_passed = 0
174 total_non_critical_failed = 0
175
George Keishing74777bd2017-02-07 01:43:38 -0600176 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500177 result.configure(stat_config={'suite_stat_level': 2,
178 'tag_stat_combine': 'tagANDanother'})
179
180 stats = result.statistics
George Keishingf8a9ebe2018-08-06 12:49:11 -0500181 print("--------------------------------------")
Peter D Phan8e13ade2021-09-16 06:15:55 -0500182 try:
183 total_critical_tc = stats.total.critical.passed + stats.total.critical.failed
184 total_critical_passed = stats.total.critical.passed
185 total_critical_failed = stats.total.critical.failed
186 except AttributeError:
187 pass
188
189 try:
190 total_non_critical_tc = stats.total.passed + stats.total.failed
191 total_non_critical_passed = stats.total.passed
192 total_non_critical_failed = stats.total.failed
193 except AttributeError:
194 pass
195
196 print("Total Test Count:\t %d" % (total_non_critical_tc + total_critical_tc))
197
198 print("Total Critical Test Failed:\t %d" % total_critical_failed)
199 print("Total Critical Test Passed:\t %d" % total_critical_passed)
200 print("Total Non-Critical Test Failed:\t %d" % total_non_critical_failed)
201 print("Total Non-Critical Test Passed:\t %d" % total_non_critical_passed)
George Keishingf8a9ebe2018-08-06 12:49:11 -0500202 print("Test Start Time:\t %s" % result.suite.starttime)
203 print("Test End Time:\t\t %s" % result.suite.endtime)
204 print("--------------------------------------")
George Keishing9fbdf792016-10-18 06:16:09 -0500205
206 # Use ResultVisitor object and save off the test data info
207 class TestResult(ResultVisitor):
208 def __init__(self):
209 self.testData = []
210
211 def visit_test(self, test):
212 self.testData += [test]
213
214 collectDataObj = TestResult()
215 result.visit(collectDataObj)
216
217 # Write the result statistics attributes to CSV file
218 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600219
220 # Default Test data
Steven Sombara5e32f22019-03-01 13:33:15 -0600221 l_test_type = test_phase
George Keishing19533d52018-04-09 03:08:03 -0500222
George Keishing360db632018-09-06 12:01:28 -0500223 l_pse_rel = 'Master'
George Keishing19533d52018-04-09 03:08:03 -0500224 if level:
225 l_pse_rel = level
226
George Keishing74777bd2017-02-07 01:43:38 -0600227 l_env = 'HW'
Steven Sombara5e32f22019-03-01 13:33:15 -0600228 l_proc = processor
George Keishing74777bd2017-02-07 01:43:38 -0600229 l_platform_type = ""
230 l_func_area = ""
231
Gunnar Mills096cd562018-03-26 10:19:12 -0500232 # System data from XML meta data
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500233 # l_system_info = get_system_details(xml_file_path)
manasarme4f79c92018-02-22 13:02:46 +0530234
235 # First let us try to collect information from keyboard input
236 # If keyboard input cannot give both information, then find from xml file.
237 if version_id and platform:
238 l_driver = version_id
239 l_platform_type = platform
George Keishingf8a9ebe2018-08-06 12:49:11 -0500240 print("BMC Version_id:%s" % version_id)
241 print("BMC Platform:%s" % platform)
George Keishing74777bd2017-02-07 01:43:38 -0600242 else:
manasarme4f79c92018-02-22 13:02:46 +0530243 # System data from XML meta data
244 l_system_info = get_system_details(xml_file_path)
245 l_driver = l_system_info[0]
246 l_platform_type = l_system_info[1]
247
248 # Driver version id and platform are mandatorily required for CSV file
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500249 # generation. If any one is not avaulable, exit CSV file generation
250 # process.
manasarme4f79c92018-02-22 13:02:46 +0530251 if l_driver and l_platform_type:
George Keishingf8a9ebe2018-08-06 12:49:11 -0500252 print("Driver and system info set.")
manasarme4f79c92018-02-22 13:02:46 +0530253 else:
George Keishingf8a9ebe2018-08-06 12:49:11 -0500254 print("Both driver and system info need to be set.\
255 CSV file is not generated.")
George Keishing74777bd2017-02-07 01:43:38 -0600256 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500257
258 # Default header
259 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600260 'test_result', 'test_name', 'pse_rel', 'driver',
261 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500262
263 l_csvlist.append(l_header)
264
265 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600266 l_base_dir = csv_dir_path
George Keishing937fe3c2019-10-29 11:20:34 -0500267 l_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600268 # Example: 2017-02-20-08-47-22_Witherspoon.csv
269 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500270
George Keishingf8a9ebe2018-08-06 12:49:11 -0500271 print("Writing data into csv file:%s" % l_csvfile)
George Keishing9fbdf792016-10-18 06:16:09 -0500272
273 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600274 # Functional Area: Suite Name
275 # Test Name: Test Case Name
276 l_func_area = str(testcase.parent).split(' ', 1)[1]
277 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500278
279 # Test Result pass=0 fail=1
280 if testcase.status == 'PASS':
281 l_test_result = 0
282 else:
283 l_test_result = 1
284
George Keishing74777bd2017-02-07 01:43:38 -0600285 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
286 l_stime = xml_to_csv_time(testcase.starttime)
287 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500288 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600289 # test_result,test_name,pse_rel,driver,
George Keishing360db632018-09-06 12:01:28 -0500290 # env,proc,platform_type,test_func_area,
Vijaye3a98372019-10-30 02:04:22 -0500291 l_data = [l_stime, l_etime, subsystem, l_test_type, l_test_result,
George Keishing74777bd2017-02-07 01:43:38 -0600292 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
293 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500294 l_csvlist.append(l_data)
295
George Keishinga96e27c2016-12-04 23:05:04 -0600296 # Open the file and write to the CSV file
297 l_file = open(l_csvfile, "w")
298 l_writer = csv.writer(l_file, lineterminator='\n')
299 l_writer.writerows(l_csvlist)
300 l_file.close()
Steven Sombar3e82e3b2019-03-21 10:33:52 -0500301 # Set file permissions 666.
302 perm = stat.S_IRUSR + stat.S_IWUSR + stat.S_IRGRP + stat.S_IWGRP + stat.S_IROTH + stat.S_IWOTH
303 os.chmod(l_csvfile, perm)
George Keishing9fbdf792016-10-18 06:16:09 -0500304
305
George Keishing74777bd2017-02-07 01:43:38 -0600306def xml_to_csv_time(xml_datetime):
307 r"""
308 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
309 and return it.
310
manasarm5dedfaf2018-02-07 14:54:54 +0530311 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500312 datetime The date in the following format: %Y%m%d
313 %H:%M:%S.%f (This is the format
314 typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600315
316 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
317 """
manasarm5dedfaf2018-02-07 14:54:54 +0530318
George Keishing74777bd2017-02-07 01:43:38 -0600319 # 20170206 05:05:19.342
George Keishing937fe3c2019-10-29 11:20:34 -0500320 l_str = datetime.datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
George Keishing74777bd2017-02-07 01:43:38 -0600321 # 2017-02-06-05-05-19
322 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
323 return str(l_str)
324
325
326def get_system_details(xml_file_path):
327 r"""
328 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500329 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600330
manasarm5dedfaf2018-02-07 14:54:54 +0530331 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500332 xml_file_path The relative or absolute path to the
333 output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600334 """
manasarm5dedfaf2018-02-07 14:54:54 +0530335
manasarme4f79c92018-02-22 13:02:46 +0530336 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600337 bmc_platform = ""
338 with open(xml_file_path, 'rt') as output:
339 tree = ElementTree.parse(output)
340
341 for node in tree.iter('msg'):
342 # /etc/os-release output is logged in the XML as msg
George Keishing360db632018-09-06 12:01:28 -0500343 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79"
George Keishing74777bd2017-02-07 01:43:38 -0600344 if '${output} = VERSION_ID=' in node.text:
George Keishing360db632018-09-06 12:01:28 -0500345 # Get BMC version (e.g. v1.99.1-96-g2a46570)
manasarme4f79c92018-02-22 13:02:46 +0530346 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600347
348 # Platform is logged in the XML as msg.
349 # Example: ${bmc_model} = Witherspoon BMC
350 if '${bmc_model} = ' in node.text:
351 bmc_platform = node.text.split(" = ")[1]
352
manasarme4f79c92018-02-22 13:02:46 +0530353 print_vars(bmc_version_id, bmc_platform)
354 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600355
356
manasarm5dedfaf2018-02-07 14:54:54 +0530357def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500358
manasarm5dedfaf2018-02-07 14:54:54 +0530359 if not gen_get_options(parser, stock_list):
360 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500361
manasarm5dedfaf2018-02-07 14:54:54 +0530362 if not validate_parms():
363 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500364
manasarm5dedfaf2018-02-07 14:54:54 +0530365 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500366
Steven Sombara5e32f22019-03-01 13:33:15 -0600367 parse_output_xml(source, dest, version_id, platform, level,
368 test_phase, processor)
George Keishing9fbdf792016-10-18 06:16:09 -0500369
manasarm5dedfaf2018-02-07 14:54:54 +0530370 return True
371
372
373# Main
374
375if not main():
376 exit(1)