blob: 5f53e866823f1d1c862869eba1f3ce6eeb35bd9e [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 Keishing09679892022-12-08 08:21:52 -06009from robot.api import ExecutionResult
10from robot.result.visitor import ResultVisitor
11from xml.etree import ElementTree
12
George Keishinge635ddc2022-12-08 07:38:02 -060013import sys
Patrick Williams57318182022-12-08 06:18:26 -060014import os
George Keishinge635ddc2022-12-08 07:38:02 -060015import getopt
16import csv
17import robot.errors
manasarm5dedfaf2018-02-07 14:54:54 +053018import re
Steven Sombar3e82e3b2019-03-21 10:33:52 -050019import stat
George Keishinge635ddc2022-12-08 07:38:02 -060020import datetime
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
George Keishing09679892022-12-08 08:21:52 -060027from gen_arg import * # NOQA
28from 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]
George Keishinge635ddc2022-12-08 07:38:02 -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,
George Keishinge635ddc2022-12-08 07:38:02 -060044 description="%(prog)s uses a robot framework API to extract test result\
45 data from output.xml generated by robot tests. For more information on the\
46 Robot Framework API, see\
47 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html",
manasarm5dedfaf2018-02-07 14:54:54 +053048 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
George Keishinge635ddc2022-12-08 07:38:02 -060049 prefix_chars='-+')
manasarm5dedfaf2018-02-07 14:54:54 +053050
51parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060052 '--source',
53 '-s',
54 help='The output.xml robot test result file path. This parameter is \
55 required.')
manasarm5dedfaf2018-02-07 14:54:54 +053056
57parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060058 '--dest',
59 '-d',
60 help='The directory path where the generated .csv files will go. This \
61 parameter is required.')
manasarm5dedfaf2018-02-07 14:54:54 +053062
manasarme4f79c92018-02-22 13:02:46 +053063parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060064 '--version_id',
65 help='Driver version of openbmc firmware which was used during test,\
66 e.g. "v2.1-215-g6e7eacb". This parameter is required.')
manasarme4f79c92018-02-22 13:02:46 +053067
68parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060069 '--platform',
70 help='OpenBMC platform which was used during test,\
71 e.g. "Witherspoon". This parameter is required.')
manasarme4f79c92018-02-22 13:02:46 +053072
George Keishing19533d52018-04-09 03:08:03 -050073parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060074 '--level',
75 help='OpenBMC release level which was used during test,\
76 e.g. "Master", "OBMC920". This parameter is required.')
Steven Sombara5e32f22019-03-01 13:33:15 -060077
78parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060079 '--test_phase',
80 help='Name of testing phase, e.g. "DVT", "SVT", etc.\
81 This parameter is optional.',
82 default="FVT")
Steven Sombara5e32f22019-03-01 13:33:15 -060083
84parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060085 '--subsystem',
86 help='Name of the subsystem, e.g. "OPENBMC" etc.\
87 This parameter is optional.',
88 default="OPENBMC")
Vijaye3a98372019-10-30 02:04:22 -050089
90parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060091 '--processor',
Steven Sombara5e32f22019-03-01 13:33:15 -060092 help='Name of processor, e.g. "P9". This parameter is optional.',
George Keishinge635ddc2022-12-08 07:38:02 -060093 default="OPENPOWER")
Steven Sombara5e32f22019-03-01 13:33:15 -060094
George Keishing19533d52018-04-09 03:08:03 -050095
manasarm5dedfaf2018-02-07 14:54:54 +053096# Populate stock_list with options we want.
97stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
98
99
George Keishinge635ddc2022-12-08 07:38:02 -0600100def exit_function(signal_number=0,
101 frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +0530102 r"""
103 Execute whenever the program ends normally or with the signals that we
104 catch (i.e. TERM, INT).
105 """
106
107 dprint_executing()
108
109 dprint_var(signal_number)
110
111 qprint_pgm_footer()
112
113
George Keishinge635ddc2022-12-08 07:38:02 -0600114def signal_handler(signal_number,
115 frame):
manasarm5dedfaf2018-02-07 14:54:54 +0530116 r"""
117 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
118 program would terminate immediately with return code 143 and without
119 calling the exit_function.
120 """
121
122 # Our convention is to set up exit_function with atexit.register() so
123 # there is no need to explicitly call exit_function from here.
124
125 dprint_executing()
126
127 # Calling exit prevents us from returning to the code that was running
128 # when the signal was received.
129 exit(0)
130
131
132def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530133 r"""
134 Validate program parameters, etc. Return True or False (i.e. pass/fail)
135 accordingly.
136 """
137
138 if not valid_file_path(source):
139 return False
140
141 if not valid_dir_path(dest):
142 return False
143
144 gen_post_validation(exit_function, signal_handler)
145
146 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500147
148
George Keishinge635ddc2022-12-08 07:38:02 -0600149def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform, level,
150 test_phase, processor):
manasarm5dedfaf2018-02-07 14:54:54 +0530151 r"""
152 Parse the robot-generated output.xml file and extract various test
153 output data. Put the extracted information into a csv file in the "dest"
154 folder.
155
156 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500157 xml_file_path The path to a Robot-generated output.xml
158 file.
159 csv_dir_path The path to the directory that is to
160 contain the .csv files generated by
161 this function.
162 version_id Version of the openbmc firmware
163 (e.g. "v2.1-215-g6e7eacb").
164 platform Platform of the openbmc system.
165 level Release level of the OpenBMC system
George Keishing360db632018-09-06 12:01:28 -0500166 (e.g. "Master").
manasarm5dedfaf2018-02-07 14:54:54 +0530167 """
168
Peter D Phan8e13ade2021-09-16 06:15:55 -0500169 # Initialize tallies
170 total_critical_tc = 0
171 total_critical_passed = 0
172 total_critical_failed = 0
173 total_non_critical_tc = 0
174 total_non_critical_passed = 0
175 total_non_critical_failed = 0
176
George Keishing74777bd2017-02-07 01:43:38 -0600177 result = ExecutionResult(xml_file_path)
George Keishinge635ddc2022-12-08 07:38:02 -0600178 result.configure(stat_config={'suite_stat_level': 2,
179 'tag_stat_combine': 'tagANDanother'})
George Keishing9fbdf792016-10-18 06:16:09 -0500180
181 stats = result.statistics
George Keishingf8a9ebe2018-08-06 12:49:11 -0500182 print("--------------------------------------")
Peter D Phan8e13ade2021-09-16 06:15:55 -0500183 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600184 total_critical_tc = stats.total.critical.passed + stats.total.critical.failed
Peter D Phan8e13ade2021-09-16 06:15:55 -0500185 total_critical_passed = stats.total.critical.passed
186 total_critical_failed = stats.total.critical.failed
187 except AttributeError:
188 pass
189
190 try:
191 total_non_critical_tc = stats.total.passed + stats.total.failed
192 total_non_critical_passed = stats.total.passed
193 total_non_critical_failed = stats.total.failed
194 except AttributeError:
195 pass
196
George Keishinge635ddc2022-12-08 07:38:02 -0600197 print("Total Test Count:\t %d" % (total_non_critical_tc + total_critical_tc))
Peter D Phan8e13ade2021-09-16 06:15:55 -0500198
199 print("Total Critical Test Failed:\t %d" % total_critical_failed)
200 print("Total Critical Test Passed:\t %d" % total_critical_passed)
201 print("Total Non-Critical Test Failed:\t %d" % total_non_critical_failed)
202 print("Total Non-Critical Test Passed:\t %d" % total_non_critical_passed)
George Keishingf8a9ebe2018-08-06 12:49:11 -0500203 print("Test Start Time:\t %s" % result.suite.starttime)
204 print("Test End Time:\t\t %s" % result.suite.endtime)
205 print("--------------------------------------")
George Keishing9fbdf792016-10-18 06:16:09 -0500206
207 # Use ResultVisitor object and save off the test data info
208 class TestResult(ResultVisitor):
209 def __init__(self):
210 self.testData = []
211
212 def visit_test(self, test):
213 self.testData += [test]
214
215 collectDataObj = TestResult()
216 result.visit(collectDataObj)
217
218 # Write the result statistics attributes to CSV file
219 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600220
221 # Default Test data
Steven Sombara5e32f22019-03-01 13:33:15 -0600222 l_test_type = test_phase
George Keishing19533d52018-04-09 03:08:03 -0500223
George Keishinge635ddc2022-12-08 07:38:02 -0600224 l_pse_rel = 'Master'
George Keishing19533d52018-04-09 03:08:03 -0500225 if level:
226 l_pse_rel = level
227
George Keishinge635ddc2022-12-08 07:38:02 -0600228 l_env = 'HW'
Steven Sombara5e32f22019-03-01 13:33:15 -0600229 l_proc = processor
George Keishing74777bd2017-02-07 01:43:38 -0600230 l_platform_type = ""
231 l_func_area = ""
232
Gunnar Mills096cd562018-03-26 10:19:12 -0500233 # System data from XML meta data
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500234 # l_system_info = get_system_details(xml_file_path)
manasarme4f79c92018-02-22 13:02:46 +0530235
236 # First let us try to collect information from keyboard input
237 # If keyboard input cannot give both information, then find from xml file.
238 if version_id and platform:
239 l_driver = version_id
240 l_platform_type = platform
George Keishingf8a9ebe2018-08-06 12:49:11 -0500241 print("BMC Version_id:%s" % version_id)
242 print("BMC Platform:%s" % platform)
George Keishing74777bd2017-02-07 01:43:38 -0600243 else:
manasarme4f79c92018-02-22 13:02:46 +0530244 # System data from XML meta data
245 l_system_info = get_system_details(xml_file_path)
246 l_driver = l_system_info[0]
247 l_platform_type = l_system_info[1]
248
249 # Driver version id and platform are mandatorily required for CSV file
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500250 # generation. If any one is not avaulable, exit CSV file generation
251 # process.
manasarme4f79c92018-02-22 13:02:46 +0530252 if l_driver and l_platform_type:
George Keishingf8a9ebe2018-08-06 12:49:11 -0500253 print("Driver and system info set.")
manasarme4f79c92018-02-22 13:02:46 +0530254 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600255 print("Both driver and system info need to be set.\
256 CSV file is not generated.")
George Keishing74777bd2017-02-07 01:43:38 -0600257 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500258
259 # Default header
George Keishinge635ddc2022-12-08 07:38:02 -0600260 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
261 'test_result', 'test_name', 'pse_rel', 'driver',
262 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500263
264 l_csvlist.append(l_header)
265
266 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600267 l_base_dir = csv_dir_path
George Keishing937fe3c2019-10-29 11:20:34 -0500268 l_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600269 # Example: 2017-02-20-08-47-22_Witherspoon.csv
270 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500271
George Keishingf8a9ebe2018-08-06 12:49:11 -0500272 print("Writing data into csv file:%s" % l_csvfile)
George Keishing9fbdf792016-10-18 06:16:09 -0500273
274 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600275 # Functional Area: Suite Name
276 # Test Name: Test Case Name
George Keishinge635ddc2022-12-08 07:38:02 -0600277 l_func_area = str(testcase.parent).split(' ', 1)[1]
George Keishing74777bd2017-02-07 01:43:38 -0600278 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500279
280 # Test Result pass=0 fail=1
George Keishinge635ddc2022-12-08 07:38:02 -0600281 if testcase.status == 'PASS':
George Keishing9fbdf792016-10-18 06:16:09 -0500282 l_test_result = 0
283 else:
284 l_test_result = 1
285
George Keishing74777bd2017-02-07 01:43:38 -0600286 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
287 l_stime = xml_to_csv_time(testcase.starttime)
288 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500289 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600290 # test_result,test_name,pse_rel,driver,
George Keishing360db632018-09-06 12:01:28 -0500291 # env,proc,platform_type,test_func_area,
George Keishinge635ddc2022-12-08 07:38:02 -0600292 l_data = [l_stime, l_etime, subsystem, l_test_type, l_test_result,
293 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
294 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500295 l_csvlist.append(l_data)
296
George Keishinga96e27c2016-12-04 23:05:04 -0600297 # Open the file and write to the CSV file
298 l_file = open(l_csvfile, "w")
George Keishinge635ddc2022-12-08 07:38:02 -0600299 l_writer = csv.writer(l_file, lineterminator='\n')
George Keishinga96e27c2016-12-04 23:05:04 -0600300 l_writer.writerows(l_csvlist)
301 l_file.close()
Steven Sombar3e82e3b2019-03-21 10:33:52 -0500302 # Set file permissions 666.
George Keishinge635ddc2022-12-08 07:38:02 -0600303 perm = stat.S_IRUSR + stat.S_IWUSR + stat.S_IRGRP + stat.S_IWGRP + stat.S_IROTH + stat.S_IWOTH
Steven Sombar3e82e3b2019-03-21 10:33:52 -0500304 os.chmod(l_csvfile, perm)
George Keishing9fbdf792016-10-18 06:16:09 -0500305
306
George Keishing74777bd2017-02-07 01:43:38 -0600307def xml_to_csv_time(xml_datetime):
308 r"""
309 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
310 and return it.
311
manasarm5dedfaf2018-02-07 14:54:54 +0530312 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500313 datetime The date in the following format: %Y%m%d
314 %H:%M:%S.%f (This is the format
315 typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600316
317 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
318 """
manasarm5dedfaf2018-02-07 14:54:54 +0530319
George Keishing74777bd2017-02-07 01:43:38 -0600320 # 20170206 05:05:19.342
George Keishing937fe3c2019-10-29 11:20:34 -0500321 l_str = datetime.datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
George Keishing74777bd2017-02-07 01:43:38 -0600322 # 2017-02-06-05-05-19
323 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
324 return str(l_str)
325
326
327def get_system_details(xml_file_path):
328 r"""
329 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500330 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600331
manasarm5dedfaf2018-02-07 14:54:54 +0530332 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500333 xml_file_path The relative or absolute path to the
334 output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600335 """
manasarm5dedfaf2018-02-07 14:54:54 +0530336
manasarme4f79c92018-02-22 13:02:46 +0530337 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600338 bmc_platform = ""
George Keishinge635ddc2022-12-08 07:38:02 -0600339 with open(xml_file_path, 'rt') as output:
George Keishing74777bd2017-02-07 01:43:38 -0600340 tree = ElementTree.parse(output)
341
George Keishinge635ddc2022-12-08 07:38:02 -0600342 for node in tree.iter('msg'):
George Keishing74777bd2017-02-07 01:43:38 -0600343 # /etc/os-release output is logged in the XML as msg
George Keishing360db632018-09-06 12:01:28 -0500344 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79"
George Keishinge635ddc2022-12-08 07:38:02 -0600345 if '${output} = VERSION_ID=' in node.text:
George Keishing360db632018-09-06 12:01:28 -0500346 # Get BMC version (e.g. v1.99.1-96-g2a46570)
manasarme4f79c92018-02-22 13:02:46 +0530347 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600348
349 # Platform is logged in the XML as msg.
350 # Example: ${bmc_model} = Witherspoon BMC
George Keishinge635ddc2022-12-08 07:38:02 -0600351 if '${bmc_model} = ' in node.text:
George Keishing74777bd2017-02-07 01:43:38 -0600352 bmc_platform = node.text.split(" = ")[1]
353
manasarme4f79c92018-02-22 13:02:46 +0530354 print_vars(bmc_version_id, bmc_platform)
355 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600356
357
manasarm5dedfaf2018-02-07 14:54:54 +0530358def main():
George Keishinge635ddc2022-12-08 07:38:02 -0600359
manasarm5dedfaf2018-02-07 14:54:54 +0530360 if not gen_get_options(parser, stock_list):
361 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500362
manasarm5dedfaf2018-02-07 14:54:54 +0530363 if not validate_parms():
364 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500365
manasarm5dedfaf2018-02-07 14:54:54 +0530366 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500367
George Keishinge635ddc2022-12-08 07:38:02 -0600368 parse_output_xml(source, dest, version_id, platform, level,
369 test_phase, processor)
George Keishing9fbdf792016-10-18 06:16:09 -0500370
manasarm5dedfaf2018-02-07 14:54:54 +0530371 return True
372
373
374# Main
375
376if not main():
377 exit(1)