Files
crosscode-platform/scripts/tests/frpc.bats
T
joshuaandClaude Opus 5.5 19709c2c75 frpc: restart it the first time the admin lines are added (a reload can't work yet)
Found in the VM test: the new install's tunnels never opened, because
frpc only reads admin_addr when it starts, so the first reload failed.
That first time (and whenever a reload fails), frpc is restarted under
systemd and the report says so; later changes reload as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:48:28 +02:00

130 lines
4.2 KiB
Bash

#!/usr/bin/env bats
# frpc.ini blocks (lib/frpc.sh, AGENT-1.0-PLAN §6): only appended, removed by their markers,
# everything written by hand unchanged byte for byte, nothing changed when `frpc verify` refuses.
setup() {
TMP="$BATS_TEST_TMPDIR"
# shellcheck source=../lib/common.sh
source "$BATS_TEST_DIRNAME/../lib/common.sh"
# shellcheck source=../lib/frpc.sh
source "$BATS_TEST_DIRNAME/../lib/frpc.sh"
FRPC_INI="$TMP/frp/frpc.ini"
FRPC_BACKUPS="$TMP/frp/backups"
mkdir -p "$TMP/frp" "$TMP/bin"
# A stub frpc: verify fails when FRPC_REFUSE is set, reload logs.
cat > "$TMP/bin/frpc" <<'EOF'
#!/usr/bin/env bash
case "$1" in
verify) [ -z "${FRPC_REFUSE:-}" ] ;;
reload) echo reload >> "$(dirname "$3")/reloads" ;;
esac
EOF
chmod +x "$TMP/bin/frpc"
FRPC_BIN="$TMP/bin/frpc"
cat > "$FRPC_INI" <<'EOF'
[common]
server_addr = 203.0.113.10
server_port = 7000
token = hand-written
# the church's own tunnel, written by hand
[events-frontend]
type = tcp
local_ip = 127.0.0.1
local_port = 3000
remote_port = 3000
EOF
cp "$FRPC_INI" "$TMP/original"
}
@test "adds a marked block at the end, and the admin lines to [common] once" {
frpc_add hope events 20010 20011
grep -qx '# >>> crosscode:hope:events (added by crosscode .*' "$FRPC_INI" || grep -q '^# >>> crosscode:hope:events' "$FRPC_INI"
grep -qx '\[hope-events-web\]' "$FRPC_INI"
grep -qx 'remote_port = 20011' "$FRPC_INI"
[ "$(grep -c '^admin_addr = 127.0.0.1$' "$FRPC_INI")" = 1 ]
[ "$(sed -n 2p "$FRPC_INI")" = "admin_addr = 127.0.0.1" ]
frpc_add bridge events 20020 20021
[ "$(grep -c '^admin_addr' "$FRPC_INI")" = 1 ]
[ "$(frpc_ports hope events)" = "20010 20011" ]
[ "$(ls "$FRPC_BACKUPS" | wc -l)" -ge 1 ]
}
@test "the hand-written lines stay exactly as they were" {
frpc_add hope events 20010 20011
# Everything except the two admin lines and the appended block is the original.
grep -v '^admin_' "$FRPC_INI" | head -n "$(wc -l < "$TMP/original")" | cmp - "$TMP/original"
}
@test "removing one block leaves the other blocks and the hand-written lines untouched" {
frpc_add hope events 20010 20011
cp "$FRPC_INI" "$TMP/with-hope"
frpc_add bridge events 20020 20021
frpc_remove bridge events
cmp "$FRPC_INI" "$TMP/with-hope"
frpc_remove hope events
grep -v '^admin_' "$FRPC_INI" | cmp - "$TMP/original"
}
@test "adding the same block twice changes nothing" {
frpc_add hope events 20010 20011
cp "$FRPC_INI" "$TMP/once"
run frpc_add hope events 20010 20011
[ "$status" -eq 0 ]
cmp "$FRPC_INI" "$TMP/once"
}
@test "a port or tunnel name that's already used is refused" {
run frpc_add other events 3000 20031
[ "$status" -eq 1 ]
[[ "$output" == *"port 3000 or 20031 is already used"* ]]
cmp "$FRPC_INI" "$TMP/original"
}
@test "a failed frpc verify leaves the file untouched" {
FRPC_REFUSE=1
export FRPC_REFUSE
run frpc_add hope events 20010 20011
[ "$status" -eq 1 ]
[[ "$output" == *"frpc verify refused"* ]]
cmp "$FRPC_INI" "$TMP/original"
[ -z "$(find "$TMP/frp" -name '.frpc.ini.*')" ]
}
@test "an admin_addr that's already set is left alone" {
sed -i 's/^server_port = 7000$/server_port = 7000\nadmin_addr = 0.0.0.0/' "$FRPC_INI"
frpc_add hope events 20010 20011
[ "$(grep -c '^admin_addr' "$FRPC_INI")" = 1 ]
grep -qx 'admin_addr = 0.0.0.0' "$FRPC_INI"
}
@test "a file without a final newline keeps its last line" {
printf '[common]\nserver_addr = x' > "$FRPC_INI"
frpc_add hope events 20010 20011
grep -qx 'server_addr = x' "$FRPC_INI"
}
@test "the first time (admin lines just added) frpc is restarted; afterwards it's reloaded" {
cat > "$TMP/bin/systemctl" <<'STUB'
#!/usr/bin/env bash
echo "systemctl $*" >> "$(dirname "$0")/../calls"
STUB
chmod +x "$TMP/bin/systemctl"
PATH="$TMP/bin:$PATH"
FRPC_NEEDS_RESTART=0
frpc_add hope events 20010 20011
[ "$FRPC_NEEDS_RESTART" = 1 ]
run frpc_reload
[ "$status" -eq 0 ]
grep -q "systemctl restart frpc" "$TMP/calls"
[[ "$output" == *"restarting it once"* ]]
# Admin lines now there: the next change reloads.
: > "$TMP/calls"
FRPC_NEEDS_RESTART=0
frpc_add bridge events 20020 20021
[ "$FRPC_NEEDS_RESTART" = 0 ]
frpc_reload
! grep -q "restart frpc" "$TMP/calls"
grep -q reload "$TMP/frp/reloads"
}