feat(Scripts): Add health and console commands to service manager and corresponding tests (#22814)

This commit is contained in:
Yehonal
2025-09-06 15:10:42 +02:00
committed by GitHub
parent d3a6c09b31
commit 119af32649
4 changed files with 477 additions and 3 deletions

View File

@@ -143,6 +143,130 @@ teardown() {
[[ "$output" =~ "on-failure|always" ]]
}
@test "service-manager: help lists health and console commands" {
run "$SCRIPT_DIR/service-manager.sh" help
[ "$status" -eq 0 ]
[[ "$output" =~ "is-running <service-name>" ]]
[[ "$output" =~ "uptime-seconds <service-name>" ]]
[[ "$output" =~ "wait-uptime <service> <sec>" ]]
[[ "$output" =~ "send <service-name>" ]]
[[ "$output" =~ "show-config <service-name>" ]]
}
@test "service-manager: pm2 uptime and wait-uptime work with mocked pm2" {
command -v jq >/dev/null 2>&1 || skip "jq not installed"
export AC_SERVICE_CONFIG_DIR="$TEST_DIR/services"
mkdir -p "$AC_SERVICE_CONFIG_DIR"
# Create registry with pm2 provider service
cat > "$AC_SERVICE_CONFIG_DIR/service_registry.json" << 'EOF'
[
{"name":"test-world","provider":"pm2","type":"service","bin_path":"/bin/worldserver","args":"","systemd_type":"--user","restart_policy":"always"}
]
EOF
# Create minimal service config and run-engine config files required by 'send'
echo "RUN_ENGINE_CONFIG_FILE=\"$AC_SERVICE_CONFIG_DIR/test-world-run-engine.conf\"" > "$AC_SERVICE_CONFIG_DIR/test-world.conf"
cat > "$AC_SERVICE_CONFIG_DIR/test-world-run-engine.conf" << 'EOF'
export SESSION_MANAGER="none"
export SESSION_NAME="test-world"
EOF
# Mock pm2
cat > "$TEST_DIR/bin/pm2" << 'EOF'
#!/usr/bin/env bash
case "$1" in
jlist)
# Produce a JSON with uptime ~20 seconds
if date +%s%N >/dev/null 2>&1; then
nowms=$(( $(date +%s%N) / 1000000 ))
else
nowms=$(( $(date +%s) * 1000 ))
fi
up=$(( nowms - 20000 ))
echo "[{\"name\":\"test-world\",\"pm2_env\":{\"status\":\"online\",\"pm_uptime\":$up}}]"
;;
id)
echo "[1]"
;;
attach|send|list|describe|logs)
exit 0
;;
*)
exit 0
;;
esac
EOF
chmod +x "$TEST_DIR/bin/pm2"
run "$SCRIPT_DIR/service-manager.sh" uptime-seconds test-world
debug_on_failure
[ "$status" -eq 0 ]
# Output should be a number >= 10
[[ "$output" =~ ^[0-9]+$ ]]
[ "$output" -ge 10 ]
run "$SCRIPT_DIR/service-manager.sh" wait-uptime test-world 10 5
debug_on_failure
[ "$status" -eq 0 ]
}
@test "service-manager: send works under pm2 with mocked pm2" {
command -v jq >/dev/null 2>&1 || skip "jq not installed"
export AC_SERVICE_CONFIG_DIR="$TEST_DIR/services"
mkdir -p "$AC_SERVICE_CONFIG_DIR"
# Create registry and config as in previous test
cat > "$AC_SERVICE_CONFIG_DIR/service_registry.json" << 'EOF'
[
{"name":"test-world","provider":"pm2","type":"service","bin_path":"/bin/worldserver","args":"","systemd_type":"--user","restart_policy":"always"}
]
EOF
echo "RUN_ENGINE_CONFIG_FILE=\"$AC_SERVICE_CONFIG_DIR/test-world-run-engine.conf\"" > "$AC_SERVICE_CONFIG_DIR/test-world.conf"
cat > "$AC_SERVICE_CONFIG_DIR/test-world-run-engine.conf" << 'EOF'
export SESSION_MANAGER="none"
export SESSION_NAME="test-world"
EOF
# pm2 mock
cat > "$TEST_DIR/bin/pm2" << 'EOF'
#!/usr/bin/env bash
case "$1" in
jlist)
if date +%s%N >/dev/null 2>&1; then
nowms=$(( $(date +%s%N) / 1000000 ))
else
nowms=$(( $(date +%s) * 1000 ))
fi
up=$(( nowms - 15000 ))
echo "[{\"name\":\"test-world\",\"pm2_env\":{\"status\":\"online\",\"pm_uptime\":$up}}]"
;;
id)
echo "[1]"
;;
send)
# simulate success
exit 0
;;
attach|list|describe|logs)
exit 0
;;
*)
exit 0
;;
esac
EOF
chmod +x "$TEST_DIR/bin/pm2"
run "$SCRIPT_DIR/service-manager.sh" send test-world "server info"
debug_on_failure
[ "$status" -eq 0 ]
}
@test "service-manager: wait-uptime times out for unknown service" {
command -v jq >/dev/null 2>&1 || skip "jq not installed"
export AC_SERVICE_CONFIG_DIR="$TEST_DIR/services"
mkdir -p "$AC_SERVICE_CONFIG_DIR"
echo "[]" > "$AC_SERVICE_CONFIG_DIR/service_registry.json"
run "$SCRIPT_DIR/service-manager.sh" wait-uptime unknown 2 1
[ "$status" -ne 0 ]
}
# ===== EXAMPLE SCRIPTS TESTS =====
@test "examples: restarter-world should show configuration error" {