You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
318 lines
12 KiB
Python
318 lines
12 KiB
Python
"""First-run database and update routes with isolated in-memory dependencies."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import stat
|
|
import sys
|
|
import threading
|
|
import types
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
# The HTTP boundary is tested with fake VM services; native libvirt is not
|
|
# available on ordinary development machines.
|
|
try:
|
|
import libvirt # noqa: F401
|
|
except ModuleNotFoundError:
|
|
stub = types.ModuleType("libvirt")
|
|
stub.libvirtError = type("libvirtError", (Exception,), {})
|
|
sys.modules["libvirt"] = stub
|
|
|
|
from virsh_panel import app as app_module
|
|
from virsh_panel.config import Settings
|
|
from virsh_panel.database import DatabasePanelState, DatabaseTemplateStore
|
|
|
|
|
|
class MemoryStore:
|
|
def __init__(self) -> None:
|
|
self.records = {}
|
|
|
|
def get(self, kind, key):
|
|
return self.records.get((kind, key))
|
|
|
|
def set(self, kind, key, value):
|
|
self.records[(kind, key)] = value
|
|
|
|
def insert(self, kind, key, value):
|
|
if (kind, key) in self.records:
|
|
return False
|
|
self.set(kind, key, value)
|
|
return True
|
|
|
|
def delete(self, kind, key):
|
|
self.records.pop((kind, key), None)
|
|
|
|
def list(self, kind):
|
|
return {key: value for (record_type, key), value in self.records.items()
|
|
if record_type == kind}
|
|
|
|
def get_secret(self, key):
|
|
return self.get("secret", key)
|
|
|
|
def set_secret(self, key, value):
|
|
self.set("secret", key, value)
|
|
|
|
def get_value(self, key):
|
|
return self.get("value", key)
|
|
|
|
def set_value(self, key, value):
|
|
self.set("value", key, value)
|
|
|
|
|
|
class FakeManager:
|
|
def current(self):
|
|
return None
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
def test_first_run_requires_database_then_admin_and_ignores_old_json(monkeypatch, tmp_path):
|
|
data_dir = tmp_path / "data"
|
|
data_dir.mkdir(mode=0o700)
|
|
(data_dir / "state.json").write_text(
|
|
'{"version":1,"admin":{"username":"old","password_hash":"old"}}',
|
|
encoding="utf-8",
|
|
)
|
|
store = MemoryStore()
|
|
calls = []
|
|
|
|
def make_components(directory, config, key, _ssh_binary):
|
|
calls.append((config, key))
|
|
return (DatabasePanelState(directory, store), FakeManager(),
|
|
DatabaseTemplateStore(store), store)
|
|
|
|
monkeypatch.setattr(app_module, "make_components", make_components)
|
|
settings = Settings(data_dir=data_dir,
|
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
|
app = app_module.create_app(settings)
|
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
|
config = {
|
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
|
"username": "panel", "password": "test-database-password",
|
|
}
|
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
|
assert client.get("/api/v1/database/status").json() == {
|
|
"configured": False, "setup_required": True,
|
|
}
|
|
assert client.get("/api/v1/setup/status").status_code == 409
|
|
assert client.post("/api/v1/database/setup", json={
|
|
**config, "setup_token": "invalid",
|
|
}).status_code == 403
|
|
assert calls == []
|
|
response = client.post("/api/v1/database/setup", json={
|
|
**config, "setup_token": token,
|
|
})
|
|
assert response.status_code == 200
|
|
assert response.json() == {"configured": True}
|
|
assert calls[0][0] == config and len(calls[0][1]) == 32
|
|
assert client.get("/api/v1/setup/status").json() == {"setup_required": True}
|
|
created = client.post("/api/v1/setup/admin", json={
|
|
"token": token, "username": "admin",
|
|
"password": "correct-horse-battery-staple",
|
|
})
|
|
assert created.status_code == 200
|
|
assert client.get("/api/v1/setup/status").json() == {"setup_required": False}
|
|
session_cookie = client.cookies.get("virsh_panel_session")
|
|
assert ("admin", "singleton") in store.records
|
|
assert not (data_dir / "setup-token").exists()
|
|
encrypted = (data_dir / "database-config.json").read_bytes()
|
|
assert b"test-database-password" not in encrypted
|
|
assert stat.S_IMODE((data_dir / "master.key").stat().st_mode) == 0o600
|
|
store.set("import_job", "interrupted-job", json.dumps({
|
|
"status": "running", "scope": "previous-host",
|
|
}))
|
|
restarted = app_module.create_app(settings)
|
|
assert json.loads(store.get("import_job", "interrupted-job"))["status"] == "failed"
|
|
with TestClient(restarted, base_url="https://panel.example.test") as client:
|
|
client.cookies.set("virsh_panel_session", session_cookie)
|
|
assert client.get("/api/v1/session").json()["authenticated"] is True
|
|
|
|
|
|
def test_update_routes_require_auth_and_never_return_token(monkeypatch, tmp_path):
|
|
data_dir = tmp_path / "data"
|
|
store = MemoryStore()
|
|
monkeypatch.setattr(app_module, "make_components", lambda directory, *_: (
|
|
DatabasePanelState(directory, store), FakeManager(),
|
|
DatabaseTemplateStore(store), store,
|
|
))
|
|
settings = Settings(data_dir=data_dir,
|
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
|
app = app_module.create_app(settings)
|
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
|
|
|
class FakeUpdater:
|
|
def __init__(self, *_args):
|
|
self.token = None
|
|
self.job = None
|
|
|
|
def settings(self):
|
|
return {
|
|
"repository": "https://g.luoli.live/root/virt.git", "branch": "main",
|
|
"token_configured": self.token is not None, "checkout_ready": True,
|
|
"current_commit": "a" * 40, "active_job_id": None,
|
|
}
|
|
|
|
def set_token(self, token):
|
|
self.token = token
|
|
|
|
def check(self):
|
|
return {
|
|
"repository": "https://g.luoli.live/root/virt.git", "branch": "main",
|
|
"current_commit": "a" * 40, "remote_commit": "b" * 40,
|
|
"status": "update_available",
|
|
}
|
|
|
|
def create_job(self, *, expected_current_commit=None, expected_remote_commit=None):
|
|
assert expected_current_commit == "a" * 40
|
|
assert expected_remote_commit == "b" * 40
|
|
self.job = {"job_id": "test-job-00000001", "status": "queued"}
|
|
return self.job
|
|
|
|
def get_job(self, job_id):
|
|
return self.job if self.job and job_id == self.job["job_id"] else None
|
|
|
|
def fail_job(self, job_id, error):
|
|
self.job = {"job_id": job_id, "status": "failed", "error": error}
|
|
|
|
monkeypatch.setattr(app_module, "Updater", FakeUpdater)
|
|
monkeypatch.setattr(app_module.subprocess, "run", lambda *args, **kwargs:
|
|
SimpleNamespace(returncode=0, stdout="inactive"))
|
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
|
assert client.get("/api/v1/update/settings").status_code == 401
|
|
client.post("/api/v1/database/setup", json={
|
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
|
"username": "panel", "password": "test-database-password",
|
|
"setup_token": token,
|
|
})
|
|
response = client.post("/api/v1/setup/admin", json={
|
|
"token": token, "username": "admin",
|
|
"password": "correct-horse-battery-staple",
|
|
})
|
|
assert response.status_code == 200
|
|
csrf = response.json()["csrf_token"]
|
|
assert client.put("/api/v1/update/settings", json={
|
|
"access_token": "some-very-private-gitea-token",
|
|
}).status_code == 403
|
|
saved = client.put("/api/v1/update/settings", json={
|
|
"access_token": "some-very-private-gitea-token",
|
|
}, headers={"X-CSRF-Token": csrf})
|
|
assert saved.status_code == 200
|
|
assert saved.json()["configured"] is True
|
|
assert "some-very-private-gitea-token" not in saved.text
|
|
checked = client.post("/api/v1/update/check", headers={
|
|
"X-CSRF-Token": csrf,
|
|
})
|
|
assert checked.status_code == 200
|
|
assert checked.json()["update_available"] is True
|
|
queued = client.post("/api/v1/update/apply", json={
|
|
"expected_current_commit": "a" * 40,
|
|
"expected_remote_commit": "b" * 40,
|
|
}, headers={
|
|
"X-CSRF-Token": csrf,
|
|
})
|
|
assert queued.status_code == 202
|
|
job_id = queued.json()["job_id"]
|
|
assert client.get(f"/api/v1/update/jobs/{job_id}").json()["status"] == "queued"
|
|
|
|
|
|
def test_vm_creation_finishes_before_update_is_queued(monkeypatch, tmp_path):
|
|
data_dir = tmp_path / "data"
|
|
store = MemoryStore()
|
|
vm_started = threading.Event()
|
|
release_vm = threading.Event()
|
|
update_received = threading.Event()
|
|
update_queued = threading.Event()
|
|
|
|
class ConfiguredManager(FakeManager):
|
|
def current(self):
|
|
return {"key_locked": False}
|
|
|
|
class BlockingVMService:
|
|
def create_vm(self, _spec):
|
|
vm_started.set()
|
|
assert release_vm.wait(timeout=5), "test did not release VM creation"
|
|
return {"uuid": "00000000-0000-4000-8000-000000000001"}
|
|
|
|
class FakeUpdater:
|
|
def __init__(self, _repo_dir, data_store):
|
|
self.store = data_store
|
|
self.job = None
|
|
|
|
def create_job(self, **_kwargs):
|
|
self.job = {"job_id": "test-job-00000001", "status": "queued"}
|
|
self.store.set_value("update.active_job", self.job["job_id"])
|
|
update_queued.set()
|
|
return self.job
|
|
|
|
def get_job(self, job_id):
|
|
return self.job if self.job and job_id == self.job["job_id"] else None
|
|
|
|
def fail_job(self, _job_id, _message):
|
|
raise AssertionError("the mocked update service must start")
|
|
|
|
monkeypatch.setattr(app_module, "make_components", lambda directory, *_: (
|
|
DatabasePanelState(directory, store), ConfiguredManager(),
|
|
DatabaseTemplateStore(store), store,
|
|
))
|
|
monkeypatch.setattr(app_module, "Updater", FakeUpdater)
|
|
monkeypatch.setattr(
|
|
app_module.subprocess, "run",
|
|
lambda argv, **_kwargs: SimpleNamespace(
|
|
returncode=3 if "is-active" in argv else 0,
|
|
stdout="inactive" if "is-active" in argv else "",
|
|
),
|
|
)
|
|
settings = Settings(data_dir=data_dir,
|
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
|
app = app_module.create_app(settings, service_factory=lambda _profile: BlockingVMService())
|
|
|
|
@app.middleware("http")
|
|
async def observe_update(request, call_next):
|
|
if request.url.path == "/api/v1/update/apply":
|
|
update_received.set()
|
|
return await call_next(request)
|
|
|
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
|
configured = client.post("/api/v1/database/setup", json={
|
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
|
"username": "panel", "password": "test-database-password",
|
|
"setup_token": token,
|
|
})
|
|
assert configured.status_code == 200
|
|
admin = client.post("/api/v1/setup/admin", json={
|
|
"token": token, "username": "admin",
|
|
"password": "correct-horse-battery-staple",
|
|
})
|
|
assert admin.status_code == 200
|
|
headers = {"X-CSRF-Token": admin.json()["csrf_token"]}
|
|
with ThreadPoolExecutor(max_workers=2) as executor:
|
|
creating = executor.submit(client.post, "/api/v1/vms", json={
|
|
"name": "new-vm", "vcpu": 2, "memory_mib": 2048,
|
|
"disk_gib": 20, "network": "default", "iso": "AlmaLinux-10.iso",
|
|
}, headers=headers)
|
|
assert vm_started.wait(timeout=5)
|
|
updating = executor.submit(client.post, "/api/v1/update/apply", json={
|
|
"expected_current_commit": "a" * 40,
|
|
"expected_remote_commit": "b" * 40,
|
|
}, headers=headers)
|
|
try:
|
|
assert update_received.wait(timeout=5)
|
|
assert not update_queued.wait(timeout=0.2)
|
|
assert not updating.done()
|
|
finally:
|
|
release_vm.set()
|
|
assert creating.result(timeout=5).status_code == 201
|
|
assert updating.result(timeout=5).status_code == 202
|
|
assert update_queued.is_set()
|
|
blocked = client.post("/api/v1/vms", json={
|
|
"name": "another-vm", "vcpu": 2, "memory_mib": 2048,
|
|
"disk_gib": 20, "network": "default", "iso": "AlmaLinux-10.iso",
|
|
}, headers=headers)
|
|
assert blocked.status_code == 409
|