blob: 920e79ecaf2afaef94d505059952639bc65b51ce [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',
60 help='Openbmc platform which was used during test,\
61 e.g. "Witherspoon".')
62
manasarm5dedfaf2018-02-07 14:54:54 +053063# Populate stock_list with options we want.
64stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
65
66
67def exit_function(signal_number=0,
68 frame=None):
manasarm5dedfaf2018-02-07 14:54:54 +053069 r"""
70 Execute whenever the program ends normally or with the signals that we
71 catch (i.e. TERM, INT).
72 """
73
74 dprint_executing()
75
76 dprint_var(signal_number)
77
78 qprint_pgm_footer()
79
80
81def signal_handler(signal_number,
82 frame):
manasarm5dedfaf2018-02-07 14:54:54 +053083 r"""
84 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
85 program would terminate immediately with return code 143 and without
86 calling the exit_function.
87 """
88
89 # Our convention is to set up exit_function with atexit.register() so
90 # there is no need to explicitly call exit_function from here.
91
92 dprint_executing()
93
94 # Calling exit prevents us from returning to the code that was running
95 # when the signal was received.
96 exit(0)
97
98
99def validate_parms():
manasarm5dedfaf2018-02-07 14:54:54 +0530100 r"""
101 Validate program parameters, etc. Return True or False (i.e. pass/fail)
102 accordingly.
103 """
104
105 if not valid_file_path(source):
106 return False
107
108 if not valid_dir_path(dest):
109 return False
110
111 gen_post_validation(exit_function, signal_handler)
112
113 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500114
115
manasarme4f79c92018-02-22 13:02:46 +0530116def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform):
manasarm5dedfaf2018-02-07 14:54:54 +0530117 r"""
118 Parse the robot-generated output.xml file and extract various test
119 output data. Put the extracted information into a csv file in the "dest"
120 folder.
121
122 Description of argument(s):
manasarme4f79c92018-02-22 13:02:46 +0530123 xml_file_path The path to a Robot-generated output.xml file.
124 csv_dir_path The path to the directory that is to contain the .csv files
125 generated by this function.
126 version_id Version of the openbmc firmware
127 (e.g. "v2.1-215-g6e7eacb").
128 platform Platform of the openbmc system.
manasarm5dedfaf2018-02-07 14:54:54 +0530129 """
130
George Keishing74777bd2017-02-07 01:43:38 -0600131 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500132 result.configure(stat_config={'suite_stat_level': 2,
133 'tag_stat_combine': 'tagANDanother'})
134
135 stats = result.statistics
136 print "--------------------------------------"
137 print "Total Test Count:\t",\
138 stats.total.critical.passed + stats.total.critical.failed
139 print "Total Test Failed:\t", stats.total.critical.failed
140 print "Total Test Passed:\t", stats.total.critical.passed
141 print "Test Start Time:\t", result.suite.starttime
142 print "Test End Time:\t\t", result.suite.endtime
143 print "--------------------------------------"
144
145 # Use ResultVisitor object and save off the test data info
146 class TestResult(ResultVisitor):
147 def __init__(self):
148 self.testData = []
149
150 def visit_test(self, test):
151 self.testData += [test]
152
153 collectDataObj = TestResult()
154 result.visit(collectDataObj)
155
156 # Write the result statistics attributes to CSV file
157 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600158
159 # Default Test data
George Keishing9fbdf792016-10-18 06:16:09 -0500160 l_subsys = 'OPENBMC'
161 l_test_type = 'FTC'
162 l_pse_rel = 'OBMC910'
George Keishing74777bd2017-02-07 01:43:38 -0600163 l_env = 'HW'
164 l_proc = 'P9'
165 l_platform_type = ""
166 l_func_area = ""
167
Gunnar Mills096cd562018-03-26 10:19:12 -0500168 # System data from XML meta data
manasarme4f79c92018-02-22 13:02:46 +0530169 #l_system_info = get_system_details(xml_file_path)
170
171 # First let us try to collect information from keyboard input
172 # If keyboard input cannot give both information, then find from xml file.
173 if version_id and platform:
174 l_driver = version_id
175 l_platform_type = platform
176 print "BMC Version_id:", version_id
177 print "BMC Platform:", platform
George Keishing74777bd2017-02-07 01:43:38 -0600178 else:
manasarme4f79c92018-02-22 13:02:46 +0530179 # System data from XML meta data
180 l_system_info = get_system_details(xml_file_path)
181 l_driver = l_system_info[0]
182 l_platform_type = l_system_info[1]
183
184 # Driver version id and platform are mandatorily required for CSV file
185 # generation. If any one is not avaulable, exit CSV file generation process.
186 if l_driver and l_platform_type:
187 print "Driver and system info set."
188 else:
189 print "\
190 Both driver and system info need to be set. CSV file is not generated."
George Keishing74777bd2017-02-07 01:43:38 -0600191 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500192
193 # Default header
194 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600195 'test_result', 'test_name', 'pse_rel', 'driver',
196 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500197
198 l_csvlist.append(l_header)
199
200 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600201 l_base_dir = csv_dir_path
George Keishing9fbdf792016-10-18 06:16:09 -0500202 l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600203 # Example: 2017-02-20-08-47-22_Witherspoon.csv
204 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500205
206 print "Writing data into csv file:", l_csvfile
207
208 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600209 # Functional Area: Suite Name
210 # Test Name: Test Case Name
211 l_func_area = str(testcase.parent).split(' ', 1)[1]
212 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500213
214 # Test Result pass=0 fail=1
215 if testcase.status == 'PASS':
216 l_test_result = 0
217 else:
218 l_test_result = 1
219
George Keishing74777bd2017-02-07 01:43:38 -0600220 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
221 l_stime = xml_to_csv_time(testcase.starttime)
222 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500223 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600224 # test_result,test_name,pse_rel,driver,
225 # env,proc,platform_tyep,test_func_area,
226 l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
227 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
228 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500229 l_csvlist.append(l_data)
230
George Keishinga96e27c2016-12-04 23:05:04 -0600231 # Open the file and write to the CSV file
232 l_file = open(l_csvfile, "w")
233 l_writer = csv.writer(l_file, lineterminator='\n')
234 l_writer.writerows(l_csvlist)
235 l_file.close()
George Keishing9fbdf792016-10-18 06:16:09 -0500236
237
George Keishing74777bd2017-02-07 01:43:38 -0600238def xml_to_csv_time(xml_datetime):
239 r"""
240 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
241 and return it.
242
manasarm5dedfaf2018-02-07 14:54:54 +0530243 Description of argument(s):
244 datetime The date in the following format: %Y%m%d %H:%M:%S.%f
245 (This is the format typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600246
247 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
248 """
manasarm5dedfaf2018-02-07 14:54:54 +0530249
George Keishing74777bd2017-02-07 01:43:38 -0600250 # 20170206 05:05:19.342
251 l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
252 # 2017-02-06-05-05-19
253 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
254 return str(l_str)
255
256
257def get_system_details(xml_file_path):
258 r"""
259 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500260 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600261
manasarm5dedfaf2018-02-07 14:54:54 +0530262 Description of argument(s):
263 xml_file_path The relative or absolute path to the output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600264 """
manasarm5dedfaf2018-02-07 14:54:54 +0530265
manasarme4f79c92018-02-22 13:02:46 +0530266 bmc_version_id = ""
George Keishing74777bd2017-02-07 01:43:38 -0600267 bmc_platform = ""
268 with open(xml_file_path, 'rt') as output:
269 tree = ElementTree.parse(output)
270
271 for node in tree.iter('msg'):
272 # /etc/os-release output is logged in the XML as msg
273 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
274 if '${output} = VERSION_ID=' in node.text:
275 # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
manasarme4f79c92018-02-22 13:02:46 +0530276 bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1]
George Keishing74777bd2017-02-07 01:43:38 -0600277
278 # Platform is logged in the XML as msg.
279 # Example: ${bmc_model} = Witherspoon BMC
280 if '${bmc_model} = ' in node.text:
281 bmc_platform = node.text.split(" = ")[1]
282
manasarme4f79c92018-02-22 13:02:46 +0530283 print_vars(bmc_version_id, bmc_platform)
284 return [str(bmc_version_id), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600285
286
manasarm5dedfaf2018-02-07 14:54:54 +0530287def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500288
manasarm5dedfaf2018-02-07 14:54:54 +0530289 if not gen_get_options(parser, stock_list):
290 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500291
manasarm5dedfaf2018-02-07 14:54:54 +0530292 if not validate_parms():
293 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500294
manasarm5dedfaf2018-02-07 14:54:54 +0530295 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500296
manasarme4f79c92018-02-22 13:02:46 +0530297 parse_output_xml(source, dest, version_id, platform)
George Keishing9fbdf792016-10-18 06:16:09 -0500298
manasarm5dedfaf2018-02-07 14:54:54 +0530299 return True
300
301
302# Main
303
304if not main():
305 exit(1)