test: verify go services are enabled

This commit is contained in:
lingniu
2026-07-02 01:55:39 +08:00
parent f4d9c9ef5a
commit 5b7f386c5a
4 changed files with 44 additions and 16 deletions

View File

@@ -32,12 +32,19 @@ class Check:
message: str
def parse_service_states(output: str) -> dict[str, str]:
states: dict[str, str] = {}
@dataclass(frozen=True)
class ServiceState:
active: str
enabled: str
def parse_service_states(output: str) -> dict[str, ServiceState]:
states: dict[str, ServiceState] = {}
for line in output.splitlines():
parts = line.split()
if len(parts) >= 2:
states[parts[0]] = parts[1]
enabled = parts[2] if len(parts) >= 3 else "enabled"
states[parts[0]] = ServiceState(parts[1], enabled)
return states
@@ -45,9 +52,13 @@ def service_checks(output: str, services: list[str]) -> list[Check]:
states = parse_service_states(output)
checks: list[Check] = []
for service in services:
state = states.get(service, "missing")
status = "pass" if state == "active" else "fail"
checks.append(Check("service." + service, status, "state=" + state))
state = states.get(service, ServiceState("missing", "missing"))
status = "pass" if state.active == "active" and state.enabled == "enabled" else "fail"
checks.append(Check(
"service." + service,
status,
"state=" + state.active + "; enabled=" + state.enabled,
))
return checks
@@ -89,7 +100,9 @@ def remote_command(services: list[str]) -> str:
quoted = " ".join(services)
return (
"for svc in " + quoted + "; do "
"printf '%s ' \"$svc\"; systemctl is-active \"$svc\" || true; "
"printf '%s ' \"$svc\"; "
"printf '%s ' \"$(systemctl is-active \"$svc\" 2>/dev/null || true)\"; "
"systemctl is-enabled \"$svc\" 2>/dev/null || true; "
"done; "
"printf '\\n--PORTS--\\n'; "
"ss -lntp"