blob: ccdf9665e443042df0abd36af16f7e5e15059435 [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
George Keishinge635ddc2022-12-08 07:38:02 -06009import csv
Patrick Williams20f38712022-12-08 06:18:26 -060010import datetime
11import getopt
12import os
manasarm5dedfaf2018-02-07 14:54:54 +053013import re
Steven Sombar3e82e3b2019-03-21 10:33:52 -050014import stat
Patrick Williams20f38712022-12-08 06:18:26 -060015import sys
16from xml.etree import ElementTree
17
18import robot.errors
19from robot.api import ExecutionResult
20from robot.result.visitor import ResultVisitor
manasarm5dedfaf2018-02-07 14:54:54 +053021
22# Remove the python library path to restore with local project path later.
23save_path_0 = sys.path[0]
24del sys.path[0]
George Keishingc2a6f092019-02-20 12:26:54 -060025sys.path.append(os.path.join(os.path.dirname(__file__), "../../lib"))
manasarm5dedfaf2018-02-07 14:54:54 +053026
Patrick Williams20f38712022-12-08 06:18:26 -060027from gen_arg import * # NOQA
George Keishing09679892022-12-08 08:21:52 -060028from gen_print import * # NOQA
29from gen_valid import * # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060030
manasarm5dedfaf2018-02-07 14:54:54 +053031# Restore sys.path[0].
32sys.path.insert(0, save_path_0)
33
Steven Sombara5e32f22019-03-01 13:33:15 -060034
35this_program = sys.argv[0]
Patrick Williams20f38712022-12-08 06:18:26 -060036info = " For more information: " + this_program + " -h"
Steven Sombara5e32f22019-03-01 13:33:15 -060037if len(sys.argv) == 1:
George Keishingbfa859a2020-04-02 00:38:24 -050038 print(info)
Steven Sombara5e32f22019-03-01 13:33:15 -060039 sys.exit(1)
40
41
manasarm5dedfaf2018-02-07 14:54:54 +053042parser = argparse.ArgumentParser(
Steven Sombara5e32f22019-03-01 13:33:15 -060043 usage=info,
Patrick Williams20f38712022-12-08 06:18:26 -060044 description=(
45 "%(prog)s uses a robot framework API to extract test result data"
46 " from output.xml generated by robot tests. For more information on"
47 " the Robot Framework API, see "
48 " http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html"
49 ),
manasarm5dedfaf2018-02-07 14:54:54 +053050 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Patrick Williams20f38712022-12-08 06:18:26 -060051 prefix_chars="-+",
52)
manasarm5dedfaf2018-02-07 14:54:54 +053053
54parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060055 "--source",
56 "-s",
57 help=(
58 "The output.xml robot test result file path. This parameter is "
59 " required."
60 ),
61)
manasarm5dedfaf2018-02-07 14:54:54 +053062
63parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060064 "--dest",
65 "-d",
66 help=(
67 "The directory path where the generated .csv files will go. This "
68 " parameter is required."
69 ),
70)
manasarm5dedfaf2018-02-07 14:54:54 +053071
manasarme4f79c92018-02-22 13:02:46 +053072parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060073 "--version_id",
74 help=(
75 "Driver version of openbmc firmware which was used during test, "
76 ' e.g. "v2.1-215-g6e7eacb". This parameter is required.'
77 ),
78)
manasarme4f79c92018-02-22 13:02:46 +053079
80parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060081 "--platform",
82 help=(
83 "OpenBMC platform which was used during test, e.g."
84 ' "Witherspoon". This parameter is required.'
85 ),
86)
manasarme4f79c92018-02-22 13:02:46 +053087
George Keishing19533d52018-04-09 03:08:03 -050088parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060089 "--level",
90 help=(
91 "OpenBMC release level which was used during test, e.g."
92 ' "Master", "OBMC920". This parameter is required.'
93 ),
94)
Steven Sombara5e32f22019-03-01 13:33:15 -060095
96parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -060097 "--test_phase",
98 help=(
99 'Name of testing phase, e.g. "DVT", "SVT", etc. This'
100 " parameter is optional."
101 ),
102 default="FVT",
103)
Steven Sombara5e32f22019-03-01 13:33:15 -0600104
105parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -0600106 "--subsystem",
107 help=(
108 'Name of the subsystem, e.g. "OPENBMC" etc. This parameter is'
109 " optional."
110 ),
111 default="OPENBMC",
112)
Vijaye3a98372019-10-30 02:04:22 -0500113
114parser.add_argument(
Patrick Williams20f38712022-12-08 06:18:26 -0600115 "--processor",
Steven Sombara5e32f22019-03-01 13:33:15 -0600116 help='Name of processor, e.g. "P9". This parameter is optional.',
Patrick Williams20f38712022-12-08 06:18:26 -0600117 default="OPENPOWER",
118)
Steven Sombara5e32f22019-03-01 13:33:15 -0600119
George Keishing19533d52018-04-09 03:08:03 -0500120
manasarm5dedfaf2018-02-07 14:54:54 +0530121# Populate stock_list with options we want.
122stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
123
124
Patrick Williams20f38712022-12-08 06:18:26 -0600125def exit_function(signal_number=0, frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +0530126 r"""
127 Execute whenever the program ends normally or with the signals that we
128 catch (i.e. TERM, INT).
129 """
130
131 dprint_executing()
132
133 dprint_var(signal_number)
134
135 qprint_pgm_footer()
136
137
Patrick Williams20f38712022-12-08 06:18:26 -0600138def signal_handler(signal_number, frame):
manasarm5dedfaf2018-02-07 14:54:54 +0530139 r"""
140 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
141 program would terminate immediately with return code 143 and without
142 calling the exit_function.
143 """
144
145 # Our convention is to set up exit_function with atexit.register() so
146 # there is no need to explicitly call exit_function from here.
147
148 dprint_executing()
149
150 # Calling exit prevents us from returning to the code that was running
151 # when the signal was received.
152 exit(0)
153
154
155def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530156 r"""
157 Validate program parameters, etc. Return True or False (i.e. pass/fail)
158 accordingly.
159 """
160
161 if not valid_file_path(source):
162 return False
163
164 if not valid_dir_path(dest):
165 return False
166
167 gen_post_validation(exit_function, signal_handler)
168
169 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500170
171
Patrick Williams20f38712022-12-08 06:18:26 -0600172def parse_output_xml(
173 xml_file_path,
174 csv_dir_path,
175 version_id,
176 platform,
177 level,
178 test_phase,
179 processor,
180):
manasarm5dedfaf2018-02-07 14:54:54 +0530181 r"""
182 Parse the robot-generated output.xml file and extract various test
183 output data. Put the extracted information into a csv file in the "dest"
184 folder.
185
186 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500187 xml_file_path The path to a Robot-generated output.xml
188 file.
189 csv_dir_path The path to the directory that is to
190 contain the .csv files generated by
191 this function.
192 version_id Version of the openbmc firmware
193 (e.g. "v2.1-215-g6e7eacb").
194 platform Platform of the openbmc system.
195 level Release level of the OpenBMC system
George Keishing360db632018-09-06 12:01:28 -0500196 (e.g. "Master").
manasarm5dedfaf2018-02-07 14:54:54 +0530197 """
198
Peter D Phan8e13ade2021-09-16 06:15:55 -0500199 # Initialize tallies
200 total_critical_tc = 0
201 total_critical_passed = 0
202 total_critical_failed = 0
203 total_non_critical_tc = 0
204 total_non_critical_passed = 0
205 total_non_critical_failed = 0
206
George Keishing74777bd2017-02-07 01:43:38 -0600207 result = ExecutionResult(xml_file_path)
Patrick Williams20f38712022-12-08 06:18:26 -0600208 result.configure(
209 stat_config={
210 "suite_stat_level": 2,
211 "tag_stat_combine": "tagANDanother",
212 }
213 )
George Keishing9fbdf792016-10-18 06:16:09 -0500214
215 stats = result.statistics
George Keishingf8a9ebe2018-08-06 12:49:11 -0500216 print("--------------------------------------")
Peter D Phan8e13ade2021-09-16 06:15:55 -0500217 try:
Patrick Williams20f38712022-12-08 06:18:26 -0600218 total_critical_tc = (
219 stats.total.critical.passed + stats.total.critical.failed
220 )
Peter D Phan8e13ade2021-09-16 06:15:55 -0500221 total_critical_passed = stats.total.critical.passed
222 total_critical_failed = stats.total.critical.failed
223 except AttributeError:
224 pass
225
226 try:
227 total_non_critical_tc = stats.total.passed + stats.total.failed
228 total_non_critical_passed = stats.total.passed
229 total_non_critical_failed = stats.total.failed
230 except AttributeError:
231 pass
232
Patrick Williams20f38712022-12-08 06:18:26 -0600233 print(
234 "Total Test Count:\t %d" % (total_non_critical_tc + total_critical_tc)
235 )
Peter D Phan8e13ade2021-09-16 06:15:55 -0500236
237 print("Total Critical Test Failed:\t %d" % total_critical_failed)
238 print("Total Critical Test Passed:\t %d" % total_critical_passed)
239 print("Total Non-Critical Test Failed:\t %d" % total_non_critical_failed)
240 print("Total Non-Critical Test Passed:\t %d" % total_non_critical_passed)
George Keishingf8a9ebe2018-08-06 12:49:11 -0500241 print("Test Start Time:\t %s" % result.suite.starttime)
242 print("Test End Time:\t\t %s" % result.suite.endtime)
243 print("--------------------------------------")
George Keishing9fbdf792016-10-18 06:16:09 -0500244
245 # Use ResultVisitor object and save off the test data info
246 class TestResult(ResultVisitor):
247 def __init__(self):
248 self.testData = []
249
250 def visit_test(self, test):
251 self.testData += [test]
252
253 collectDataObj = TestResult()
254 result.visit(collectDataObj)
255
256 # Write the result statistics attributes to CSV file
257 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600258
259 # Default Test data
Steven Sombara5e32f22019-03-01 13:33:15 -0600260 l_test_type = test_phase
George Keishing19533d52018-04-09 03:08:03 -0500261
Patrick Williams20f38712022-12-08 06:18:26 -0600262 l_pse_rel = "Master"
George Keishing19533d52018-04-09 03:08:03 -0500263 if level:
264 l_pse_rel = level
265
Patrick Williams20f38712022-12-08 06:18:26 -0600266 l_env = "HW"
Steven Sombara5e32f22019-03-01 13:33:15 -0600267 l_proc = processor
George Keishing74777bd2017-02-07 01:43:38 -0600268 l_platform_type = ""
269 l_func_area = ""
270
Gunnar Mills096cd562018-03-26 10:19:12 -0500271 # System data from XML meta data
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500272 # l_system_info = get_system_details(xml_file_path)
manasarme4f79c92018-02-22 13:02:46 +0530273
274 # First let us try to collect information from keyboard input
275 # If keyboard input cannot give both information, then find from xml file.
276 if version_id and platform:
277 l_driver = version_id
278 l_platform_type = platform
George Keishingf8a9ebe2018-08-06 12:49:11 -0500279 print("BMC Version_id:%s" % version_id)
280 print("BMC Platform:%s" % platform)
George Keishing74777bd2017-02-07 01:43:38 -0600281 else:
manasarme4f79c92018-02-22 13:02:46 +0530282 # System data from XML meta data
283 l_system_info = get_system_details(xml_file_path)
284 l_driver = l_system_info[0]
285 l_platform_type = l_system_info[1]
286
287 # Driver version id and platform are mandatorily required for CSV file
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500288 # generation. If any one is not avaulable, exit CSV file generation
289 # process.
manasarme4f79c92018-02-22 13:02:46 +0530290 if l_driver and l_platform_type:
George Keishingf8a9ebe2018-08-06 12:49:11 -0500291 print("Driver and system info set.")
manasarme4f79c92018-02-22 13:02:46 +0530292 else:
Patrick Williams20f38712022-12-08 06:18:26 -0600293 print(
294 "Both driver and system info need to be set. CSV"
295 " file is not generated."
296 )
George Keishing74777bd2017-02-07 01:43:38 -0600297 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500298
299 # Default header
Patrick Williams20f38712022-12-08 06:18:26 -0600300 l_header = [
301 "test_start",
302 "test_end",
303 "subsys",
304 "test_type",
305 "test_result",
306 "test_name",
307 "pse_rel",
308 "driver",
309 "env",
310 "proc",
311 "platform_type",
312 "test_func_area",
313 ]
George Keishing9fbdf792016-10-18 06:16:09 -0500314
315 l_csvlist.append(l_header)
316
317 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600318 l_base_dir = csv_dir_path
George Keishing937fe3c2019-10-29 11:20:34 -0500319 l_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600320 # Example: 2017-02-20-08-47-22_Witherspoon.csv
321 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500322
George Keishingf8a9ebe2018-08-06 12:49:11 -0500323 print("Writing data into csv file:%s" % l_csvfile)
George Keishing9fbdf792016-10-18 06:16:09 -0500324
325 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600326 # Functional Area: Suite Name
327 # Test Name: Test Case Name
Patrick Williams20f38712022-12-08 06:18:26 -0600328 l_func_area = str(testcase.parent).split(" ", 1)[1]
George Keishing74777bd2017-02-07 01:43:38 -0600329 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500330
331 # Test Result pass=0 fail=1
Patrick Williams20f38712022-12-08 06:18:26 -0600332 if testcase.status == "PASS":
George Keishing9fbdf792016-10-18 06:16:09 -0500333 l_test_result = 0
George Keishingc6f90c52023-02-16 03:09:25 -0600334 elif testcase.status == "SKIP":
335 # Skipped test result should not be mark pass or fail.
336 continue
George Keishing9fbdf792016-10-18 06:16:09 -0500337 else:
338 l_test_result = 1
339
George Keishing74777bd2017-02-07 01:43:38 -0600340 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
341 l_stime = xml_to_csv_time(testcase.starttime)
342 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500343 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600344 # test_result,test_name,pse_rel,driver,
George Keishing360db632018-09-06 12:01:28 -0500345 # env,proc,platform_type,test_func_area,
Patrick Williams20f38712022-12-08 06:18:26 -0600346 l_data = [
347 l_stime,
348 l_etime,
349 subsystem,
350 l_test_type,
351 l_test_result,
352 l_test_name,
353 l_pse_rel,
354 l_driver,
355 l_env,
356 l_proc,
357 l_platform_type,
358 l_func_area,
359 ]
George Keishing9fbdf792016-10-18 06:16:09 -0500360 l_csvlist.append(l_data)
361
George Keishinga96e27c2016-12-04 23:05:04 -0600362 # Open the file and write to the CSV file
363 l_file = open(l_csvfile, "w")
Patrick Williams20f38712022-12-08 06:18:26 -0600364 l_writer = csv.writer(l_file, lineterminator="\n")
George Keishinga96e27c2016-12-04 23:05:04 -0600365 l_writer.writerows(l_csvlist)
366 l_file.close()
Steven Sombar3e82e3b2019-03-21 10:33:52 -0500367 # Set file permissions 666.
Patrick Williams20f38712022-12-08 06:18:26 -0600368 perm = (
369 stat.S_IRUSR
370 + stat.S_IWUSR
371 + stat.S_IRGRP
372 + stat.S_IWGRP
373 + stat.S_IROTH
374 + stat.S_IWOTH
375 )
Steven Sombar3e82e3b2019-03-21 10:33:52 -0500376 os.chmod(l_csvfile, perm)
George Keishing9fbdf792016-10-18 06:16:09 -0500377
378
George Keishing74777bd2017-02-07 01:43:38 -0600379def xml_to_csv_time(xml_datetime):
380 r"""
381 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
382 and return it.
383
manasarm5dedfaf2018-02-07 14:54:54 +0530384 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500385 datetime The date in the following format: %Y%m%d
386 %H:%M:%S.%f (This is the format
387 typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600388
389 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
390 """
manasarm5dedfaf2018-02-07 14:54:54 +0530391
George Keishing74777bd2017-02-07 01:43:38 -0600392 # 20170206 05:05:19.342
George Keishing937fe3c2019-10-29 11:20:34 -0500393 l_str = datetime.datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
George Keishing74777bd2017-02-07 01:43:38 -0600394 # 2017-02-06-05-05-19
395 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
396 return str(l_str)
397
398
399def get_system_details(xml_file_path):
400 r"""
401 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500402 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600403
manasarm5dedfaf2018-02-07 14:54:54 +0530404 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500405 xml_file_path The relative or absolute path to the
406 output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600407 """
manasarm5dedfaf2018-02-07 14:54:54 +0530408
manasarme4f79c92018-02-22 13:02:46 +0530409 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600410 bmc_platform = ""
Patrick Williams20f38712022-12-08 06:18:26 -0600411 with open(xml_file_path, "rt") as output:
George Keishing74777bd2017-02-07 01:43:38 -0600412 tree = ElementTree.parse(output)
413
Patrick Williams20f38712022-12-08 06:18:26 -0600414 for node in tree.iter("msg"):
George Keishing74777bd2017-02-07 01:43:38 -0600415 # /etc/os-release output is logged in the XML as msg
George Keishing360db632018-09-06 12:01:28 -0500416 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79"
Patrick Williams20f38712022-12-08 06:18:26 -0600417 if "${output} = VERSION_ID=" in node.text:
George Keishing360db632018-09-06 12:01:28 -0500418 # Get BMC version (e.g. v1.99.1-96-g2a46570)
manasarme4f79c92018-02-22 13:02:46 +0530419 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600420
421 # Platform is logged in the XML as msg.
422 # Example: ${bmc_model} = Witherspoon BMC
Patrick Williams20f38712022-12-08 06:18:26 -0600423 if "${bmc_model} = " in node.text:
George Keishing74777bd2017-02-07 01:43:38 -0600424 bmc_platform = node.text.split(" = ")[1]
425
manasarme4f79c92018-02-22 13:02:46 +0530426 print_vars(bmc_version_id, bmc_platform)
427 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600428
429
manasarm5dedfaf2018-02-07 14:54:54 +0530430def main():
manasarm5dedfaf2018-02-07 14:54:54 +0530431 if not gen_get_options(parser, stock_list):
432 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500433
manasarm5dedfaf2018-02-07 14:54:54 +0530434 if not validate_parms():
435 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500436
manasarm5dedfaf2018-02-07 14:54:54 +0530437 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500438
Patrick Williams20f38712022-12-08 06:18:26 -0600439 parse_output_xml(
440 source, dest, version_id, platform, level, test_phase, processor
441 )
George Keishing9fbdf792016-10-18 06:16:09 -0500442
manasarm5dedfaf2018-02-07 14:54:54 +0530443 return True
444
445
446# Main
447
448if not main():
449 exit(1)