manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 3 | r""" |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 4 | Use robot framework API to extract test result data from output.xml generated |
| 5 | by robot tests. For more information on the Robot Framework API, see |
| 6 | http://robot-framework.readthedocs.io/en/3.0/autodoc/robot.result.html |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 7 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 8 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 9 | import sys |
| 10 | import os |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 11 | import getopt |
| 12 | import csv |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 13 | import robot.errors |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 14 | import re |
| 15 | from datetime import datetime |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 16 | from robot.api import ExecutionResult |
| 17 | from robot.result.visitor import ResultVisitor |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 18 | from xml.etree import ElementTree |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 19 | |
| 20 | # Remove the python library path to restore with local project path later. |
| 21 | save_path_0 = sys.path[0] |
| 22 | del sys.path[0] |
| 23 | sys.path.append(os.path.join(os.path.dirname(__file__), "../../../lib")) |
| 24 | |
| 25 | from gen_arg import * |
| 26 | from gen_print import * |
| 27 | from gen_valid import * |
| 28 | |
| 29 | # Restore sys.path[0]. |
| 30 | sys.path.insert(0, save_path_0) |
| 31 | |
| 32 | parser = 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 | |
| 43 | parser.add_argument( |
| 44 | '--source', |
| 45 | '-s', |
| 46 | help='The output.xml robot test result file path.') |
| 47 | |
| 48 | parser.add_argument( |
| 49 | '--dest', |
| 50 | '-d', |
| 51 | help='The directory path where the generated .csv files will go.') |
| 52 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 53 | parser.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 | |
| 58 | parser.add_argument( |
| 59 | '--platform', |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 60 | help='OpenBMC platform which was used during test,\ |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 61 | e.g. "Witherspoon".') |
| 62 | |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 63 | parser.add_argument( |
| 64 | '--level', |
| 65 | help='OpenBMC release level which was used during test,\ |
| 66 | e.g. "OBMC920".') |
| 67 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 68 | # Populate stock_list with options we want. |
| 69 | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] |
| 70 | |
| 71 | |
| 72 | def exit_function(signal_number=0, |
| 73 | frame=None): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 74 | r""" |
| 75 | Execute whenever the program ends normally or with the signals that we |
| 76 | catch (i.e. TERM, INT). |
| 77 | """ |
| 78 | |
| 79 | dprint_executing() |
| 80 | |
| 81 | dprint_var(signal_number) |
| 82 | |
| 83 | qprint_pgm_footer() |
| 84 | |
| 85 | |
| 86 | def signal_handler(signal_number, |
| 87 | frame): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 88 | r""" |
| 89 | Handle signals. Without a function to catch a SIGTERM or SIGINT, the |
| 90 | program would terminate immediately with return code 143 and without |
| 91 | calling the exit_function. |
| 92 | """ |
| 93 | |
| 94 | # Our convention is to set up exit_function with atexit.register() so |
| 95 | # there is no need to explicitly call exit_function from here. |
| 96 | |
| 97 | dprint_executing() |
| 98 | |
| 99 | # Calling exit prevents us from returning to the code that was running |
| 100 | # when the signal was received. |
| 101 | exit(0) |
| 102 | |
| 103 | |
| 104 | def validate_parms(): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 105 | r""" |
| 106 | Validate program parameters, etc. Return True or False (i.e. pass/fail) |
| 107 | accordingly. |
| 108 | """ |
| 109 | |
| 110 | if not valid_file_path(source): |
| 111 | return False |
| 112 | |
| 113 | if not valid_dir_path(dest): |
| 114 | return False |
| 115 | |
| 116 | gen_post_validation(exit_function, signal_handler) |
| 117 | |
| 118 | return True |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 119 | |
| 120 | |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 121 | def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform, level): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 122 | r""" |
| 123 | Parse the robot-generated output.xml file and extract various test |
| 124 | output data. Put the extracted information into a csv file in the "dest" |
| 125 | folder. |
| 126 | |
| 127 | Description of argument(s): |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 128 | xml_file_path The path to a Robot-generated output.xml file. |
| 129 | csv_dir_path The path to the directory that is to contain the .csv files |
| 130 | generated by this function. |
| 131 | version_id Version of the openbmc firmware |
| 132 | (e.g. "v2.1-215-g6e7eacb"). |
| 133 | platform Platform of the openbmc system. |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 134 | level Release level of the OpenBMC system (e.g. "OBMC920"). |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 135 | """ |
| 136 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 137 | result = ExecutionResult(xml_file_path) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 138 | result.configure(stat_config={'suite_stat_level': 2, |
| 139 | 'tag_stat_combine': 'tagANDanother'}) |
| 140 | |
| 141 | stats = result.statistics |
| 142 | print "--------------------------------------" |
| 143 | print "Total Test Count:\t",\ |
| 144 | stats.total.critical.passed + stats.total.critical.failed |
| 145 | print "Total Test Failed:\t", stats.total.critical.failed |
| 146 | print "Total Test Passed:\t", stats.total.critical.passed |
| 147 | print "Test Start Time:\t", result.suite.starttime |
| 148 | print "Test End Time:\t\t", result.suite.endtime |
| 149 | print "--------------------------------------" |
| 150 | |
| 151 | # Use ResultVisitor object and save off the test data info |
| 152 | class TestResult(ResultVisitor): |
| 153 | def __init__(self): |
| 154 | self.testData = [] |
| 155 | |
| 156 | def visit_test(self, test): |
| 157 | self.testData += [test] |
| 158 | |
| 159 | collectDataObj = TestResult() |
| 160 | result.visit(collectDataObj) |
| 161 | |
| 162 | # Write the result statistics attributes to CSV file |
| 163 | l_csvlist = [] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 164 | |
| 165 | # Default Test data |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 166 | l_subsys = 'OPENBMC' |
| 167 | l_test_type = 'FTC' |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 168 | |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 169 | l_pse_rel = 'OBMC910' |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 170 | if level: |
| 171 | l_pse_rel = level |
| 172 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 173 | l_env = 'HW' |
| 174 | l_proc = 'P9' |
| 175 | l_platform_type = "" |
| 176 | l_func_area = "" |
| 177 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 178 | # System data from XML meta data |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 179 | #l_system_info = get_system_details(xml_file_path) |
| 180 | |
| 181 | # First let us try to collect information from keyboard input |
| 182 | # If keyboard input cannot give both information, then find from xml file. |
| 183 | if version_id and platform: |
| 184 | l_driver = version_id |
| 185 | l_platform_type = platform |
| 186 | print "BMC Version_id:", version_id |
| 187 | print "BMC Platform:", platform |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 188 | else: |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 189 | # System data from XML meta data |
| 190 | l_system_info = get_system_details(xml_file_path) |
| 191 | l_driver = l_system_info[0] |
| 192 | l_platform_type = l_system_info[1] |
| 193 | |
| 194 | # Driver version id and platform are mandatorily required for CSV file |
| 195 | # generation. If any one is not avaulable, exit CSV file generation process. |
| 196 | if l_driver and l_platform_type: |
| 197 | print "Driver and system info set." |
| 198 | else: |
| 199 | print "\ |
| 200 | Both driver and system info need to be set. CSV file is not generated." |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 201 | sys.exit() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 202 | |
| 203 | # Default header |
| 204 | l_header = ['test_start', 'test_end', 'subsys', 'test_type', |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 205 | 'test_result', 'test_name', 'pse_rel', 'driver', |
| 206 | 'env', 'proc', 'platform_type', 'test_func_area'] |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 207 | |
| 208 | l_csvlist.append(l_header) |
| 209 | |
| 210 | # Generate CSV file onto the path with current time stamp |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 211 | l_base_dir = csv_dir_path |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 212 | l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S") |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 213 | # Example: 2017-02-20-08-47-22_Witherspoon.csv |
| 214 | l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv" |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 215 | |
| 216 | print "Writing data into csv file:", l_csvfile |
| 217 | |
| 218 | for testcase in collectDataObj.testData: |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 219 | # Functional Area: Suite Name |
| 220 | # Test Name: Test Case Name |
| 221 | l_func_area = str(testcase.parent).split(' ', 1)[1] |
| 222 | l_test_name = str(testcase) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 223 | |
| 224 | # Test Result pass=0 fail=1 |
| 225 | if testcase.status == 'PASS': |
| 226 | l_test_result = 0 |
| 227 | else: |
| 228 | l_test_result = 1 |
| 229 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 230 | # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S" |
| 231 | l_stime = xml_to_csv_time(testcase.starttime) |
| 232 | l_etime = xml_to_csv_time(testcase.endtime) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 233 | # Data Sequence: test_start,test_end,subsys,test_type, |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 234 | # test_result,test_name,pse_rel,driver, |
| 235 | # env,proc,platform_tyep,test_func_area, |
| 236 | l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result, |
| 237 | l_test_name, l_pse_rel, l_driver, l_env, l_proc, |
| 238 | l_platform_type, l_func_area] |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 239 | l_csvlist.append(l_data) |
| 240 | |
George Keishing | a96e27c | 2016-12-04 23:05:04 -0600 | [diff] [blame] | 241 | # Open the file and write to the CSV file |
| 242 | l_file = open(l_csvfile, "w") |
| 243 | l_writer = csv.writer(l_file, lineterminator='\n') |
| 244 | l_writer.writerows(l_csvlist) |
| 245 | l_file.close() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 246 | |
| 247 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 248 | def xml_to_csv_time(xml_datetime): |
| 249 | r""" |
| 250 | Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format |
| 251 | and return it. |
| 252 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 253 | Description of argument(s): |
| 254 | datetime The date in the following format: %Y%m%d %H:%M:%S.%f |
| 255 | (This is the format typically found in an XML file.) |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 256 | |
| 257 | The date returned will be in the following format: %Y-%m-%d-%H-%M-%S |
| 258 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 259 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 260 | # 20170206 05:05:19.342 |
| 261 | l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f") |
| 262 | # 2017-02-06-05-05-19 |
| 263 | l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S") |
| 264 | return str(l_str) |
| 265 | |
| 266 | |
| 267 | def get_system_details(xml_file_path): |
| 268 | r""" |
| 269 | Get the system data from output.xml generated by robot and return it. |
Gunnar Mills | 28e403b | 2017-10-25 16:16:38 -0500 | [diff] [blame] | 270 | The list returned will be in the following order: [driver,platform] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 271 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 272 | Description of argument(s): |
| 273 | xml_file_path The relative or absolute path to the output.xml file. |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 274 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 275 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 276 | bmc_version_id = "" |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 277 | bmc_platform = "" |
| 278 | with open(xml_file_path, 'rt') as output: |
| 279 | tree = ElementTree.parse(output) |
| 280 | |
| 281 | for node in tree.iter('msg'): |
| 282 | # /etc/os-release output is logged in the XML as msg |
| 283 | # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty" |
| 284 | if '${output} = VERSION_ID=' in node.text: |
| 285 | # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty) |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 286 | bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 287 | |
| 288 | # Platform is logged in the XML as msg. |
| 289 | # Example: ${bmc_model} = Witherspoon BMC |
| 290 | if '${bmc_model} = ' in node.text: |
| 291 | bmc_platform = node.text.split(" = ")[1] |
| 292 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 293 | print_vars(bmc_version_id, bmc_platform) |
| 294 | return [str(bmc_version_id), str(bmc_platform)] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 295 | |
| 296 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 297 | def main(): |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 298 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 299 | if not gen_get_options(parser, stock_list): |
| 300 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 301 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 302 | if not validate_parms(): |
| 303 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 304 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 305 | qprint_pgm_header() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 306 | |
George Keishing | 19533d5 | 2018-04-09 03:08:03 -0500 | [diff] [blame] | 307 | parse_output_xml(source, dest, version_id, platform, level) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 308 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 309 | return True |
| 310 | |
| 311 | |
| 312 | # Main |
| 313 | |
| 314 | if not main(): |
| 315 | exit(1) |