From 5f628162e298a2198b4084ceef0ba874676f330b Mon Sep 17 00:00:00 2001 From: Michael Zhao Date: Wed, 10 Sep 2025 20:37:25 -0500 Subject: [PATCH 1/4] Added test runner and github action --- .github/workflows/test.yml | 17 +++++++++++++++++ scripts/test-runner.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100755 scripts/test-runner.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b15e4a1 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,17 @@ +name: Run Tests + +on: + pull_request: + types: [opened, synchronize] + +jobs: + backend-test: + name: Run Backend Tests + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Run Test Script + run: | + ./scripts/test-runner.sh diff --git a/scripts/test-runner.sh b/scripts/test-runner.sh new file mode 100755 index 0000000..d1eb724 --- /dev/null +++ b/scripts/test-runner.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# This script will run the test using docker compose! +# It will poll for the jury-testing container status until it has exited. +# Then it will read the container logs to see if any tests have failed. +# Logs will be printed to the console. +# Finally, it will exit and kill the docker compose containers + +# Start docker containers and wait a second (just for fun) +# docker compose -f docker-compose.test.yml up -d +# sleep 1 + +echo "Running tests..." + +timeout=0 # Max timeout set to 60 seconds, may need to change if longer tests! +while [ "$timeout" -lt 60 ]; do + if [ "$(docker inspect --format '{{.State.Status}}' jury-testing)" = "exited" ]; then + break + fi + sleep 1 +done + +# Get logs and get rid of containers +logs=$(docker logs jury-testing) +# docker compose -f docker-compose.test.yml down + +# If no failed lines, exit with success +failed=$(echo "$logs" | grep "failed") +if [[ -z "$failed" ]]; then + echo "Success :)" + exit 0 +fi + +# Otherwise, exit with failure +printf "$logs" +printf "\n\n###################################\n##### THERE ARE TEST FAILURES #####\n###################################\n" +exit 1 From 39e7e51db02b48db622fe524ad67ea51d4d3a06c Mon Sep 17 00:00:00 2001 From: Michael Zhao Date: Wed, 10 Sep 2025 20:40:30 -0500 Subject: [PATCH 2/4] Oops forgot to add docker compose up --- scripts/test-runner.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/test-runner.sh b/scripts/test-runner.sh index d1eb724..19ff8d9 100755 --- a/scripts/test-runner.sh +++ b/scripts/test-runner.sh @@ -7,8 +7,8 @@ # Finally, it will exit and kill the docker compose containers # Start docker containers and wait a second (just for fun) -# docker compose -f docker-compose.test.yml up -d -# sleep 1 +docker compose -f docker-compose.test.yml up -d +sleep 1 echo "Running tests..." @@ -22,7 +22,7 @@ done # Get logs and get rid of containers logs=$(docker logs jury-testing) -# docker compose -f docker-compose.test.yml down +docker compose -f docker-compose.test.yml down # If no failed lines, exit with success failed=$(echo "$logs" | grep "failed") From f819d555844d1c872a915af3eb19267e3c330565 Mon Sep 17 00:00:00 2001 From: Michael Zhao Date: Wed, 10 Sep 2025 20:59:58 -0500 Subject: [PATCH 3/4] Fix issue with env not being defined and added retry check to backend connection in test runner --- .github/workflows/test.yml | 1 + tests/main.go | 6 +++++- tests/src/requests.go | 11 ++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b15e4a1..19a2f59 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,4 +14,5 @@ jobs: - name: Run Test Script run: | + echo "JURY_ADMIN_PASSWORD=admin\nEMAIL_FROM=test@example.com\nEMAIL_HOST=smtp.example.com\nEMAIL_USERNAME=test\nEMAIL_PASSWORD=test" >> .env ./scripts/test-runner.sh diff --git a/tests/main.go b/tests/main.go index 067fdd4..0e57a68 100644 --- a/tests/main.go +++ b/tests/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "os" "tests/src" "tests/tests" ) @@ -21,7 +22,10 @@ func main() { defer db.Client().Disconnect(context.Background()) // Wait for backend to load - src.WaitForBackend(logger) + err := src.WaitForBackend(logger) + if err != nil { + os.Exit(1) + } // Create a context with the database and logger context := &src.Context{ diff --git a/tests/src/requests.go b/tests/src/requests.go index 2c3f8ba..05a791a 100644 --- a/tests/src/requests.go +++ b/tests/src/requests.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "errors" "io" "net/http" "strings" @@ -15,17 +16,24 @@ import ( type H map[string]any // WaitForBackend will wait for the backend to load, checking the URL every 5 seconds -func WaitForBackend(logger *Logger) { +func WaitForBackend(logger *Logger) error { logger.Log(Info, "Waiting for backend to load...\n") url := getBaseUrl() + retry := 3 // Retry 3 times before giving up for { // Send a GET request to the backend res, err := http.Get(url) if err != nil { + if (retry < 0) { + logger.Log(Error, "Error: failed to connect to backend. Aborting.") + return errors.New("failed to connect to backend") + } + logger.Log(Info, "Error sending GET request to %s, waiting 5 seconds: %s\n", url, err.Error()) time.Sleep(5 * time.Second) + retry-- continue } @@ -34,6 +42,7 @@ func WaitForBackend(logger *Logger) { break } } + return nil } func GetRequest(logger *Logger, url string, authHeader string) string { From 318698de938c60bb7ba67b074ccb1340335511a5 Mon Sep 17 00:00:00 2001 From: Michael Zhao Date: Wed, 10 Sep 2025 22:18:19 -0500 Subject: [PATCH 4/4] Fixed env loading in github action --- .github/workflows/test.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 19a2f59..544eca8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,5 +14,11 @@ jobs: - name: Run Test Script run: | - echo "JURY_ADMIN_PASSWORD=admin\nEMAIL_FROM=test@example.com\nEMAIL_HOST=smtp.example.com\nEMAIL_USERNAME=test\nEMAIL_PASSWORD=test" >> .env + cat > .env <