161 lines
5.4 KiB
Python
161 lines
5.4 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from l4d2web.app import create_app
|
|
from l4d2web.auth import hash_password
|
|
from l4d2web.db import init_db, session_scope
|
|
from l4d2web.models import Blueprint, BlueprintOverlay, Overlay, Server, User
|
|
from l4d2web.services.host_commands import CommandResult
|
|
|
|
|
|
@pytest.fixture
|
|
def server_with_blueprint(tmp_path, monkeypatch):
|
|
db_url = f"sqlite:///{tmp_path/'facade.db'}"
|
|
monkeypatch.setenv("DATABASE_URL", db_url)
|
|
app = create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"})
|
|
init_db()
|
|
|
|
with app.app_context():
|
|
with session_scope() as session:
|
|
user = User(username="alice", password_digest=hash_password("secret"), admin=False)
|
|
session.add(user)
|
|
session.flush()
|
|
|
|
overlay = Overlay(name="Standard Overlay", path="standard")
|
|
session.add(overlay)
|
|
session.flush()
|
|
|
|
blueprint = Blueprint(
|
|
user_id=user.id,
|
|
name="default",
|
|
arguments='["-tickrate 100"]',
|
|
config='["sv_consistency 1"]',
|
|
)
|
|
session.add(blueprint)
|
|
session.flush()
|
|
|
|
session.add(BlueprintOverlay(blueprint_id=blueprint.id, overlay_id=overlay.id, position=0))
|
|
|
|
server = Server(user_id=user.id, blueprint_id=blueprint.id, name="alpha", port=27015)
|
|
session.add(server)
|
|
session.flush()
|
|
server_id = server.id
|
|
|
|
return server_id
|
|
|
|
|
|
def test_initialize_uses_l4d2ctl_with_latest_blueprint_data(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
server_with_blueprint,
|
|
) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
def fake_run_command(cmd, **kwargs):
|
|
del kwargs
|
|
calls.append(list(cmd))
|
|
spec_path = Path(cmd[cmd.index("-f") + 1])
|
|
spec = spec_path.read_text()
|
|
assert "sv_consistency 1" in spec
|
|
assert "standard" in spec
|
|
assert "Standard Overlay" not in spec
|
|
return CommandResult(returncode=0, stdout="", stderr="")
|
|
|
|
monkeypatch.setattr("l4d2web.services.host_commands.run_command", fake_run_command)
|
|
monkeypatch.setattr(
|
|
"l4d2web.services.l4d2_facade.initialize_instance",
|
|
lambda *args, **kwargs: pytest.fail("facade must not call l4d2host.initialize_instance directly"),
|
|
raising=False,
|
|
)
|
|
|
|
from l4d2web.services.l4d2_facade import initialize_server
|
|
|
|
initialize_server(server_with_blueprint)
|
|
|
|
assert calls[0][:3] == ["l4d2ctl", "initialize", "alpha"]
|
|
assert calls[0][3] == "-f"
|
|
|
|
|
|
def test_install_and_lifecycle_commands_use_l4d2ctl(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
server_with_blueprint,
|
|
) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
def fake_run_command(cmd, **kwargs):
|
|
del kwargs
|
|
calls.append(list(cmd))
|
|
return CommandResult(returncode=0, stdout="", stderr="")
|
|
|
|
monkeypatch.setattr("l4d2web.services.host_commands.run_command", fake_run_command)
|
|
for name in ["SteamInstaller", "start_instance", "stop_instance", "delete_instance"]:
|
|
monkeypatch.setattr(
|
|
f"l4d2web.services.l4d2_facade.{name}",
|
|
lambda *args, **kwargs: pytest.fail(f"facade must not call l4d2host {name} directly"),
|
|
raising=False,
|
|
)
|
|
|
|
from l4d2web.services.l4d2_facade import delete_server, install_runtime, start_server, stop_server
|
|
|
|
install_runtime()
|
|
start_server(server_with_blueprint)
|
|
stop_server(server_with_blueprint)
|
|
delete_server(server_with_blueprint)
|
|
|
|
assert calls == [
|
|
["l4d2ctl", "install"],
|
|
["l4d2ctl", "start", "alpha"],
|
|
["l4d2ctl", "stop", "alpha"],
|
|
["l4d2ctl", "delete", "alpha"],
|
|
]
|
|
|
|
|
|
def test_server_status_parses_l4d2ctl_json(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
def fake_run_command(cmd, **kwargs):
|
|
del kwargs
|
|
calls.append(list(cmd))
|
|
return CommandResult(
|
|
returncode=0,
|
|
stdout='{"state":"running","raw_active_state":"active","raw_sub_state":"running"}',
|
|
stderr="",
|
|
)
|
|
|
|
monkeypatch.setattr("l4d2web.services.host_commands.run_command", fake_run_command)
|
|
monkeypatch.setattr(
|
|
"l4d2web.services.l4d2_facade.get_instance_status",
|
|
lambda *args, **kwargs: pytest.fail("facade must not call l4d2host.get_instance_status directly"),
|
|
raising=False,
|
|
)
|
|
|
|
from l4d2web.services.l4d2_facade import server_status
|
|
|
|
status = server_status("alpha")
|
|
|
|
assert calls == [["l4d2ctl", "status", "alpha", "--json"]]
|
|
assert status.state == "running"
|
|
assert status.raw_active_state == "active"
|
|
assert status.raw_sub_state == "running"
|
|
|
|
|
|
def test_server_logs_stream_l4d2ctl_logs(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[list[str]] = []
|
|
|
|
def fake_stream_command(cmd):
|
|
calls.append(list(cmd))
|
|
return iter(["one", "two"])
|
|
|
|
monkeypatch.setattr("l4d2web.services.host_commands.stream_command", fake_stream_command)
|
|
monkeypatch.setattr(
|
|
"l4d2web.services.l4d2_facade.stream_instance_logs",
|
|
lambda *args, **kwargs: pytest.fail("facade must not call l4d2host.stream_instance_logs directly"),
|
|
raising=False,
|
|
)
|
|
|
|
from l4d2web.services.l4d2_facade import stream_server_logs
|
|
|
|
lines = list(stream_server_logs("alpha", lines=10, follow=False))
|
|
|
|
assert calls == [["l4d2ctl", "logs", "alpha", "--lines", "10", "--no-follow"]]
|
|
assert lines == ["one", "two"]
|