blob: 691e81a04035864448813838e4512d4114b9fa78 [file] [edit]
############################################################################
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
############################################################################
"""A job update checks partition access for the entire requested QOS list."""
import os
import re
import pytest
import atf
name = os.path.splitext(os.path.basename(__file__))[0]
account = f"{name}_acct"
high_qos = f"{name}_high"
low_qos = f"{name}_low"
qos_request = f"{high_qos},{low_qos}"
source_partition = f"{name}_both"
@pytest.fixture(scope="module", autouse=True)
def setup():
atf.require_version(
(26, 11, 1),
"sbin/slurmctld",
reason="Ticket 23033: job updates check every requested QOS since 26.11.1",
)
atf.require_accounting(modify=True)
for enforcement in ("associations", "qos", "limits"):
atf.require_config_parameter_includes("AccountingStorageEnforce", enforcement)
atf.require_config_parameter("EnforcePartLimits", "ALL")
atf.require_slurm_running()
admin = atf.properties["slurm-user"]
partitions = [
source_partition,
f"{name}_allow",
f"{name}_deny",
f"{name}_low",
f"{name}_deny_high",
]
try:
for qos, priority in ((high_qos, 100), (low_qos, 10)):
atf.run_command(
f"sacctmgr -i add qos {qos} Priority={priority}", user=admin, fatal=True
)
atf.run_command(f"sacctmgr -i add account {account}", user=admin, fatal=True)
atf.run_command(
f"sacctmgr -i add user {atf.properties['test-user']} "
f"Account={account} QOS={qos_request}",
user=admin,
fatal=True,
)
# The controller must know both priorities before it sorts the request.
for qos, priority in ((high_qos, 100), (low_qos, 10)):
pattern = rf"QOS={qos}\(\d+\).*Priority={priority}\b"
for _ in atf.timer(fatal=True):
output = atf.run_command_output(
f"scontrol show assoc_mgr flags=qos qos={qos}", quiet=True
)
if re.search(pattern, output, re.S):
break
for partition, access in zip(
partitions,
(
f"AllowQos={qos_request}",
f"AllowQos={high_qos}",
f"DenyQos={low_qos}",
f"AllowQos={low_qos}",
f"DenyQos={high_qos}",
),
):
atf.run_command(
f"scontrol create PartitionName={partition} Nodes=ALL {access}",
user=admin,
fatal=True,
)
yield
finally:
for partition in partitions:
atf.run_command(
f"scontrol delete PartitionName={partition}", user=admin, quiet=True
)
atf.run_command(
f"sacctmgr -i delete user {atf.properties['test-user']} Account={account}",
user=admin,
quiet=True,
)
atf.run_command(f"sacctmgr -i delete account {account}", user=admin, quiet=True)
atf.run_command(f"sacctmgr -i delete qos {qos_request}", user=admin, quiet=True)
def _submit():
return atf.submit_job_sbatch(
f"--hold -A {account} -p {source_partition} --qos={qos_request} "
"-n1 --mem=1 -t1 -o /dev/null --wrap='sleep infinity'",
fatal=True,
)
@pytest.mark.parametrize("access", ["allow", "deny", "low", "deny_high"])
@pytest.mark.parametrize(
"replace_qos", [False, True], ids=["keep_list", "replace_list"]
)
def test_update_rejects_inaccessible_qos(access, replace_qos):
"""Submit and update must both reject a list containing a forbidden QOS."""
partition = f"{name}_{access}"
result = atf.run_command(
f"sbatch --hold -A {account} -p {partition} --qos={qos_request} "
"-n1 --mem=1 -t1 -o /dev/null --wrap='sleep infinity'",
xfail=True,
)
assert result["exit_code"] != 0, "Submission accepted a forbidden QOS in the list"
assert (
"Invalid qos specification" in result["stderr"]
), "Submission was rejected for an unexpected reason"
job_id = _submit()
qos_update = f"QOS={qos_request}" if replace_qos else ""
# Run as the job owner: privileged users bypass this validation.
result = atf.run_command(
f"scontrol update JobId={job_id} Partition={partition} {qos_update}",
xfail=True,
)
assert result["exit_code"] != 0, "Update accepted a forbidden QOS in the list"
assert (
"Invalid qos specification" in result["stderr"]
), "Update was rejected for an unexpected reason"
assert (
atf.get_job_parameter(job_id, "Partition") == source_partition
), "Rejected update still changed the partition"
assert (
atf.get_job_parameter(job_id, "QOS") == qos_request
), "Rejected update still changed the QOS"
@pytest.mark.parametrize(
"access, qos", [("allow", high_qos), ("deny", high_qos), ("low", low_qos)]
)
def test_update_replaces_list_with_allowed_single_qos(access, qos):
"""A replacement must be checked without the old list's forbidden QOS."""
job_id = _submit()
partition = f"{name}_{access}"
atf.run_command(
f"scontrol update JobId={job_id} Partition={partition} QOS={qos}",
fatal=True,
)
assert (
atf.get_job_parameter(job_id, "Partition") == partition
), "Update did not move the job to the partition"
assert (
atf.get_job_parameter(job_id, "QOS") == qos
), "QOS list was not replaced by the single QOS"