Fix docker setup (#279)

* Add support for host and database

* Order the optional parameters correctly
This commit is contained in:
Sakib Hasan 2017-05-15 13:38:30 -04:00 committed by GitHub
parent a45e22eeb8
commit 18ddc84929
3 changed files with 22 additions and 12 deletions

View File

@ -360,20 +360,20 @@ Destroying models may result in errors due to foreign key integrity. First delet
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
MYSQL_HOST=<HOST> MYSQL_PORT=<PORT> MYSQL_USER=<USER> MYSQL_PASSWORD=<PASSWORD> MYSQL_DATABASE=<DATABASE> 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
SET MYSQL_HOST=<HOST> SET MYSQL_PORT=<PORT> SET MYSQL_USER=<USER> SET MYSQL_PASSWORD=<PASSWORD> SET MYSQL_DATABASE=<DATABASE> SET CI=true 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>
source setup.sh <HOST> <PORT> <USER> <PASSWORD> <DATABASE>
```
where `<USER>`, `<PASSWORD>` and `<PORT>` are optional parameters. The default values are `root`, `pass` and `3306`.
where `<HOST>`, `<PORT>`, `<USER>`, `<PASSWORD>` and `<DATABASE>` are optional parameters. The default values are `localhost`, `3306`, `root`, `pass` and `testdb` respectively.
- Run the test:
```bash
npm test

View File

@ -11,17 +11,25 @@ PLAIN='\033[0m'
## variables
MYSQL_CONTAINER="mysql_c"
HOST="localhost"
USER="root"
PASSWORD="pass"
PORT=3306
DATABASE="testdb"
if [ "$1" ]; then
USER=$1
HOST=$1
fi
if [ "$2" ]; then
PASSWORD=$2
PORT=$2
fi
if [ "$3" ]; then
PORT=$3
USER=$3
fi
if [ "$4" ]; then
PASSWORD=$4
fi
if [ "$5" ]; then
DATABASE=$5
fi
## check if docker exists
@ -29,7 +37,7 @@ 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"
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Docker not found. Terminating setup.${PLAIN}\n\n"
exit 1
fi
printf "\n${CYAN}Found docker. Moving on with the setup.${PLAIN}\n"
@ -49,7 +57,7 @@ printf "\n${CYAN}Image successfully built.${PLAIN}\n"
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"
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Error starting container. Terminating setup.${PLAIN}\n\n"
exit 1
fi
docker cp ./test/schema.sql $MYSQL_CONTAINER:/home/ > /dev/null 2>&1
@ -83,18 +91,20 @@ while [ "$OUTPUT" -ne 0 ] && [ "$TIMEOUT" -gt 0 ]
done
if [ "$TIMEOUT" -le 0 ]; then
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Failed to export schema. Terminating setup.${PLAIN}\n"
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Failed to export schema. Terminating setup.${PLAIN}\n\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_HOST=$HOST
export MYSQL_PORT=$PORT
export MYSQL_USER=$USER
export MYSQL_PASSWORD=$PASSWORD
export MYSQL_DATABASE=$DATABASE
printf "\n${CYAN}Env variables set.${PLAIN}\n"
printf "\n${CYAN}Status: ${PLAIN}${GREEN}Set up completed successfully.${PLAIN}\n"
printf "\n${CYAN}Instance url: ${YELLOW}mysql://$USER:$PASSWORD@$HOST/$DATABASE${PLAIN}\n"
printf "\n${CYAN}To run the test suite:${PLAIN} ${YELLOW}npm test${PLAIN}\n\n"

View File

@ -15,7 +15,7 @@ global.getConfig = function(options) {
var dbConf = {
host: process.env.MYSQL_HOST || config.host || 'localhost',
port: process.env.MYSQL_PORT || config.port || 3306,
database: 'myapp_test',
database: process.env.MYSQL_DATABASE || 'myapp_test',
username: process.env.MYSQL_USER || config.username,
password: process.env.MYSQL_PASSWORD || config.password,
createDatabase: true,