testsAndMisc/linux_configuration/tests/test_tmp_quota.py
Krzysztof kuhy Rudnicki 70189149b2
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Add fix_tmp_space.sh: 30G /tmp, /var/disktmp
/tmp is the stock tmp.mount tmpfs (size=50%, usrquota) and systemd caps
each user at 80% of it at login; a heavy build hit that cap with EDQUOT.

fix_tmp_space.sh writes a tmp.mount drop-in with the stock options and
size=30G, reloads systemd once and remounts /tmp live (never an unmount:
the X11 sockets live there). tmp_quota.py raises each logged-in user's
cap live to 80% of 30G through quotactl_fd, since systemd sets it only
at login. A sparse 64G ext4 image is loop-mounted at /var/disktmp (1777)
through one fstab line; tmpfiles.d empties it at boot and ages it 10d.

The fstab line adds x-systemd.before=systemd-tmpfiles-setup.service:
nofail drops the ordering, and in vmbox tmpfiles started 22 ms before
the mount finished, so the boot-time emptying could miss the image.

Failure paths:
- /tmp not a tmpfs: fail-closed, nothing written.
- another fstab line owns /var/disktmp: fail-closed.
- image exists but is not ext4: fail-closed, never reformatted.
- quotactl or remount fails: fail-closed, non-zero exit.
- image fails to mount at boot: fail-open on purpose. nofail keeps the
  boot going and the 1777 directory underneath takes writes uncapped,
  instead of every command that uses it failing. --verify-only exits 1.

Passed in vmbox, not verified on the host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWW9EDj5WPLpxuWXv29
2026-11-22 11:55:06 +01:00

156 lines
5.5 KiB
Python

"""Tests for linux_configuration/fixes/tmp_quota.py (quotactl_fd helper).
The kernel is never called: every test passes a fake ``syscall`` that reads
or fills the ``if_dqblk`` struct at the address it is given, the way
quotactl_fd(2) does.
"""
from __future__ import annotations
import ctypes
import errno
from typing import TYPE_CHECKING
import pytest
import tmp_quota as tq
if TYPE_CHECKING:
from pathlib import Path
LIMIT_KIB = 25165824
USED_BYTES = 3 * 1024 * 1024
UID = 1000
class FakeKernel:
"""Holds one user's quota and answers Q_GETQUOTA / Q_SETQUOTA."""
def __init__(self, limit_kib: int = 0, *, fail: int = 0) -> None:
"""Start with ``limit_kib`` and fail every call with ``fail`` if set."""
self.limit_kib = limit_kib
self.fail = fail
self.ignore_set = False
self.calls: list[tuple[int, int, int]] = []
self.set_blocks: list[tq.IfDqblk] = []
def __call__(self, nr: int, fd: int, cmd: int, qid: int, addr: int) -> int:
"""Act as syscall(2) for quotactl_fd."""
assert fd >= 0
self.calls.append((nr, cmd, qid))
if self.fail:
ctypes.set_errno(self.fail)
return -1
block = tq.IfDqblk.from_address(addr)
if cmd == tq.qcmd(tq.Q_GETQUOTA):
block.dqb_bhardlimit = self.limit_kib
block.dqb_bsoftlimit = self.limit_kib
block.dqb_curspace = USED_BYTES
block.dqb_valid = 63
else:
copy = tq.IfDqblk.from_buffer_copy(block)
self.set_blocks.append(copy)
if not self.ignore_set:
self.limit_kib = block.dqb_bhardlimit
return 0
def test_qcmd_matches_the_kernel_encoding() -> None:
"""QCMD(Q_GETQUOTA, USRQUOTA) is 0x80000700."""
assert tq.qcmd(tq.Q_GETQUOTA) == 0x80000700
assert tq.qcmd(tq.Q_SETQUOTA) == 0x80000800
def test_get_reads_limit_and_use(tmp_path: Path) -> None:
"""GET returns the hard limit in KiB and the use converted to KiB."""
kernel = FakeKernel(LIMIT_KIB)
quota = tq.get_quota(str(tmp_path), UID, kernel)
assert quota == tq.Quota(limit_kib=LIMIT_KIB, used_kib=3072)
assert kernel.calls == [(443, 0x80000700, UID)]
def test_get_reports_the_kernel_error(tmp_path: Path) -> None:
"""A failing syscall becomes a QuotaError naming the mount, uid and errno."""
kernel = FakeKernel(fail=errno.ESRCH)
with pytest.raises(tq.QuotaError, match="No such process") as caught:
tq.get_quota(str(tmp_path), UID, kernel)
assert str(tmp_path) in str(caught.value)
assert "uid 1000" in str(caught.value)
def test_get_reports_an_unopenable_mount(tmp_path: Path) -> None:
"""A mount point that cannot be opened is a QuotaError, not a traceback."""
with pytest.raises(tq.QuotaError, match="cannot open"):
tq.get_quota(str(tmp_path / "missing"), UID, FakeKernel())
def test_set_writes_only_the_block_limits(tmp_path: Path) -> None:
"""SET sends soft = hard = limit and QIF_BLIMITS, nothing else."""
kernel = FakeKernel(1600201)
quota = tq.set_limit(str(tmp_path), UID, LIMIT_KIB, kernel)
(sent,) = kernel.set_blocks
assert (sent.dqb_bhardlimit, sent.dqb_bsoftlimit) == (LIMIT_KIB, LIMIT_KIB)
assert sent.dqb_valid == tq.QIF_BLIMITS
assert quota.limit_kib == LIMIT_KIB
def test_set_fails_when_the_limit_does_not_stick(tmp_path: Path) -> None:
"""A read-back that differs from the request is a QuotaError."""
kernel = FakeKernel(1600201)
kernel.ignore_set = True
with pytest.raises(tq.QuotaError, match="reads back 1600201"):
tq.set_limit(str(tmp_path), UID, LIMIT_KIB, kernel)
def test_main_get_prints_two_numbers(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""``get`` prints "<limit_kib> <used_kib>" for the shell to read."""
argv = ["get", "--uid", "1000", "--mount", str(tmp_path)]
assert tq.main(argv, FakeKernel(LIMIT_KIB)) == 0
assert capsys.readouterr().out == f"{LIMIT_KIB} 3072\n"
def test_main_set_prints_the_read_back(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""``set`` prints the quota as the kernel reports it afterwards."""
argv = ["set", "--uid", "1000", "--mount", str(tmp_path)]
argv += ["--limit-kib", str(LIMIT_KIB)]
assert tq.main(argv, FakeKernel(1)) == 0
assert capsys.readouterr().out == f"{LIMIT_KIB} 3072\n"
def test_main_reports_failure_on_stderr(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A QuotaError exits 1 with one clear line on stderr."""
argv = ["get", "--uid", "1000", "--mount", str(tmp_path)]
assert tq.main(argv, FakeKernel(fail=errno.EPERM)) == 1
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err.startswith("tmp_quota: ")
assert "Operation not permitted" in captured.err
def test_main_rejects_a_negative_limit(tmp_path: Path) -> None:
"""A negative limit is an argument error, never sent to the kernel."""
kernel = FakeKernel()
argv = ["set", "--uid", "1000", "--mount", str(tmp_path), "--limit-kib", "-1"]
with pytest.raises(SystemExit) as caught:
tq.main(argv, kernel)
assert caught.value.code == 2
assert kernel.calls == []
def test_libc_syscall_is_typed() -> None:
"""The real syscall entry point has explicit argument and return types."""
fn = tq.libc_syscall()
assert fn.restype is ctypes.c_long
assert fn.argtypes == [
ctypes.c_long,
ctypes.c_int,
ctypes.c_uint,
ctypes.c_uint,
ctypes.c_void_p,
]