Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script to start docker container with db for unit tests #3434

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you don't want to set up a up a test DB locally, you can run
If you don't want to set up a test DB locally, you can run

`./scripts/start-db-in-docker.sh`. This script will start a docker container
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You run scripts/docker-shell-with-started-db from the command line, right?

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
Expand Down
33 changes: 33 additions & 0 deletions scripts/docker-shell-with-started-db
Original file line number Diff line number Diff line change
@@ -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 "$@"
50 changes: 50 additions & 0 deletions scripts/start-db-in-docker.sh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In scripts/docker-shell-with-started-db the db variable will be set to mysql if e.g. DB=mysql8; so the two ORs are not needed.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? We don't have Golang code...


bootDB "${DB:-"notset"}"
moreSetup

set +e
exec /bin/bash "$@"