Skip to content

Commit 1b349fc

Browse files
Add test runner and github action (#305)
* Added test runner and github action * Oops forgot to add docker compose up * Fix issue with env not being defined and added retry check to backend connection in test runner * Fixed env loading in github action
1 parent 4432765 commit 1b349fc

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Run Tests
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
7+
jobs:
8+
backend-test:
9+
name: Run Backend Tests
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Run Test Script
16+
run: |
17+
cat > .env <<EOF
18+
JURY_ADMIN_PASSWORD=admin
19+
20+
EMAIL_HOST=smtp.example.com
21+
EMAIL_USERNAME=test
22+
EMAIL_PASSWORD=test
23+
EOF
24+
./scripts/test-runner.sh

scripts/test-runner.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# This script will run the test using docker compose!
4+
# It will poll for the jury-testing container status until it has exited.
5+
# Then it will read the container logs to see if any tests have failed.
6+
# Logs will be printed to the console.
7+
# Finally, it will exit and kill the docker compose containers
8+
9+
# Start docker containers and wait a second (just for fun)
10+
docker compose -f docker-compose.test.yml up -d
11+
sleep 1
12+
13+
echo "Running tests..."
14+
15+
timeout=0 # Max timeout set to 60 seconds, may need to change if longer tests!
16+
while [ "$timeout" -lt 60 ]; do
17+
if [ "$(docker inspect --format '{{.State.Status}}' jury-testing)" = "exited" ]; then
18+
break
19+
fi
20+
sleep 1
21+
done
22+
23+
# Get logs and get rid of containers
24+
logs=$(docker logs jury-testing)
25+
docker compose -f docker-compose.test.yml down
26+
27+
# If no failed lines, exit with success
28+
failed=$(echo "$logs" | grep "failed")
29+
if [[ -z "$failed" ]]; then
30+
echo "Success :)"
31+
exit 0
32+
fi
33+
34+
# Otherwise, exit with failure
35+
printf "$logs"
36+
printf "\n\n###################################\n##### THERE ARE TEST FAILURES #####\n###################################\n"
37+
exit 1

tests/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"os"
56
"tests/src"
67
"tests/tests"
78
)
@@ -21,7 +22,10 @@ func main() {
2122
defer db.Client().Disconnect(context.Background())
2223

2324
// Wait for backend to load
24-
src.WaitForBackend(logger)
25+
err := src.WaitForBackend(logger)
26+
if err != nil {
27+
os.Exit(1)
28+
}
2529

2630
// Create a context with the database and logger
2731
context := &src.Context{

tests/src/requests.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/base64"
66
"encoding/json"
7+
"errors"
78
"io"
89
"net/http"
910
"strings"
@@ -15,17 +16,24 @@ import (
1516
type H map[string]any
1617

1718
// WaitForBackend will wait for the backend to load, checking the URL every 5 seconds
18-
func WaitForBackend(logger *Logger) {
19+
func WaitForBackend(logger *Logger) error {
1920
logger.Log(Info, "Waiting for backend to load...\n")
2021

2122
url := getBaseUrl()
23+
retry := 3 // Retry 3 times before giving up
2224

2325
for {
2426
// Send a GET request to the backend
2527
res, err := http.Get(url)
2628
if err != nil {
29+
if (retry < 0) {
30+
logger.Log(Error, "Error: failed to connect to backend. Aborting.")
31+
return errors.New("failed to connect to backend")
32+
}
33+
2734
logger.Log(Info, "Error sending GET request to %s, waiting 5 seconds: %s\n", url, err.Error())
2835
time.Sleep(5 * time.Second)
36+
retry--
2937
continue
3038
}
3139

@@ -34,6 +42,7 @@ func WaitForBackend(logger *Logger) {
3442
break
3543
}
3644
}
45+
return nil
3746
}
3847

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

0 commit comments

Comments
 (0)