Add docker setup (#278)
* Add docker setup Add a setup script that spawns a docker container before running test Easy for contributors now to test, no need for a local instance * Update readme with docs on running test * Allow check for container status Check for if the container is up before proceeding * Add options for customized user,pass,port * Fix seeding on database Only seed the database if user specifies CI=true OR when the tests actually run on the CI * Update readme with windows env var
This commit is contained in:
parent
eefa6813c6
commit
a45e22eeb8
24
README.md
24
README.md
|
@ -356,7 +356,25 @@ Destroying models may result in errors due to foreign key integrity. First delet
|
|||
|
||||
## Running tests
|
||||
|
||||
The tests in this repository are mainly integration tests, meaning you will need to run them using our preconfigured test server.
|
||||
### Own instance
|
||||
If you have a local or remote MySQL instance and would like to use that to run the test suite, use the following command:
|
||||
- Linux
|
||||
```bash
|
||||
MYSQL_HOST=<HOST> MYSQL_USER=<USER> MYSQL_PASSWORD=<PASSWORD> MYSQL_PORT=<PORT> CI=true npm test
|
||||
```
|
||||
- Windows
|
||||
```bash
|
||||
SET MYSQL_HOST=<HOST> SET MYSQL_USER=<USER> SET MYSQL_PASSWORD=<PASSWORD> SET MYSQL_PORT=<PORT> SET CI=true npm test
|
||||
```
|
||||
|
||||
1. Ask a core developer for instructions on how to set up test server credentials on your machine
|
||||
2. `npm test`
|
||||
### Docker
|
||||
If you do not have a local MySQL instance, you can also run the test suite with very minimal requirements.
|
||||
- Assuming you have [Docker](https://docs.docker.com/engine/installation/) installed, run the following script which would spawn a MySQL instance on your local:
|
||||
```bash
|
||||
source setup.sh <USER> <PASSWORD> <PORT>
|
||||
```
|
||||
where `<USER>`, `<PASSWORD>` and `<PORT>` are optional parameters. The default values are `root`, `pass` and `3306`.
|
||||
- Run the test:
|
||||
```bash
|
||||
npm test
|
||||
```
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
if (!process.env.TEST_MYSQL_USER &&
|
||||
!process.env.MYSQL_USER &&
|
||||
!process.env.CI) {
|
||||
if (!process.env.CI) {
|
||||
return console.log('Not seeding DB with test db');
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
#!/bin/bash
|
||||
|
||||
### Shell script to spin up a docker container for mysql.
|
||||
|
||||
## color codes
|
||||
RED='\033[1;31m'
|
||||
GREEN='\033[1;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
CYAN='\033[1;36m'
|
||||
PLAIN='\033[0m'
|
||||
|
||||
## variables
|
||||
MYSQL_CONTAINER="mysql_c"
|
||||
USER="root"
|
||||
PASSWORD="pass"
|
||||
PORT=3306
|
||||
if [ "$1" ]; then
|
||||
USER=$1
|
||||
fi
|
||||
if [ "$2" ]; then
|
||||
PASSWORD=$2
|
||||
fi
|
||||
if [ "$3" ]; then
|
||||
PORT=$3
|
||||
fi
|
||||
|
||||
## check if docker exists
|
||||
printf "\n${RED}>> Checking for docker${PLAIN} ${GREEN}...${PLAIN}"
|
||||
docker -v > /dev/null 2>&1
|
||||
DOCKER_EXISTS=$?
|
||||
if [ "$DOCKER_EXISTS" -ne 0 ]; then
|
||||
printf "\n${CYAN}Status: ${PLAIN}${RED}Docker not found. Terminating setup.${PLAIN}\n"
|
||||
exit 1
|
||||
fi
|
||||
printf "\n${CYAN}Found docker. Moving on with the setup.${PLAIN}\n"
|
||||
|
||||
|
||||
## cleaning up previous builds
|
||||
printf "\n${RED}>> Finding old builds and cleaning up${PLAIN} ${GREEN}...${PLAIN}"
|
||||
docker rm -f $MYSQL_CONTAINER > /dev/null 2>&1
|
||||
printf "\n${CYAN}Clean up complete.${PLAIN}\n"
|
||||
|
||||
## pull latest mysql image
|
||||
printf "\n${RED}>> Pulling latest mysql image${PLAIN} ${GREEN}...${PLAIN}"
|
||||
docker pull mysql:latest > /dev/null 2>&1
|
||||
printf "\n${CYAN}Image successfully built.${PLAIN}\n"
|
||||
|
||||
## run the mysql container
|
||||
printf "\n${RED}>> Starting the mysql container${PLAIN} ${GREEN}...${PLAIN}"
|
||||
CONTAINER_STATUS=$(docker run --name $MYSQL_CONTAINER -e MYSQL_ROOT_USER=$USER -e MYSQL_ROOT_PASSWORD=$PASSWORD -p $PORT:3306 -d mysql:latest 2>&1)
|
||||
if [[ "$CONTAINER_STATUS" == *"Error"* ]]; then
|
||||
printf "\n${CYAN}Status: ${PLAIN}${RED}Error starting container. Terminating setup.${PLAIN}\n"
|
||||
exit 1
|
||||
fi
|
||||
docker cp ./test/schema.sql $MYSQL_CONTAINER:/home/ > /dev/null 2>&1
|
||||
printf "\n${CYAN}Container is up and running.${PLAIN}\n"
|
||||
|
||||
## export the schema to the mysql database
|
||||
printf "\n${RED}>> Exporting schema to database${PLAIN} ${GREEN}...${PLAIN}\n"
|
||||
|
||||
## command to export schema
|
||||
docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD < /home/schema.sql" > /dev/null 2>&1
|
||||
|
||||
## variables needed to health check export schema
|
||||
OUTPUT=$?
|
||||
TIMEOUT=120
|
||||
TIME_PASSED=0
|
||||
WAIT_STRING="."
|
||||
|
||||
printf "\n${GREEN}Waiting for database to respond with updated schema $WAIT_STRING${PLAIN}"
|
||||
while [ "$OUTPUT" -ne 0 ] && [ "$TIMEOUT" -gt 0 ]
|
||||
do
|
||||
docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD < /home/schema.sql" > /dev/null 2>&1
|
||||
OUTPUT=$?
|
||||
sleep 1s
|
||||
TIMEOUT=$((TIMEOUT - 1))
|
||||
TIME_PASSED=$((TIME_PASSED + 1))
|
||||
|
||||
if [ "$TIME_PASSED" -eq 5 ]; then
|
||||
printf "${GREEN}.${PLAIN}"
|
||||
TIME_PASSED=0
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$TIMEOUT" -le 0 ]; then
|
||||
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Failed to export schema. Terminating setup.${PLAIN}\n"
|
||||
exit 1
|
||||
fi
|
||||
printf "\n${CYAN}Successfully exported schema to database.${PLAIN}\n"
|
||||
|
||||
## set env variables for running test
|
||||
printf "\n${RED}>> Setting env variables to run test${PLAIN} ${GREEN}...${PLAIN}"
|
||||
export MYSQL_HOST=0.0.0.0
|
||||
export MYSQL_PORT=$PORT
|
||||
export MYSQL_USER=$USER
|
||||
export MYSQL_PASSWORD=$PASSWORD
|
||||
printf "\n${CYAN}Env variables set.${PLAIN}\n"
|
||||
|
||||
printf "\n${CYAN}Status: ${PLAIN}${GREEN}Set up completed successfully.${PLAIN}\n"
|
||||
printf "\n${CYAN}To run the test suite:${PLAIN} ${YELLOW}npm test${PLAIN}\n\n"
|
Loading…
Reference in New Issue