feat: wire native MariaDB port takeover into install_db.sh when PORT=3306
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
REAL_SCRIPT="$PLUGIN_DIR/scripts/install_db.sh"
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
trap 'rm -rf "$TMP_DIR"' EXIT
|
||||
|
||||
fail() {
|
||||
echo "FAIL: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Extract the exact, real maybe_takeover_native_port() function body from the
|
||||
# real script (rather than reimplementing it), so this test exercises the
|
||||
# actual production logic without triggering the script's own main() (which
|
||||
# needs a full install environment).
|
||||
FUNCS_FILE="$TMP_DIR/funcs.sh"
|
||||
{
|
||||
echo '#!/bin/bash'
|
||||
echo 'log() { echo "[INFO] $*"; }'
|
||||
echo 'die() { echo "[BŁĄD] $*" >&2; exit 1; }'
|
||||
awk '/^maybe_takeover_native_port\(\)/,/^\}/' "$REAL_SCRIPT"
|
||||
} > "$FUNCS_FILE"
|
||||
|
||||
grep -q '^maybe_takeover_native_port()' "$FUNCS_FILE" \
|
||||
|| fail "could not extract maybe_takeover_native_port() from $REAL_SCRIPT"
|
||||
|
||||
# shellcheck source=/dev/null
|
||||
source "$FUNCS_FILE"
|
||||
|
||||
mkdir -p "$TMP_DIR/setup"
|
||||
CALL_LOG="$TMP_DIR/calls.log"
|
||||
cat > "$TMP_DIR/setup/native_mariadb_port_takeover.sh" <<EOF
|
||||
#!/bin/bash
|
||||
echo "CALLED" >> "$CALL_LOG"
|
||||
exit 0
|
||||
EOF
|
||||
chmod 755 "$TMP_DIR/setup/native_mariadb_port_takeover.sh"
|
||||
|
||||
NATIVE_TAKEOVER_SCRIPT="$TMP_DIR/setup/native_mariadb_port_takeover.sh"
|
||||
|
||||
# --- Scenario 1: PORT=3306 must call the takeover script ---
|
||||
PORT="3306"
|
||||
: > "$CALL_LOG"
|
||||
maybe_takeover_native_port
|
||||
grep -Fxq "CALLED" "$CALL_LOG" || fail "expected takeover script to be called when PORT=3306"
|
||||
|
||||
# --- Scenario 2: PORT!=3306 must NOT call the takeover script ---
|
||||
PORT="33033"
|
||||
: > "$CALL_LOG"
|
||||
maybe_takeover_native_port
|
||||
[ ! -s "$CALL_LOG" ] || fail "expected takeover script NOT to be called when PORT=33033"
|
||||
|
||||
# --- Scenario 3: takeover script failure must abort (non-zero exit) ---
|
||||
cat > "$TMP_DIR/setup/native_mariadb_port_takeover.sh" <<EOF
|
||||
#!/bin/bash
|
||||
echo "CALLED-FAIL" >> "$CALL_LOG"
|
||||
exit 1
|
||||
EOF
|
||||
chmod 755 "$TMP_DIR/setup/native_mariadb_port_takeover.sh"
|
||||
|
||||
PORT="3306"
|
||||
set +e
|
||||
FAIL_OUTPUT="$(maybe_takeover_native_port 2>&1)"
|
||||
FAIL_STATUS=$?
|
||||
set -e
|
||||
[ "$FAIL_STATUS" -ne 0 ] || fail "maybe_takeover_native_port should fail when the takeover script fails"
|
||||
printf '%s\n' "$FAIL_OUTPUT" | grep -Fq "Przejęcie portu" \
|
||||
|| fail "expected a clear error message on takeover failure, got: $FAIL_OUTPUT"
|
||||
|
||||
echo "install_db_native_takeover_wiring_test: OK"
|
||||
Reference in New Issue
Block a user