Metadata-Version: 2.5
Name: pytest-systemd-slice
Version: 0.1.0
Summary: Run each pytest session (with all xdist workers) in its own systemd scope inside a user slice, so memory limits and systemd-oomd hit the tests, not your desktop.
Project-URL: Homepage, https://github.com/mpasternak/pytest-systemd-slice
Project-URL: Repository, https://github.com/mpasternak/pytest-systemd-slice
Project-URL: Issues, https://github.com/mpasternak/pytest-systemd-slice/issues
Project-URL: Changelog, https://github.com/mpasternak/pytest-systemd-slice/blob/main/CHANGELOG.md
Author-email: Michał Pasternak <michal.dtz@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: cgroup,linux,memory,oom,pytest,slice,systemd,xdist
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Requires-Dist: pytest>=7
Description-Content-Type: text/markdown

# pytest-systemd-slice

Run every pytest session — together with all its `pytest-xdist` workers — in
its own systemd scope inside a **user slice** (`pytest.slice` by default).

Why: `pytest -n 16` on a big Django test suite can eat tens of gigabytes. When
it runs in your terminal's cgroup, it competes with your desktop, browser and
editor, and when memory runs out the OOM killer (or a frozen desktop) picks
the victim. Put the tests in their own slice and you can:

- cap them: `MemoryMax=32G` hits the tests, not KWin/GNOME Shell;
- let `systemd-oomd` kill the *test run* under memory pressure;
- give them a lower `CPUWeight=` so the desktop stays responsive;
- watch them with `systemd-cgtop` / `systemctl --user status pytest.slice`.

## How it works

Early at startup — before pytest captures output and before xdist starts its
workers — the plugin re-executes the very same command line under

```
systemd-run --user --scope --slice=pytest.slice -- <python> <original argv>
```

`systemd-run --scope` registers a transient scope and then `exec`s the command,
so the PID, terminal, environment, working directory, Ctrl+C and the exit code
stay the same: `uv run pytest`, `make test`, `tox` and IDE runners keep working.
The header of the run shows where it landed:

```
systemd: running in pytest.slice (run-p123456-i789.scope)
```

Nothing happens — pytest runs exactly as without the plugin — when:

- not on Linux, `systemd-run` is missing or there is no systemd user instance
  (the user bus is looked up in `$XDG_RUNTIME_DIR`, falling back to
  `/run/user/<uid>`, so it also works from a tmux server run as a system service);
- `CI` is set (unless a slice is requested explicitly by option or environment);
- it is an xdist worker or the run is already in the slice;
- pytest was called programmatically (`pytest.main([...])`) or with `--help`/`--version`;
- it is disabled (see below).

Docker containers started by the tests (testcontainers etc.) are *not* moved:
they belong to the Docker daemon's cgroup.

## Installation

Per project:

```
uv add --dev pytest-systemd-slice      # or: pip install pytest-systemd-slice
```

For **all** projects on your machine without touching them, install it into a
directory of its own and put that on `PYTHONPATH` (pytest finds the plugin via
its entry point there; it has no dependencies besides pytest itself):

```
uv pip install --no-deps --target ~/.local/lib/pytest-systemd-slice pytest-systemd-slice
echo 'export PYTHONPATH="$HOME/.local/lib/pytest-systemd-slice${PYTHONPATH:+:$PYTHONPATH}"' >> ~/.bashrc
```

The module is Python 3.9+ syntax-compatible, so it is harmless in older venvs.

## Configuring the slice

The slice is created on demand. Give it limits with a unit file, e.g.
`~/.config/systemd/user/pytest.slice`:

```ini
[Unit]
Description=pytest runs (pytest-systemd-slice)

[Slice]
MemoryHigh=30G
MemoryMax=32G
MemorySwapMax=2G
CPUWeight=50
IOWeight=50
# Under sustained memory pressure systemd-oomd kills a whole test run
# (one scope), not your desktop.
ManagedOOMMemoryPressure=kill
ManagedOOMMemoryPressureLimit=40%
```

then `systemctl --user daemon-reload`.

## Options

| Command line | Environment | ini | Meaning |
|---|---|---|---|
| `--systemd-slice=NAME` | `PYTEST_SYSTEMD_SLICE=NAME` | `systemd_slice = NAME` | slice to use (default `pytest.slice`; `.slice` is appended if missing) |
| `--no-systemd-slice` | `PYTEST_SYSTEMD_SLICE=0` | `systemd_slice = off` | do not re-execute |
| `--systemd-slice-property=K=V` (repeatable) | | `systemd_slice_properties` (lines) | extra `systemd-run --property` for the scope of this run, e.g. `MemoryMax=8G` |

Precedence: command line, then environment, then ini. `-p no:pytest_systemd_slice`
disables the plugin entirely.

## License

MIT
