Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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: |
cat > .env <<EOF
JURY_ADMIN_PASSWORD=admin
[email protected]
EMAIL_HOST=smtp.example.com
EMAIL_USERNAME=test
EMAIL_PASSWORD=test
EOF
./scripts/test-runner.sh
37 changes: 37 additions & 0 deletions scripts/test-runner.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"os"
"tests/src"
"tests/tests"
)
Expand All @@ -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{
Expand Down
11 changes: 10 additions & 1 deletion tests/src/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
Expand All @@ -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
}

Expand All @@ -34,6 +42,7 @@ func WaitForBackend(logger *Logger) {
break
}
}
return nil
}

func GetRequest(logger *Logger, url string, authHeader string) string {
Expand Down