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 | |
| 52 | # Populate stock_list with options we want. |
| 53 | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] |
| 54 | |
| 55 | |
| 56 | def 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 | |
| 71 | def 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 | |
| 90 | def 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 106 | |
| 107 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 108 | def parse_output_xml(xml_file_path, csv_dir_path): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 109 | |
| 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 121 | result = ExecutionResult(xml_file_path) |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 122 | 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 148 | |
| 149 | # Default Test data |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 150 | l_subsys = 'OPENBMC' |
| 151 | l_test_type = 'FTC' |
| 152 | l_pse_rel = 'OBMC910' |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 153 | 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 166 | |
| 167 | # Default header |
| 168 | l_header = ['test_start', 'test_end', 'subsys', 'test_type', |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 169 | 'test_result', 'test_name', 'pse_rel', 'driver', |
| 170 | 'env', 'proc', 'platform_type', 'test_func_area'] |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 171 | |
| 172 | l_csvlist.append(l_header) |
| 173 | |
| 174 | # Generate CSV file onto the path with current time stamp |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 175 | l_base_dir = csv_dir_path |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 176 | l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S") |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 177 | # Example: 2017-02-20-08-47-22_Witherspoon.csv |
| 178 | l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv" |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 179 | |
| 180 | print "Writing data into csv file:", l_csvfile |
| 181 | |
| 182 | for testcase in collectDataObj.testData: |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 183 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 187 | |
| 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 194 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 197 | # Data Sequence: 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_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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 203 | l_csvlist.append(l_data) |
| 204 | |
George Keishing | a96e27c | 2016-12-04 23:05:04 -0600 | [diff] [blame] | 205 | # 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 Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 210 | |
| 211 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 212 | def xml_to_csv_time(xml_datetime): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 213 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 214 | 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 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 218 | 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 Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 221 | |
| 222 | The date returned will be in the following format: %Y-%m-%d-%H-%M-%S |
| 223 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 224 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 225 | # 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 | |
| 232 | def get_system_details(xml_file_path): |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 233 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 234 | r""" |
| 235 | 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] | 236 | The list returned will be in the following order: [driver,platform] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 237 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 238 | Description of argument(s): |
| 239 | 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] | 240 | """ |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 241 | |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 242 | 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 Keishing | 3f2d192 | 2017-03-07 00:30:44 -0600 | [diff] [blame] | 261 | return [str(bmc_version), str(bmc_platform)] |
George Keishing | 74777bd | 2017-02-07 01:43:38 -0600 | [diff] [blame] | 262 | |
| 263 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 264 | def main(): |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 265 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 266 | if not gen_get_options(parser, stock_list): |
| 267 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 268 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 269 | if not validate_parms(): |
| 270 | return False |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 271 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 272 | qprint_pgm_header() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 273 | |
| 274 | parse_output_xml(source, dest) |
| 275 | |
manasarm | 5dedfaf | 2018-02-07 14:54:54 +0530 | [diff] [blame] | 276 | return True |
| 277 | |
| 278 | |
| 279 | # Main |
| 280 | |
| 281 | if not main(): |
| 282 | exit(1) |