Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 137 additions & 1 deletion .github/scripts/test_rocgdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT

import argparse
import fnmatch
import glob
import json
import logging
Expand All @@ -26,6 +27,11 @@
)
logger = logging.getLogger(__name__)

# Valid tier names recognised by --tier. Mirrors VALID_TEST_CATEGORIES in
# TheRock's test_runner.py and the tiers in .github/test-runner/
# test_categories.yaml (from which gen_ctestfile.py builds CTestTestfile.cmake).
VALID_TIERS = ("quick", "standard", "comprehensive", "full")

# Status symbols used throughout.
STATUS_PASS = "[✓]"
STATUS_FAIL = "[X]"
Expand Down Expand Up @@ -432,6 +438,18 @@ def parse_arguments() -> argparse.Namespace:
"Automatically discovers all gdb.* directories with .exp files. "
"Mutually exclusive with --tests and --gpu-tests.",
)
test_group.add_argument(
"--tier",
type=str,
default=os.environ.get("TEST_TYPE"),
choices=list(VALID_TIERS) + [None],
help="Test tier to run (quick/standard/comprehensive/full). When set, "
"loads test_patterns from <testsuite-dir>/test_categories.yaml and "
"uses them in place of the other selection flags. Falls back to the "
"TEST_TYPE env var when unset. This is the primary entry point used by "
"TheRock's test_runner.py + ctest (see the installed CTestTestfile.cmake). "
"Mutually exclusive with --tests, --gpu-tests and --cpu-tests.",
)

parser.add_argument(
"--testsuite-dir",
Expand Down Expand Up @@ -1463,6 +1481,115 @@ def discover_test_directories(
return sorted(test_dirs)


def _load_test_categories_yaml(testsuite_dir: Path) -> dict:
"""
Load test_categories.yaml from an installed testsuite tree.

ROCgdb's source copy lives in .github/test-runner/; TheRock installs it
next to the testsuite at <testsuite_dir>/test_categories.yaml.

Args:
testsuite_dir: Path to the GDB testsuite directory.

Returns:
Parsed YAML config as a dict.

Exits:
Code 1 if the YAML file is missing or PyYAML is unavailable.
"""
yaml_path = testsuite_dir / "test_categories.yaml"
if not yaml_path.is_file():
_log_error_and_exit(
f"--tier was provided but {yaml_path} does not exist. "
"The installed ROCgdb testsuite is missing test_categories.yaml; "
"use one of --tests/--gpu-tests/--cpu-tests instead, or rebuild "
"against a ROCgdb that ships it."
)

try:
import yaml
except ImportError:
_log_error_and_exit(
"PyYAML is required to use --tier. Install it via `pip install pyyaml`."
)

with yaml_path.open("r", encoding="utf-8") as f:
return yaml.safe_load(f) or {}


def _resolve_tier_tests(
tier: str, testsuite_dir: Path, fallback_tests: List[str]
) -> List[str]:
"""
Expand a tier name to the underlying list of .exp file paths.

Reads test_patterns + exclude from test_categories.yaml for the requested
tier, globs the patterns relative to testsuite_dir, applies fnmatch-based
exclude filtering, and returns a sorted, de-duplicated path list ready to
be used as the --tests value.

Args:
tier: One of VALID_TIERS.
testsuite_dir: Path to the GDB testsuite directory.
fallback_tests: Tests to use if the tier expands to nothing (logged as
a warning, not an error).

Returns:
List of .exp paths relative to testsuite_dir.

Exits:
Code 1 if tier is missing from the YAML.
"""
cfg = _load_test_categories_yaml(testsuite_dir)
categories = cfg.get("test_categories") or {}
tier_cfg = categories.get(tier)
if not tier_cfg:
_log_error_and_exit(
f"--tier '{tier}' not found in test_categories.yaml "
f"(known tiers: {sorted(categories.keys())})."
)

patterns = tier_cfg.get("test_patterns") or []
excludes = tier_cfg.get("exclude") or []

matched: Set[str] = set()
for pattern in patterns:
if not isinstance(pattern, str):
continue
if any(ch in pattern for ch in ("*", "?", "[")):
for hit in testsuite_dir.glob(pattern):
if hit.is_file() and hit.suffix == ".exp":
matched.add(str(hit.relative_to(testsuite_dir)))
else:
candidate = testsuite_dir / pattern
if candidate.is_file():
matched.add(pattern)
else:
logger.warning(
f"Tier '{tier}' references {pattern}, but it does not exist "
f"under {testsuite_dir}. Skipping."
)

filtered: List[str] = []
for rel_path in sorted(matched):
if any(fnmatch.fnmatch(rel_path, pat) for pat in excludes):
continue
filtered.append(rel_path)

if not filtered:
logger.warning(
f"Tier '{tier}' expanded to an empty test list (patterns: "
f"{patterns}, excludes: {excludes}). Falling back to: {fallback_tests}."
)
return list(fallback_tests)

logger.info(
f"Tier '{tier}' expanded to {len(filtered)} test file(s) "
f"(from {len(patterns)} pattern(s), after {len(excludes)} exclude(s))."
)
return filtered


def expand_test_paths(test_list: List[str], testsuite_dir: Path) -> List[str]:
"""
Expand test paths to a list of .exp files, validating existence.
Expand Down Expand Up @@ -1929,7 +2056,16 @@ def main() -> None:

# Handle test selection based on command-line options.
# This must happen before print_configuration() since it displays args.tests.
if args.gpu_tests:
if args.tier:
# Primary entry point for TheRock's test_runner.py + ctest: expand the
# tier from test_categories.yaml into a concrete .exp list.
logger.info(f"Resolving --tier '{args.tier}' against test_categories.yaml")
args.tests = _resolve_tier_tests(
args.tier,
rocgdb_testsuite_dir,
fallback_tests=["gdb.rocm", "gdb.dwarf2"],
)
elif args.gpu_tests:
args.tests = ["gdb.rocm"]
logger.info("Using --gpu-tests: running gdb.rocm tests only")
elif args.cpu_tests:
Expand Down
33 changes: 33 additions & 0 deletions .github/test-runner/CTestTestfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by .github/test-runner/gen_ctestfile.py from
# .github/test-runner/test_categories.yaml. Do not edit by hand.
#
# Rerun the generator after editing test_categories.yaml:
# python3 .github/test-runner/gen_ctestfile.py
#
# Each test invokes the canonical launcher installed one level above
# the testsuite tree (tests/rocgdb/test_rocgdb.py) via the relative
# path ../../test_rocgdb.py. ctest sets each test's working directory
# to the directory containing this CTestTestfile.cmake, so the path
# resolves inside the install tree and the layout stays relocatable.
# --try-rocm-path tells the launcher to use the absolute ROCM_PATH
# exported by TheRock's test_runner.py for locating the testsuite.

add_test(rocgdb_quick "python3" "../../test_rocgdb.py" "--tier" "quick" "--try-rocm-path")
set_tests_properties(rocgdb_quick PROPERTIES
LABELS "quick;pre-commit;smoke"
TIMEOUT 300)

add_test(rocgdb_standard "python3" "../../test_rocgdb.py" "--tier" "standard" "--try-rocm-path")
set_tests_properties(rocgdb_standard PROPERTIES
LABELS "standard;pr;precheckin"
TIMEOUT 2700)

add_test(rocgdb_comprehensive "python3" "../../test_rocgdb.py" "--tier" "comprehensive" "--try-rocm-path")
set_tests_properties(rocgdb_comprehensive PROPERTIES
LABELS "comprehensive;nightly;extended"
TIMEOUT 14400)

add_test(rocgdb_full "python3" "../../test_rocgdb.py" "--tier" "full" "--try-rocm-path")
set_tests_properties(rocgdb_full PROPERTIES
LABELS "full;all"
TIMEOUT 28800)
Comment thread
dileepr1 marked this conversation as resolved.
160 changes: 160 additions & 0 deletions .github/test-runner/gen_ctestfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env python3
# Copyright Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT
"""
Generate .github/test-runner/CTestTestfile.cmake from test_categories.yaml.

These test-runner files live under .github/ (not next to gdb's upstream
sources) to keep ROCgdb close to upstream. TheRock's ROCgdb cmake wrapper
(debug-tools/rocgdb/CMakeLists.txt) installs them, when present, into the
testsuite tree with EXISTS-guarded install rules so the wrapper still works
with upstream gdb sources that do not ship these files. The generated file
lands at:

<artifacts>/tests/rocgdb/gdb/testsuite/CTestTestfile.cmake

TheRock's generic test_runner.py then drives it via:

ctest -L <tier> --test-dir <artifacts>/tests/rocgdb/gdb/testsuite

Each emitted test invokes the canonical launcher, which TheRock installs
one level above the testsuite tree at <artifacts>/tests/rocgdb/test_rocgdb.py
(from this repo's .github/scripts/test_rocgdb.py). ctest sets each test's
working directory to the directory containing this CTestTestfile.cmake, so
the launcher is reached via the relative path ../../test_rocgdb.py. This
keeps the install tree fully relocatable: no env-var plumbing, no absolute
paths, and no component-specific knowledge on the TheRock side.

This generator is a developer tool. After editing test_categories.yaml, run:

python3 .github/test-runner/gen_ctestfile.py

and commit the regenerated CTestTestfile.cmake alongside the YAML.
"""

import argparse
import sys
from pathlib import Path

try:
import yaml
except ImportError:
sys.stderr.write(
"ERROR: PyYAML is required. Install it via `pip install pyyaml`.\n"
)
sys.exit(1)


VALID_TIERS = ("quick", "standard", "comprehensive", "full")

SCRIPT_DIR = Path(__file__).resolve().parent
DEFAULT_YAML = SCRIPT_DIR / "test_categories.yaml"
DEFAULT_OUT = SCRIPT_DIR / "CTestTestfile.cmake"

# Path from the installed CTestTestfile.cmake
# (tests/rocgdb/gdb/testsuite/) up to the installed launcher
# (tests/rocgdb/test_rocgdb.py).
LAUNCHER_REL = "../../test_rocgdb.py"


def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument(
"--yaml",
type=Path,
default=DEFAULT_YAML,
help=f"Path to test_categories.yaml (default: {DEFAULT_YAML}).",
)
p.add_argument(
"--out",
type=Path,
default=DEFAULT_OUT,
help=f"Output path for CTestTestfile.cmake (default: {DEFAULT_OUT}).",
)
return p.parse_args()


def _quote_labels(labels) -> str:
# CTest LABELS property uses ';' as separator. Strip any stray ';' from
# the YAML-supplied labels (defensive - YAML shouldn't have them, but
# CTest would silently split and produce wrong labels).
cleaned = [str(label).replace(";", "_") for label in labels]
return ";".join(cleaned)


def render(yaml_path: Path) -> str:
with yaml_path.open("r", encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}

categories = cfg.get("test_categories") or {}
execution_settings = cfg.get("execution_settings") or {}
default_timeout = int(execution_settings.get("default_timeout", 300))
category_timeouts = execution_settings.get("category_timeouts") or {}

lines = [
"# Generated by .github/test-runner/gen_ctestfile.py from",
"# .github/test-runner/test_categories.yaml. Do not edit by hand.",
"#",
"# Rerun the generator after editing test_categories.yaml:",
"# python3 .github/test-runner/gen_ctestfile.py",
"#",
"# Each test invokes the canonical launcher installed one level above",
"# the testsuite tree (tests/rocgdb/test_rocgdb.py) via the relative",
"# path ../../test_rocgdb.py. ctest sets each test's working directory",
"# to the directory containing this CTestTestfile.cmake, so the path",
"# resolves inside the install tree and the layout stays relocatable.",
"# --try-rocm-path tells the launcher to use the absolute ROCM_PATH",
"# exported by TheRock's test_runner.py for locating the testsuite.",
"",
]

emitted_any = False
for tier in VALID_TIERS:
spec = categories.get(tier)
if not spec:
continue
labels = spec.get("labels") or [tier]
timeout = int(category_timeouts.get(tier, default_timeout))
# ctest test names must be bare CMake identifiers (no spaces /
# quotes) so `ctest --print-labels` and label-regex consumers can
# match them deterministically.
test_name = f"rocgdb_{tier}"

# --try-rocm-path makes the launcher prefer the absolute ROCM_PATH that
# TheRock's test_runner.py exports (derived from THEROCK_BIN_DIR) over
# the relative OUTPUT_ARTIFACTS_DIR. ctest runs each test with its CWD
# set to this directory, so a relative OUTPUT_ARTIFACTS_DIR like
# "./build" would otherwise resolve against the testsuite dir and fail.
lines.append(
f'add_test({test_name} "python3" "{LAUNCHER_REL}" '
f'"--tier" "{tier}" "--try-rocm-path")'
)
lines.append(f"set_tests_properties({test_name} PROPERTIES")
lines.append(f' LABELS "{_quote_labels(labels)}"')
lines.append(f" TIMEOUT {timeout})")
lines.append("")
emitted_any = True

if not emitted_any:
sys.stderr.write(
f"ERROR: no test_categories.<tier> entries found in {yaml_path}\n"
)
sys.exit(1)

return "\n".join(lines).rstrip() + "\n"


def main() -> None:
args = parse_args()
if not args.yaml.is_file():
sys.stderr.write(f"ERROR: YAML file not found: {args.yaml}\n")
sys.exit(1)

content = render(args.yaml)
args.out.parent.mkdir(parents=True, exist_ok=True)
args.out.write_text(content, encoding="utf-8")
print(f"Wrote {args.out} ({len(content)} bytes)")


if __name__ == "__main__":
main()
Loading
Loading