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
10 changes: 9 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install -y bats
sudo apt-get update && sudo apt-get install -y bats nodejs shellcheck
pip install pytest pyyaml

# Ubuntu's AppArmor policy blocks unprivileged user namespaces; the
# os-justfile bats tests run the k8s recipe inside one.
- name: Allow unprivileged user namespaces
run: |
if [ -e /proc/sys/kernel/apparmor_restrict_unprivileged_userns ]; then
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
fi

- name: Set up just
uses: taiki-e/install-action@4cef1412cce204788f482e778a0b9187f9626a29 # v2
with:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Thanks for contributing. This repository is optimized for AI coding agents first
2. AI agents must query the `projectbluefin` MCP server (`search_knowledge(query, limit)`, `get_factory_status()`, `get_work_queue()`) before investigating or implementing.
3. Make the smallest change that solves the problem.
4. Run `just validate` on every change that touches the element graph.
`just test-unit` runs pytest (needs `python3` with `pytest` and `pyyaml`) and bats; without a local `bats` it runs bats from a digest-pinned container through `podman`.
5. Validate documentation changes with `python3 .github/scripts/docs-checks.py`.
6. Use Conventional Commits (`feat:`, `fix:`, `docs:`, `ci:`, `chore:`).
7. Attribution trailers are required on all AI-assisted commits:
Expand Down
13 changes: 11 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ default:
# Same bst2 container image FSDK/dakota CI uses -- pinned by SHA.
export oras_image := env("ORAS_IMAGE", "ghcr.io/oras-project/oras:v1.3.4")
export bst2_image := env("BST2_IMAGE", "registry.gitlab.com/freedesktop-sdk/infrastructure/freedesktop-sdk-docker-images/bst2:64eb0b4930d57a92710822898fb73af6cc1ae35d")
# bats for `just test-unit` when none is installed -- pinned by digest.
export bats_image := env("BATS_IMAGE", "docker.io/bats/bats:1.14.0@sha256:5322b877351fda0cc435de8c6116de7d0a2ec79d7c680132a0ef329a633bc66f")

# Prefix for podman calls: empty when rootless podman works, "sudo" otherwise.
sudo_cmd := if `podman info >/dev/null 2>&1 && echo 1 || echo 0` == "1" { "" } else { "sudo" }
Expand Down Expand Up @@ -68,11 +70,18 @@ validate: gen-dev-keys
python3 .github/scripts/check-renovate-series.py
just bst show --deps all oci/bluefin-server-image.bst oci/k0s-sysext.bst oci/kubestellar-sysext.bst oci/zfs-sysext.bst oci/kubeadm-sysext.bst

# Run the unit test suite (pytest + bats).
# Run the unit test suite (pytest + bats; bats from a container if not installed).
[group('dev')]
test-unit:
#!/usr/bin/env bash
set -euo pipefail
python3 -m pytest tests/unit -q
bats tests/unit
if command -v bats >/dev/null 2>&1; then
exec bats tests/unit
fi
echo "==> bats is not installed; running it from ${bats_image}" >&2
exec {{sudo_cmd}} podman run --rm --security-opt label=disable -e CI \
-v "{{justfile_directory()}}:/code:ro" -w /code "${bats_image}" tests/unit

# ── Build ─────────────────────────────────────────────────────────────
# Build the k0s and KubeStellar systemd-sysext images.
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = tests/unit
addopts = -ra
181 changes: 181 additions & 0 deletions tests/unit/_systemd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
"""Read systemd configuration the way systemd does, so tests assert meaning.

A test built on these helpers survives comments, whitespace and key order in
the payload, and fails when a setting systemd would act on changes.
"""

from __future__ import annotations

import fnmatch
import re
import shlex
from collections.abc import Iterable
from pathlib import Path
from typing import NamedTuple

# systemd cannot remove a dependency, so an empty assignment of one of these is
# ignored. An empty Condition*= or Assert*= clears every condition or assert;
# any other empty assignment resets just that key (e.g. ExecStart= in a drop-in).
_DEPENDENCIES = frozenset(
{
"After",
"Before",
"BindsTo",
"Conflicts",
"JoinsNamespaceOf",
"OnFailure",
"OnSuccess",
"PartOf",
"PropagatesReloadTo",
"PropagatesStopTo",
"ReloadPropagatedFrom",
"Requires",
"Requisite",
"StopPropagatedFrom",
"Upholds",
"Wants",
}
)
_EXEC_KEYS = (
"ExecCondition",
"ExecStartPre",
"ExecStart",
"ExecStartPost",
"ExecReload",
"ExecStop",
"ExecStopPost",
)
_IN_WORD_VARIABLE = re.compile(r"\$\$|\$\{(\w+)\}")


class SystemdFile:
"""A unit, drop-in, .network or other systemd.syntax(7) file.

``SystemdFile(unit, *dropins)`` merges the files in the order given, the
way systemd applies drop-ins after the unit. Repeated keys accumulate.
"""

def __init__(self, *paths: Path) -> None:
self.sections: dict[str, dict[str, list[str]]] = {}
for path in paths:
self._merge(path)

def _merge(self, path: Path) -> None:
section = None
for line in _logical_lines(path.read_text(encoding="utf-8")):
if line.startswith("[") and line.endswith("]"):
section = self.sections.setdefault(line[1:-1], {})
continue
key, equals, value = line.partition("=")
if section is None or not equals:
raise ValueError(f"{path}: not a Key=value line in a section: {line!r}")
key, value = key.strip(), value.strip()
if value:
section.setdefault(key, []).append(value)
elif key.startswith(("Condition", "Assert")):
group = "Condition" if key.startswith("Condition") else "Assert"
for name in [name for name in section if name.startswith(group)]:
del section[name]
elif key not in _DEPENDENCIES:
section.pop(key, None)

def values(self, section: str, key: str) -> list[str]:
"""Every value *key* holds after merging, in assignment order."""
return list(self.sections.get(section, {}).get(key, []))

def value(self, section: str, key: str) -> str | None:
"""A single-valued setting: the last assignment wins."""
values = self.values(section, key)
return values[-1] if values else None

def words(self, section: str, key: str) -> list[str]:
"""A space-separated list setting such as After=, Wants= or WantedBy=."""
return [word for value in self.values(section, key) for word in value.split()]

def commands(self, key: str = "ExecStart") -> list[list[str]]:
"""Each [Service] *key* command line as argv, Environment= expanded.

Command prefixes (``-``, ``+``, ``!``...) are dropped; ``$VAR`` as a
word splits at whitespace, ``${VAR}`` stays one word, ``$$`` is ``$``.
"""
environment = {}
for assignment in self.values("Service", "Environment"):
for pair in shlex.split(assignment):
name, _, value = pair.partition("=")
environment[name] = value
commands = []
for line in self.values("Service", key):
argv = []
for word in shlex.split(line.lstrip("-@:+!|")):
if re.fullmatch(r"\$\w+", word):
argv.extend(environment.get(word[1:], "").split())
else:
argv.append(
_IN_WORD_VARIABLE.sub(
lambda m: environment.get(m[1], "") if m[1] else "$", word
)
)
commands.append(argv)
return commands

def all_commands(self) -> list[list[str]]:
return [argv for key in _EXEC_KEYS for argv in self.commands(key)]


def _logical_lines(text: str) -> Iterable[str]:
"""Non-comment lines with backslash continuations joined, as systemd does."""
pending = None
for raw in text.splitlines():
if raw.strip()[:1] in ("#", ";"):
continue
line = raw if pending is None else pending + raw
if (len(line) - len(line.rstrip("\\"))) % 2:
pending = line[:-1] + " "
continue
pending = None
if line.strip():
yield line.strip()
if pending is not None and pending.strip():
yield pending.strip()


def preset_rules(path: Path) -> list[tuple[str, str]]:
"""``(verb, unit glob)`` for each rule of a preset file, in order."""
rules = []
for line in path.read_text(encoding="utf-8").splitlines():
words = line.split()
if words and words[0][0] not in "#;":
rules.append((words[0], words[1]))
return rules


def preset(unit: str, files: Iterable[Path]) -> str | None:
"""The verb ``systemctl preset`` applies to *unit*, or None if no rule matches.

systemd reads preset files in filename order across all directories and the
first matching rule wins.
"""
for path in sorted(files, key=lambda path: path.name):
for verb, pattern in preset_rules(path):
if fnmatch.fnmatchcase(unit, pattern):
return verb
return None


class Tmpfile(NamedTuple):
type: str
path: str
mode: str = "-"
user: str = "-"
group: str = "-"
age: str = "-"
argument: str = "-"


def tmpfiles(path: Path) -> list[Tmpfile]:
"""The rules of a tmpfiles.d file; omitted trailing fields read as ``-``."""
return [
Tmpfile(*line.split(None, 6))
for line in (raw.strip() for raw in path.read_text(encoding="utf-8").splitlines())
if line and not line.startswith("#")
]
21 changes: 21 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Shared fixtures for the unit tests."""

import importlib.util
import os
import shutil
import sys
from pathlib import Path

Expand All @@ -10,6 +12,25 @@
SCRIPT_PATH = REPO_ROOT / ".github" / "scripts" / "docs-checks.py"


def _required_tool(name: str) -> str:
path = shutil.which(name)
if path is None:
if os.environ.get("CI") == "true":
pytest.fail(f"{name} is not installed; .github/workflows/unit-tests.yml must install it")
pytest.skip(f"{name} is not installed")
return path


@pytest.fixture(scope="session")
def node() -> str:
return _required_tool("node")


@pytest.fixture(scope="session")
def shellcheck() -> str:
return _required_tool("shellcheck")


def _load_module():
spec = importlib.util.spec_from_file_location("docs_checks", SCRIPT_PATH)
module = importlib.util.module_from_spec(spec)
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/os-justfile_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
# Results that must outlive the namespace (the call log) are copied back
# into BATS_TEST_TMPDIR, which is outside both tmpfs masks.

# skip_unless_ci <reason> -- skip locally, but fail in CI (CI=true), where a
# skip would silently drop every k8s recipe test from the gate.
skip_unless_ci() {
if [ "${CI:-}" = true ]; then
echo "CI must run this test, but: $1" >&2
return 1
fi
skip "$1"
}

setup() {
if ! unshare --map-root-user --mount true 2>/dev/null; then
skip "unprivileged user+mount namespaces are unavailable"
skip_unless_ci "unprivileged user+mount namespaces are unavailable"
fi
if ! command -v just >/dev/null 2>&1; then
skip "just is not installed"
skip_unless_ci "just is not installed"
fi

REPO_ROOT="$(cd "${BATS_TEST_DIRNAME}/../.." && pwd)"
Expand Down
Loading