Matteo Scandolo | 142e627 | 2020-04-29 17:36:59 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import argparse |
| 16 | import os |
| 17 | import xml.etree.ElementTree as ET |
| 18 | from datetime import datetime |
| 19 | |
| 20 | dash = "-" * 75 |
| 21 | double_dash = "=" * 75 |
| 22 | |
| 23 | |
| 24 | def cut_string(str): |
| 25 | return (str[:48] + "..") if len(str) > 50 else str |
| 26 | |
| 27 | def format_key(str): |
| 28 | return str.replace("plot-", "").replace("-", " ") |
| 29 | |
| 30 | def read_file(file, plot_folder): |
| 31 | # create element tree object |
| 32 | tree = ET.parse(file) |
| 33 | |
| 34 | # get root element |
| 35 | root = tree.getroot() |
| 36 | |
| 37 | results = {} |
| 38 | |
Matteo Scandolo | 3022859 | 2020-05-08 15:25:56 -0700 | [diff] [blame] | 39 | |
| 40 | startTimer = 0 |
Matteo Scandolo | 142e627 | 2020-04-29 17:36:59 -0700 | [diff] [blame] | 41 | print(double_dash) |
| 42 | print("{:<50}{:>10}{:>15}".format("Test Name", "Status", "Duration (s)")) |
| 43 | print(double_dash) |
| 44 | for test in root.findall("./suite/test"): |
| 45 | status = test.find(".status") |
| 46 | name = test.attrib["name"] |
| 47 | start = status.attrib["starttime"] |
| 48 | end = status.attrib["endtime"] |
| 49 | s = datetime.strptime(start[:-4], "%Y%m%d %H:%M:%S") |
| 50 | e = datetime.strptime(end[:-4], "%Y%m%d %H:%M:%S") |
| 51 | diff = e - s |
Matteo Scandolo | 3022859 | 2020-05-08 15:25:56 -0700 | [diff] [blame] | 52 | time = startTimer + diff.seconds |
| 53 | print("{:<50}{:>10}{:>15}".format(cut_string(name), status.attrib["status"], time)) |
Matteo Scandolo | 142e627 | 2020-04-29 17:36:59 -0700 | [diff] [blame] | 54 | print(dash) |
| 55 | |
| 56 | # check if the test has a tag that starts with "plot-", |
| 57 | # if so store the result to create plot files for Jenkins |
| 58 | for tag in test.findall("./tags/tag"): |
| 59 | if "plot-" in tag.text: |
Matteo Scandolo | 3022859 | 2020-05-08 15:25:56 -0700 | [diff] [blame] | 60 | results[tag.text] = time |
| 61 | startTimer = time |
Matteo Scandolo | 142e627 | 2020-04-29 17:36:59 -0700 | [diff] [blame] | 62 | |
| 63 | if not os.path.isdir(plot_folder): |
| 64 | os.mkdir(plot_folder) |
| 65 | |
| 66 | for k, v in results.items(): |
| 67 | f = open("%s/%s.txt" % (plot_folder, k), "a") |
| 68 | f.write("%s\n" % format_key(k)) |
| 69 | f.write(str(v)) |
| 70 | f.close() |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | parser = argparse.ArgumentParser(prog="collect-result") |
| 75 | parser.add_argument("-r", "--robot-output", help="he robot output.xml file to process", default="output.xml"), |
| 76 | parser.add_argument("-p", "--plot-folder", help="here to output the files needed for the Jenkins plots", default="plots") |
| 77 | |
| 78 | args = parser.parse_args() |
| 79 | |
| 80 | read_file(args.robot_output, args.plot_folder) |