diff --git a/README.md b/README.md index d6a10e3cb4e..cf82b40dac6 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,14 @@ keep it up to date, documenting the purpose of the various types of tests. By default `rspec` will randomly pick between postgres and mysql. +#### Running the Units Tests in Docker + +If you don't want to set up a up a test DB locally, you can run +`./scripts/start-db-in-docker.sh`. This script will start a docker container +with a running postgres. Then you can run the tests from the docker container +by running `rspec`. + +#### Running the Test DB Locally If postgres is not running on your OSX machine, you can start up a server by doing the following: ``` brew services start postgresql diff --git a/scripts/docker-shell-with-started-db b/scripts/docker-shell-with-started-db new file mode 100755 index 00000000000..b3c87a9b4f8 --- /dev/null +++ b/scripts/docker-shell-with-started-db @@ -0,0 +1,33 @@ +#!/bin/bash +set -e -u + +ROOT_DIR_PATH="$(cd $(dirname $0)/.. && pwd)" +cd "${ROOT_DIR_PATH}" + +# if DB not set, default to postgres because we usually use +# postgres in our deployments +db=${DB:-"postgres"} + +if [ "$db" = "mysql" ]; then + docker_image=cloudfoundry/tas-runtime-mysql-5.7 +elif [ "$db" = "mysql8" ]; then + docker_image=cloudfoundry/tas-runtime-mysql-8.0 +else + docker_image=cloudfoundry/tas-runtime-postgres +fi + + +if [[ "${DB}" =~ mysql ]]; then + db=mysql +fi + +docker run \ + --rm \ + -it \ + --privileged \ + -v "${PWD}:/cloud_controller_ng" \ + -e "DB=$db" \ + --cap-add ALL \ + -w /cloud_controller_ng \ + $docker_image \ + /cloud_controller_ng/scripts/start-db-in-docker.sh "$@" diff --git a/scripts/start-db-in-docker.sh b/scripts/start-db-in-docker.sh new file mode 100755 index 00000000000..1adc6852072 --- /dev/null +++ b/scripts/start-db-in-docker.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +set -e -u + +SCRIPT_PATH="$(cd "$(dirname "${0}")" && pwd)" + +function bootDB { + db="$1" + + if [ "${db}" = "postgres" ]; then + launchDB="(/postgres-entrypoint.sh postgres &> /var/log/postgres-boot.log) &" + testConnection="psql -h localhost -U postgres -c '\conninfo'" + elif [ "${db}" = "mysql" ] || [ "${db}" = "mysql-5.6" ] || [ "${db}" = "mysql8" ]; then + launchDB="(MYSQL_ROOT_PASSWORD=password /mysql-entrypoint.sh mysqld &> /var/log/mysql-boot.log) &" + testConnection="mysql -h localhost -u root -D mysql -e '\s;' --password='password'" + else + echo "skipping database" + return 0 + fi + + echo -n "booting ${db}" + eval "$launchDB" + for _ in $(seq 1 60); do + if eval "${testConnection}" &> /dev/null; then + echo "connection established to ${db}" + return 0 + fi + echo -n "." + sleep 1 + done + eval "${testConnection}" || true + echo "unable to connect to ${db}" + exit 1 +} + +function moreSetup { + apt-get update + apt-get install -y libpq-dev default-libmysqlclient-dev + bundle install + rake db:create +} + +cd /cloud_controller_ng +export GOPATH=$PWD + +bootDB "${DB:-"notset"}" +moreSetup + +set +e +exec /bin/bash "$@"