From ddbbad0ff4f5c6916f12401a054fbe8b5a8c85df Mon Sep 17 00:00:00 2001 From: Amelia Downs Date: Tue, 12 Sep 2023 21:34:38 +0000 Subject: [PATCH 1/2] add script to start docker container with db for unit tests I don't work on a machine that has a local DB. When I started working on readiness healthcheck stuff I spent 2+ hours just trying to get one setup. In the end, I made this script from a similar one in cf-networking-release. I don't promise that it works for every CAPI use case (it only works with postgres for example), but it works great for when a new person wants to run some tests and iterate quickly. To use, run: `./scripts/docker-shell-with-started-db` --- README.md | 8 ++++++ scripts/docker-shell-with-started-db | 22 ++++++++++++++ scripts/start-db-in-docker.sh | 43 ++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100755 scripts/docker-shell-with-started-db create mode 100755 scripts/start-db-in-docker.sh 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..0335cc71807 --- /dev/null +++ b/scripts/docker-shell-with-started-db @@ -0,0 +1,22 @@ +#!/bin/bash +set -e -u + +ROOT_DIR_PATH="$(cd $(dirname $0)/.. && pwd)" +cd "${ROOT_DIR_PATH}" + +db=postgres +docker_image=cloudfoundry/tas-runtime-postgres + +# This script only works for postgres because the tas-runtime mysql images do +# not have ruby on them. + +docker run \ + --rm \ + -it \ + --privileged \ + -v "${PWD}:/cloud_controller_ng" \ + -e "DB=$db" \ + --cap-add ALL \ + -w /cloud_controller_ng \ + $docker_image \ + /bin/bash /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..4baafab2a32 --- /dev/null +++ b/scripts/start-db-in-docker.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +set -e -u + +function bootDB { + db="$1" + + launchDB="(/postgres-entrypoint.sh postgres &> /var/log/postgres-boot.log) &" + testConnection="psql -h localhost -U postgres -c '\conninfo'" + createTestDB="psql -h localhost -U postgres -c 'create database cc_test'" + + echo -n "booting ${db}" + eval "$launchDB" + for _ in $(seq 1 60); do + if eval "${testConnection}" &> /dev/null; then + break + fi + echo -n "." + sleep 1 + done + + if eval "${testConnection}" &> /dev/null; then + echo "connection established to ${db}" + else + echo "unable to connect to ${db}" + exit 1 + fi +} + +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 From 31343da3b6610a9846126a13ff599a4c44d992f7 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Thu, 19 Oct 2023 20:16:41 +0000 Subject: [PATCH 2/2] Add mysql docker support for testing Signed-off-by: Geoff Franks Co-authored-by: Geoff Franks --- scripts/docker-shell-with-started-db | 21 +++++++++++++----- scripts/start-db-in-docker.sh | 33 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/scripts/docker-shell-with-started-db b/scripts/docker-shell-with-started-db index 0335cc71807..b3c87a9b4f8 100755 --- a/scripts/docker-shell-with-started-db +++ b/scripts/docker-shell-with-started-db @@ -4,11 +4,22 @@ set -e -u ROOT_DIR_PATH="$(cd $(dirname $0)/.. && pwd)" cd "${ROOT_DIR_PATH}" -db=postgres -docker_image=cloudfoundry/tas-runtime-postgres +# if DB not set, default to postgres because we usually use +# postgres in our deployments +db=${DB:-"postgres"} -# This script only works for postgres because the tas-runtime mysql images do -# not have ruby on them. +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 \ @@ -19,4 +30,4 @@ docker run \ --cap-add ALL \ -w /cloud_controller_ng \ $docker_image \ - /bin/bash /cloud_controller_ng/scripts/start-db-in-docker.sh + /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 index 4baafab2a32..1adc6852072 100755 --- a/scripts/start-db-in-docker.sh +++ b/scripts/start-db-in-docker.sh @@ -2,29 +2,35 @@ set -e -u +SCRIPT_PATH="$(cd "$(dirname "${0}")" && pwd)" + function bootDB { db="$1" - - launchDB="(/postgres-entrypoint.sh postgres &> /var/log/postgres-boot.log) &" - testConnection="psql -h localhost -U postgres -c '\conninfo'" - createTestDB="psql -h localhost -U postgres -c 'create database cc_test'" + + 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 - break + echo "connection established to ${db}" + return 0 fi echo -n "." sleep 1 done - - if eval "${testConnection}" &> /dev/null; then - echo "connection established to ${db}" - else - echo "unable to connect to ${db}" - exit 1 - fi + eval "${testConnection}" || true + echo "unable to connect to ${db}" + exit 1 } function moreSetup { @@ -39,5 +45,6 @@ export GOPATH=$PWD bootDB "${DB:-"notset"}" moreSetup + set +e -exec /bin/bash +exec /bin/bash "$@"