blob: ae9aab8d539391cfe5742f5254059cfc710dddda [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\
George Keishing11b7af92018-06-11 10:39:01 -050037 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html",
manasarm5dedfaf2018-02-07 14:54:54 +053038 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
39 prefix_chars='-+')
40
41parser.add_argument(
42 '--source',
43 '-s',
44 help='The output.xml robot test result file path.')
45
46parser.add_argument(
47 '--dest',
48 '-d',
49 help='The directory path where the generated .csv files will go.')
50
manasarme4f79c92018-02-22 13:02:46 +053051parser.add_argument(
52 '--version_id',
53 help='Driver version of openbmc firmware which was used during test,\
54 e.g. "v2.1-215-g6e7eacb".')
55
56parser.add_argument(
57 '--platform',
George Keishing19533d52018-04-09 03:08:03 -050058 help='OpenBMC platform which was used during test,\
manasarme4f79c92018-02-22 13:02:46 +053059 e.g. "Witherspoon".')
60
George Keishing19533d52018-04-09 03:08:03 -050061parser.add_argument(
62 '--level',
63 help='OpenBMC release level which was used during test,\
64 e.g. "OBMC920".')
65
manasarm5dedfaf2018-02-07 14:54:54 +053066# Populate stock_list with options we want.
67stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
68
69
70def exit_function(signal_number=0,
71 frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +053072 r"""
73 Execute whenever the program ends normally or with the signals that we
74 catch (i.e. TERM, INT).
75 """
76
77 dprint_executing()
78
79 dprint_var(signal_number)
80
81 qprint_pgm_footer()
82
83
84def signal_handler(signal_number,
85 frame):
manasarm5dedfaf2018-02-07 14:54:54 +053086 r"""
87 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
88 program would terminate immediately with return code 143 and without
89 calling the exit_function.
90 """
91
92 # Our convention is to set up exit_function with atexit.register() so
93 # there is no need to explicitly call exit_function from here.
94
95 dprint_executing()
96
97 # Calling exit prevents us from returning to the code that was running
98 # when the signal was received.
99 exit(0)
100
101
102def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530103 r"""
104 Validate program parameters, etc. Return True or False (i.e. pass/fail)
105 accordingly.
106 """
107
108 if not valid_file_path(source):
109 return False
110
111 if not valid_dir_path(dest):
112 return False
113
114 gen_post_validation(exit_function, signal_handler)
115
116 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500117
118
George Keishing19533d52018-04-09 03:08:03 -0500119def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform, level):
manasarm5dedfaf2018-02-07 14:54:54 +0530120 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.
George Keishing19533d52018-04-09 03:08:03 -0500132 level Release level of the OpenBMC system (e.g. "OBMC920").
manasarm5dedfaf2018-02-07 14:54:54 +0530133 """
134
George Keishing74777bd2017-02-07 01:43:38 -0600135 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500136 result.configure(stat_config={'suite_stat_level': 2,
137 'tag_stat_combine': 'tagANDanother'})
138
139 stats = result.statistics
140 print "--------------------------------------"
141 print "Total Test Count:\t",\
142 stats.total.critical.passed + stats.total.critical.failed
143 print "Total Test Failed:\t", stats.total.critical.failed
144 print "Total Test Passed:\t", stats.total.critical.passed
145 print "Test Start Time:\t", result.suite.starttime
146 print "Test End Time:\t\t", result.suite.endtime
147 print "--------------------------------------"
148
149 # Use ResultVisitor object and save off the test data info
150 class TestResult(ResultVisitor):
151 def __init__(self):
152 self.testData = []
153
154 def visit_test(self, test):
155 self.testData += [test]
156
157 collectDataObj = TestResult()
158 result.visit(collectDataObj)
159
160 # Write the result statistics attributes to CSV file
161 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600162
163 # Default Test data
George Keishing9fbdf792016-10-18 06:16:09 -0500164 l_subsys = 'OPENBMC'
165 l_test_type = 'FTC'
George Keishing19533d52018-04-09 03:08:03 -0500166
George Keishing9fbdf792016-10-18 06:16:09 -0500167 l_pse_rel = 'OBMC910'
George Keishing19533d52018-04-09 03:08:03 -0500168 if level:
169 l_pse_rel = level
170
George Keishing74777bd2017-02-07 01:43:38 -0600171 l_env = 'HW'
172 l_proc = 'P9'
173 l_platform_type = ""
174 l_func_area = ""
175
Gunnar Mills096cd562018-03-26 10:19:12 -0500176 # System data from XML meta data
manasarme4f79c92018-02-22 13:02:46 +0530177 #l_system_info = get_system_details(xml_file_path)
178
179 # First let us try to collect information from keyboard input
180 # If keyboard input cannot give both information, then find from xml file.
181 if version_id and platform:
182 l_driver = version_id
183 l_platform_type = platform
184 print "BMC Version_id:", version_id
185 print "BMC Platform:", platform
George Keishing74777bd2017-02-07 01:43:38 -0600186 else:
manasarme4f79c92018-02-22 13:02:46 +0530187 # System data from XML meta data
188 l_system_info = get_system_details(xml_file_path)
189 l_driver = l_system_info[0]
190 l_platform_type = l_system_info[1]
191
192 # Driver version id and platform are mandatorily required for CSV file
193 # generation. If any one is not avaulable, exit CSV file generation process.
194 if l_driver and l_platform_type:
195 print "Driver and system info set."
196 else:
197 print "\
198 Both driver and system info need to be set. CSV file is not generated."
George Keishing74777bd2017-02-07 01:43:38 -0600199 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500200
201 # Default header
202 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600203 'test_result', 'test_name', 'pse_rel', 'driver',
204 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500205
206 l_csvlist.append(l_header)
207
208 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600209 l_base_dir = csv_dir_path
George Keishing9fbdf792016-10-18 06:16:09 -0500210 l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600211 # Example: 2017-02-20-08-47-22_Witherspoon.csv
212 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500213
214 print "Writing data into csv file:", l_csvfile
215
216 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600217 # Functional Area: Suite Name
218 # Test Name: Test Case Name
219 l_func_area = str(testcase.parent).split(' ', 1)[1]
220 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500221
222 # Test Result pass=0 fail=1
223 if testcase.status == 'PASS':
224 l_test_result = 0
225 else:
226 l_test_result = 1
227
George Keishing74777bd2017-02-07 01:43:38 -0600228 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
229 l_stime = xml_to_csv_time(testcase.starttime)
230 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500231 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600232 # test_result,test_name,pse_rel,driver,
233 # env,proc,platform_tyep,test_func_area,
234 l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
235 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
236 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500237 l_csvlist.append(l_data)
238
George Keishinga96e27c2016-12-04 23:05:04 -0600239 # Open the file and write to the CSV file
240 l_file = open(l_csvfile, "w")
241 l_writer = csv.writer(l_file, lineterminator='\n')
242 l_writer.writerows(l_csvlist)
243 l_file.close()
George Keishing9fbdf792016-10-18 06:16:09 -0500244
245
George Keishing74777bd2017-02-07 01:43:38 -0600246def xml_to_csv_time(xml_datetime):
247 r"""
248 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
249 and return it.
250
manasarm5dedfaf2018-02-07 14:54:54 +0530251 Description of argument(s):
252 datetime The date in the following format: %Y%m%d %H:%M:%S.%f
253 (This is the format typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600254
255 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
256 """
manasarm5dedfaf2018-02-07 14:54:54 +0530257
George Keishing74777bd2017-02-07 01:43:38 -0600258 # 20170206 05:05:19.342
259 l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
260 # 2017-02-06-05-05-19
261 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
262 return str(l_str)
263
264
265def get_system_details(xml_file_path):
266 r"""
267 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500268 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600269
manasarm5dedfaf2018-02-07 14:54:54 +0530270 Description of argument(s):
271 xml_file_path The relative or absolute path to the output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600272 """
manasarm5dedfaf2018-02-07 14:54:54 +0530273
manasarme4f79c92018-02-22 13:02:46 +0530274 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600275 bmc_platform = ""
276 with open(xml_file_path, 'rt') as output:
277 tree = ElementTree.parse(output)
278
279 for node in tree.iter('msg'):
280 # /etc/os-release output is logged in the XML as msg
281 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
282 if '${output} = VERSION_ID=' in node.text:
283 # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
manasarme4f79c92018-02-22 13:02:46 +0530284 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600285
286 # Platform is logged in the XML as msg.
287 # Example: ${bmc_model} = Witherspoon BMC
288 if '${bmc_model} = ' in node.text:
289 bmc_platform = node.text.split(" = ")[1]
290
manasarme4f79c92018-02-22 13:02:46 +0530291 print_vars(bmc_version_id, bmc_platform)
292 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600293
294
manasarm5dedfaf2018-02-07 14:54:54 +0530295def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500296
manasarm5dedfaf2018-02-07 14:54:54 +0530297 if not gen_get_options(parser, stock_list):
298 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500299
manasarm5dedfaf2018-02-07 14:54:54 +0530300 if not validate_parms():
301 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500302
manasarm5dedfaf2018-02-07 14:54:54 +0530303 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500304
George Keishing19533d52018-04-09 03:08:03 -0500305 parse_output_xml(source, dest, version_id, platform, level)
George Keishing9fbdf792016-10-18 06:16:09 -0500306
manasarm5dedfaf2018-02-07 14:54:54 +0530307 return True
308
309
310# Main
311
312if not main():
313 exit(1)