Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
shell: bash
run: |
set -euo pipefail
mapfile -t ALL < <(git ls-files 'tests/**/test_*.py' \
mapfile -t ALL < <(git ls-files ':(glob)tests/**/test_*.py' \
| grep -v '^tests/sim/' | grep -v '^tests/e2e/' | sort)
SEL=()
for i in "${!ALL[@]}"; do
Expand Down
21 changes: 3 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import subprocess
import uuid
from pathlib import Path
from typing import Literal

import numpy as np
import pytest

from systemd_probe import classify_systemd_probe

SEQ_LEN = 1024
SAMPLES_PER_SHARD = 128

Expand Down Expand Up @@ -115,22 +116,6 @@ def corpus_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:
return d


# Messages meaning "this user/host has no usable system manager", not "the probe broke".
_SYSTEMD_UNAVAILABLE = (
"Interactive authentication required",
"Access denied",
"Failed to connect to bus",
"System has not been booted with systemd",
"Permission denied",
)


def _classify_systemd_probe(returncode: int, stderr: str) -> Literal["ok", "unavailable", "error"]:
if returncode == 0:
return "ok"
return "unavailable" if any(m in stderr for m in _SYSTEMD_UNAVAILABLE) else "error"


@pytest.fixture(scope="session")
def systemd_manager() -> None:
"""Skip unless this user may start transient units on the system manager, as the product does.
Expand Down Expand Up @@ -158,7 +143,7 @@ def systemd_manager() -> None:
timeout=30,
check=False,
)
verdict = _classify_systemd_probe(probe.returncode, probe.stderr)
verdict = classify_systemd_probe(probe.returncode, probe.stderr)
reason = f"rc={probe.returncode} {probe.stderr.strip()[-500:]}"
except FileNotFoundError as exc:
verdict, reason = "unavailable", repr(exc)
Expand Down
20 changes: 20 additions & 0 deletions tests/systemd_probe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Classification of the `systemd-run` probe result (imported by conftest and its unit test)."""

from __future__ import annotations

from typing import Literal

# Messages meaning "this user/host has no usable system manager", not "the probe broke".
_SYSTEMD_UNAVAILABLE = (
"Interactive authentication required",
"Access denied",
"Failed to connect to bus",
"System has not been booted with systemd",
"Permission denied",
)


def classify_systemd_probe(returncode: int, stderr: str) -> Literal["ok", "unavailable", "error"]:
if returncode == 0:
return "ok"
return "unavailable" if any(m in stderr for m in _SYSTEMD_UNAVAILABLE) else "error"
8 changes: 4 additions & 4 deletions tests/test_systemd_probe_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from conftest import _classify_systemd_probe
from systemd_probe import classify_systemd_probe

DENIED = [
"Failed to start transient service unit: Interactive authentication required.",
Expand All @@ -14,17 +14,17 @@


def test_ok() -> None:
assert _classify_systemd_probe(0, "") == "ok"
assert classify_systemd_probe(0, "") == "ok"


@pytest.mark.parametrize("stderr", DENIED)
def test_unavailable(stderr: str) -> None:
assert _classify_systemd_probe(1, stderr) == "unavailable"
assert classify_systemd_probe(1, stderr) == "unavailable"


@pytest.mark.parametrize(
"stderr",
["Failed to start transient service unit: Resource temporarily unavailable (EAGAIN)", ""],
)
def test_error(stderr: str) -> None:
assert _classify_systemd_probe(1, stderr) == "error"
assert classify_systemd_probe(1, stderr) == "error"
Loading