Skip to content

Commit

Permalink
Merge pull request #149 from adhocteam/dcloud/test-script-improvements
Browse files Browse the repository at this point in the history
Improvements and fixes to bin/run-tests
  • Loading branch information
dcloud authored Feb 5, 2021
2 parents 5a31b94 + 68a76de commit f327246
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
87 changes: 70 additions & 17 deletions bin/run-tests
Original file line number Diff line number Diff line change
@@ -1,24 +1,77 @@
#!/bin/bash

echo "Running tests in using test config 'docker-compose.test.yml'"
# Start containers
docker-compose -f 'docker-compose.test.yml' up -d
declare -a options=(frontend backend)
declare lr=">>>>>>>>"
declare -i exit_code=0

# Let postgres initialize
echo "Give postgres a few seconds to start up..."
sleep 5
log() {
echo "$lr $*"
}

# Migrate and seed db
docker exec test-backend bash -c "yarn db:migrate"
docker exec test-backend bash -c "yarn db:seed;"
check_exit() {
if [[ "$1" -ne 0 ]]; then
echo "$lr last docker-compose command failed"
((exit_code++))
fi
}

# Test backend
docker exec test-backend bash -c "yarn test:ci"
main() {
local opt=""

# Test frontend
docker exec test-frontend bash -c "yarn --cwd frontend run test:ci"
for o in "${options[@]}"; do
if [[ "${1}" == "$o" ]]; then
opt="$o";
fi
done

# Cleanup
docker-compose \
-f 'docker-compose.test.yml' \
down --volumes
log "Running tests in using test config 'docker-compose.test.yml'"
# Start containers
docker-compose -f 'docker-compose.test.yml' up -d
check_exit "$?"

# Let postgres initialize
echo
log "Giving postgres a few seconds to start up..."
sleep 5

# Migrate and seed db
echo
log "Migrating & seeding db"
docker exec test-backend bash -c "yarn db:migrate"
check_exit "$?"
docker exec test-backend bash -c "yarn db:seed;"
check_exit "$?"

if [[ "$opt" == "backend" || -z "$opt" ]]; then
# Test backend
echo
log "Running backend tests"
docker exec test-backend bash -c "yarn test:ci"
check_exit "$?"
fi

if [[ "$opt" == "frontend" || -z "$opt" ]]; then
# Test frontend
echo
log "Running frontend tests"
docker exec test-frontend bash -c "yarn --cwd frontend run test:ci"
check_exit "$?"
fi

# Cleanup
echo
log "Cleaning up test containers"
docker-compose \
-f 'docker-compose.test.yml' \
down --volumes
check_exit "$?"

if [[ $exit_code -ne 0 ]]; then
echo
log "Errors occurred during script execution"
fi

exit "$exit_code"
}

main "$@"
1 change: 1 addition & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
test-frontend:
build:
context: .
container_name: test-frontend
command: yarn start
user: ${CURRENT_USER:-root}
stdin_open: true
Expand Down
4 changes: 4 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ When writing tests that create database records, it might also help to use a `tr

To simplify running tests in Docker, there is a bash script, `./bin/run-tests` that will run the appropriate commands to start `test-` variations of the services used in tests. You should be able to run tests using that command while your development Docker environment is running. The script uses a separate `docker-compose.test.yml` which does not create a user-accessible network and cleans up after itself once tests have run.

This script is written such that it will log errors, but won't exit if a docker command fails. It will count the number of errors and the number of errors will be the exit code (`$?`) for the script. So if three docker commands fail, the exit code would be 3.

By default, `./bin/run-tests` will run both backend and frontend tests. If you want to run only one set of tests, supply 'frontend' or 'backend' as a parameter. So to run only the backend tests, you'd run `./bin/run-tests backend`.

### Running tests in your development Docker environment

When running tests in Docker, be aware that there are tests that will modify/delete database records. For tests to run, the 'db' service needs to exist and `db:migrate` and `db:seed` need to have been run (to create the tables and populate certain records).
Expand Down

0 comments on commit f327246

Please sign in to comment.