Skip to content

Commit

Permalink
e2e/standard: test for backup & restore with passphrase (#1369)
Browse files Browse the repository at this point in the history
Inspired by #1309 (review), this adds tests for backup/restore with a passphrase.
  • Loading branch information
alxndrsn authored Feb 4, 2025
1 parent dfe0269 commit 6c35af7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/actions/install-postgres-client/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Install PostgreSQL Client

inputs:
postgres-version:
type: integer
required: true

runs:
using: composite
steps:
- name: Install postgres-client v${{ inputs.postgres-version }}
shell: bash
run: >
sudo apt-get update &&
sudo apt-get --yes remove --purge 'postgres*' &&
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list &&
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &&
sudo apt-get update &&
sudo apt-get --yes install postgresql-client-${{ inputs.postgres-version }} &&
printf "Postgres version: $(pg_dump --version): " &&
([[ "$(pg_dump --version | cut -d' ' -f3)" = ${{ inputs.postgres-version }}.* ]] && echo OK) || (echo Incorrect && exit 1)
15 changes: 15 additions & 0 deletions .github/workflows/standard-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ jobs:
cache: 'npm'
- run: npm ci
- run: node lib/bin/create-docker-databases.js

- name: E2E Test
timeout-minutes: 10
run: ./test/e2e/standard/run-tests.sh

- uses: ./.github/actions/install-postgres-client
with:
postgres-version: 14
- name: Grant postgres superuser role
# As per #1368, this role is required to create the pgrowlocks extension
run: psql -c 'ALTER ROLE jubilant SUPERUSER' postgresql://postgres:odktest@localhost/postgres
- name: Backup/restore tests
timeout-minutes: 10
run: ./test/e2e/standard/backup-restore.sh

- name: Backend Logs
if: always()
run: "! [[ -f ./server.log ]] || cat ./server.log"
87 changes: 87 additions & 0 deletions test/e2e/standard/backup-restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash -eu
set -o pipefail

serverUrl=http://localhost:8383

log() {
if [[ ${testname-} = "" ]]; then
echo "[test/e2e/standard/backup-restore] $*"
else
echo "[test/e2e/standard/backup-restore] [$testname] $*"
fi
}

backup() {
local contentType="$1"
local postBody="$2"

backupDir="$(mktemp --directory)"
cd "$backupDir"

target="backup.zip"
creds="$(echo -n '[email protected]:secret1234' | base64)"
wget \
--header "X-Forwarded-Proto: https" \
--header "Content-Type: $contentType" \
--header="Authorization: Basic $creds" \
"$serverUrl/v1/backup" \
--post-data "$postBody" \
-O "$target"
cd -
}

if ! curl -s -o /dev/null "$serverUrl"; then
log "Backend is not running - cannot run tests."
exit 1
fi

testname=no-passphrase
log "Testing with no passphrase supplied for backup..."
backup text/plain ''
log " Restoring with no passphrase..."
node ./lib/bin/restore.js "$backupDir/$target"
log " Restoring with explicit empty passphrase..."
node ./lib/bin/restore.js "$backupDir/$target" ""
log " Restoring with incorrect passphrase..."
if node ./lib/bin/restore.js "$backupDir/$target" "wrong-passphrase"; then
log " Incorrect passphrase should have been rejected."
exit 1
fi

testname=empty-passphrase
log "Testing with empty passphrase supplied for backup..."
backup application/json '{"passphrase":""}'
log " Restoring with no passphrase..."
node ./lib/bin/restore.js "$backupDir/$target"
log " Restoring with explicit empty passphrase..."
node ./lib/bin/restore.js "$backupDir/$target" ""
log " Restoring with incorrect passphrase..."
if node ./lib/bin/restore.js "$backupDir/$target" "wrong-passphrase"; then
log " Incorrect passphrase should have been rejected."
exit 1
fi

testname=with-passphrase
log "Testing with explicit passphrase supplied for backup..."
backup application/json '{"passphrase":"megasecret"}'
log " Restoring with correct passphrase..."
node ./lib/bin/restore.js "$backupDir/$target" megasecret
log " Restoring without passphrase..."
if node ./lib/bin/restore.js "$backupDir/$target"; then
log " Missing passphrase should have been rejected."
exit 1
fi
log " Restoring with empty passphrase..."
if node ./lib/bin/restore.js "$backupDir/$target" ""; then
log " Empty passphrase should have been rejected."
exit 1
fi
log " Restoring with incorrect passphrase..."
if node ./lib/bin/restore.js "$backupDir/$target" "wrong-passphrase"; then
log " Incorrect passphrase should have been rejected."
exit 1
fi

testname=

log "All checks passed OK."
2 changes: 1 addition & 1 deletion test/e2e/standard/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kill_child_processes() {
trap kill_child_processes EXIT

log "Starting backend..."
make run &
make run | tee server.log &

log "Waiting for backend to start..."
timeout 30 bash -c "while ! curl -s -o /dev/null $serverUrl; do sleep 1; done"
Expand Down

0 comments on commit 6c35af7

Please sign in to comment.