From 18ddc84929f8b7f8f8e1141a41b464155889436d Mon Sep 17 00:00:00 2001 From: Sakib Hasan Date: Mon, 15 May 2017 13:38:30 -0400 Subject: [PATCH] Fix docker setup (#279) * Add support for host and database * Order the optional parameters correctly --- README.md | 8 ++++---- setup.sh | 24 +++++++++++++++++------- test/init.js | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a826d5f..5ab0602 100644 --- a/README.md +++ b/README.md @@ -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= MYSQL_USER= MYSQL_PASSWORD= MYSQL_PORT= CI=true npm test +MYSQL_HOST= MYSQL_PORT= MYSQL_USER= MYSQL_PASSWORD= MYSQL_DATABASE= CI=true npm test ``` - Windows ```bash -SET MYSQL_HOST= SET MYSQL_USER= SET MYSQL_PASSWORD= SET MYSQL_PORT= SET CI=true npm test +SET MYSQL_HOST= SET MYSQL_PORT= SET MYSQL_USER= SET MYSQL_PASSWORD= SET MYSQL_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 +source setup.sh ``` -where ``, `` and `` are optional parameters. The default values are `root`, `pass` and `3306`. +where ``, ``, ``, `` and `` are optional parameters. The default values are `localhost`, `3306`, `root`, `pass` and `testdb` respectively. - Run the test: ```bash npm test diff --git a/setup.sh b/setup.sh index cebe692..ba31568 100644 --- a/setup.sh +++ b/setup.sh @@ -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" diff --git a/test/init.js b/test/init.js index af9efbe..a4bfcbb 100644 --- a/test/init.js +++ b/test/init.js @@ -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,