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