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