Skip to content

Commit

Permalink
test: Integration test for migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
raghuvar-vijay committed May 21, 2024
1 parent 57433d1 commit d5586db
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/test_migration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: migration-tests

on:
pull_request:
push:
branches:
- main

env:
CARGO_TERM_COLOR: always
jobs:
test:
name: Migration test
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
ports:
- 5432:5432
env:
SQLX_VERSION: 0.7.4
SQLX_FEATURES: postgres,rustls,sqlite
RUSTFLAGS: -C link-arg=-s
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-musl

- name: Cache dependencies
uses: Swatinem/rust-cache@v2
with:
prefix-key: cache-dependencies

- name: Massage sqlx features env variable...
run: |
TMP=${{ env.SQLX_FEATURES }}
SQLX_FEATURES_CACHE="${TMP//,/_}"
echo "SQLX_FEATURES_CACHE=${SQLX_FEATURES_CACHE}" >> $GITHUB_ENV
- name: Cache sqlx
id: cache_sqlx
uses: Swatinem/rust-cache@v2
with:
prefix-key: cache-sqlx
key: ${{ runner.os }}-sqlx-${{ env.SQLX_VERSION }}-${{ env.SQLX_FEATURES_CACHE }}
cache-directories: |
~/.cargo/bin/sqlx
~/.cargo/bin/cargo-sqlx
- name: Install sqlx-cli
# if: ${{ steps.cache_sqlx.outputs.cache-hit }}
run: cargo install sqlx-cli --version=${{ env.SQLX_VERSION }} --features=${{ env.SQLX_FEATURES }} --no-default-features --locked


- name: Checkout previous commit
run: git checkout HEAD~1

- name: Migrate database
run: |
sudo apt-get install libpq-dev -y
SKIP_DOCKER=true ./scripts/init_db.sh
- name: Migrate auditor client sqlite database
run: |
./scripts/init_client_sqlite.sh
- name: Switch back to original commit
run: git checkout -

- name: Install musl
run: |
sudo apt-get install musl musl-tools
- name: Test priority plugin
run: SKIP_COMPILATION=true ./scripts/test_migration.sh

123 changes: 123 additions & 0 deletions scripts/test_migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env bash
#!/usr/bin/env bash
set -x
set -eo pipefail

RELEASE_MODE=${RELEASE_MODE:=false}
ENV_DIR=${ENV_DIR:=".env_test"}

function compile_auditor() {
if [ "$RELEASE_MODE" = true ]; then
cargo build --bin auditor --release
else
cargo build --bin auditor
fi
}

function start_auditor_main() {
if [[ -z "${SKIP_COMPILATION}" ]]; then
compile_auditor
fi
if [ "$RELEASE_MODE" = true ]; then
AUDITOR_APPLICATION__ADDR=0.0.0.0 ./target/release/auditor &
else
AUDITOR_APPLICATION__ADDR=0.0.0.0 ./target/debug/auditor &
fi
AUDITOR_SERVER_PID=$!
COUNTER=0
until curl http://localhost:8000/health_check; do
echo >&2 "Auditor is still unavailable - sleeping"
((COUNTER = COUNTER + 1))
if [ "$COUNTER" -gt "30" ]; then
echo >&2 "Auditor did not come up in time."
stop_auditor
echo >&2 "Exiting."
exit 1
fi
sleep 1
done
}

function stop_auditor() {
echo >&2 "Stopping Auditor"
kill $AUDITOR_SERVER_PID
wait $AUDITOR_SERVER_PID
}


function fill_auditor() {
curl -X POST --header "Content-Type: application/json" \
--data '{ "record_id": "1", "meta": {"site_id": ["test"], "user_id": ["raghuvar"], "group_id": ["group1"]}, "components": [{ "name": "NumCPUs", "amount": 40, "scores": [{ "name": "HEPSPEC", "value": 1.2 }] }], "start_time": "2022-06-27T15:00:00Z", "stop_time": "2022-06-27T15:01:00Z" }' \
http://localhost:8000/record
curl -X POST --header "Content-Type: application/json" \
--data '{ "record_id": "2", "meta": {"site_id": ["test"], "user_id": ["raghuvar"], "group_id": ["group1"]}, "components": [{ "name": "NumCPUs", "amount": 40, "scores": [{ "name": "HEPSPEC", "value": 1.5 }] }], "start_time": "2022-06-27T16:00:00Z", "stop_time": "2022-06-27T16:04:00Z" }' \
http://localhost:8000/record
}

function fill_auditor_new() {
curl -X POST --header "Content-Type: application/json" \
--data '{ "record_id": "3", "meta": {"site_id": ["test"], "user_id": ["raghuvar"], "group_id": ["group2"]}, "components": [{ "name": "NumCPUs", "amount": 20, "scores": [{ "name": "HEPSPEC", "value": 1.8 }] }], "start_time": "2022-06-27T14:00:00Z", "stop_time": "2023-06-27T14:08:00Z" }' \
http://localhost:8000/record
curl -X POST --header "Content-Type: application/json" \
--data '{ "record_id": "4", "meta": {"site_id": ["test"], "user_id": ["raghuvar"], "group_id": ["group2"]}, "components": [{ "name": "NumCPUs", "amount": 10, "scores": [{ "name": "HEPSPEC", "value": 0.8 }] }], "start_time": "2022-06-27T13:00:00Z", "stop_time": "2022-06-27T13:01:00Z" }' \
http://localhost:8000/record

}

function test_collector() {
TEST1=$(curl -X GET http://localhost:8000/records | jq)

if [ "$(echo $TEST1 | jq '. | length')" != 4 ]
then
echo >&2 "Incorrect number of records in auditor database."
stop_auditor
exit 1
fi

if [ "$(echo $TEST1 | jq '.[] | select(.record_id=="1") | .components | .[] | .scores | .[] | .value')" != 1.2 ]
then
echo >&2 "Incorrect score of record in accounting database. Returned record:"
echo >&2 $TEST1
stop_auditor
exit 1
fi

expected_json='{ "record_id": "1", "meta": {"site_id": ["test"], "user_id": ["raghuvar"], "group_id": ["group1"]}, "components": [{ "name": "NumCPUs", "amount": 40, "scores": [{ "name": "HEPSPEC", "value": 1.2 }] }], "start_time": "2022-06-27T15:00:00Z", "stop_time": "2022-06-27T15:01:00Z","runtime":60 }'

TEST2=$(curl -X GET http://localhost:8000/record/"1" | jq)

if [ "$(echo "$TEST2" | jq -c 'walk(if type == "object" then to_entries | sort_by(.key) | from_entries else . end)' | tr -d '[:space:]')" != "$(echo "$expected_json" | jq -c 'walk(if type == "object" then to_entries | sort_by(.key) | from_entries else . end)' | tr -d '[:space:]')" ]; then
echo >&2 "The content of TEST2 does not match the expected JSON data."
stop_auditor
exit 1
fi

}

function auditor_main() {
SKIP_DOCKER=true ./scripts/init_db.sh
./scripts/init_slurm_collector_sqlite.sh
./scripts/init_client_sqlite.sh
start_auditor_main
fill_auditor
kill $AUDITOR_SERVER_PID
}

function auditor_new_migration() {
sqlx migrate run --source migrations --database-url=postgres://postgres:password@localhost:5432/auditor
start_auditor_main
fill_auditor_new
test_collector
kill $AUDITOR_SERVER_PID
}

git checkout main

auditor_main

git checkout -

auditor_new_migration

trap "cleanup_exit" SIGINT SIGQUIT SIGTERM EXIT

0 comments on commit d5586db

Please sign in to comment.