From ed5341c0d924b89f7f78e019038bf162e578eaf4 Mon Sep 17 00:00:00 2001 From: Russ Tedrake Date: Wed, 25 Nov 2026 07:36:18 -0500 Subject: [PATCH] Bring CI to parity with underactuated; test this revision's wheel - The pip job now builds and tests a wheel from the commit under test, instead of installing manipulation[all] from PyPI. This catches packaging problems before a release, and the "requires new pip wheels" label is no longer needed. It still installs CPU-only PyTorch. - Prefetch remote packages with retries via htmlbook's shared prefetch_remotes(), so a transient download failure does not fail CI. - Use the shared htmlbook notebook coverage check; the Drake offline-cache test moves to htmlbook as test_drake_remotes.py. - Match underactuated's workflow otherwise: Poetry 2.5.1, setup-python@v7, a pinned lint runner, and a separate macOS LCM multicast step that tolerates a missing route. - Brewfile: install graphviz, which supplies dot for pydot (gprof2dot does not). Co-Authored-By: Claude Opus 5.5 --- .github/workflows/main.yml | 56 ++++++++-------- book/htmlbook | 2 +- book/test_notebook_coverage.py | 92 +++----------------------- docs/testing.md | 29 ++++++-- manipulation/test/test_remote_cache.py | 53 --------------- setup/mac/Brewfile | 2 +- setup/mac/install_prereqs.sh | 2 +- setup/prefetch_remotes.py | 32 ++++++--- 8 files changed, 89 insertions(+), 179 deletions(-) delete mode 100644 manipulation/test/test_remote_cache.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 78b13af7..e61708e6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -152,7 +152,7 @@ jobs: sudo -H -u runner ./setup/submodule_checkout # Install system packages as root INSTALL_JUPYTER=0 ./setup/ubuntu/24.04/install_prereqs.sh - sudo -H -u runner python3 -m pip install --user --break-system-packages poetry==2.1.4 + sudo -H -u runner python3 -m pip install --user --break-system-packages poetry==2.5.1 sudo -H -u runner python3 -m poetry config virtualenvs.in-project true sudo -H -u runner python3 -m poetry install --all-extras --with dev rm -rf /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin /var/lib/apt/lists/* /var/log/apt/* @@ -182,25 +182,23 @@ jobs: if-no-files-found: ignore retention-days: 14 - # TODO(russt): add noble-pip-core workflow which only runs the tests in manipulation, - # but uses the manipulation module from pip. + # TODO(russt): add noble-pip-core workflow which only runs the tests in + # manipulation, but uses the manipulation module from pip. noble-pip-all: - name: ubuntu noble using pip all + name: ubuntu noble using wheel all runs-on: ubuntu-24.04 container: ubuntu:24.04 needs: [check-approval] if: > - ((github.event_name == 'pull_request' && - github.event.pull_request.head.repo.full_name == github.repository) - || - (github.event_name == 'pull_request_target' && - github.event.pull_request.head.repo.full_name != github.repository && - needs.check-approval.outputs.should-run == 'true') - || - (github.event_name != 'pull_request' && - github.event_name != 'pull_request_target')) - && - !contains(github.event.pull_request.labels.*.name, 'requires new pip wheels') + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name == github.repository) + || + (github.event_name == 'pull_request_target' && + github.event.pull_request.head.repo.full_name != github.repository && + needs.check-approval.outputs.should-run == 'true') + || + (github.event_name != 'pull_request' && + github.event_name != 'pull_request_target') steps: - name: pre-checkout setup run: | @@ -248,14 +246,17 @@ jobs: sudo -H -u runner ./setup/submodule_checkout # Install system packages as root ./setup/ubuntu/24.04/install_prereqs.sh - rm -rf ./manipulation # remove local module, force pip module. + # Test this revision's wheel before publishing a release to PyPI. + sudo -H -u runner python3 -m pip wheel --no-deps --wheel-dir /tmp/manipulation-wheels . + WHEEL_PATH="$(find /tmp/manipulation-wheels -name 'manipulation-*.whl')" + rm -rf ./manipulation # Force the tests to import the installed wheel. # CPU wheels are a CI-only choice; leave published requirements unchanged. sudo -H -u runner python3 -m pip install --user --break-system-packages \ --index-url https://download.pytorch.org/whl/cpu 'torch>=2.3,<3.0' 'torchvision>=0.18,<1.0' # Keep the subsequent resolver from replacing these with GPU builds. sudo -H -u runner python3 -c 'from importlib.metadata import version; print("\n".join(f"{p}=={version(p)}" for p in ("torch", "torchvision")))' > /tmp/ci-torch-constraints.txt sudo -H -u runner python3 -m pip install --user --break-system-packages \ - --constraint /tmp/ci-torch-constraints.txt --upgrade manipulation[all] pytest pytest-timeout 'pytest-xdist>=3.6,<4' + --constraint /tmp/ci-torch-constraints.txt "${WHEEL_PATH}[all]" pytest pytest-timeout 'pytest-xdist>=3.6,<4' sudo -H -u runner python3 -c 'import torch, torchvision; print(torch.__version__, torchvision.__version__); assert "+cpu" in torch.__version__ and "+cpu" in torchvision.__version__; assert torch.version.cuda is None' rm -rf /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin /var/lib/apt/lists/* /var/log/apt/* shell: bash @@ -287,6 +288,7 @@ jobs: tahoe: name: macos tahoe 26 + # Drake 1.58+ only publishes macOS 26 (arm64) wheels. runs-on: macos-26 needs: [check-approval] if: > @@ -317,7 +319,7 @@ jobs: submodules: recursive - name: setup python - uses: actions/setup-python@v5 + uses: actions/setup-python@v7 with: python-version: '3.13' @@ -337,22 +339,24 @@ jobs: run: | ./setup/submodule_checkout ./setup/mac/install_prereqs.sh - python -m pip install --user poetry==2.1.4 + python -m pip install --user poetry==2.5.1 python -m poetry config virtualenvs.in-project true python -m poetry env use "$(python -c 'import sys; print(sys.executable)')" python -m poetry install --all-extras --with dev - # Enable lcm multicast; see drake #22322 for details. - sudo route -nv delete 224.0.0.0/4 - sudo route -nv add -net 224.0.0.0/4 -interface lo0 shell: zsh -efuo pipefail {0} + - name: configure LCM multicast loopback + run: | + # https://drake.mit.edu/troubleshooting.html#lcm-macos + sudo route -nv delete 224.0.0.0/4 || true + sudo route -nv add -net 224.0.0.0/4 -interface lo0 + - name: prefetch remote packages run: XDG_CACHE_HOME=/tmp .venv/bin/python -m setup.prefetch_remotes shell: zsh -efuo pipefail {0} - name: test run: | - set -euo pipefail XDG_CACHE_HOME=/tmp DRAKE_ALLOW_NETWORK=lcm:meshcat .venv/bin/python -m pytest -n 2 --dist loadfile -ra --durations=30 --junitxml=test-results/pytest.xml shell: zsh -efuo pipefail {0} @@ -366,7 +370,7 @@ jobs: retention-days: 14 lint: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 needs: [check-approval] if: > (github.event_name == 'pull_request' && @@ -396,7 +400,7 @@ jobs: submodules: recursive - name: setup python - uses: actions/setup-python@v5 + uses: actions/setup-python@v7 with: python-version: '3.10' @@ -420,7 +424,7 @@ jobs: - name: Install and configure Poetry uses: snok/install-poetry@v1 with: - version: 2.1.4 + version: 2.5.1 virtualenvs-create: true virtualenvs-in-project: true diff --git a/book/htmlbook b/book/htmlbook index e9b1d5bf..9e9d38c3 160000 --- a/book/htmlbook +++ b/book/htmlbook @@ -1 +1 @@ -Subproject commit e9b1d5bf70629bb8674b58e0c14ff968fcab1c39 +Subproject commit 9e9d38c33f00bb6ca6ce3dba733322e344db1602 diff --git a/book/test_notebook_coverage.py b/book/test_notebook_coverage.py index 4ee57a6e..cc56b0a5 100644 --- a/book/test_notebook_coverage.py +++ b/book/test_notebook_coverage.py @@ -1,22 +1,17 @@ """Check that every notebook in the book is executed by some test. -A notebook is covered when an `ipynb_test(...)` call in a `test_*.py` file in -the notebook's directory registers it. Exercise notebooks are generated from -the private solutions checkout, so a registration in -`solutions/notebooks//` also covers `book//exercises/`; that -check is skipped when the checkout is absent. - -Notebooks that cannot run in CI must be listed in `UNTESTED` with a reason, so -that disabling a test leaves a visible record. +See htmlbook/notebook_coverage.py. Notebooks that cannot run in CI must be +listed in `UNTESTED` with a reason. """ -from __future__ import annotations - -import ast -import subprocess from pathlib import Path import pytest +from htmlbook.notebook_coverage import ( + assert_book_notebooks_are_tested, + assert_exercise_notebooks_are_tested, + assert_untested_list_is_current, +) REPO_ROOT = Path(__file__).resolve().parents[1] BOOK = REPO_ROOT / "book" @@ -35,83 +30,16 @@ } -def _book_notebooks() -> list[Path]: - """Tracked notebooks in book/, excluding submodules.""" - output = subprocess.check_output( - ["git", "ls-files", "--", "*.ipynb"], cwd=BOOK, text=True - ) - return sorted(BOOK / line for line in output.splitlines()) - - -def _registered_notebooks(directory: Path) -> set[str]: - """Notebook names registered via ipynb_test() in directory/test_*.py.""" - names = set() - for test_file in directory.glob("test_*.py"): - tree = ast.parse(test_file.read_text(encoding="utf-8")) - for node in ast.walk(tree): - if ( - isinstance(node, ast.Call) - and isinstance(node.func, ast.Name) - and node.func.id == "ipynb_test" - and node.args - and isinstance(node.args[0], ast.Constant) - ): - names.add(node.args[0].value) - return names - - -def _is_exercise(notebook: Path) -> bool: - return notebook.parent.name == "exercises" - - -def _publicly_covered(notebook: Path) -> bool: - return notebook.name in _registered_notebooks(notebook.parent) - - -def _relative(notebook: Path) -> str: - return notebook.relative_to(BOOK).as_posix() - - def test_book_notebooks_are_tested() -> None: - missing = [ - _relative(nb) - for nb in _book_notebooks() - if not _is_exercise(nb) - and not _publicly_covered(nb) - and _relative(nb) not in UNTESTED - ] - assert not missing, ( - "These notebooks are not run by any test. Register each one with " - "ipynb_test() in a test_*.py file in its directory, or add it to " - f"UNTESTED in {Path(__file__).name} with the reason it cannot run:\n " - + "\n ".join(missing) - ) + assert_book_notebooks_are_tested(BOOK, UNTESTED) def test_untested_list_is_current() -> None: - notebooks = {_relative(nb): nb for nb in _book_notebooks()} - stale = [] - for path in UNTESTED: - if path not in notebooks: - stale.append(f"{path}: no such tracked notebook") - elif _publicly_covered(notebooks[path]): - stale.append(f"{path}: now registered with ipynb_test()") - assert not stale, "Remove these entries from UNTESTED:\n " + "\n ".join(stale) + assert_untested_list_is_current(BOOK, UNTESTED) @pytest.mark.skipif( not SOLUTIONS_NOTEBOOKS.is_dir(), reason="needs the solutions checkout" ) def test_exercise_notebooks_are_tested() -> None: - missing = [] - for nb in _book_notebooks(): - if not _is_exercise(nb) or _publicly_covered(nb): - continue - chapter = nb.parent.parent.name - if nb.name not in _registered_notebooks(SOLUTIONS_NOTEBOOKS / chapter): - missing.append(_relative(nb)) - assert not missing, ( - "These exercise notebooks are not run by any public or solutions test. " - "Register each one with ipynb_test() in " - "solutions/notebooks//test_*.py:\n " + "\n ".join(missing) - ) + assert_exercise_notebooks_are_tested(BOOK, SOLUTIONS_NOTEBOOKS) diff --git a/docs/testing.md b/docs/testing.md index 93a69d09..f01c3311 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -5,7 +5,9 @@ normal user cache (or `XDG_CACHE_HOME`, when set). Each CI test job runs `python -m setup.prefetch_remotes` before pytest, using the same Python installation, user, and `XDG_CACHE_HOME=/tmp` as the tests. -The pip job prefetches using the installed manipulation wheel, matching its tests. +The prefetch retries failed downloads (see `book/htmlbook/drake_remotes.py`). +The pip job builds a wheel from the commit under test, installs it, and +prefetches using that installation, matching its tests. During pytest, `DRAKE_ALLOW_NETWORK=lcm:meshcat` disables PackageMap downloads while allowing Meshcat and LCM. Notebook subprocesses inherit these settings. An uncached remote package therefore fails instead of silently downloading. @@ -21,13 +23,30 @@ DRAKE_ALLOW_NETWORK=lcm:meshcat .venv/bin/python -m pytest -ra If a test reports that PackageMap networking is disabled and no matching cache entry exists, check that prefetch and pytest use the same user and cache directory. -If a test adds a new remote package, add it to `setup/prefetch_remotes.py`, which -also works with the pip job's published wheel. This list covers packages used -by tests; it deliberately excludes models such as Gymnasium Robotics that -notebooks only download in interactive mode. +If a test adds a new remote package, add it to `setup/prefetch_remotes.py`. +This list covers packages used by tests; it deliberately excludes models such +as Gymnasium Robotics that notebooks only download in interactive mode. See Drake's [network policy documentation](https://drake.mit.edu/doxygen_cxx/group__allow__network.html). +# Notebook coverage + +`book/test_notebook_coverage.py` fails when a tracked notebook is not +registered with `ipynb_test()` in a `test_*.py` file in its directory. Notebooks +that cannot run in CI go in its `UNTESTED` mapping with a reason. Exercise +notebooks are covered by tests in the solutions checkout; that check is skipped +when `solutions/` is absent. The shared logic is in +`book/htmlbook/notebook_coverage.py`. + +# Solutions in CI + +When the `SOLUTIONS_TOKEN` repository secret is set, each test job clones +`RobotLocomotion/manipulation-solutions` at the revision in +`solutions_sha.txt`, and pytest runs its tests too. Without the secret, the +jobs skip the solutions tests. Fork pull requests do not receive secrets until +a maintainer applies the `approved-for-ci` label, which runs the workflow +through `pull_request_target`. + # CI timings Each test job prints its 30 slowest pytest phases and uploads a JUnit report as diff --git a/manipulation/test/test_remote_cache.py b/manipulation/test/test_remote_cache.py deleted file mode 100644 index bc8d7cf8..00000000 --- a/manipulation/test/test_remote_cache.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Check the CI download policy in fresh processes, as notebook tests use.""" - -import hashlib -import os -import subprocess -import sys -import zipfile - - -def test_remote_cache_without_network(tmp_path): - archive = tmp_path / "model.zip" - with zipfile.ZipFile(archive, "w") as bundle: - bundle.writestr("model/asset.txt", "cached model") - checksum = hashlib.sha256(archive.read_bytes()).hexdigest() - env = os.environ.copy() - env["XDG_CACHE_HOME"] = str(tmp_path / "cache") - (tmp_path / "cache").mkdir() - env["DRAKE_ALLOW_NETWORK"] = "lcm:meshcat" - - def resolve(url): - return subprocess.run( - [ - sys.executable, - "-c", - "from pathlib import Path\n" - "from pydrake.multibody.parsing import PackageMap\n" - "packages = PackageMap.MakeEmpty()\n" - "packages.AddRemote('test_model', PackageMap.RemoteParams(\n" - f" urls=[{url!r}], sha256={checksum!r}, strip_prefix='model'))\n" - "asset = Path(packages.GetPath('test_model')) / 'asset.txt'\n" - "assert asset.read_text() == 'cached model'\n", - ], - env=env, - capture_output=True, - text=True, - timeout=30, - ) - - # An HTTPS-only package must fail immediately when the cache is empty. - remote_url = "https://example.invalid/model.zip" - missing = resolve(remote_url) - assert missing.returncode != 0 - assert "DRAKE_ALLOW_NETWORK" in missing.stderr - assert "test_model" in missing.stderr - - # Seed the real Drake cache without requiring internet access in this test. - prefetched = resolve(archive.as_uri()) - assert prefetched.returncode == 0, prefetched.stderr - archive.unlink() - - # A separate process can resolve the HTTPS-only package from that cache. - cached = resolve(remote_url) - assert cached.returncode == 0, cached.stderr diff --git a/setup/mac/Brewfile b/setup/mac/Brewfile index bbb85180..14e3445f 100644 --- a/setup/mac/Brewfile +++ b/setup/mac/Brewfile @@ -3,4 +3,4 @@ brew "python@3.13" brew "tidy-html5" -brew "gprof2dot" # Supplies dot for pydot +brew "graphviz" # Supplies dot for pydot diff --git a/setup/mac/install_prereqs.sh b/setup/mac/install_prereqs.sh index e33a6206..1951a099 100755 --- a/setup/mac/install_prereqs.sh +++ b/setup/mac/install_prereqs.sh @@ -31,7 +31,7 @@ set -euxo pipefail -if [[ "${EUID}" -eq 0 ]]; then +if [[ "${EUID:-}" -eq 0 ]]; then echo 'ERROR: This script must NOT be run as root' >&2 exit 1 fi diff --git a/setup/prefetch_remotes.py b/setup/prefetch_remotes.py index a6d448ce..fe0a494f 100644 --- a/setup/prefetch_remotes.py +++ b/setup/prefetch_remotes.py @@ -1,22 +1,34 @@ -"""Prefetch library and notebook packages using the test job's installation.""" +"""Prefetch library and notebook packages using the test job's installation. + +CI runs `python -m setup.prefetch_remotes` from the repository root before +pytest; see docs/testing.md. In the pip job, this imports the installed wheel. +""" + +import sys +from pathlib import Path from pydrake.multibody.parsing import PackageMap from manipulation.remotes import AddMujocoMenagerie, AddRby1Remote, AddSpotRemote +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "book")) +from htmlbook.drake_remotes import prefetch_remotes # noqa: E402 + if __name__ == "__main__": packages = PackageMap() - print("fetching drake_models") - packages.GetPath("drake_models") + remotes = ["drake_models"] # Only prefetch packages used by tests. In particular, the Gymnasium # Robotics notebook deliberately skips its remote models during CI. - for add_package in (AddMujocoMenagerie, AddSpotRemote, AddRby1Remote): - name = add_package(packages) - print(f"fetching {name}") - packages.GetPath(name) + remotes += [AddMujocoMenagerie(packages), AddSpotRemote(packages)] + try: + # This downloads immediately, to convert the model for Drake. + remotes.append(AddRby1Remote(packages)) + except RuntimeError as error: + # The package is registered before the download; retry it below. + print(f"fetching rby1 failed: {error}", flush=True) + remotes.append("rby1") # book/trajectories/iris_builder.ipynb registers this package directly. - # Keep this in the checkout so pip CI can also use older manipulation wheels. packages.AddRemote( "gcs", PackageMap.RemoteParams( @@ -27,5 +39,5 @@ strip_prefix="gcs-arxiv_paper_version", ), ) - print("fetching gcs") - packages.GetPath("gcs") + remotes.append("gcs") + prefetch_remotes(packages, remotes)