[PATCH v4 3/8] docker, tests: reference DATABASE_* as possible
You-Sheng Yang
vicamo at gmail.com
Thu Dec 9 14:38:38 AEDT 2021
Signed-off-by: You-Sheng Yang <vicamo at gmail.com>
---
.github/workflows/ci.yaml | 40 +++++++++++++++++++--------------
tools/docker/entrypoint.sh | 45 ++++++++++++++++++++++++++------------
2 files changed, 55 insertions(+), 30 deletions(-)
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 59bc752..d37cdbf 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -25,13 +25,20 @@ jobs:
matrix:
python: [3.6, 3.7, 3.8, 3.9]
db: [postgres, mysql]
+ env:
+ DATABASE_TYPE: "${{ matrix.db }}"
+ DATABASE_HOST: "127.0.0.1"
+ DATABASE_NAME: patchwork
+ DATABASE_USER: patchwork
+ DATABASE_PASSWORD: password
+ MYSQL_ROOT_PASSWORD: root
services:
postgres:
image: postgres:latest
env:
- POSTGRES_DB: patchwork
- POSTGRES_PASSWORD: patchwork
- POSTGRES_USER: patchwork
+ POSTGRES_DB: ${{ env.DATABASE_NAME }}
+ POSTGRES_PASSWORD: ${{ env.DATABASE_PASSWORD }}
+ POSTGRES_USER: ${{ env.DATABASE_USER }}
ports:
- 5432:5432
options: >-
@@ -42,10 +49,10 @@ jobs:
mysql:
image: mysql:latest
env:
- MYSQL_DATABASE: patchwork
- MYSQL_USER: patchwork
- MYSQL_PASSWORD: patchwork
- MYSQL_ROOT_PASSWORD: root
+ MYSQL_DATABASE: ${{ env.DATABASE_NAME }}
+ MYSQL_USER: ${{ env.DATABASE_USER }}
+ MYSQL_PASSWORD: ${{ env.DATABASE_PASSWORD }}
+ MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PASSWORD }}
ports:
- 3306:3306
options: >-
@@ -64,22 +71,23 @@ jobs:
run: python -m pip install tox tox-gh-actions codecov
- name: Log database configuration (mysql)
if: ${{ matrix.db == 'mysql' }}
- run: mysql -h 127.0.0.1 -e "SELECT VERSION(), CURRENT_USER();" -uroot -proot patchwork
+ run: |
+ mysql -h 127.0.0.1 -e "SELECT VERSION(), CURRENT_USER();" \
+ -uroot -p${MYSQL_ROOT_PASSWORD} ${DATABASE_NAME}
- name: Log database configuration (postgres)
if: ${{ matrix.db == 'postgres' }}
- run: psql -h 127.0.0.1 -c "SELECT VERSION(), CURRENT_USER, current_database()" -U patchwork -d patchwork
+ run: |
+ psql -h 127.0.0.1 -c "SELECT VERSION(), CURRENT_USER, current_database()" \
+ -U ${DATABASE_USER} -d ${DATABASE_NAME}
env:
- PGPASSWORD: patchwork
+ PGPASSWORD: ${{ env.DATABASE_PASSWORD }}
- name: Modify database user permissions (mysql)
if: ${{ matrix.db == 'mysql' }}
- run: mysql -h 127.0.0.1 -e "GRANT ALL ON \`test\\_patchwork%\`.* to 'patchwork'@'%';" -uroot -proot
+ run: |
+ mysql -h 127.0.0.1 -e "GRANT ALL ON \`test\\_${DATABASE_NAME}%\`.* to '${DATABASE_USER}'@'%';" \
+ -uroot -p${MYSQL_ROOT_PASSWORD}
- name: Run unit tests (via tox)
run: tox
- env:
- DATABASE_TYPE: "${{ matrix.db }}"
- DATABASE_USER: "patchwork"
- DATABASE_PASSWORD: "patchwork"
- DATABASE_HOST: "127.0.0.1"
docs:
name: Build docs
runs-on: ubuntu-latest
diff --git a/tools/docker/entrypoint.sh b/tools/docker/entrypoint.sh
index 7ae9803..faa36b3 100755
--- a/tools/docker/entrypoint.sh
+++ b/tools/docker/entrypoint.sh
@@ -1,40 +1,57 @@
#!/bin/bash
set -euo pipefail
-DATABASE_TYPE=${DATABASE_TYPE:-mysql}
+export DATABASE_HOST=${DATABASE_HOST:-}
+export DATABASE_PORT=${DATABASE_PORT:-}
+export DATABASE_NAME=${DATABASE_NAME:-patchwork}
+export DATABASE_USER=${DATABASE_USER:-patchwork}
+export DATABASE_PASSWORD=${DATABASE_PASSWORD:-password}
+
+case "${DATABASE_TYPE:-}" in
+postgres)
+ export PGPORT=${DATABASE_PORT}
+ export PGPASSWORD=${DATABASE_PASSWORD}
+ psql_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} "--username=${DATABASE_USER}" )
+ ;;
+*)
+ export DATABASE_TYPE=mysql
+ mysql_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} ${DATABASE_PORT:+--port=${DATABASE_PORT}} "--user=${DATABASE_USER}" "--password=${DATABASE_PASSWORD}" )
+ mysql_root_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} ${DATABASE_PORT:+--port=${DATABASE_PORT}} "--user=root" "--password=${MYSQL_ROOT_PASSWORD:-}" )
+ ;;
+esac
# functions
test_db_connection() {
if [ ${DATABASE_TYPE} = "postgres" ]; then
- echo ';' | psql -h $DATABASE_HOST -U postgres 2> /dev/null > /dev/null
+ echo ';' | psql "${psql_args[@]}" 2> /dev/null > /dev/null
else
- mysqladmin -h $DATABASE_HOST -u patchwork --password=password ping > /dev/null 2> /dev/null
+ mysqladmin "${mysql_root_args[@]}" ping > /dev/null 2> /dev/null
fi
}
test_database() {
if [ ${DATABASE_TYPE} = "postgres" ]; then
- echo ';' | psql -h $DATABASE_HOST -U postgres patchwork 2> /dev/null
+ echo ';' | psql "${psql_args[@]}" "${DATABASE_NAME}" 2> /dev/null
else
- echo ';' | mysql -h $DATABASE_HOST -u patchwork -ppassword patchwork 2> /dev/null
+ echo ';' | mysql "${mysql_args[@]}" "${DATABASE_NAME}" 2> /dev/null
fi
}
reset_data_mysql() {
- mysql -uroot -ppassword -h $DATABASE_HOST << EOF
-DROP DATABASE IF EXISTS patchwork;
-CREATE DATABASE patchwork CHARACTER SET utf8;
-GRANT ALL ON patchwork.* TO 'patchwork' IDENTIFIED BY 'password';
-GRANT ALL ON \`test\\_patchwork%\`.* to 'patchwork'@'%';
+ mysql "${mysql_root_args[@]}" << EOF
+DROP DATABASE IF EXISTS ${DATABASE_NAME};
+CREATE DATABASE ${DATABASE_NAME} CHARACTER SET utf8;
+GRANT ALL ON ${DATABASE_NAME}.* TO '${DATABASE_USER}' IDENTIFIED BY '${DATABASE_PASSWORD}';
+GRANT ALL ON \`test\\_${DATABASE_NAME}%\`.* to '${DATABASE_USER}'@'%';
FLUSH PRIVILEGES;
EOF
}
reset_data_postgres() {
- psql -h $DATABASE_HOST -U postgres <<EOF
-DROP DATABASE IF EXISTS patchwork;
-CREATE DATABASE patchwork WITH ENCODING = 'UTF8';
+ psql "${psql_args[@]}" <<EOF
+DROP DATABASE IF EXISTS ${DATABASE_NAME};
+CREATE DATABASE ${DATABASE_NAME} WITH ENCODING = 'UTF8';
EOF
}
@@ -87,7 +104,7 @@ set -e
# check if db is connected
if ! test_db_connection; then
- echo "The database seems not to be connected, or the patchwork user is broken"
+ echo "The database seems not to be connected, or the ${DATABASE_USER} user is broken"
echo "MySQL/Postgres may still be starting. Waiting 5 seconds."
sleep 5
if ! test_db_connection; then
--
2.32.0
More information about the Patchwork
mailing list