mirror of
https://github.com/deunix-educ/voice-assistant.git
synced 2026-11-26 20:19:43 +01:00
F7 : le serveur démarre avec la machine, comme service systemd (make server-install)
server/voice-server.service : unité modèle (@USER@, @SERVER_DIR@) ; le serveur tourne sous le compte de l'utilisateur dans server/, comme avec make run. Démarrage après le réseau ; relancé après 5 s en cas de plantage, pas relancé quand la configuration ou les modèles sont à corriger (RestartPreventExitStatus=78). Protections légères (NoNewPrivileges, PrivateTmp, ProtectSystem=full...). Le serveur démarre sans broker et s'y reconnecte seul ; SIGTERM le déconnecte proprement. __main__.py : EXIT_SETUP = 78 (EX_CONFIG) pour une configuration invalide, des dépendances ou des modèles absents. Makefile : server-install (sudo, refuse si un serveur tourne déjà), server-uninstall, server-status, server-log, server-unit (affiche l'unité sans rien installer), server-guard : make run et make run-echo refusent de démarrer tant que le service tourne (deux serveurs de même identifiant MQTT se chassent du broker en boucle). Tests : rendu de l'unité, systemd-analyze verify sans avertissement, code 78 cohérent avec l'unité, garde de make run (systemctl simulé). Vérifié sans sudo sous systemd-run --user avec les mêmes protections. CONTEXT.md : prompt de référence supprimé par l'utilisateur (resté dans l'historique). La suppression du fichier n'est pas dans ce commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from voice_server.__main__ import main
|
||||
from voice_server.__main__ import EXIT_SETUP, main
|
||||
from voice_server.settings import read_config
|
||||
|
||||
SERVER_DIR = Path(__file__).resolve().parents[1]
|
||||
@@ -213,7 +213,7 @@ def test_mqtt_host_stays_readable_by_the_makefile() -> None:
|
||||
def test_server_refuses_to_start_on_a_conflict_and_says_why(config_copy: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
||||
"""Deux fichiers définissent access.default : pas de trace d'appels, le message nomme les deux fichiers."""
|
||||
config_copy.write_text(config_copy.read_text(encoding="utf-8") + "\naccess:\n default: standard\n", encoding="utf-8")
|
||||
assert main(["--config", str(config_copy)]) == 1
|
||||
assert main(["--config", str(config_copy)]) == EXIT_SETUP
|
||||
error = capsys.readouterr().err
|
||||
assert "configuration invalide" in error
|
||||
assert "« access.default » est défini à la fois dans" in error
|
||||
@@ -222,5 +222,5 @@ def test_server_refuses_to_start_on_a_conflict_and_says_why(config_copy: Path, c
|
||||
|
||||
def test_server_refuses_to_start_on_a_missing_included_file(config_copy: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
||||
(config_copy.parent / "conf" / "home.yaml").unlink()
|
||||
assert main(["--config", str(config_copy)]) == 1
|
||||
assert main(["--config", str(config_copy)]) == EXIT_SETUP
|
||||
assert "fichier introuvable" in capsys.readouterr().err
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Étape F7 : l'unité systemd du serveur (server/voice-server.service) et les cibles du Makefile qui l'installent."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import getpass
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from voice_server.__main__ import EXIT_SETUP
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
|
||||
pytestmark = pytest.mark.skipif(shutil.which("make") is None, reason="make absent")
|
||||
|
||||
|
||||
def make(*args: str, path: str | None = None) -> subprocess.CompletedProcess[str]:
|
||||
"""Lance make à la racine du dépôt, avec un PATH choisi au besoin."""
|
||||
env = {**os.environ, "PATH": path} if path else None
|
||||
return subprocess.run(["make", "-s", *args], cwd=REPO, capture_output=True, text=True, env=env, check=False)
|
||||
|
||||
|
||||
def render(folder: Path, user: str = "alice") -> str:
|
||||
result = make("server-unit", f"SERVICE_USER={user}", f"SERVER_DIR={folder}")
|
||||
assert result.returncode == 0, result.stderr
|
||||
return result.stdout
|
||||
|
||||
|
||||
def test_unit_is_rendered_for_the_given_user_and_folder(tmp_path: Path) -> None:
|
||||
text = render(tmp_path)
|
||||
|
||||
assert "@USER@" not in text and "@SERVER_DIR@" not in text
|
||||
assert "User=alice" in text
|
||||
assert f"WorkingDirectory={tmp_path}" in text
|
||||
assert f"ExecStart={tmp_path}/.venv/bin/python -m voice_server --config config.yaml" in text
|
||||
|
||||
|
||||
def test_unit_does_not_restart_when_the_setup_is_wrong(tmp_path: Path) -> None:
|
||||
"""Configuration ou modèles à corriger : le code de sortie du serveur est celui que systemd ne relance pas."""
|
||||
lines = render(tmp_path).splitlines()
|
||||
|
||||
assert f"RestartPreventExitStatus={EXIT_SETUP}" in lines
|
||||
assert "Restart=on-failure" in lines
|
||||
|
||||
|
||||
@pytest.mark.skipif(shutil.which("systemd-analyze") is None, reason="systemd-analyze absent")
|
||||
def test_unit_passes_systemd_analyze(tmp_path: Path) -> None:
|
||||
"""systemd lit l'unité sans erreur ni avertissement (programme, dossier et compte existent)."""
|
||||
python = tmp_path / ".venv" / "bin" / "python"
|
||||
python.parent.mkdir(parents=True)
|
||||
python.symlink_to(Path(sys.executable).resolve())
|
||||
unit = tmp_path / "voice-server.service"
|
||||
unit.write_text(render(tmp_path, user=getpass.getuser()), encoding="utf-8")
|
||||
|
||||
result = subprocess.run(["systemd-analyze", "verify", str(unit)], capture_output=True, text=True, check=False)
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stderr.strip() == ""
|
||||
|
||||
|
||||
def fake_systemctl(folder: Path, exit_code: int) -> str:
|
||||
"""PATH où « systemctl » répond toujours exit_code (0 = le service est actif, 3 = inactif)."""
|
||||
script = folder / "systemctl"
|
||||
script.write_text(f"#!/bin/sh\nexit {exit_code}\n", encoding="utf-8")
|
||||
script.chmod(0o755)
|
||||
return f"{folder}{os.pathsep}{os.environ['PATH']}"
|
||||
|
||||
|
||||
def test_manual_run_is_refused_while_the_service_runs(tmp_path: Path) -> None:
|
||||
"""Deux serveurs de même identifiant MQTT se chassent du broker : la garde de « make run » refuse."""
|
||||
result = make("server-guard", path=fake_systemctl(tmp_path, 0))
|
||||
|
||||
assert result.returncode != 0
|
||||
assert "tourne deja" in result.stdout + result.stderr
|
||||
|
||||
|
||||
def test_manual_run_is_allowed_when_the_service_is_stopped(tmp_path: Path) -> None:
|
||||
assert make("server-guard", path=fake_systemctl(tmp_path, 3)).returncode == 0
|
||||
|
||||
|
||||
def test_run_depends_on_the_guard() -> None:
|
||||
"""« make run » et « make run-echo » passent par la garde (make -n n'exécute rien)."""
|
||||
for target in ("run", "run-echo"):
|
||||
result = subprocess.run(["make", "-n", target], cwd=REPO, capture_output=True, text=True, check=False)
|
||||
assert "systemctl is-active --quiet voice-server" in result.stdout, target
|
||||
Reference in New Issue
Block a user