working on tests

This commit is contained in:
Jev
2026-04-08 00:07:36 +02:00
parent f488af4959
commit 6f8b88898f
4 changed files with 203 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
import subprocess
import time
from collections.abc import Generator
import pytest
CONTAINER = "cli-tools-test"
IMAGE = "images:debian/bookworm"
REPO_ROOT = "/home/jev/projects/cli-tools"
def _lxc(*args: str, capture: bool = False) -> subprocess.CompletedProcess:
return subprocess.run(
["lxc", *args],
check=True,
capture_output=capture,
text=capture,
)
def _wait_for_network(container: str, retries: int = 20, delay: float = 2.0) -> None:
print("[container] Waiting for network...", flush=True)
for _ in range(retries):
result = subprocess.run(
["lxc", "exec", container, "--", "bash", "-c", "ping -c1 8.8.8.8 &>/dev/null"],
capture_output=True,
)
if result.returncode == 0:
print("[container] Network ready", flush=True)
return
time.sleep(delay)
raise RuntimeError(f"Container {container!r} did not get network access")
@pytest.fixture(scope="session")
def container() -> Generator[str, None, None]:
# Clean up any stale container from a previous run
subprocess.run(["lxc", "delete", "--force", CONTAINER], capture_output=True)
print(f"\n[container] Launching {CONTAINER}...", flush=True)
_lxc("launch", IMAGE, CONTAINER)
time.sleep(3)
_wait_for_network(CONTAINER)
print("[container] Installing sudo and python3-pip...", flush=True)
_lxc(
"exec", CONTAINER, "--",
"bash", "-c",
"""
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq sudo python3-pip
""",
)
print("[container] Creating jev user...", flush=True)
_lxc(
"exec", CONTAINER, "--",
"bash", "-c",
"""
useradd -m -s /bin/bash -u 1000 jev
usermod -aG sudo jev
echo 'jev ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/jev
chmod 440 /etc/sudoers.d/jev
""",
)
print("[container] Installing invoke...", flush=True)
_lxc(
"exec", CONTAINER, "--",
"bash", "-c",
"pip3 install invoke --quiet --break-system-packages",
)
print("[container] Mounting repo...", flush=True)
_lxc("config", "device", "add", CONTAINER, "repo", "disk",
f"source={REPO_ROOT}", "path=/home/jev/cli-tools")
print("[container] Ready\n", flush=True)
yield CONTAINER
print(f"\n[container] Destroying {CONTAINER}...", flush=True)
_lxc("delete", "--force", CONTAINER)
+45
View File
@@ -0,0 +1,45 @@
import subprocess
def lxc_exec(container: str, cmd: str, check: bool = True) -> subprocess.CompletedProcess:
"""Run a command in the container as the jev user."""
return subprocess.run(
["lxc", "exec", container, "--user", "1000", "--", "bash", "-lc", cmd],
check=check,
)
def run_task(container: str, task: str) -> None:
lxc_exec(container, f"cd /home/jev/cli-tools && invoke {task.replace('_', '-')}")
def test_install_apt_packages(container: str) -> None:
run_task(container, "install_apt_packages")
for binary in ["git", "micro", "tree", "detox"]:
result = lxc_exec(container, f"which {binary}", check=False)
assert result.returncode == 0, f"{binary!r} not found after install_apt_packages"
def test_install_docker(container: str) -> None:
run_task(container, "install_docker")
result = lxc_exec(container, "docker --version", check=False)
assert result.returncode == 0, "docker not found after install_docker"
def test_install_uv(container: str) -> None:
run_task(container, "install_uv")
result = lxc_exec(container, "~/.local/bin/uv --version", check=False)
assert result.returncode == 0, "uv not found after install_uv"
def test_install_fzf(container: str) -> None:
run_task(container, "install_fzf")
for binary in ["fzf", "batcat"]:
result = lxc_exec(container, f"which {binary}", check=False)
assert result.returncode == 0, f"{binary!r} not found after install_fzf"
def test_install_zoxide(container: str) -> None:
run_task(container, "install_zoxide")
result = lxc_exec(container, "which zoxide", check=False)
assert result.returncode == 0, "zoxide not found after install_zoxide"