1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
from __future__ import annotations
import argparse
import json
import os
import xml.etree.ElementTree as ET
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Generator
from tools.stats.upload_stats_lib import (
download_s3_artifacts,
is_rerun_disabled_tests,
unzip,
upload_workflow_stats_to_s3,
)
from tools.stats.upload_test_stats import process_xml_element
TESTCASE_TAG = "testcase"
SEPARATOR = ";"
def process_report(
report: Path,
) -> dict[str, dict[str, int]]:
"""
Return a list of disabled tests that should be re-enabled and those that are still
flaky (failed or skipped)
"""
root = ET.parse(report)
# All rerun tests from a report are grouped here:
#
# * Success test should be re-enable if it's green after rerunning in all platforms
# where it is currently disabled
# * Failures from pytest because pytest-flakefinder is used to run the same test
# multiple times, some could fails
# * Skipped tests from unittest
#
# We want to keep track of how many times the test fails (num_red) or passes (num_green)
all_tests: dict[str, dict[str, int]] = {}
for test_case in root.iter(TESTCASE_TAG):
parsed_test_case = process_xml_element(test_case)
# Under --rerun-disabled-tests mode, a test is skipped when:
# * it's skipped explicitly inside PyTorch code
# * it's skipped because it's a normal enabled test
# * or it's falky (num_red > 0 and num_green > 0)
# * or it's failing (num_red > 0 and num_green == 0)
#
# We care only about the latter two here
skipped = parsed_test_case.get("skipped", None)
# NB: Regular ONNX tests could return a list of subskips here where each item in the
# list is a skipped message. In the context of rerunning disabled tests, we could
# ignore this case as returning a list of subskips only happens when tests are run
# normally
if skipped and (
type(skipped) is list or "num_red" not in skipped.get("message", "")
):
continue
name = parsed_test_case.get("name", "")
classname = parsed_test_case.get("classname", "")
filename = parsed_test_case.get("file", "")
if not name or not classname or not filename:
continue
# Check if the test is a failure
failure = parsed_test_case.get("failure", None)
disabled_test_id = SEPARATOR.join([name, classname, filename])
if disabled_test_id not in all_tests:
all_tests[disabled_test_id] = {
"num_green": 0,
"num_red": 0,
}
# Under --rerun-disabled-tests mode, if a test is not skipped or failed, it's
# counted as a success. Otherwise, it's still flaky or failing
if skipped:
try:
stats = json.loads(skipped.get("message", ""))
except json.JSONDecodeError:
stats = {}
all_tests[disabled_test_id]["num_green"] += stats.get("num_green", 0)
all_tests[disabled_test_id]["num_red"] += stats.get("num_red", 0)
elif failure:
# As a failure, increase the failure count
all_tests[disabled_test_id]["num_red"] += 1
else:
all_tests[disabled_test_id]["num_green"] += 1
return all_tests
def get_test_reports(
repo: str, workflow_run_id: int, workflow_run_attempt: int
) -> Generator[Path, None, None]:
"""
Gather all the test reports from S3 and GHA. It is currently not possible to guess which
test reports are from rerun_disabled_tests workflow because the name doesn't include the
test config. So, all reports will need to be downloaded and examined
"""
with TemporaryDirectory() as temp_dir:
print("Using temporary directory:", temp_dir)
os.chdir(temp_dir)
artifact_paths = download_s3_artifacts(
"test-reports", workflow_run_id, workflow_run_attempt
)
for path in artifact_paths:
unzip(path)
yield from Path(".").glob("**/*.xml")
def get_disabled_test_name(test_id: str) -> tuple[str, str, str, str]:
"""
Follow flaky bot convention here, if that changes, this will also need to be updated
"""
name, classname, filename = test_id.split(SEPARATOR)
return f"{name} (__main__.{classname})", name, classname, filename
def prepare_record(
workflow_id: int,
workflow_run_attempt: int,
name: str,
classname: str,
filename: str,
flaky: bool,
num_red: int = 0,
num_green: int = 0,
) -> tuple[Any, dict[str, Any]]:
"""
Prepare the record to save onto S3
"""
key = (
workflow_id,
workflow_run_attempt,
name,
classname,
filename,
)
record = {
"workflow_id": workflow_id,
"workflow_run_attempt": workflow_run_attempt,
"name": name,
"classname": classname,
"filename": filename,
"flaky": flaky,
"num_green": num_green,
"num_red": num_red,
}
return key, record
def save_results(
workflow_id: int,
workflow_run_attempt: int,
all_tests: dict[str, dict[str, int]],
) -> None:
"""
Save the result to S3, which then gets put into the HUD backened database
"""
should_be_enabled_tests = {
name: stats
for name, stats in all_tests.items()
if "num_green" in stats
and stats["num_green"]
and "num_red" in stats
and stats["num_red"] == 0
}
still_flaky_tests = {
name: stats
for name, stats in all_tests.items()
if name not in should_be_enabled_tests
}
records = {}
for test_id, stats in all_tests.items():
num_green = stats.get("num_green", 0)
num_red = stats.get("num_red", 0)
disabled_test_name, name, classname, filename = get_disabled_test_name(test_id)
key, record = prepare_record(
workflow_id=workflow_id,
workflow_run_attempt=workflow_run_attempt,
name=name,
classname=classname,
filename=filename,
flaky=test_id in still_flaky_tests,
num_green=num_green,
num_red=num_red,
)
records[key] = record
# Log the results
print(f"The following {len(should_be_enabled_tests)} tests should be re-enabled:")
for test_id, stats in should_be_enabled_tests.items():
disabled_test_name, name, classname, filename = get_disabled_test_name(test_id)
print(f" {disabled_test_name} from {filename}")
print(f"The following {len(still_flaky_tests)} are still flaky:")
for test_id, stats in still_flaky_tests.items():
num_green = stats.get("num_green", 0)
num_red = stats.get("num_red", 0)
disabled_test_name, name, classname, filename = get_disabled_test_name(test_id)
print(
f" {disabled_test_name} from {filename}, failing {num_red}/{num_red + num_green}"
)
upload_workflow_stats_to_s3(
workflow_id,
workflow_run_attempt,
"rerun_disabled_tests",
list(records.values()),
)
def main(repo: str, workflow_run_id: int, workflow_run_attempt: int) -> None:
"""
Find the list of all disabled tests that should be re-enabled
"""
# Aggregated across all jobs
all_tests: dict[str, dict[str, int]] = {}
for report in get_test_reports(
args.repo, args.workflow_run_id, args.workflow_run_attempt
):
tests = process_report(report)
# The scheduled workflow has both rerun disabled tests and memory leak check jobs.
# We are only interested in the former here
if not is_rerun_disabled_tests(tests):
continue
for name, stats in tests.items():
if name not in all_tests:
all_tests[name] = stats.copy()
else:
all_tests[name]["num_green"] += stats.get("num_green", 0)
all_tests[name]["num_red"] += stats.get("num_red", 0)
save_results(
workflow_run_id,
workflow_run_attempt,
all_tests,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Upload test artifacts from GHA to S3")
parser.add_argument(
"--workflow-run-id",
type=int,
required=True,
help="id of the workflow to get artifacts from",
)
parser.add_argument(
"--workflow-run-attempt",
type=int,
required=True,
help="which retry of the workflow this is",
)
parser.add_argument(
"--repo",
type=str,
required=True,
help="which GitHub repo this workflow run belongs to",
)
args = parser.parse_args()
main(args.repo, args.workflow_run_id, args.workflow_run_attempt)
|