|
| 1 | +"""UNIT TESTS FOR RUN. |
| 2 | +
|
| 3 | +This module contains unit tests for the shapepipe.run module, in |
| 4 | +particular the MPI-launcher gating of the mpi4py import (#744): a bare |
| 5 | +``shapepipe_run`` must never initialize MPI, otherwise the whole process |
| 6 | +aborts inside an ``srun``-launched shell whose Open MPI lacks SLURM PMI |
| 7 | +support. |
| 8 | +
|
| 9 | +:Author: Claude (on behalf of Cail Daley) <cail.daley@cea.fr> |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +SNIPPET = "import shapepipe.run as r; print(r.import_mpi)" |
| 20 | + |
| 21 | +# Env vars that either mark an MPI launcher (the gate) or make Open MPI |
| 22 | +# believe it was direct-launched by srun (the failure mode under test). |
| 23 | +_SCRUBBED_PREFIXES = ("OMPI_", "PMI_", "PMIX_", "SLURM_") |
| 24 | + |
| 25 | + |
| 26 | +def _import_mpi_flag(extra_env): |
| 27 | + """Report shapepipe.run.import_mpi in a subprocess with a clean env.""" |
| 28 | + env = { |
| 29 | + key: value |
| 30 | + for key, value in os.environ.items() |
| 31 | + if not key.startswith(_SCRUBBED_PREFIXES) |
| 32 | + } |
| 33 | + env.update(extra_env) |
| 34 | + result = subprocess.run( |
| 35 | + [sys.executable, "-c", SNIPPET], |
| 36 | + env=env, |
| 37 | + capture_output=True, |
| 38 | + text=True, |
| 39 | + ) |
| 40 | + assert result.returncode == 0, ( |
| 41 | + f"subprocess failed (exit {result.returncode}): {result.stderr}" |
| 42 | + ) |
| 43 | + return result.stdout.strip() |
| 44 | + |
| 45 | + |
| 46 | +def test_bare_launch_skips_mpi(): |
| 47 | + """A bare launch (no MPI launcher env) must not import/init MPI.""" |
| 48 | + assert _import_mpi_flag({}) == "False" |
| 49 | + |
| 50 | + |
| 51 | +def test_mpirun_launch_imports_mpi(): |
| 52 | + """An mpirun-style env (OMPI_COMM_WORLD_SIZE) must import MPI.""" |
| 53 | + pytest.importorskip("mpi4py") |
| 54 | + assert _import_mpi_flag({"OMPI_COMM_WORLD_SIZE": "1"}) == "True" |
0 commit comments