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"

View File

@@ -6,10 +6,10 @@ from tools import go_systemd_prod_smoke as smoke
class GoSystemdProdSmokeTest(unittest.TestCase):
def test_service_checks_require_all_go_units_active(self):
output = """
lingniu-go-gateway.service active
lingniu-go-history-writer.service active
lingniu-go-stat-writer.service active
lingniu-go-realtime-api.service active
lingniu-go-gateway.service active enabled
lingniu-go-history-writer.service active enabled
lingniu-go-stat-writer.service active enabled
lingniu-go-realtime-api.service active enabled
"""
checks = smoke.service_checks(output, smoke.DEFAULT_SERVICES)
@@ -18,8 +18,8 @@ lingniu-go-realtime-api.service active
def test_service_checks_fail_when_unit_is_missing_or_inactive(self):
output = """
lingniu-go-gateway.service active
lingniu-go-history-writer.service failed
lingniu-go-gateway.service active enabled
lingniu-go-history-writer.service failed enabled
"""
checks = smoke.service_checks(output, smoke.DEFAULT_SERVICES)
@@ -28,6 +28,20 @@ lingniu-go-history-writer.service failed
self.assertEqual(by_name["service.lingniu-go-history-writer.service"].status, "fail")
self.assertEqual(by_name["service.lingniu-go-stat-writer.service"].status, "fail")
def test_service_checks_fail_when_unit_is_not_enabled(self):
output = """
lingniu-go-gateway.service active disabled
lingniu-go-history-writer.service active enabled
lingniu-go-stat-writer.service active enabled
lingniu-go-realtime-api.service active enabled
"""
checks = smoke.service_checks(output, smoke.DEFAULT_SERVICES)
by_name = {check.name: check for check in checks}
self.assertEqual(by_name["service.lingniu-go-gateway.service"].status, "fail")
self.assertIn("enabled=disabled", by_name["service.lingniu-go-gateway.service"].message)
def test_port_checks_require_expected_go_processes(self):
output = """
LISTEN 0 4096 *:32960 *:* users:(("gateway",pid=10,fd=3))