Files
grm/tests/unit/test_registry.py
T
emil 34742bab40
Post-merge / detect-type (push) Successful in 53s
Post-merge / release (push) Successful in 1m14s
Post-merge / validate-commit-msg (push) Successful in 1m25s
Post-merge / vikunja (push) Successful in 1m21s
Post-merge / badges (push) Successful in 1m45s
Post-merge / configure-repo (push) Successful in 1m15s
Post-merge / sync-wiki (push) Successful in 3m5s
Post-merge / publish (push) Successful in 1m1s
GRM-136: refactor: rename PyPI package from gitea-runner-manager to grm
2026-07-06 06:06:13 +00:00

100 lines
3.5 KiB
Python

"""Unit tests for the runner registry."""
from __future__ import annotations
from pathlib import Path
import pytest
from grm.exceptions import GRMError
from grm.registry import RunnerRegistry
class TestRunnerRegistry:
def test_init_default_path(self) -> None:
reg = RunnerRegistry()
assert reg._path == Path.home() / ".local" / "share" / "grm" / "runners.json"
def test_add_and_get(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add(
"r1",
host="10.0.0.1",
user="ubuntu",
key="/key",
gitea_url="https://git.example.com",
labels="docker:docker://alpine:latest",
)
info = reg.get("r1")
assert info is not None
assert info["host"] == "10.0.0.1"
assert info["user"] == "ubuntu"
assert info["key"] == "/key"
assert info["gitea_url"] == "https://git.example.com"
assert info["labels"] == "docker:docker://alpine:latest"
assert "created_at" in info
def test_add_without_labels(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add("r1", host="10.0.0.1", user="ubuntu")
info = reg.get("r1")
assert info is not None
assert info["labels"] is None
def test_get_missing(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
assert reg.get("missing") is None
def test_remove(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add("r1", host="10.0.0.1", user="ubuntu")
reg.remove("r1")
assert reg.get("r1") is None
def test_list(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add("r1", host="10.0.0.1", user="ubuntu")
reg.add("r2", host="10.0.0.2", user="root")
runners = reg.list()
assert len(runners) == 2
assert runners["r1"]["host"] == "10.0.0.1"
assert runners["r2"]["host"] == "10.0.0.2"
def test_update(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add("r1", host="10.0.0.1", user="ubuntu")
reg.update("r1", host="10.0.0.2")
info = reg.get("r1")
assert info is not None
assert info["host"] == "10.0.0.2"
assert info["user"] == "ubuntu"
def test_update_missing(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
with pytest.raises(GRMError, match="not found in registry"):
reg.update("missing", host="10.0.0.1")
def test_persistence(self, tmp_path: Path) -> None:
path = tmp_path / "runners.json"
reg1 = RunnerRegistry(path)
reg1.add("r1", host="10.0.0.1", user="ubuntu")
reg2 = RunnerRegistry(path)
info = reg2.get("r1")
assert info is not None
assert info["host"] == "10.0.0.1"
def test_corrupt_file(self, tmp_path: Path) -> None:
path = tmp_path / "runners.json"
path.write_text("not json")
reg = RunnerRegistry(path)
assert reg.list() == {}
def test_list_returns_copy(self, tmp_path: Path) -> None:
reg = RunnerRegistry(tmp_path / "runners.json")
reg.add("r1", host="10.0.0.1", user="ubuntu")
runners = reg.list()
runners["r1"]["host"] = "modified"
info = reg.get("r1")
assert info is not None
assert info["host"] == "10.0.0.1"