Skip to content

Commit

Permalink
Separate WP test DB.
Browse files Browse the repository at this point in the history
  • Loading branch information
reksar committed Apr 27, 2022
1 parent 6818278 commit 05994e5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SMTP_PORT=587
IMAP_AUTH_PORT=12345

WORDPRESS_DB_NAME=wp
WORDPRESS_TEST_DB_NAME=wptest
WEBMAIL_DB_NAME=webmail

POSTFIX_SPOOLDIR=/var/spool/postfix
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
hostname: mysql
image: mysql:8
environment:
DATABASES: $WORDPRESS_DB_NAME $WEBMAIL_DB_NAME
DATABASES: $WORDPRESS_DB_NAME $WORDPRESS_TEST_DB_NAME $WEBMAIL_DB_NAME
MYSQL_RANDOM_ROOT_PASSWORD: 1
env_file:
- .env
Expand Down
10 changes: 4 additions & 6 deletions utils/composer/wp-testlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
# We need just to install the WP testlib and config without all other shit like
# a new WP installation and new DB setup. DB creation can be skipped in the
# original script, but a new WP installation - is not.
#
# Used the same DB for dev and tests, but table prefixes are different.
# Default test prefix is `wptests_`. For example, see:
# https://develop.svn.wordpress.org/tags/5.9/wp-tests-config-sample.php

WP_TESTS_SUITE_DIR=$TESTS_SUITE_DIR/wp
WP_TESTS_INCLUDES=$WP_TESTS_SUITE_DIR/includes
WP_TESTS_DATA=$WP_TESTS_SUITE_DIR/data
WP_TESTS_CONFIG=$WP_TESTS_SUITE_DIR/wp-tests-config.php

# TODO: check the WP_VERSION of an existing testlib.
# TODO: check the WP_VERSION of an existing testlib if need to test different
# WP versions.
if [ -d $WP_TESTS_INCLUDES ] \
&& [ -d $WP_TESTS_DATA ] \
&& [ -f $WP_TESTS_CONFIG ]
Expand All @@ -42,11 +39,12 @@ svn co --quiet $WP_URL/tests/phpunit/data/ $WP_TESTS_DATA

echo Configuring WP tests suite

# See https://develop.svn.wordpress.org/tags/5.9/wp-tests-config-sample.php
curl -s $WP_URL/wp-tests-config-sample.php > $WP_TESTS_CONFIG

sed -i "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" $WP_TESTS_CONFIG

DB_NAME="getenv('WORDPRESS_DB_NAME', 'wordpress')"
DB_NAME="getenv('WORDPRESS_TEST_DB_NAME', 'wordpress')"
sed -i "s/'youremptytestdbnamehere'/$DB_NAME/" $WP_TESTS_CONFIG

DB_USER="getenv('WORDPRESS_DB_USER', 'admin')"
Expand Down
20 changes: 16 additions & 4 deletions utils/mysql/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

readonly EXTRA_FILE_TEXT="[client] password=${MYSQL_ROOT_PASSWORD}"

readonly SOCKET=`
mysqld --verbose --help \
| grep '^socket\s' \
Expand All @@ -9,20 +11,30 @@ readonly SOCKET=`

sql() {
# `--defaults-extra-file` must be the first arg.
mysql --defaults-extra-file=<(printf "%s\n" "[client]" "password=${MYSQL_ROOT_PASSWORD}") -uroot -hlocalhost --comments --protocol=socket --socket="${SOCKET}" "$@"
mysql \
--defaults-extra-file=<(printf "%s\n" $EXTRA_FILE_TEXT) \
--comments \
--protocol=socket \
--socket="${SOCKET}" \
-u root \
-h localhost \
"$@"
}

if [ -n $ADMIN_NAME ] && [ -n $ADMIN_PASSWORD ]
then
echo Creating user $ADMIN_NAME
sql --database=mysql <<<"CREATE USER '$ADMIN_NAME'@'%' IDENTIFIED BY '$ADMIN_PASSWORD';"
sql --database=mysql \
<<<"CREATE USER '$ADMIN_NAME'@'%' IDENTIFIED BY '$ADMIN_PASSWORD';"

for DB in $DATABASES
do
echo Creating database $DB
sql --database=mysql <<<"CREATE DATABASE IF NOT EXISTS \`$DB\`;"
sql --database=mysql \
<<<"CREATE DATABASE IF NOT EXISTS \`$DB\`;"

echo Grant user $ADMIN_NAME access to $DB
sql --database=mysql <<<"GRANT ALL ON \`${DB//_/\\_}\`.* TO '$ADMIN_NAME'@'%';"
sql --database=mysql \
<<<"GRANT ALL ON \`${DB//_/\\_}\`.* TO '$ADMIN_NAME'@'%';"
done
fi

0 comments on commit 05994e5

Please sign in to comment.