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', |
| 60 | help='Openbmc platform which was used during test,\ |
| 61 | e.g. "Witherspoon".') |
| 62 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 63 | # Populate stock_list with options we want. |
| 64 | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] |
| 65 | |
| 66 | |
| 67 | def exit_function(signal_number=0, |
| 68 | frame=None): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 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): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 83 | 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 | |
| 99 | def validate_parms(): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 100 | 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 114 | |
| 115 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 116 | def parse_output_xml(xml_file_path, csv_dir_path, version_id, platform): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 117 | 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): |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 123 | 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. |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 129 | """ |
| 130 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 131 | result = ExecutionResult(xml_file_path) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 132 | 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 158 | |
| 159 | # Default Test data |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 160 | l_subsys = 'OPENBMC' |
| 161 | l_test_type = 'FTC' |
| 162 | l_pse_rel = 'OBMC910' |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 163 | l_env = 'HW' |
| 164 | l_proc = 'P9' |
| 165 | l_platform_type = "" |
| 166 | l_func_area = "" |
| 167 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame^] | 168 | # System data from XML meta data |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 169 | #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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 178 | else: |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 179 | # 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 191 | sys.exit() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 192 | |
| 193 | # Default header |
| 194 | l_header = ['test_start', 'test_end', 'subsys', 'test_type', |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 195 | 'test_result', 'test_name', 'pse_rel', 'driver', |
| 196 | 'env', 'proc', 'platform_type', 'test_func_area'] |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 197 | |
| 198 | l_csvlist.append(l_header) |
| 199 | |
| 200 | # Generate CSV file onto the path with current time stamp |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 201 | l_base_dir = csv_dir_path |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 202 | l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S") |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 203 | # Example: 2017-02-20-08-47-22_Witherspoon.csv |
| 204 | l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv" |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 205 | |
| 206 | print "Writing data into csv file:", l_csvfile |
| 207 | |
| 208 | for testcase in collectDataObj.testData: |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 209 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 213 | |
| 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 220 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 223 | # Data Sequence: test_start,test_end,subsys,test_type, |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 224 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 229 | l_csvlist.append(l_data) |
| 230 | |
George Keishing | a96e27c | 2016-12-04 23:05:04 -0600 | [diff] [blame] | 231 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 236 | |
| 237 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 238 | def 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 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 243 | 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 246 | |
| 247 | The date returned will be in the following format: %Y-%m-%d-%H-%M-%S |
| 248 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 249 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 250 | # 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 | |
| 257 | def get_system_details(xml_file_path): |
| 258 | r""" |
| 259 | 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] | 260 | The list returned will be in the following order: [driver,platform] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 261 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 262 | Description of argument(s): |
| 263 | 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] | 264 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 265 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 266 | bmc_version_id = "" |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 267 | 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) |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 276 | bmc_version_id = str(node.text.split("VERSION_ID=")[1])[1:-1] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 277 | |
| 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 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 283 | print_vars(bmc_version_id, bmc_platform) |
| 284 | return [str(bmc_version_id), str(bmc_platform)] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 285 | |
| 286 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 287 | def main(): |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 288 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 289 | if not gen_get_options(parser, stock_list): |
| 290 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 291 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 292 | if not validate_parms(): |
| 293 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 294 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 295 | qprint_pgm_header() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 296 | |
manasarm | e4f79c9 | 2018-02-22 13:02:46 +0530 | [diff] [blame] | 297 | parse_output_xml(source, dest, version_id, platform) |
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 | return True |
| 300 | |
| 301 | |
| 302 | # Main |
| 303 | |
| 304 | if not main(): |
| 305 | exit(1) |