Sanitized single-commit public mirror of recipe-maintainer. - Removed test-ssh/.testenv (live creds); added test-ssh/.testenv.example placeholders. - Removed plans/ and planned-updates/ (deployment-planning docs) so no client/ deployment domains appear in the public repo. - All other secret stores were already gitignored. - docs.coopcloud.tech retained as a submodule (public upstream).
220 lines
7.2 KiB
Bash
Executable File
220 lines
7.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# Test: verify all abra.sh database maintenance and purge commands
|
|
#
|
|
# Creates test data (admin user, rooms, messages) then exercises
|
|
# every abra.sh command and checks for expected output.
|
|
set -euo pipefail
|
|
|
|
SERVER="cctest.autonomic.zone"
|
|
DOMAIN="matrix-synapse.cctest.autonomic.zone"
|
|
STACK_NAME="matrix-synapse_cctest_autonomic_zone"
|
|
ADMIN_USER="purgetest_admin"
|
|
ADMIN_PASS="purgetest_pass_123"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
check() {
|
|
local name="$1"
|
|
local result="$2"
|
|
local expected="$3"
|
|
if echo "$result" | grep -q "$expected"; then
|
|
echo " PASS: $name"
|
|
PASS=$((PASS + 1))
|
|
else
|
|
echo " FAIL: $name (expected '$expected')"
|
|
echo " Got: $result"
|
|
FAIL=$((FAIL + 1))
|
|
fi
|
|
}
|
|
|
|
# Run abra cmd and strip ANSI escape sequences
|
|
run_db_cmd() {
|
|
script -qefc "abra app cmd $DOMAIN db $* --chaos --no-input" /dev/null 2>&1 | sed 's/\x1b\[[0-9;?]*[a-zA-Z]//g' | grep -v '^\]'
|
|
}
|
|
|
|
run_app_cmd() {
|
|
script -qefc "abra app cmd $DOMAIN app $* --chaos --no-input" /dev/null 2>&1 | sed 's/\x1b\[[0-9;?]*[a-zA-Z]//g' | grep -v '^\]'
|
|
}
|
|
|
|
# Run curl inside the app container
|
|
app_curl() {
|
|
ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) curl -s $*"
|
|
}
|
|
|
|
echo "=== Test: abra.sh database maintenance and purge commands ==="
|
|
echo ""
|
|
|
|
# --- Setup: create admin user ---
|
|
echo "--- Setup ---"
|
|
|
|
echo "Registering admin user..."
|
|
REGISTER_OUT=$(run_app_cmd register_admin $ADMIN_USER $ADMIN_PASS 2>&1) || true
|
|
if echo "$REGISTER_OUT" | grep -q "Success\|already"; then
|
|
echo " Admin user ready"
|
|
else
|
|
echo " Register output: $REGISTER_OUT"
|
|
fi
|
|
|
|
echo "Getting admin token..."
|
|
TOKEN_RAW=$(run_app_cmd get_token $ADMIN_USER $ADMIN_PASS)
|
|
TOKEN=$(echo "$TOKEN_RAW" | grep -oE 'syt_[A-Za-z0-9_]+' | head -1)
|
|
if [ -z "$TOKEN" ]; then
|
|
echo " FATAL: Could not get admin token"
|
|
echo " Raw: $TOKEN_RAW"
|
|
exit 1
|
|
fi
|
|
echo " Token: ${TOKEN:0:20}..."
|
|
|
|
# --- Create test data ---
|
|
echo ""
|
|
echo "--- Creating test data ---"
|
|
|
|
# Create a room with messages
|
|
ROOM_ID=$(app_curl -X POST "http://localhost:8008/_matrix/client/r0/createRoom" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "'{"'"'"name"'"'":"'"'"Purge Test Room"'"'"}'" 2>/dev/null \
|
|
| python3 -c "import sys,json; print(json.load(sys.stdin).get('room_id',''))" 2>/dev/null) || true
|
|
|
|
if [ -z "$ROOM_ID" ]; then
|
|
# Try with simpler quoting
|
|
ROOM_ID=$(ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) \
|
|
curl -s -X POST http://localhost:8008/_matrix/client/r0/createRoom \
|
|
-H 'Authorization: Bearer $TOKEN' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{\"name\":\"Purge Test Room\"}'")
|
|
ROOM_ID=$(echo "$ROOM_ID" | python3 -c "import sys,json; print(json.load(sys.stdin).get('room_id',''))")
|
|
fi
|
|
|
|
if [ -z "$ROOM_ID" ]; then
|
|
echo " FATAL: Could not create room"
|
|
exit 1
|
|
fi
|
|
echo " Created room: $ROOM_ID"
|
|
|
|
# Send some messages
|
|
for i in $(seq 1 5); do
|
|
ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) \
|
|
curl -s -X PUT 'http://localhost:8008/_matrix/client/r0/rooms/${ROOM_ID}/send/m.room.message/msg${i}' \
|
|
-H 'Authorization: Bearer $TOKEN' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{\"msgtype\":\"m.text\",\"body\":\"Test message ${i}\"}'" > /dev/null
|
|
done
|
|
echo " Sent 5 messages"
|
|
|
|
# Create a second room that the admin will leave (to test empty_rooms / purge_empty_rooms)
|
|
EMPTY_ROOM_ID=$(ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) \
|
|
curl -s -X POST http://localhost:8008/_matrix/client/r0/createRoom \
|
|
-H 'Authorization: Bearer $TOKEN' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{\"name\":\"Empty Test Room\"}'")
|
|
EMPTY_ROOM_ID=$(echo "$EMPTY_ROOM_ID" | python3 -c "import sys,json; print(json.load(sys.stdin).get('room_id',''))")
|
|
echo " Created empty room: $EMPTY_ROOM_ID"
|
|
|
|
# Leave and forget the empty room
|
|
ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) \
|
|
curl -s -X POST 'http://localhost:8008/_matrix/client/r0/rooms/${EMPTY_ROOM_ID}/leave' \
|
|
-H 'Authorization: Bearer $TOKEN'" > /dev/null
|
|
ssh "$SERVER" "docker exec \$(docker ps --filter name=${STACK_NAME}_app -q) \
|
|
curl -s -X POST 'http://localhost:8008/_matrix/client/r0/rooms/${EMPTY_ROOM_ID}/forget' \
|
|
-H 'Authorization: Bearer $TOKEN'" > /dev/null
|
|
echo " Left and forgot empty room"
|
|
|
|
echo ""
|
|
echo "--- Testing db commands ---"
|
|
|
|
# Test db_size
|
|
echo "Testing db_size..."
|
|
RESULT=$(run_db_cmd db_size)
|
|
check "db_size shows database size" "$RESULT" "db_size"
|
|
check "db_size shows top tables" "$RESULT" "total_size"
|
|
|
|
# Test state_bloat
|
|
echo "Testing state_bloat..."
|
|
RESULT=$(run_db_cmd state_bloat)
|
|
check "state_bloat shows header" "$RESULT" "state_entries"
|
|
|
|
# Test empty_rooms
|
|
echo "Testing empty_rooms..."
|
|
RESULT=$(run_db_cmd empty_rooms)
|
|
check "empty_rooms shows header" "$RESULT" "room_id"
|
|
|
|
# Test reindex
|
|
echo "Testing reindex..."
|
|
RESULT=$(run_db_cmd reindex)
|
|
check "reindex completes" "$RESULT" "REINDEX complete"
|
|
check "reindex shows size" "$RESULT" "db_size"
|
|
|
|
# Test vacuum_full
|
|
echo "Testing vacuum_full..."
|
|
RESULT=$(run_db_cmd vacuum_full)
|
|
check "vacuum_full completes" "$RESULT" "VACUUM FULL complete"
|
|
check "vacuum_full shows size" "$RESULT" "db_size"
|
|
|
|
echo ""
|
|
echo "--- Testing app commands ---"
|
|
|
|
# Test register_admin
|
|
echo "Testing register_admin..."
|
|
RESULT=$(run_app_cmd register_admin testadmin_new testpass_new_456)
|
|
check "register_admin succeeds" "$RESULT" "Success"
|
|
|
|
# Test get_token
|
|
echo "Testing get_token..."
|
|
RESULT=$(run_app_cmd get_token $ADMIN_USER $ADMIN_PASS)
|
|
check "get_token returns token" "$RESULT" "syt_"
|
|
|
|
# Test get_token with bad password
|
|
echo "Testing get_token with bad password..."
|
|
RESULT=$(run_app_cmd get_token $ADMIN_USER wrongpassword)
|
|
check "get_token rejects bad password" "$RESULT" "Invalid"
|
|
|
|
# Test purge_remote_media
|
|
echo "Testing purge_remote_media..."
|
|
RESULT=$(run_app_cmd purge_remote_media 30 $TOKEN)
|
|
check "purge_remote_media returns deleted count" "$RESULT" "deleted"
|
|
|
|
# Test purge_history
|
|
echo "Testing purge_history..."
|
|
RESULT=$(run_app_cmd purge_history $ROOM_ID 0 $TOKEN)
|
|
check "purge_history returns purge_id" "$RESULT" "purge_id"
|
|
|
|
# Test purge_empty_rooms
|
|
echo "Testing purge_empty_rooms..."
|
|
RESULT=$(run_app_cmd purge_empty_rooms $TOKEN)
|
|
check "purge_empty_rooms finds empty room" "$RESULT" "$EMPTY_ROOM_ID"
|
|
|
|
# Test purge_room (purge the main test room)
|
|
echo "Testing purge_room..."
|
|
RESULT=$(run_app_cmd purge_room $ROOM_ID $TOKEN)
|
|
check "purge_room returns result" "$RESULT" "kicked_users"
|
|
|
|
# Test usage messages
|
|
echo ""
|
|
echo "--- Testing usage messages ---"
|
|
RESULT=$(run_app_cmd purge_remote_media 2>&1) || true
|
|
check "purge_remote_media shows usage" "$RESULT" "Usage"
|
|
|
|
RESULT=$(run_app_cmd purge_room 2>&1) || true
|
|
check "purge_room shows usage" "$RESULT" "Usage"
|
|
|
|
RESULT=$(run_app_cmd purge_history 2>&1) || true
|
|
check "purge_history shows usage" "$RESULT" "Usage"
|
|
|
|
RESULT=$(run_app_cmd purge_empty_rooms 2>&1) || true
|
|
check "purge_empty_rooms shows usage" "$RESULT" "Usage"
|
|
|
|
RESULT=$(run_app_cmd get_token 2>&1) || true
|
|
check "get_token shows usage" "$RESULT" "Usage"
|
|
|
|
RESULT=$(run_app_cmd register_admin 2>&1) || true
|
|
check "register_admin shows usage" "$RESULT" "Usage"
|
|
|
|
echo ""
|
|
echo "=== Results ==="
|
|
echo "PASS: $PASS"
|
|
echo "FAIL: $FAIL"
|
|
if [ "$FAIL" -gt 0 ]; then
|
|
exit 1
|
|
fi
|