#!/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 } mkdir -p "$TMP_DIR/bin" "$TMP_DIR/my.cnf.d" CALL_LOG="$TMP_DIR/calls.log" cat > "$TMP_DIR/bin/da" <> "$CALL_LOG" exit 0 EOF chmod 755 "$TMP_DIR/bin/da" cat > "$TMP_DIR/my.cnf" <<'EOF' [client] port=3306 [mysqld] datadir=/var/lib/mysql port=3306 socket=/var/lib/mysql/mysql.sock EOF cat > "$TMP_DIR/mysql.conf" <<'EOF' user=da_admin passwd=secret port=3306 socket=/var/lib/mysql/mysql.sock host= EOF NATIVE_TAKEOVER_LOG="$TMP_DIR/takeover.log" \ source "$SCRIPT" NATIVE_MY_CNF="$TMP_DIR/my.cnf" NATIVE_MY_CNF_D="$TMP_DIR/my.cnf.d" NATIVE_MYSQL_CONF="$TMP_DIR/mysql.conf" NEW_NATIVE_PORT="3307" DA_CLI_BIN="$TMP_DIR/bin/da" NATIVE_CURRENT_MYSQL_INST="mariadb" # --- disable_custombuild_mysql_management --- : > "$CALL_LOG" disable_custombuild_mysql_management grep -Fxq "build set mysql_inst no" "$CALL_LOG" \ || fail "expected 'da build set mysql_inst no' to be called, got: $(cat "$CALL_LOG")" [ "$DID_SET_MYSQL_INST_NO" -eq 1 ] || fail "expected DID_SET_MYSQL_INST_NO to be set" # --- idempotency: already "no" must skip calling da --- : > "$CALL_LOG" DID_SET_MYSQL_INST_NO=0 NATIVE_CURRENT_MYSQL_INST="no" disable_custombuild_mysql_management [ ! -s "$CALL_LOG" ] || fail "expected no 'da' call when mysql_inst is already 'no'" [ "$DID_SET_MYSQL_INST_NO" -eq 0 ] || fail "DID_SET_MYSQL_INST_NO should stay 0 when already 'no'" NATIVE_CURRENT_MYSQL_INST="mariadb" # --- apply_config_mutations --- CONFIG_BACKUPS=() apply_config_mutations grep -Fxq "port=3307" "$TMP_DIR/mysql.conf" \ || fail "expected mysql.conf port to be rewritten to 3307, got: $(cat "$TMP_DIR/mysql.conf")" grep -Fxq "passwd=secret" "$TMP_DIR/mysql.conf" \ || fail "expected mysql.conf passwd to be preserved untouched" grep -A2 '^\[mysqld\]' "$TMP_DIR/my.cnf" | grep -Fxq "port=3307" \ || fail "expected [mysqld] port in my.cnf to be rewritten to 3307" grep -A1 '^\[client\]' "$TMP_DIR/my.cnf" | grep -Fxq "port=3306" \ || fail "expected [client] port in my.cnf to be left at 3306 (only server sections rewritten)" [ "${#CONFIG_BACKUPS[@]}" -ge 2 ] \ || fail "expected at least 2 backup entries (my.cnf + mysql.conf), got ${#CONFIG_BACKUPS[@]}" # --- rollback_config_changes --- rollback_config_changes grep -A2 '^\[mysqld\]' "$TMP_DIR/my.cnf" | grep -Fxq "port=3306" \ || fail "expected [mysqld] port in my.cnf to be restored to 3306 after rollback" grep -Fxq "port=3306" "$TMP_DIR/mysql.conf" \ || fail "expected mysql.conf port to be restored to 3306 after rollback" # --- restore_custombuild_mysql_management --- : > "$CALL_LOG" DID_SET_MYSQL_INST_NO=1 restore_custombuild_mysql_management grep -Fxq "build set mysql_inst mariadb" "$CALL_LOG" \ || fail "expected 'da build set mysql_inst mariadb' to be called on rollback, got: $(cat "$CALL_LOG")" echo "native_mariadb_port_takeover_mutation_test: OK"