Files
grm/tests/unit/test_registry.py
T

86 lines
3.1 KiB
Python

"""Unit tests for the runner registry."""
from __future__ import annotations
from pathlib import Path
import pytest
from gitea_runner_manager.exceptions import GRMError
from gitea_runner_manager.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", mode="docker", gitea_url="https://git.example.com")
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["mode"] == "docker"
assert info["gitea_url"] == "https://git.example.com"
assert "created_at" in info
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"