File size: 36,269 Bytes
3f4bb1d | 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 | # Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
import contextlib
import os
import select
import signal
import subprocess
import sys
import time
import pytest
from junitparser import Error, JUnitXml, TestCase, TestSuite
from prettytable import PrettyTable
# Local imports
import test_settings as test_settings # isort: skip
def pytest_ignore_collect(collection_path, config):
# Skip collection and run each test script individually
return True
COLD_CACHE_BUFFER = 700
"""Extra seconds added to the first camera-enabled test's hard timeout.
The first test that uses ``enable_cameras=True`` may compile shaders during its
run (~600 s). This buffer prevents that from being misreported as a test
timeout. Only the first such test gets the extension — after it runs, the
on-disk cache is populated.
"""
STARTUP_DEADLINE = 120
"""Seconds to wait for AppLauncher init or pytest collection before declaring a
startup hang.
AppLauncher prints ``[ISAACLAB] AppLauncher initialization complete`` to
``sys.__stderr__`` (never suppressed) when Kit finishes initializing, and pytest
prints ``collected N items`` to stdout after collection. If neither appears
within this deadline the process is treated as hung. Kit startup can exceed
60 s on cold CI workers, so this catches real startup hangs without killing
legitimate slow launches.
"""
STARTUP_HANG_RETRIES = 2
"""Number of times to retry a test that hangs during startup before giving up."""
TIMEOUT_RETRIES = 0
"""Number of times to retry a test that reaches its hard timeout before giving up."""
PROCESS_FAILURE_RETRIES_BY_FILE = {
"test_visualizer_integration_physx.py": 4,
"test_visualizer_integration_newton.py": 4,
"test_visualizer_tiled_integration_physx.py": 4,
"test_visualizer_tiled_integration_newton.py": 4,
}
"""Extra fresh-process attempts for visualizer tests that can enter stale render states."""
SHUTDOWN_GRACE_PERIOD = 30
"""Seconds to wait for clean exit after the JUnit XML report file appears.
When a test completes and writes its JUnit report, the subprocess may hang
during ``SimulationApp.close()`` or Kit shutdown. Rather than wasting the
full hard timeout, we give the process a short grace period to exit, then
kill it. The test results are taken from the report file (pass/fail), not
from the kill.
"""
def capture_test_output_with_timeout(cmd, timeout, env, startup_deadline=0, report_file=""):
"""Run a command with timeout and capture all output while streaming in real-time.
Args:
cmd: Command to execute.
timeout: Maximum wall-clock seconds before the process is killed.
env: Environment variables for the subprocess.
startup_deadline: If > 0, the process is killed early when neither
``AppLauncher initialization complete`` (stderr) nor ``collected``
(stdout) appears within this many seconds.
report_file: Path to the JUnit XML report file. When set, the process
is given only :data:`SHUTDOWN_GRACE_PERIOD` seconds to exit after
the file appears on disk.
Returns:
Tuple of ``(returncode, stdout_bytes, stderr_bytes, kill_reason,
wall_time, pre_kill_diag)``. *kill_reason* is ``""`` for normal exits,
``"timeout"`` for hard timeouts, ``"startup_hang"`` when the process
did not reach pytest collection in time, or ``"shutdown_hang"`` when
the test completed but the process hung during shutdown.
"""
stdout_data = b""
stderr_data = b""
process = None
try:
# Each test gets its own session so orphaned Kit/Isaac Sim child
# processes cannot send SIGHUP to the next test's process group.
process = subprocess.Popen(
cmd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0,
universal_newlines=False,
start_new_session=True,
)
pgid = os.getpgid(process.pid)
stdout_fd = process.stdout.fileno()
stderr_fd = process.stderr.fileno()
try:
import fcntl
for fd in [stdout_fd, stderr_fd]:
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
except ImportError:
pass
start_time = time.time()
startup_done = startup_deadline <= 0
shutdown_deadline = 0.0
while process.poll() is None:
elapsed = time.time() - start_time
if not startup_done:
if b"AppLauncher initialization complete" in stderr_data or b"collected " in stdout_data:
startup_done = True
if report_file and not shutdown_deadline and os.path.exists(report_file):
shutdown_deadline = time.time() + SHUTDOWN_GRACE_PERIOD
kill_reason = None
if not startup_done and elapsed > startup_deadline:
kill_reason = "startup_hang"
elif shutdown_deadline and time.time() > shutdown_deadline:
kill_reason = "shutdown_hang"
elif elapsed > timeout:
kill_reason = "timeout"
if kill_reason:
pre_kill_diag = _capture_system_diagnostics()
# Kill the entire process group (test + any Kit children).
try:
os.killpg(pgid, signal.SIGKILL)
except (ProcessLookupError, PermissionError, OSError):
process.kill()
try:
remaining_stdout, remaining_stderr = process.communicate(timeout=5)
stdout_data += remaining_stdout
stderr_data += remaining_stderr
except subprocess.TimeoutExpired:
pass
wall_time = time.time() - start_time
return -1, stdout_data, stderr_data, kill_reason, wall_time, pre_kill_diag
try:
ready_fds, _, _ = select.select([stdout_fd, stderr_fd], [], [], 0.1)
for fd in ready_fds:
with contextlib.suppress(OSError):
if fd == stdout_fd:
chunk = process.stdout.read(1024)
if chunk:
stdout_data += chunk
sys.stdout.buffer.write(chunk)
sys.stdout.buffer.flush()
elif fd == stderr_fd:
chunk = process.stderr.read(1024)
if chunk:
stderr_data += chunk
sys.stderr.buffer.write(chunk)
sys.stderr.buffer.flush()
except OSError:
time.sleep(0.1)
continue
# Drain any output the process wrote before or just after exiting.
try:
remaining_stdout, remaining_stderr = process.communicate(timeout=10)
stdout_data += remaining_stdout
stderr_data += remaining_stderr
except Exception:
pass
# Kill any orphaned child processes (Kit, Isaac Sim) left by the test.
try:
os.killpg(pgid, signal.SIGKILL)
time.sleep(1)
except (ProcessLookupError, PermissionError, OSError):
pass
wall_time = time.time() - start_time
return process.returncode, stdout_data, stderr_data, "", wall_time, ""
except Exception as e:
if process is not None and process.poll() is None:
process.kill()
with contextlib.suppress(Exception):
rem_out, rem_err = process.communicate(timeout=5)
stdout_data += rem_out
stderr_data += rem_err
stdout_data += f"\n[capture error: {e}]\n".encode()
return -1, stdout_data, stderr_data, "", 0.0, ""
_SIGNAL_DESCRIPTIONS = {
1: "SIGHUP — session leader exit or orphaned process cleanup",
6: "SIGABRT",
9: "SIGKILL — likely OOM killed",
11: "SIGSEGV — segmentation fault",
15: "SIGTERM",
}
def _signal_description(sig):
"""Return a human-readable description for a process killed by a signal."""
base = f"Process killed by signal {sig}"
desc = _SIGNAL_DESCRIPTIONS.get(sig)
return f"{base} ({desc})" if desc else base
def _create_error_report(prefix, file_name, message, details):
"""Create a JUnit XML error report for a test that failed to produce its own.
Returns a :class:`JUnitXml` object ready to be written to disk.
"""
suite_name = os.path.splitext(file_name)[0]
suite = TestSuite(name=f"{prefix}_{suite_name}")
case = TestCase(name="test_execution", classname=suite_name)
error = Error(message=message)
error.text = details
case.result = error
suite.add_testcase(case)
report = JUnitXml()
report.add_testsuite(suite)
return report
def _get_diagnostics(pre_kill_diag=""):
"""Return system diagnostics, truncated to 10 000 chars."""
diag = pre_kill_diag or _capture_system_diagnostics()
if len(diag) > 10000:
diag = diag[:10000] + "\n... (truncated)"
return diag
def _capture_system_diagnostics():
"""Capture system diagnostics (GPU, memory, processes) for crash investigation.
All errors are caught and reported inline so this never raises.
"""
sections = []
try:
r = subprocess.run(["nvidia-smi"], capture_output=True, text=True, timeout=10)
if r.stdout:
sections.append(f"--- nvidia-smi ---\n{r.stdout.strip()}")
except Exception as e:
sections.append(f"--- nvidia-smi --- FAILED: {e}")
try:
with open("/proc/meminfo") as f:
lines = f.readlines()
keys = ("MemTotal", "MemFree", "MemAvailable", "Committed_AS", "SwapTotal", "SwapFree")
relevant = [line.strip() for line in lines if any(line.startswith(k) for k in keys)]
if relevant:
sections.append("--- /proc/meminfo ---\n" + "\n".join(relevant))
except Exception as e:
sections.append(f"--- /proc/meminfo --- FAILED: {e}")
cgroup_lines = []
for path in (
"/sys/fs/cgroup/memory.current",
"/sys/fs/cgroup/memory.max",
"/sys/fs/cgroup/memory.events",
"/sys/fs/cgroup/memory/memory.usage_in_bytes",
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
"/sys/fs/cgroup/memory/memory.oom_control",
):
try:
with open(path) as f:
cgroup_lines.append(f"{path}: {f.read().strip()}")
except FileNotFoundError:
pass
except Exception as e:
cgroup_lines.append(f"{path}: FAILED ({e})")
if cgroup_lines:
sections.append("--- cgroup memory ---\n" + "\n".join(cgroup_lines))
try:
r = subprocess.run(["ps", "auxf"], capture_output=True, text=True, timeout=5)
if r.stdout:
sections.append(f"--- process tree (ps auxf) ---\n{r.stdout.strip()}")
except Exception as e:
sections.append(f"--- process tree --- FAILED: {e}")
try:
r = subprocess.run(["dmesg", "-T"], capture_output=True, text=True, timeout=5)
if r.stdout:
lines = r.stdout.strip().split("\n")
sections.append("--- dmesg (last 30 lines) ---\n" + "\n".join(lines[-30:]))
except Exception:
pass
return "\n\n".join(sections)
def _read_test_report(report_file, file_name):
"""Read a pytest JUnit report and return its summary fields."""
report = JUnitXml.fromfile(report_file)
for suite in report:
if suite.name == "pytest":
suite.name = os.path.splitext(file_name)[0]
report.write(report_file)
errors = int(report.errors) if report.errors is not None else 0
failures = int(report.failures) if report.failures is not None else 0
skipped = int(report.skipped) if report.skipped is not None else 0
tests = int(report.tests) if report.tests is not None else 0
time_elapsed = float(report.time) if report.time is not None else 0.0
return report, errors, failures, skipped, tests, time_elapsed
def _retry_failed_test_in_fresh_process(
*,
test_file,
file_name,
cmd,
timeout,
env,
startup_deadline,
report_file,
report,
errors,
failures,
skipped,
tests,
time_elapsed,
returncode,
stdout_data,
stderr_data,
kill_reason,
wall_time,
pre_kill_diag,
):
"""Retry selected failed test files in a fresh subprocess."""
has_test_failures = errors > 0 or failures > 0
process_failure_attempts = 0
max_process_failure_retries = PROCESS_FAILURE_RETRIES_BY_FILE.get(file_name, 0)
while has_test_failures and process_failure_attempts < max_process_failure_retries:
process_failure_attempts += 1
print(
f"⚠️ {test_file}: failed in subprocess"
f" (attempt {process_failure_attempts}/{max_process_failure_retries + 1}), retrying in fresh process..."
)
with contextlib.suppress(FileNotFoundError):
os.remove(report_file)
returncode, stdout_data, stderr_data, kill_reason, wall_time, pre_kill_diag = capture_test_output_with_timeout(
cmd, timeout, env, startup_deadline=startup_deadline, report_file=report_file
)
if not os.path.exists(report_file):
break
try:
report, errors, failures, skipped, tests, time_elapsed = _read_test_report(report_file, file_name)
has_test_failures = errors > 0 or failures > 0
except Exception as e:
print(f"Error reading retry test report {report_file}: {e}")
has_test_failures = True
errors = 1
failures = 0
skipped = 0
tests = 0
time_elapsed = 0.0
break
return (
report,
errors,
failures,
skipped,
tests,
time_elapsed,
returncode,
stdout_data,
stderr_data,
kill_reason,
wall_time,
pre_kill_diag,
has_test_failures,
)
def run_individual_tests(test_files, workspace_root, isaacsim_ci):
"""Run each test file separately, ensuring one finishes before starting the next."""
failed_tests = []
test_status = {}
xml_reports = []
cold_cache_applied = False
for test_file in test_files:
print(f"\n\n🚀 Running {test_file} independently...\n")
file_name = os.path.basename(test_file)
env = os.environ.copy()
env["PYTHONFAULTHANDLER"] = "1"
timeout = test_settings.PER_TEST_TIMEOUTS.get(file_name, test_settings.DEFAULT_TIMEOUT)
# Read the test file once for cold-cache check.
try:
with open(test_file) as fh:
test_content = fh.read()
except OSError:
test_content = ""
# The first camera-enabled test in a fresh container compiles shaders
# (~600 s). Give it extra time so that doesn't look like a test timeout.
is_cold_cache_test = not cold_cache_applied and "enable_cameras=True" in test_content
if is_cold_cache_test:
timeout += COLD_CACHE_BUFFER
cold_cache_applied = True
print(f"⏱️ Adding {COLD_CACHE_BUFFER}s cold-cache buffer (timeout now {timeout}s)")
extra = COLD_CACHE_BUFFER if is_cold_cache_test else 0
startup_deadline = min(timeout, STARTUP_DEADLINE + extra)
cmd = [
sys.executable,
"-m",
"pytest",
"-s",
"--no-header",
f"--config-file={workspace_root}/pyproject.toml",
f"--junitxml=tests/test-reports-{str(file_name)}.xml",
"--tb=short",
]
if isaacsim_ci:
cmd.append("-m")
cmd.append("isaacsim_ci")
cmd.append(str(test_file))
report_file = f"tests/test-reports-{str(file_name)}.xml"
# -- Run with retry on startup hang or hard timeout -----------------
returncode, stdout_data, stderr_data, kill_reason = -1, b"", b"", ""
wall_time, pre_kill_diag = 0.0, ""
startup_hang_attempts = 0
timeout_attempts = 0
while True:
with contextlib.suppress(FileNotFoundError):
os.remove(report_file)
returncode, stdout_data, stderr_data, kill_reason, wall_time, pre_kill_diag = (
capture_test_output_with_timeout(
cmd, timeout, env, startup_deadline=startup_deadline, report_file=report_file
)
)
has_report = os.path.exists(report_file)
if kill_reason == "startup_hang" and startup_hang_attempts < STARTUP_HANG_RETRIES:
startup_hang_attempts += 1
print(
f"⚠️ {test_file}: startup hang detected after {startup_deadline}s"
f" (attempt {startup_hang_attempts}/{STARTUP_HANG_RETRIES + 1}), retrying..."
)
if stderr_data:
print("=== STDERR (last 5000 chars) ===")
print(stderr_data.decode("utf-8", errors="replace")[-5000:])
diag = pre_kill_diag or _capture_system_diagnostics()
if len(diag) > 10000:
diag = diag[:10000] + "\n... (truncated)"
print(diag)
continue
if kill_reason == "timeout" and not has_report and timeout_attempts < TIMEOUT_RETRIES:
timeout_attempts += 1
print(
f"⚠️ {test_file}: timeout detected after {timeout}s"
f" (attempt {timeout_attempts}/{TIMEOUT_RETRIES + 1}), retrying..."
)
if stdout_data:
print("=== STDOUT (last 5000 chars) ===")
print(stdout_data.decode("utf-8", errors="replace")[-5000:])
if stderr_data:
print("=== STDERR (last 5000 chars) ===")
print(stderr_data.decode("utf-8", errors="replace")[-5000:])
diag = pre_kill_diag or _capture_system_diagnostics()
if len(diag) > 10000:
diag = diag[:10000] + "\n... (truncated)"
print(diag)
continue
break
# -- Resolve result from kill_reason and report file ----------------
has_report = os.path.exists(report_file)
if kill_reason == "startup_hang":
diag = _get_diagnostics(pre_kill_diag)
print(f"⚠️ {test_file}: startup hang after {STARTUP_HANG_RETRIES + 1} attempt(s)")
print(diag)
msg = f"Startup hang after {startup_deadline}s (retried {STARTUP_HANG_RETRIES} time(s))"
details = f"{msg}\n\n=== SYSTEM DIAGNOSTICS ===\n{diag}\n\n"
if stderr_data:
details += "=== STDERR (last 5000 chars) ===\n"
details += stderr_data.decode("utf-8", errors="replace")[-5000:] + "\n"
if stdout_data:
details += "=== STDOUT (last 2000 chars) ===\n"
details += stdout_data.decode("utf-8", errors="replace")[-2000:] + "\n"
error_report = _create_error_report("startup_hang", file_name, msg, details)
error_report.write(report_file)
xml_reports.append(error_report)
failed_tests.append(test_file)
test_status[test_file] = {
"errors": 1,
"failures": 0,
"skipped": 0,
"tests": 1,
"result": "STARTUP_HANG",
"time_elapsed": 0.0,
"wall_time": wall_time,
}
continue
if kill_reason == "timeout" and not has_report:
diag = _get_diagnostics(pre_kill_diag)
print(f"Test {test_file} timed out after {timeout} seconds...")
print(diag)
msg = f"Timeout after {timeout} seconds (retried {timeout_attempts} time(s))"
details = f"{msg}\n\n=== SYSTEM DIAGNOSTICS ===\n{diag}\n\n"
if stdout_data:
details += "=== STDOUT (last 5000 chars) ===\n"
details += stdout_data.decode("utf-8", errors="replace")[-5000:] + "\n"
if stderr_data:
details += "=== STDERR (last 5000 chars) ===\n"
details += stderr_data.decode("utf-8", errors="replace")[-5000:] + "\n"
error_report = _create_error_report("timeout", file_name, msg, details)
error_report.write(report_file)
xml_reports.append(error_report)
failed_tests.append(test_file)
test_status[test_file] = {
"errors": 1,
"failures": 0,
"skipped": 0,
"tests": 1,
"result": "TIMEOUT",
"time_elapsed": timeout,
"wall_time": wall_time,
}
continue
if not has_report:
reason = (
_signal_description(-returncode)
if returncode < 0
else f"Process exited with code {returncode} but produced no report"
)
diag = _get_diagnostics()
print(f"⚠️ {test_file}: {reason}")
print(diag)
details = f"{reason}\n\n=== SYSTEM DIAGNOSTICS ===\n{diag}\n\n"
if stdout_data:
details += "=== STDOUT (last 2000 chars) ===\n"
details += stdout_data.decode("utf-8", errors="replace")[-2000:] + "\n"
if stderr_data:
details += "=== STDERR (last 2000 chars) ===\n"
details += stderr_data.decode("utf-8", errors="replace")[-2000:] + "\n"
error_report = _create_error_report("crash", file_name, reason, details)
error_report.write(report_file)
xml_reports.append(error_report)
failed_tests.append(test_file)
test_status[test_file] = {
"errors": 1,
"failures": 0,
"skipped": 0,
"tests": 1,
"result": "CRASHED",
"time_elapsed": 0.0,
"wall_time": wall_time,
}
continue
# -- Report file exists: parse actual test results -----------------
if kill_reason in ("shutdown_hang", "timeout"):
print(f"⚠️ {test_file}: shutdown hanged (killed after {wall_time:.0f}s, test had completed)")
try:
report, errors, failures, skipped, tests, time_elapsed = _read_test_report(report_file, file_name)
except Exception as e:
print(f"Error reading test report {report_file}: {e}")
failed_tests.append(test_file)
test_status[test_file] = {
"errors": 1,
"failures": 0,
"skipped": 0,
"tests": 0,
"result": "FAILED",
"time_elapsed": 0.0,
"wall_time": wall_time,
}
continue
has_test_failures = errors > 0 or failures > 0
(
report,
errors,
failures,
skipped,
tests,
time_elapsed,
returncode,
stdout_data,
stderr_data,
kill_reason,
wall_time,
pre_kill_diag,
has_test_failures,
) = _retry_failed_test_in_fresh_process(
test_file=test_file,
file_name=file_name,
cmd=cmd,
timeout=timeout,
env=env,
startup_deadline=startup_deadline,
report_file=report_file,
report=report,
errors=errors,
failures=failures,
skipped=skipped,
tests=tests,
time_elapsed=time_elapsed,
returncode=returncode,
stdout_data=stdout_data,
stderr_data=stderr_data,
kill_reason=kill_reason,
wall_time=wall_time,
pre_kill_diag=pre_kill_diag,
)
xml_reports.append(report)
shutdown_hanged = kill_reason in ("shutdown_hang", "timeout") and not has_test_failures
if has_test_failures or (returncode != 0 and not shutdown_hanged):
failed_tests.append(test_file)
if shutdown_hanged:
result = "passed (shutdown hanged)"
elif has_test_failures:
result = "FAILED"
else:
result = "passed"
test_status[test_file] = {
"errors": errors,
"failures": failures,
"skipped": skipped,
"tests": tests,
"result": result,
"time_elapsed": time_elapsed,
"wall_time": wall_time,
}
print("~~~~~~~~~~~~ Finished running all tests")
return failed_tests, test_status, xml_reports
def _collect_test_files(
source_dirs,
filter_pattern,
exclude_pattern,
include_files,
quarantined_only,
curobo_only,
):
"""Collect test files from source directories, applying all active filters."""
test_files = []
for source_dir in source_dirs:
if not os.path.exists(source_dir):
print(f"Error: source directory not found at {source_dir}")
pytest.exit("Source directory not found", returncode=1)
for root, _, files in os.walk(source_dir):
for file in files:
if not (file.startswith("test_") and file.endswith(".py")):
continue
# Mode-exclusive filters (each bypasses TESTS_TO_SKIP)
if quarantined_only:
if file not in test_settings.QUARANTINED_TESTS:
continue
elif curobo_only:
if file not in test_settings.CUROBO_TESTS:
continue
else:
# An explicit include_files entry overrides TESTS_TO_SKIP, allowing
# dedicated jobs (e.g. test-environments-training) to run tests that
# are otherwise excluded from general CI runs.
if file in test_settings.TESTS_TO_SKIP and file not in include_files:
print(f"Skipping {file} as it's in the skip list")
continue
full_path = os.path.join(root, file)
if filter_pattern and filter_pattern not in full_path:
print(f"Skipping {full_path} (does not match include pattern: {filter_pattern})")
continue
if exclude_pattern and any(p.strip() in full_path for p in exclude_pattern.split(",")):
print(f"Skipping {full_path} (matches exclude pattern: {exclude_pattern})")
continue
if include_files and file not in include_files:
print(f"Skipping {full_path} (not in include files list)")
continue
test_files.append(full_path)
# Apply file-level sharding: sort deterministically, then select every Nth file.
# Skip when include_files is set — in that case the test's own conftest handles
# sharding at the test-item level (e.g. parametrized test cases).
shard_index = os.environ.get("TEST_SHARD_INDEX", "")
shard_count = os.environ.get("TEST_SHARD_COUNT", "")
if shard_index and shard_count and not include_files:
shard_index = int(shard_index)
shard_count = int(shard_count)
test_files.sort()
test_files = [f for i, f in enumerate(test_files) if i % shard_count == shard_index]
print(f"Shard {shard_index}/{shard_count}: selected {len(test_files)} test files")
return test_files
def _write_empty_report():
"""Write an empty JUnit XML report so downstream CI steps find a valid file."""
os.makedirs("tests", exist_ok=True)
result_file = os.environ.get("TEST_RESULT_FILE", "full_report.xml")
report = JUnitXml()
report.write(f"tests/{result_file}")
print(f"Wrote empty report to tests/{result_file}")
def pytest_sessionstart(session):
"""Intercept pytest startup to execute tests in the correct order."""
# Get the workspace root directory (one level up from tools)
workspace_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
source_dirs = [
os.path.join(workspace_root, "scripts"),
os.path.join(workspace_root, "source"),
]
# Get filter pattern from environment variable or command line
filter_pattern = os.environ.get("TEST_FILTER_PATTERN", "")
exclude_pattern = os.environ.get("TEST_EXCLUDE_PATTERN", "")
include_files_str = os.environ.get("TEST_INCLUDE_FILES", "")
quarantined_only = os.environ.get("TEST_QUARANTINED_ONLY", "false") == "true"
curobo_only = os.environ.get("TEST_CUROBO_ONLY", "false") == "true"
isaacsim_ci = os.environ.get("ISAACSIM_CI_SHORT", "false") == "true"
# Parse include files list (comma-separated paths)
include_files = set()
if include_files_str:
for f in include_files_str.split(","):
f = f.strip()
if f:
include_files.add(os.path.basename(f))
# Also try to get from pytest config
if hasattr(session.config, "option") and hasattr(session.config.option, "filter_pattern"):
filter_pattern = filter_pattern or getattr(session.config.option, "filter_pattern", "")
if hasattr(session.config, "option") and hasattr(session.config.option, "exclude_pattern"):
exclude_pattern = exclude_pattern or getattr(session.config.option, "exclude_pattern", "")
print("=" * 50)
print("CONFTEST.PY DEBUG INFO")
print("=" * 50)
print(f"Filter pattern: '{filter_pattern}'")
print(f"Exclude pattern: '{exclude_pattern}'")
print(f"Include files: {include_files if include_files else 'none'}")
print(f"Quarantined-only mode: {quarantined_only}")
print(f"Curobo-only mode: {curobo_only}")
print(f"TEST_FILTER_PATTERN env var: '{os.environ.get('TEST_FILTER_PATTERN', 'NOT_SET')}'")
print(f"TEST_EXCLUDE_PATTERN env var: '{os.environ.get('TEST_EXCLUDE_PATTERN', 'NOT_SET')}'")
print(f"TEST_INCLUDE_FILES env var: '{os.environ.get('TEST_INCLUDE_FILES', 'NOT_SET')}'")
print(f"TEST_QUARANTINED_ONLY env var: '{os.environ.get('TEST_QUARANTINED_ONLY', 'NOT_SET')}'")
print(f"TEST_CUROBO_ONLY env var: '{os.environ.get('TEST_CUROBO_ONLY', 'NOT_SET')}'")
print("=" * 50)
# Get all test files in the source directories
test_files = _collect_test_files(
source_dirs,
filter_pattern,
exclude_pattern,
include_files,
quarantined_only,
curobo_only,
)
if isaacsim_ci:
new_test_files = []
for test_file in test_files:
with open(test_file) as f:
if "@pytest.mark.isaacsim_ci" in f.read():
new_test_files.append(test_file)
test_files = new_test_files
if not test_files:
if quarantined_only:
print("No quarantined tests configured — nothing to run.")
_write_empty_report()
pytest.exit("No quarantined tests configured", returncode=0)
if filter_pattern:
print(f"No test files found matching filter pattern '{filter_pattern}' — nothing to run.")
_write_empty_report()
pytest.exit("No test files found for filter", returncode=0)
print("No test files found in source directory")
pytest.exit("No test files found", returncode=1)
print(f"Found {len(test_files)} test files after filtering:")
for test_file in test_files:
print(f" - {test_file}")
# Run all tests individually
failed_tests, test_status, xml_reports = run_individual_tests(test_files, workspace_root, isaacsim_ci)
print("failed tests:", failed_tests)
# Collect reports
print("~~~~~~~~~ Collecting final report...")
# Merge in-memory report objects collected during the test run. Reading the
# on-disk files again risks losing <failure> elements if the junitparser
# read/write round-trip does not preserve them faithfully.
full_report = JUnitXml()
for xml_report in xml_reports:
print(xml_report)
full_report += xml_report
print("~~~~~~~~~~~~ Writing final report...")
# write content to full report
result_file = os.environ.get("TEST_RESULT_FILE", "full_report.xml")
full_report_path = f"tests/{result_file}"
print(f"Using result file: {result_file}")
full_report.write(full_report_path)
print("~~~~~~~~~~~~ Report written to", full_report_path)
# print test status in a nice table
# Calculate the number and percentage of passing tests
num_tests = len(test_status)
num_passing = len([p for p in test_files if test_status[p]["result"].startswith("passed")])
num_failing = len([p for p in test_files if test_status[p]["result"] == "FAILED"])
num_timeout = len([p for p in test_files if test_status[p]["result"] == "TIMEOUT"])
num_crashed = len([p for p in test_files if test_status[p]["result"] == "CRASHED"])
num_startup_hang = len([p for p in test_files if test_status[p]["result"] == "STARTUP_HANG"])
if num_tests == 0:
passing_percentage = 100
else:
passing_percentage = num_passing / num_tests * 100
# Print summaries of test results
summary_str = "\n\n"
summary_str += "===================\n"
summary_str += "Test Result Summary\n"
summary_str += "===================\n"
summary_str += f"Total: {num_tests}\n"
summary_str += f"Passing: {num_passing}\n"
summary_str += f"Failing: {num_failing}\n"
summary_str += f"Crashed: {num_crashed}\n"
summary_str += f"Startup Hang: {num_startup_hang}\n"
summary_str += f"Timeout: {num_timeout}\n"
summary_str += f"Passing Percentage: {passing_percentage:.2f}%\n"
total_wall = sum(test_status[test_path]["wall_time"] for test_path in test_files)
total_test = sum(test_status[test_path]["time_elapsed"] for test_path in test_files)
summary_str += f"Total Wall Time: {total_wall // 3600:.0f}h{total_wall // 60 % 60:.0f}m{total_wall % 60:.2f}s\n"
summary_str += f"Total Test Time: {total_test // 3600:.0f}h{total_test // 60 % 60:.0f}m{total_test % 60:.2f}s"
summary_str += "\n\n=======================\n"
summary_str += "Per Test Result Summary\n"
summary_str += "=======================\n"
per_test_result_table = PrettyTable(field_names=["Test Path", "Result", "Test (s)", "Wall (s)", "# Tests"])
per_test_result_table.align["Test Path"] = "l"
per_test_result_table.align["Test (s)"] = "r"
per_test_result_table.align["Wall (s)"] = "r"
for test_path in test_files:
num_tests_passed = (
test_status[test_path]["tests"]
- test_status[test_path]["failures"]
- test_status[test_path]["errors"]
- test_status[test_path]["skipped"]
)
per_test_result_table.add_row(
[
test_path,
test_status[test_path]["result"],
f"{test_status[test_path]['time_elapsed']:0.2f}",
f"{test_status[test_path]['wall_time']:0.2f}",
f"{num_tests_passed}/{test_status[test_path]['tests']}",
]
)
summary_str += per_test_result_table.get_string()
# Print summary to console and log file
print(summary_str)
# Exit pytest after custom execution to prevent normal pytest from overwriting our report
pytest.exit(
"Custom test execution completed",
returncode=0 if (num_failing == 0 and num_timeout == 0 and num_crashed == 0 and num_startup_hang == 0) else 1,
)
|