Skip to content

Commit

Permalink
chore: run the tests on a temporary container
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDecMeetsMore committed Jul 17, 2024
1 parent cc08ebd commit b2e82e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ _setup_client_node:
dev: _setup_db
cd scheduler && cargo run

# Test
test: _setup_db
cd scheduler && cargo nextest run --workspace
# Run the tests on a temporary DB container
test:
bash ./scripts/run_tests.sh

# Lint
lint: _setup_db
Expand Down
31 changes: 31 additions & 0 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /bin/bash

# Search for a free port to bind the temporary PG container
BASE_PORT=1234
INCREMENT=1

PORT=$BASE_PORT
IS_FREE=$(netstat -taln | grep $PORT)

while [[ -n "$IS_FREE" ]]; do
PORT=$((PORT + INCREMENT))
IS_FREE=$(netstat -taln | grep $PORT)
done

# Generate a random name for the temporary PG container
RANDOM_NAME="pg_test_$(date +%s)"

# Launch a PG container
docker run -d --name $RANDOM_NAME -p $PORT:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=scheduler postgres:13

# Set the DATABASE_URL environment variable
export DATABASE_URL="postgres://postgres:postgres@localhost:${PORT}/scheduler"

# Run the migrations
cd scheduler/crates/infra && sqlx migrate run && cd ../../..

# Run the tests
cd scheduler && cargo nextest run --workspace && cd ..

# Stop and remove the temporary PG container
docker rm -f $RANDOM_NAME

0 comments on commit b2e82e9

Please sign in to comment.