blob: dc6169dfb56a21774f35faec35d6e0e9b04bfe51 [file] [edit]
############################################################################
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
############################################################################
"""Test SPANK S_SLURM_RESTART_COUNT after a requeue.
Requeue a batch job once and verify that both its batch script and regular step
see restart counts 0 and 1 through spank_get_item(). It requires slurmd 26.11.1
or newer, which is where that item returns the job restart count (ticket 26121).
"""
import re
import pytest
import atf
# From slurm/slurm.h
SLURM_BATCH_SCRIPT = 0xFFFFFFFB
JOB_SCRIPT = "job.sh"
RESTART_LOG_RE = re.compile(
r"caller=(?P<caller>\S+) "
r"self_job_id=(?P<self_job_id>\d+) "
r"self_step_id=(?P<self_step_id>\d+) "
r"restart_count=(?P<restart_count>\d+) "
r"rc=(?P<rc>-?\d+)"
)
@pytest.fixture(scope="module", autouse=True)
def setup(spank_plugin):
"""Setup test environment once for all tests"""
atf.require_version(
(26, 11, 1),
component="sbin/slurmd",
reason="Ticket 26121: S_SLURM_RESTART_COUNT returns the job restart count in 26.11.1",
)
# The plugin is inert unless SPANK_HOOK_LOG_RESTART_COUNT is set, so no args
# needed
atf.require_config_parameter(
"required", spank_plugin, delimiter=" ", source="plugstack"
)
atf.require_config_parameter_includes("SchedulerParameters", ("requeue_delay", 0))
atf.require_config_parameter_includes("SchedulerParameters", ("sched_interval", 1))
atf.require_slurm_running()
def get_restart_count_records(spank_tmp):
"""Returns the restart-count log as a list of dicts."""
log_path = f"{spank_tmp}/spank_restart_count.log"
content = atf.run_command_output(f"cat {log_path} 2>/dev/null", quiet=True)
records = []
for match in RESTART_LOG_RE.finditer(content):
record = match.groupdict()
for key, value in record.items():
if key != "caller":
record[key] = int(value)
records.append(record)
return records
def test_sbatch_requeue_restart_count(spank_tmp):
"""Test the SPANK restart count on both runs of a requeued batch job."""
ready_file = "requeue.ready"
atf.make_bash_script(
JOB_SCRIPT,
f"""
srun true
if [ "${{SLURM_RESTART_COUNT:-0}}" -eq 0 ]; then
touch {ready_file}
sleep infinity
fi
""",
)
job_id = atf.submit_job_sbatch(
f"-J spank_restart_count -N1 -t2 --requeue -o /dev/null {JOB_SCRIPT}",
env_vars="SPANK_HOOK_LOG_RESTART_COUNT=1",
fatal=True,
)
atf.wait_for_file(ready_file, fatal=True)
atf.run_command(
f"scontrol requeue {job_id}",
user=atf.properties["slurm-user"],
fatal=True,
)
atf.wait_for_job_state(job_id, "DONE", fatal=True)
assert (
atf.get_job_parameter(job_id, "JobState") == "COMPLETED"
), f"Requeued job {job_id} should have COMPLETED"
assert (
int(atf.get_job_parameter(job_id, "Restarts", default=0)) == 1
), f"Job {job_id} should have been requeued exactly once"
expected_steps = {SLURM_BATCH_SCRIPT, 0}
records = get_restart_count_records(spank_tmp)
errors = [record for record in records if record["rc"]]
assert not errors, f"spank_get_item() failed to read the restart count: {errors}"
for step_id in expected_steps:
restart_counts = [
record["restart_count"]
for record in records
if record["self_job_id"] == job_id and record["self_step_id"] == step_id
]
step_name = (
"batch script" if step_id == SLURM_BATCH_SCRIPT else f"step {step_id}"
)
assert restart_counts == [0, 1], (
f"spank_get_item should report restart counts [0, 1] for the "
f"{step_name} of requeued job {job_id}, but reported {restart_counts}"
)