blob: 6236a975f033029f74295fad376deb67e1389156 [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
9import sys, os
George Keishing9fbdf792016-10-18 06:16:09 -050010import getopt
11import csv
George Keishing9fbdf792016-10-18 06:16:09 -050012import robot.errors
manasarm5dedfaf2018-02-07 14:54:54 +053013import re
14from datetime import datetime
George Keishing9fbdf792016-10-18 06:16:09 -050015from robot.api import ExecutionResult
16from robot.result.visitor import ResultVisitor
George Keishing74777bd2017-02-07 01:43:38 -060017from xml.etree import ElementTree
manasarm5dedfaf2018-02-07 14:54:54 +053018
19# Remove the python library path to restore with local project path later.
20save_path_0 = sys.path[0]
21del sys.path[0]
22sys.path.append(os.path.join(os.path.dirname(__file__), "../../../lib"))
23
24from gen_arg import *
25from gen_print import *
26from gen_valid import *
27
28# Restore sys.path[0].
29sys.path.insert(0, save_path_0)
30
31parser = argparse.ArgumentParser(
32 usage='%(prog)s [OPTIONS]',
33 description="%(prog)s uses a robot framework API to extract test result\
34 data from output.xml generated by robot tests. For more information on the\
35 Robot Framework API, see\
36 http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html and\
37 https://gerrit.openbmc-project.xyz/#/c/8885/15/tools/oem/ibm/\
38 gen_csv_results.py",
39 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
40 prefix_chars='-+')
41
42parser.add_argument(
43 '--source',
44 '-s',
45 help='The output.xml robot test result file path.')
46
47parser.add_argument(
48 '--dest',
49 '-d',
50 help='The directory path where the generated .csv files will go.')
51
52# Populate stock_list with options we want.
53stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
54
55
56def exit_function(signal_number=0,
57 frame=None):
58
59 r"""
60 Execute whenever the program ends normally or with the signals that we
61 catch (i.e. TERM, INT).
62 """
63
64 dprint_executing()
65
66 dprint_var(signal_number)
67
68 qprint_pgm_footer()
69
70
71def signal_handler(signal_number,
72 frame):
73
74 r"""
75 Handle signals. Without a function to catch a SIGTERM or SIGINT, the
76 program would terminate immediately with return code 143 and without
77 calling the exit_function.
78 """
79
80 # Our convention is to set up exit_function with atexit.register() so
81 # there is no need to explicitly call exit_function from here.
82
83 dprint_executing()
84
85 # Calling exit prevents us from returning to the code that was running
86 # when the signal was received.
87 exit(0)
88
89
90def validate_parms():
91
92 r"""
93 Validate program parameters, etc. Return True or False (i.e. pass/fail)
94 accordingly.
95 """
96
97 if not valid_file_path(source):
98 return False
99
100 if not valid_dir_path(dest):
101 return False
102
103 gen_post_validation(exit_function, signal_handler)
104
105 return True
George Keishing9fbdf792016-10-18 06:16:09 -0500106
107
George Keishing74777bd2017-02-07 01:43:38 -0600108def parse_output_xml(xml_file_path, csv_dir_path):
manasarm5dedfaf2018-02-07 14:54:54 +0530109
110 r"""
111 Parse the robot-generated output.xml file and extract various test
112 output data. Put the extracted information into a csv file in the "dest"
113 folder.
114
115 Description of argument(s):
116 xml_file_path The path to a Robot-generated output.xml file.
117 csv_dir_path The path to the directory that is to contain the .csv files
118 generated by this function.
119 """
120
George Keishing74777bd2017-02-07 01:43:38 -0600121 result = ExecutionResult(xml_file_path)
George Keishing9fbdf792016-10-18 06:16:09 -0500122 result.configure(stat_config={'suite_stat_level': 2,
123 'tag_stat_combine': 'tagANDanother'})
124
125 stats = result.statistics
126 print "--------------------------------------"
127 print "Total Test Count:\t",\
128 stats.total.critical.passed + stats.total.critical.failed
129 print "Total Test Failed:\t", stats.total.critical.failed
130 print "Total Test Passed:\t", stats.total.critical.passed
131 print "Test Start Time:\t", result.suite.starttime
132 print "Test End Time:\t\t", result.suite.endtime
133 print "--------------------------------------"
134
135 # Use ResultVisitor object and save off the test data info
136 class TestResult(ResultVisitor):
137 def __init__(self):
138 self.testData = []
139
140 def visit_test(self, test):
141 self.testData += [test]
142
143 collectDataObj = TestResult()
144 result.visit(collectDataObj)
145
146 # Write the result statistics attributes to CSV file
147 l_csvlist = []
George Keishing74777bd2017-02-07 01:43:38 -0600148
149 # Default Test data
George Keishing9fbdf792016-10-18 06:16:09 -0500150 l_subsys = 'OPENBMC'
151 l_test_type = 'FTC'
152 l_pse_rel = 'OBMC910'
George Keishing74777bd2017-02-07 01:43:38 -0600153 l_env = 'HW'
154 l_proc = 'P9'
155 l_platform_type = ""
156 l_func_area = ""
157
158 # System data from XML meta data
159 l_system_info = get_system_details(xml_file_path)
160 l_driver = l_system_info[0]
161 if l_system_info[1]:
162 l_platform_type = l_system_info[1]
163 else:
164 print "System model is not set"
165 sys.exit()
George Keishing9fbdf792016-10-18 06:16:09 -0500166
167 # Default header
168 l_header = ['test_start', 'test_end', 'subsys', 'test_type',
George Keishing74777bd2017-02-07 01:43:38 -0600169 'test_result', 'test_name', 'pse_rel', 'driver',
170 'env', 'proc', 'platform_type', 'test_func_area']
George Keishing9fbdf792016-10-18 06:16:09 -0500171
172 l_csvlist.append(l_header)
173
174 # Generate CSV file onto the path with current time stamp
George Keishing74777bd2017-02-07 01:43:38 -0600175 l_base_dir = csv_dir_path
George Keishing9fbdf792016-10-18 06:16:09 -0500176 l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
George Keishing74777bd2017-02-07 01:43:38 -0600177 # Example: 2017-02-20-08-47-22_Witherspoon.csv
178 l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
George Keishing9fbdf792016-10-18 06:16:09 -0500179
180 print "Writing data into csv file:", l_csvfile
181
182 for testcase in collectDataObj.testData:
George Keishing74777bd2017-02-07 01:43:38 -0600183 # Functional Area: Suite Name
184 # Test Name: Test Case Name
185 l_func_area = str(testcase.parent).split(' ', 1)[1]
186 l_test_name = str(testcase)
George Keishing9fbdf792016-10-18 06:16:09 -0500187
188 # Test Result pass=0 fail=1
189 if testcase.status == 'PASS':
190 l_test_result = 0
191 else:
192 l_test_result = 1
193
George Keishing74777bd2017-02-07 01:43:38 -0600194 # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
195 l_stime = xml_to_csv_time(testcase.starttime)
196 l_etime = xml_to_csv_time(testcase.endtime)
George Keishing9fbdf792016-10-18 06:16:09 -0500197 # Data Sequence: test_start,test_end,subsys,test_type,
George Keishing74777bd2017-02-07 01:43:38 -0600198 # test_result,test_name,pse_rel,driver,
199 # env,proc,platform_tyep,test_func_area,
200 l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
201 l_test_name, l_pse_rel, l_driver, l_env, l_proc,
202 l_platform_type, l_func_area]
George Keishing9fbdf792016-10-18 06:16:09 -0500203 l_csvlist.append(l_data)
204
George Keishinga96e27c2016-12-04 23:05:04 -0600205 # Open the file and write to the CSV file
206 l_file = open(l_csvfile, "w")
207 l_writer = csv.writer(l_file, lineterminator='\n')
208 l_writer.writerows(l_csvlist)
209 l_file.close()
George Keishing9fbdf792016-10-18 06:16:09 -0500210
211
George Keishing74777bd2017-02-07 01:43:38 -0600212def xml_to_csv_time(xml_datetime):
manasarm5dedfaf2018-02-07 14:54:54 +0530213
George Keishing74777bd2017-02-07 01:43:38 -0600214 r"""
215 Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
216 and return it.
217
manasarm5dedfaf2018-02-07 14:54:54 +0530218 Description of argument(s):
219 datetime The date in the following format: %Y%m%d %H:%M:%S.%f
220 (This is the format typically found in an XML file.)
George Keishing74777bd2017-02-07 01:43:38 -0600221
222 The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
223 """
manasarm5dedfaf2018-02-07 14:54:54 +0530224
George Keishing74777bd2017-02-07 01:43:38 -0600225 # 20170206 05:05:19.342
226 l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
227 # 2017-02-06-05-05-19
228 l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
229 return str(l_str)
230
231
232def get_system_details(xml_file_path):
manasarm5dedfaf2018-02-07 14:54:54 +0530233
George Keishing74777bd2017-02-07 01:43:38 -0600234 r"""
235 Get the system data from output.xml generated by robot and return it.
Gunnar Mills28e403b2017-10-25 16:16:38 -0500236 The list returned will be in the following order: [driver,platform]
George Keishing74777bd2017-02-07 01:43:38 -0600237
manasarm5dedfaf2018-02-07 14:54:54 +0530238 Description of argument(s):
239 xml_file_path The relative or absolute path to the output.xml file.
George Keishing74777bd2017-02-07 01:43:38 -0600240 """
manasarm5dedfaf2018-02-07 14:54:54 +0530241
George Keishing74777bd2017-02-07 01:43:38 -0600242 bmc_version = ""
243 bmc_platform = ""
244 with open(xml_file_path, 'rt') as output:
245 tree = ElementTree.parse(output)
246
247 for node in tree.iter('msg'):
248 # /etc/os-release output is logged in the XML as msg
249 # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
250 if '${output} = VERSION_ID=' in node.text:
251 # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
252 bmc_version = str(node.text.split("VERSION_ID=")[1])[1:-1]
253
254 # Platform is logged in the XML as msg.
255 # Example: ${bmc_model} = Witherspoon BMC
256 if '${bmc_model} = ' in node.text:
257 bmc_platform = node.text.split(" = ")[1]
258
259 print "BMC Version:", bmc_version
260 print "BMC Platform:", bmc_platform
George Keishing3f2d1922017-03-07 00:30:44 -0600261 return [str(bmc_version), str(bmc_platform)]
George Keishing74777bd2017-02-07 01:43:38 -0600262
263
manasarm5dedfaf2018-02-07 14:54:54 +0530264def main():
George Keishing9fbdf792016-10-18 06:16:09 -0500265
manasarm5dedfaf2018-02-07 14:54:54 +0530266 if not gen_get_options(parser, stock_list):
267 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500268
manasarm5dedfaf2018-02-07 14:54:54 +0530269 if not validate_parms():
270 return False
George Keishing9fbdf792016-10-18 06:16:09 -0500271
manasarm5dedfaf2018-02-07 14:54:54 +0530272 qprint_pgm_header()
George Keishing9fbdf792016-10-18 06:16:09 -0500273
274 parse_output_xml(source, dest)
275
manasarm5dedfaf2018-02-07 14:54:54 +0530276 return True
277
278
279# Main
280
281if not main():
282 exit(1)