feat: add detection functions for native MariaDB port takeover
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
SCRIPT="$PLUGIN_DIR/scripts/setup/native_mariadb_port_takeover.sh"
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
trap 'rm -rf "$TMP_DIR"' EXIT
|
||||
|
||||
fail() {
|
||||
echo "FAIL: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Source only the detection functions, without triggering main() (main() is
|
||||
# added in Task 4, guarded behind a BASH_SOURCE check).
|
||||
NATIVE_TAKEOVER_LOG="$TMP_DIR/takeover.log" \
|
||||
source "$SCRIPT"
|
||||
|
||||
# --- check_os_guard: AlmaLinux 9 must pass ---
|
||||
cat > "$TMP_DIR/os-release-alma9" <<'EOF'
|
||||
ID="almalinux"
|
||||
VERSION_ID="9.4"
|
||||
PRETTY_NAME="AlmaLinux 9.4"
|
||||
EOF
|
||||
OS_RELEASE_FILE="$TMP_DIR/os-release-alma9" check_os_guard \
|
||||
|| fail "AlmaLinux 9 should pass the OS guard"
|
||||
|
||||
# --- check_os_guard: AlmaLinux 8 must pass ---
|
||||
cat > "$TMP_DIR/os-release-alma8" <<'EOF'
|
||||
ID="almalinux"
|
||||
VERSION_ID="8.10"
|
||||
EOF
|
||||
OS_RELEASE_FILE="$TMP_DIR/os-release-alma8" check_os_guard \
|
||||
|| fail "AlmaLinux 8 should pass the OS guard"
|
||||
|
||||
# --- check_os_guard: CloudLinux must be refused ---
|
||||
cat > "$TMP_DIR/os-release-cloudlinux" <<'EOF'
|
||||
ID="cloudlinux"
|
||||
VERSION_ID="8.9"
|
||||
ID_LIKE="rhel fedora centos"
|
||||
EOF
|
||||
if ( OS_RELEASE_FILE="$TMP_DIR/os-release-cloudlinux" check_os_guard 2>/dev/null ); then
|
||||
fail "CloudLinux must be refused by the OS guard"
|
||||
fi
|
||||
|
||||
# --- check_os_guard: AlmaLinux 7 (out of supported range) must be refused ---
|
||||
cat > "$TMP_DIR/os-release-alma7" <<'EOF'
|
||||
ID="almalinux"
|
||||
VERSION_ID="7.9"
|
||||
EOF
|
||||
if ( OS_RELEASE_FILE="$TMP_DIR/os-release-alma7" check_os_guard 2>/dev/null ); then
|
||||
fail "AlmaLinux 7 must be refused by the OS guard (only 8/9 supported)"
|
||||
fi
|
||||
|
||||
# --- read_native_mysql_conf + already_migrated ---
|
||||
cat > "$TMP_DIR/mysql.conf.3306" <<'EOF'
|
||||
user=da_admin
|
||||
passwd=secret
|
||||
port=3306
|
||||
socket=/var/lib/mysql/mysql.sock
|
||||
host=
|
||||
EOF
|
||||
NATIVE_MYSQL_CONF="$TMP_DIR/mysql.conf.3306" read_native_mysql_conf \
|
||||
|| fail "read_native_mysql_conf should succeed on a valid mysql.conf"
|
||||
[ "$NATIVE_CURRENT_USER" = "da_admin" ] || fail "expected user da_admin, got: $NATIVE_CURRENT_USER"
|
||||
[ "$NATIVE_CURRENT_PORT" = "3306" ] || fail "expected port 3306, got: $NATIVE_CURRENT_PORT"
|
||||
already_migrated && fail "already_migrated should be false when native port is still 3306"
|
||||
|
||||
cat > "$TMP_DIR/mysql.conf.3307" <<'EOF'
|
||||
user=da_admin
|
||||
passwd=secret
|
||||
port=3307
|
||||
socket=/var/lib/mysql/mysql.sock
|
||||
host=
|
||||
EOF
|
||||
NATIVE_MYSQL_CONF="$TMP_DIR/mysql.conf.3307" read_native_mysql_conf \
|
||||
|| fail "read_native_mysql_conf should succeed on an already-migrated mysql.conf"
|
||||
already_migrated || fail "already_migrated should be true when native port is 3307"
|
||||
|
||||
# --- read_current_mysql_inst ---
|
||||
cat > "$TMP_DIR/options.conf" <<'EOF'
|
||||
mysql_inst=mariadb
|
||||
mariadb=10.6
|
||||
EOF
|
||||
CUSTOMBUILD_OPTIONS_CONF="$TMP_DIR/options.conf" read_current_mysql_inst \
|
||||
|| fail "read_current_mysql_inst should succeed"
|
||||
[ "$NATIVE_CURRENT_MYSQL_INST" = "mariadb" ] || fail "expected mysql_inst=mariadb, got: $NATIVE_CURRENT_MYSQL_INST"
|
||||
|
||||
# --- preflight_new_port_free using a faked `ss` on PATH ---
|
||||
mkdir -p "$TMP_DIR/bin"
|
||||
cat > "$TMP_DIR/bin/ss" <<'STUB'
|
||||
#!/bin/bash
|
||||
# Pretends port 3307 is free and port 9999 is occupied.
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
*:3307*) exit 0 ;;
|
||||
*:9999*)
|
||||
echo "header line"
|
||||
echo "occupied line"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
exit 0
|
||||
STUB
|
||||
chmod 755 "$TMP_DIR/bin/ss"
|
||||
|
||||
PATH="$TMP_DIR/bin:$PATH" NEW_NATIVE_PORT="3307" preflight_new_port_free \
|
||||
|| fail "preflight_new_port_free should pass when the target port is free"
|
||||
|
||||
if ( PATH="$TMP_DIR/bin:$PATH" NEW_NATIVE_PORT="9999" preflight_new_port_free 2>/dev/null ); then
|
||||
fail "preflight_new_port_free should refuse when the target port is already occupied"
|
||||
fi
|
||||
|
||||
echo "native_mariadb_port_takeover_detect_test: OK"
|
||||
Reference in New Issue
Block a user