blob: 8f771280d6ac1dfec22c46d72a47d5b30bb8d2e0 [file] [edit]
############################################################################
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
############################################################################
"""Test reservation access based on Groups."""
import pytest
import atf
test_user = atf.properties["test-user"]
slurm_user = atf.properties["slurm-user"]
RESV_NAME = "test_resv"
GROUP_IN = "test_123_23_in"
GROUP_OUT = "test_123_23_out"
OUT_USER = "test_123_23_user"
PART_NAME = "test_123_23_part"
# Root and SlurmUser have access to every reservation
pytestmark = pytest.mark.skipif(
test_user in ("root", slurm_user),
reason=f"This test requires SlurmTestUser ({test_user}) to be neither root nor SlurmUser ({slurm_user}).",
)
# SETUP
@pytest.fixture(scope="module", autouse=True)
def setup():
# The groups are created on this host, so slurmctld must run here too.
# custom-qa-checks R011 flags this; keep it.
atf.require_auto_config("creates local groups that slurmctld must resolve")
atf.require_sudo_rights()
# Issue 51580: with association enforcement, AllowedPartitions alone admits
# users when no member of Groups has an association
atf.require_config_parameter("AccountingStorageEnforce", None)
atf.require_slurm_running()
@pytest.fixture(scope="module")
def group_in(setup):
atf.run_command(
f"getent group {GROUP_IN} || groupadd {GROUP_IN}", user="root", fatal=True
)
yield GROUP_IN
atf.run_command(f"groupdel {GROUP_IN}", user="root", fatal=True)
@pytest.fixture(scope="module")
def group_out(setup):
atf.run_command(
f"getent group {GROUP_OUT} || groupadd {GROUP_OUT}", user="root", fatal=True
)
yield GROUP_OUT
atf.run_command(f"groupdel {GROUP_OUT}", user="root", fatal=True)
@pytest.fixture(scope="module")
def out_user(setup):
atf.run_command(f"id {OUT_USER} || useradd -M {OUT_USER}", user="root", fatal=True)
yield OUT_USER
atf.run_command(f"userdel {OUT_USER}", user="root", fatal=True)
@pytest.fixture(scope="module", autouse=True)
def groups(group_in, group_out, out_user):
"""Add the test user to IN and the dummy user to OUT"""
atf.run_command(f"usermod -aG {group_in} {test_user}", user="root", fatal=True)
atf.run_command(f"usermod -aG {group_out} {out_user}", user="root", fatal=True)
user_groups = atf.run_command_output(f"id -nG {test_user}", fatal=True).split()
assert group_in in user_groups, f"{test_user} should be in {group_in}"
assert group_out not in user_groups, f"{test_user} should not be in {group_out}"
@pytest.fixture(scope="module")
def partition():
atf.run_command(
f"scontrol create partitionname={PART_NAME} nodes=ALL",
user=slurm_user,
fatal=True,
)
yield PART_NAME
atf.run_command(
f"scontrol delete partitionname={PART_NAME}",
user=slurm_user,
fatal=True,
)
@pytest.fixture(scope="function")
def resv():
yield RESV_NAME
# Jobs that just ended can still block the delete for a moment
for _ in atf.timer(fatal=True):
if RESV_NAME not in atf.get_reservations(user=slurm_user, fatal=True):
break
atf.run_command(f"scontrol delete reservationname={RESV_NAME}", user=slurm_user)
# HELPER FUNCTIONS
def create_resv(access, **run_command_kwargs):
"""Create a one node reservation with the given access fields"""
return atf.run_command(
f"scontrol create reservationname={RESV_NAME} start=now duration=10 "
f"nodecnt=1 {access}",
user=slurm_user,
**run_command_kwargs,
)
def update_resv(access):
"""Update the access fields of the reservation"""
return atf.run_command(
f"scontrol update reservationname={RESV_NAME} {access}", user=slurm_user
)
def get_resv_field(field):
"""Return a field of the reservation, read as SlurmUser"""
return atf.get_reservations(user=slurm_user, fatal=True)[RESV_NAME].get(field)
def get_resv_groups():
"""Return the groups of the reservation as a set"""
return set((get_resv_field("Groups") or "").split(","))
def srun_in_resv(user=None, partition=None):
"""Run a short job in the reservation"""
part_opt = f"-p {partition}" if partition else ""
return atf.run_command(
f"srun -N1 --reservation={RESV_NAME} {part_opt} true", user=user
)
# TESTS
@pytest.mark.parametrize(
"groups_list, allowed",
[
([GROUP_IN], True),
([GROUP_OUT], False),
([GROUP_IN, GROUP_OUT], True),
([GROUP_OUT, GROUP_IN], True),
],
ids=["in", "out", "in_out", "out_in"],
)
def test_group_access(resv, groups_list, allowed):
"""Verify that only members of a listed group can use the reservation"""
create_resv(f"groups={','.join(groups_list)}", fatal=True)
assert get_resv_groups() == set(groups_list), "Groups should be the given list"
assert get_resv_field("Users") is None, "Users should not be set"
result = srun_in_resv()
if allowed:
assert result["exit_code"] == 0, f"{test_user} should be allowed"
else:
assert result["exit_code"] != 0, f"{test_user} should be denied"
assert "Access denied" in result["stderr"], "The job should be denied access"
def test_primary_group_access(resv):
"""Verify that members through their primary group can use the reservation"""
primary = atf.run_command_output(f"id -gn {test_user}", fatal=True).strip()
create_resv(f"groups={primary}", fatal=True)
assert srun_in_resv()["exit_code"] == 0, f"{test_user} should be allowed"
def test_update_groups(resv):
"""Verify that Groups+= and Groups-= add and remove access"""
create_resv(f"groups={GROUP_OUT}", fatal=True)
result = srun_in_resv()
assert result["exit_code"] != 0, f"{test_user} should be denied"
assert "Access denied" in result["stderr"], "The job should be denied access"
assert update_resv(f"groups+={GROUP_IN}")["exit_code"] == 0, "Groups+= failed"
assert get_resv_groups() == {GROUP_OUT, GROUP_IN}, f"{GROUP_IN} should be added"
assert srun_in_resv()["exit_code"] == 0, f"{test_user} should be allowed"
assert update_resv(f"groups-={GROUP_IN}")["exit_code"] == 0, "Groups-= failed"
assert get_resv_groups() == {GROUP_OUT}, f"{GROUP_IN} should be removed"
result = srun_in_resv()
assert result["exit_code"] != 0, f"{test_user} should be denied again"
assert "Access denied" in result["stderr"], "The job should be denied access"
def test_privileged_users(resv):
"""Verify that root and SlurmUser can use any group reservation"""
create_resv(f"groups={GROUP_OUT}", fatal=True)
result = srun_in_resv()
assert result["exit_code"] != 0, f"{test_user} should be denied"
assert "Access denied" in result["stderr"], "The job should be denied access"
for user in ("root", slurm_user):
assert srun_in_resv(user=user)["exit_code"] == 0, f"{user} should be allowed"
def test_users_and_groups_create(resv):
"""Verify that a new reservation cannot have both Users and Groups"""
result = create_resv(f"users={test_user} groups={GROUP_IN}")
assert result["exit_code"] != 0, "Users and Groups together should fail"
assert RESV_NAME not in atf.get_reservations(
user=slurm_user, fatal=True
), "The reservation should not exist"
result = create_resv(f"groups={GROUP_IN}")
assert result["exit_code"] == 0, "The same reservation without Users should work"
def test_users_and_groups_update(resv):
"""Verify that a Groups reservation cannot be updated to add Users"""
create_resv(f"groups={GROUP_IN}", fatal=True)
assert update_resv(f"users={test_user}")["exit_code"] != 0, "Users should fail"
assert get_resv_groups() == {GROUP_IN}, "Groups should not change"
assert get_resv_field("Users") is None, "Users should not be set"
result = update_resv(f"groups={GROUP_IN},{GROUP_OUT}")
assert result["exit_code"] == 0, "A Groups update should work"
def test_groups_update_on_users_resv(resv):
"""Verify that a Users reservation cannot be updated to add Groups"""
create_resv(f"users={test_user}", fatal=True)
assert update_resv(f"groups={GROUP_IN}")["exit_code"] != 0, "Groups should fail"
assert get_resv_field("Groups") is None, "Groups should not be set"
assert get_resv_field("Users") == test_user, "Users should not change"
def test_groups_and_partitions(resv, partition):
"""Verify that a job needs both Groups and AllowedPartitions to allow it"""
create_resv(f"groups={GROUP_IN} allowedpartitions={partition}", fatal=True)
default_part = atf.default_partition()
result = srun_in_resv(partition=partition)
assert result["exit_code"] == 0, "Group and partition allow the job"
result = srun_in_resv(partition=default_part)
assert result["exit_code"] != 0, "The partition should deny the job"
assert (
"Problem using reservation" in result["stderr"]
), "The reservation should deny the job"
# Only the partition list changes
result = update_resv(f"allowedpartitions+={default_part}")
assert result["exit_code"] == 0, "AllowedPartitions+= failed"
result = srun_in_resv(partition=default_part)
assert result["exit_code"] == 0, "Group and partition allow the job now"
def test_unset_groups(resv, partition):
"""Verify that group='' removes the Groups access field"""
create_resv(f"groups={GROUP_OUT} allowedpartitions={partition}", fatal=True)
result = srun_in_resv(partition=partition)
assert result["exit_code"] != 0, f"{test_user} should be denied"
assert "Access denied" in result["stderr"], "The job should be denied access"
assert update_resv("group=''")["exit_code"] == 0, "group='' failed"
assert get_resv_field("Groups") is None, "Groups should be unset"
result = srun_in_resv(partition=partition)
assert result["exit_code"] == 0, "Only the partition limits the job now"