Files
multica/scripts/install.test.sh
Multica Eve 220fa58264 fix: guide SSH installs to token login (#5318)
Co-authored-by: Eve <eve@multica-ai.local>
Co-authored-by: multica-agent <github@multica.ai>
2026-07-13 16:42:46 +08:00

244 lines
5.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Build a self-contained sandbox with stub `curl` and a tarball that the
# release-binary fallback path will download. Each test supplies its own
# `brew` stub to model a specific Homebrew failure mode.
_setup_sandbox() {
local tmp="$1"
local stub_bin="$tmp/stub-bin"
local install_bin="$tmp/install-bin"
local payload_dir="$tmp/payload"
mkdir -p "$stub_bin" "$install_bin" "$payload_dir"
cat >"$payload_dir/multica" <<'STUB'
#!/usr/bin/env bash
echo "multica v0.3.2 (commit: test)"
STUB
chmod +x "$payload_dir/multica"
tar -czf "$tmp/multica.tar.gz" -C "$payload_dir" multica
cat >"$stub_bin/curl" <<'STUB'
#!/usr/bin/env bash
if [[ "$*" == *"-sI"* ]]; then
printf 'HTTP/2 302\r\nlocation: https://github.com/multica-ai/multica/releases/tag/v0.3.2\r\n'
exit 0
fi
out=""
while [[ $# -gt 0 ]]; do
case "$1" in
-o)
out="$2"
shift 2
;;
*)
shift
;;
esac
done
if [[ -z "$out" ]]; then
echo "stub curl expected -o" >&2
exit 2
fi
cp "$MULTICA_TEST_ARCHIVE" "$out"
STUB
chmod +x "$stub_bin/curl"
}
_run_installer() {
local tmp="$1"
local out="$tmp/install.out"
local err="$tmp/install.err"
if ! PATH="$tmp/stub-bin:$tmp/install-bin:/usr/bin:/bin" \
MULTICA_BIN_DIR="$tmp/install-bin" \
MULTICA_TEST_ARCHIVE="$tmp/multica.tar.gz" \
bash "$ROOT_DIR/scripts/install.sh" >"$out" 2>"$err"; then
echo "install.sh exited non-zero" >&2
cat "$out" >&2 || true
cat "$err" >&2 || true
return 1
fi
if [[ ! -x "$tmp/install-bin/multica" ]]; then
echo "expected fallback binary at $tmp/install-bin/multica" >&2
cat "$out" >&2 || true
cat "$err" >&2 || true
return 1
fi
if ! grep -q "Homebrew output (last 80 lines):" "$err"; then
echo "expected diagnostic tail in stderr" >&2
cat "$err" >&2 || true
return 1
fi
}
test_brew_install_failure_falls_back_to_release_binary() {
local tmp
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' RETURN
_setup_sandbox "$tmp"
cat >"$tmp/stub-bin/brew" <<'STUB'
#!/usr/bin/env bash
case "${1:-}" in
tap)
exit 0
;;
install)
echo "simulated brew install failure" >&2
exit 42
;;
list)
exit 1
;;
*)
exit 0
;;
esac
STUB
chmod +x "$tmp/stub-bin/brew"
_run_installer "$tmp"
}
test_brew_tap_failure_falls_back_to_release_binary() {
local tmp
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' RETURN
_setup_sandbox "$tmp"
cat >"$tmp/stub-bin/brew" <<'STUB'
#!/usr/bin/env bash
case "${1:-}" in
tap)
echo "simulated brew tap failure" >&2
exit 17
;;
*)
echo "brew $* should not be reached after tap failure" >&2
exit 99
;;
esac
STUB
chmod +x "$tmp/stub-bin/brew"
_run_installer "$tmp"
}
test_remote_ssh_install_prints_token_login_hint() {
local tmp
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' RETURN
_setup_sandbox "$tmp"
cat >"$tmp/stub-bin/brew" <<'STUB'
#!/usr/bin/env bash
case "${1:-}" in
tap)
exit 0
;;
install)
echo "simulated brew install failure" >&2
exit 42
;;
list)
exit 1
;;
*)
exit 0
;;
esac
STUB
chmod +x "$tmp/stub-bin/brew"
(
export SSH_CONNECTION="192.0.2.10 54321 198.51.100.20 22"
_run_installer "$tmp"
)
if ! grep -q "Looks like a remote/SSH session" "$tmp/install.out"; then
echo "expected remote/SSH token-login hint in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if ! grep -q "https://multica.ai/settings?tab=tokens" "$tmp/install.out"; then
echo "expected direct API Tokens settings URL in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if ! grep -q "Settings > API Tokens" "$tmp/install.out"; then
echo "expected API Tokens tab name in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if ! grep -q "multica login --token <YOUR_TOKEN>" "$tmp/install.out"; then
echo "expected token login command in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if grep -q "multica config set server_url" "$tmp/install.out"; then
echo "did not expect default cloud server config command in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if grep -q "multica config set app_url" "$tmp/install.out"; then
echo "did not expect default cloud app config command in installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
}
test_local_install_does_not_print_token_login_hint() {
local tmp
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' RETURN
_setup_sandbox "$tmp"
cat >"$tmp/stub-bin/brew" <<'STUB'
#!/usr/bin/env bash
case "${1:-}" in
tap)
exit 0
;;
install)
echo "simulated brew install failure" >&2
exit 42
;;
list)
exit 1
;;
*)
exit 0
;;
esac
STUB
chmod +x "$tmp/stub-bin/brew"
(
unset SSH_CONNECTION SSH_CLIENT SSH_TTY
_run_installer "$tmp"
)
if grep -q "Looks like a remote/SSH session" "$tmp/install.out"; then
echo "did not expect remote/SSH token-login hint in local installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
if grep -q "multica login --token <YOUR_TOKEN>" "$tmp/install.out"; then
echo "did not expect token login command in local installer output" >&2
cat "$tmp/install.out" >&2 || true
return 1
fi
}
test_brew_install_failure_falls_back_to_release_binary
test_brew_tap_failure_falls_back_to_release_binary
test_remote_ssh_install_prints_token_login_hint
test_local_install_does_not_print_token_login_hint
echo "install.sh tests passed"