From 5242df2b7d17acfca0b6589347b807109f47f55f Mon Sep 17 00:00:00 2001 From: Steven Tang Date: Sun, 4 Dec 2022 18:02:36 +1100 Subject: [PATCH 1/2] ci: upgrade testing for SQLite --- .github/workflows/ci.yml | 17 +++++++++ .gitignore | 1 + testing/run_api_tests.sh | 80 ++++++++++++++++++++++++++++++---------- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index afbdf34..98c5829 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,3 +101,20 @@ jobs: - name: Build run: go build -v . + + migration: + name: Migration tests + runs-on: ubuntu-latest + + steps: + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.19 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Run sqlite + run: ./testing/run_api_tests.sh sqlite y diff --git a/.gitignore b/.gitignore index 84f20a6..34c07e3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ wakapi build *.exe *.db +*.zip config*.yml !config.default.yml !testing/config.testing.yml diff --git a/testing/run_api_tests.sh b/testing/run_api_tests.sh index ffdb196..7722fe2 100755 --- a/testing/run_api_tests.sh +++ b/testing/run_api_tests.sh @@ -1,7 +1,5 @@ #!/bin/bash - -echo "Compiling." -CGO_ENABLED=0 go build +set -e if ! command -v newman &> /dev/null then @@ -9,26 +7,61 @@ then exit 1 fi -cd "$(dirname "$0")" +script_path=$(realpath "${BASH_SOURCE[0]}") +script_dir=$(dirname "$script_path") -echo "Creating database and schema ..." -sqlite3 wakapi_testing.db < schema.sql +echo "Compiling." +(cd "$script_dir/.." || exit 1; CGO_ENABLED=0 go build) -echo "Importing seed data ..." -sqlite3 wakapi_testing.db < data.sql +cd "$script_dir" || exit 1 +# Download previous release (when upgrade testing) +INITIAL_RUN_EXE="../wakapi" +if [[ "$2" == "y" ]]; then + if [ ! -f wakapi_linux_amd64.zip ]; then + curl https://github.com/muety/wakapi/releases/latest/download/wakapi_linux_amd64.zip -O -L + fi + unzip -o wakapi_linux_amd64.zip + INITIAL_RUN_EXE="./wakapi" + echo "Running tests with release version" +fi + +# Initialise test data +case $1 in + sqlite|*) + rm -f wakapi_testing.db + + echo "Creating database and schema ..." + sqlite3 wakapi_testing.db < schema.sql + + echo "Importing seed data ..." + sqlite3 wakapi_testing.db < data.sql + + CONFIG="config.testing.yml" + ;; +esac + +wait_for_wakapi () { + counter=0 + echo "Waiting for Wakapi to come up ..." + until curl --output /dev/null --silent --get --fail http://localhost:3000/api/health; do + if [ "$counter" -ge 5 ]; then + echo "Waited for 5s, but Wakapi failed to come up ..." + exit 1 + fi + + printf '.' + sleep 1 + counter=$((counter+1)) + done + printf "\n" +} + +# Run tests echo "Running Wakapi testing instance in background ..." -../wakapi -config config.testing.yml & +"$INITIAL_RUN_EXE" -config "$CONFIG" & pid=$! -echo "Waiting for Wakapi to come up ..." -until $(curl --output /dev/null --silent --get --fail http://localhost:3000/api/health); do - printf '.' - sleep 1 -done - -echo "" - echo "Running test collection ..." newman run "wakapi_api_tests.postman_collection.json" exit_code=$? @@ -36,7 +69,16 @@ exit_code=$? echo "Shutting down Wakapi ..." kill -TERM $pid -echo "Deleting database ..." -rm wakapi_testing.db +# Run upgrade tests +if [[ "$2" == "y" ]]; then + echo "Now running migrations with build" + ../wakapi -config "$CONFIG" & + pid=$! + wait_for_wakapi + echo "Shutting down Wakapi ..." + kill -TERM $pid +fi + +echo "Exiting with status $exit_code" exit $exit_code From f5395e36ad7fabf52b8f7c6a5f51820976fb92d1 Mon Sep 17 00:00:00 2001 From: Steven Tang Date: Tue, 6 Dec 2022 18:28:23 +1100 Subject: [PATCH 2/2] ci: SQLite upgrade testing comments --- .github/workflows/ci.yml | 2 +- testing/run_api_tests.sh | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98c5829..cd58d91 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,4 +117,4 @@ jobs: uses: actions/checkout@v2 - name: Run sqlite - run: ./testing/run_api_tests.sh sqlite y + run: ./testing/run_api_tests.sh sqlite --migration diff --git a/testing/run_api_tests.sh b/testing/run_api_tests.sh index 7722fe2..447c6fb 100755 --- a/testing/run_api_tests.sh +++ b/testing/run_api_tests.sh @@ -1,5 +1,4 @@ #!/bin/bash -set -e if ! command -v newman &> /dev/null then @@ -7,6 +6,15 @@ then exit 1 fi +for i in "$@"; do + case $i in + --migration) + MIGRATION=1 + shift + ;; + esac +done + script_path=$(realpath "${BASH_SOURCE[0]}") script_dir=$(dirname "$script_path") @@ -16,13 +24,14 @@ echo "Compiling." cd "$script_dir" || exit 1 # Download previous release (when upgrade testing) -INITIAL_RUN_EXE="../wakapi" -if [[ "$2" == "y" ]]; then +initial_run_exe="../wakapi" +if [[ $MIGRATION -eq 1 ]]; then if [ ! -f wakapi_linux_amd64.zip ]; then + echo "Downloading latest release" curl https://github.com/muety/wakapi/releases/latest/download/wakapi_linux_amd64.zip -O -L fi unzip -o wakapi_linux_amd64.zip - INITIAL_RUN_EXE="./wakapi" + initial_run_exe="./wakapi" echo "Running tests with release version" fi @@ -37,7 +46,7 @@ case $1 in echo "Importing seed data ..." sqlite3 wakapi_testing.db < data.sql - CONFIG="config.testing.yml" + config="config.testing.yml" ;; esac @@ -54,13 +63,15 @@ wait_for_wakapi () { sleep 1 counter=$((counter+1)) done + sleep 1 printf "\n" } # Run tests echo "Running Wakapi testing instance in background ..." -"$INITIAL_RUN_EXE" -config "$CONFIG" & +"$initial_run_exe" -config "$config" & pid=$! +wait_for_wakapi echo "Running test collection ..." newman run "wakapi_api_tests.postman_collection.json" @@ -70,9 +81,9 @@ echo "Shutting down Wakapi ..." kill -TERM $pid # Run upgrade tests -if [[ "$2" == "y" ]]; then - echo "Now running migrations with build" - ../wakapi -config "$CONFIG" & +if [[ $MIGRATION -eq 1 ]]; then + echo "Running migrations with build" + ../wakapi -config "$config" & pid=$! wait_for_wakapi