#!/bin/sh set -eu root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) fake_bin=$(mktemp -d /tmp/overcooked-installer-fakes.XXXXXX) test_root=$(mktemp -d /tmp/overcooked-installer-db-test.XXXXXX) trap 'rm -rf "$fake_bin" "$test_root"' EXIT cp "$root/test/fake-docker.sh" "$fake_bin/docker" cp "$root/test/fake-curl.sh" "$fake_bin/curl" chmod 755 "$fake_bin/docker" "$fake_bin/curl" run_install() { mode=$1 output=$2 printf '%s\n' "$mode" > "$test_root/db-state" : > "$test_root/docker.log" set +e PATH="$fake_bin:$PATH" \ FAKE_DOCKER_LOG="$test_root/docker.log" \ FAKE_DB_STATE="$test_root/db-state" \ OVERCOOKED_ROOT="$test_root/root" \ OVERCOOKED_SERVER_IP=203.0.113.10 \ OVERCOOKED_API_PORT=29880 \ OVERCOOKED_WEB_PORT=29883 \ OVERCOOKED_E_PORT=29896 \ OVERCOOKED_AUTH_PUBLIC_KEYS='{"auth-1":"PUBLIC_KEY"}' \ OVERCOOKED_ACTIVATION_CODE=TEST-CODE \ "$root/install.sh" > "$output" 2>&1 status=$? set -e if [ "$status" -ne 0 ]; then cat "$output" >&2 fi return "$status" } first_output=$test_root/first.out run_install ok "$first_output" first_e_password=$(sed -n 's/^PRODUCT_E_POSTGRES_PASSWORD=//p' "$test_root/root/production.env") first_s_password=$(sed -n 's/^S_POSTGRES_PASSWORD=//p' "$test_root/root/production.env") [ "${#first_e_password}" = 64 ] [ "${#first_s_password}" = 64 ] [ "$(stat -c '%a' "$test_root/root/production.env")" = 600 ] ! rg -n 'volume rm|down .*-[^ ]*v' "$test_root/docker.log" second_output=$test_root/second.out run_install ok "$second_output" second_e_password=$(sed -n 's/^PRODUCT_E_POSTGRES_PASSWORD=//p' "$test_root/root/production.env") second_s_password=$(sed -n 's/^S_POSTGRES_PASSWORD=//p' "$test_root/root/production.env") [ "$first_e_password" = "$second_e_password" ] [ "$first_s_password" = "$second_s_password" ] ! rg -n 'volume rm|down .*-[^ ]*v' "$test_root/docker.log" echo "MALICIOUS=\$(touch $test_root/marker)" >> "$test_root/root/production.env" third_output=$test_root/third.out run_install ok "$third_output" [ ! -e "$test_root/marker" ] mismatch_output=$test_root/mismatch.out if run_install mismatch "$mismatch_output"; then echo 'password mismatch unexpectedly succeeded' >&2 exit 1 fi rg -q 'DATABASE_PASSWORD_MISMATCH_NEEDS_MANUAL_RECOVERY' "$mismatch_output" ! rg -n '^volume rm|down .*-[^ ]*v' "$test_root/docker.log" [ "$first_e_password" = "$(sed -n 's/^PRODUCT_E_POSTGRES_PASSWORD=//p' "$test_root/root/production.env")" ] wrong_role_output=$test_root/wrong-role.out if run_install wrong-role "$wrong_role_output"; then echo 'wrong database role unexpectedly succeeded' >&2 exit 1 fi rg -q 'DATABASE_PASSWORD_MISMATCH_NEEDS_MANUAL_RECOVERY' "$wrong_role_output" ! rg -n '^volume rm|down .*-[^ ]*v' "$test_root/docker.log" s_mismatch_output=$test_root/s-mismatch.out if run_install s-mismatch "$s_mismatch_output"; then echo 'S password mismatch unexpectedly succeeded' >&2 exit 1 fi rg -q 'DATABASE_PASSWORD_MISMATCH_NEEDS_MANUAL_RECOVERY' "$s_mismatch_output" ! rg -n '^volume rm|down .*-[^ ]*v' "$test_root/docker.log" reset_output=$test_root/reset.out printf 'reset-once\n' > "$test_root/db-state" PATH="$fake_bin:$PATH" \ FAKE_DOCKER_LOG="$test_root/docker.log" \ FAKE_DB_STATE="$test_root/db-state" \ OVERCOOKED_ROOT="$test_root/root" \ OVERCOOKED_SERVER_IP=203.0.113.10 \ OVERCOOKED_API_PORT=29880 \ OVERCOOKED_WEB_PORT=29883 \ OVERCOOKED_E_PORT=29896 \ OVERCOOKED_AUTH_PUBLIC_KEYS='{"auth-1":"PUBLIC_KEY"}' \ OVERCOOKED_ACTIVATION_CODE=TEST-CODE \ OVERCOOKED_RESET_FAILED_INSTALL=1 \ OVERCOOKED_CONFIRM_NO_BUSINESS_DATA=YES \ "$root/install.sh" > "$reset_output" 2>&1 rg -q '已收到显式无业务数据确认' "$reset_output" rg -q '^volume rm fake-e-postgres$' "$test_root/docker.log" echo 'database credential retry/reset tests passed'