From 39448a08717f20858e6efc80af4dd833645d151d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 14 Mar 2024 12:42:03 +0000 Subject: Fix test db setup on MySQL/Mariadb + changed default --- tests/create_test_db.sh | 113 ++++++++++++++++++++++++++++++++++++++++++ tests/create_test_db_pgsql.sh | 63 ----------------------- tests/phpunit.xml | 6 +-- tests/unit/UnitTestCase.php | 4 +- 4 files changed, 118 insertions(+), 68 deletions(-) create mode 100755 tests/create_test_db.sh delete mode 100755 tests/create_test_db_pgsql.sh (limited to 'tests') diff --git a/tests/create_test_db.sh b/tests/create_test_db.sh new file mode 100755 index 000000000..b98f5e2a5 --- /dev/null +++ b/tests/create_test_db.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +# +# Copyright (c) 2016 Hubzilla +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# + +# Exit if anything fails +set -e + +# +# Initialize some defaults if they're not set by the environment +# +: ${HZ_TEST_DB_TYPE:=postgres} + +case $HZ_TEST_DB_TYPE in + postgres | pg | pgsql ) + db_type="postgres" + default_charset="UTF8" + root_user="postgres" + root_passwd="" + ;; + + mariadb | mysql ) + db_type="mysql" + default_charset="utf8mb4" + root_user="root" + root_passwd="root" + ;; + + * ) + echo "Unknown database type: '${HZ_TEST_DB_TYPE}'" + exit + ;; +esac + +: ${HZ_TEST_DB_ROOT_USER:=$root_user} +: ${HZ_TEST_DB_ROOT_PASS:=$root_passwd} +: ${HZ_TEST_DB_USER:=test_user} +: ${HZ_TEST_DB_PASS:=hubzilla} +: ${HZ_TEST_DB_NAME:=hubzilla_test_db} +: ${HZ_TEST_DB_CHARSET:=$default_charset} + +echo "Creating Hubzilla test db..." + +if [[ $db_type == "postgres" ]] +then + psql --version + psql -U $HZ_TEST_DB_ROOT_USER -c "SELECT VERSION();" + + psql -U $HZ_TEST_DB_ROOT_USER -v ON_ERROR_STOP=1 <<-EOSQL + DROP DATABASE IF EXISTS $HZ_TEST_DB_NAME; + DROP USER IF EXISTS $HZ_TEST_DB_USER; + + CREATE USER $HZ_TEST_DB_USER WITH PASSWORD '$HZ_TEST_DB_PASS'; + CREATE DATABASE $HZ_TEST_DB_NAME + WITH + OWNER $HZ_TEST_DB_USER + ENCODING $HZ_TEST_DB_CHARSET; + + EOSQL + + export PGPASSWORD=$HZ_TEST_DB_PASS + + # Import table structure + echo "Importing schema..." + psql -U $HZ_TEST_DB_USER -v ON_ERROR_STOP=1 $HZ_TEST_DB_NAME < ./install/schema_postgres.sql + + # Show databases and tables + psql -U $HZ_TEST_DB_USER -l + psql -U $HZ_TEST_DB_USER -d $HZ_TEST_DB_NAME -c "\dt;" +else + echo -e "\n--------------" + echo "Client version:" + echo -e "--------------\n" + mysql --version + + mysql -v -u $HZ_TEST_DB_ROOT_USER -p$HZ_TEST_DB_ROOT_PASS -Ns -e "SELECT VERSION();" + + mysql -u $HZ_TEST_DB_ROOT_USER -p$HZ_TEST_DB_ROOT_PASS <<-EOSQL + DROP DATABASE IF EXISTS $HZ_TEST_DB_NAME; + CREATE DATABASE $HZ_TEST_DB_NAME CHARACTER SET $HZ_TEST_DB_CHARSET; + + DROP USER IF EXISTS $HZ_TEST_DB_USER; + CREATE USER $HZ_TEST_DB_USER IDENTIFIED BY '$HZ_TEST_DB_PASS'; + + GRANT ALL ON ${HZ_TEST_DB_NAME}.* TO $HZ_TEST_DB_USER; + EOSQL + + echo -e "\n--------------" + echo "Importing schema..." + echo -e "--------------\n" + mysql -u $HZ_TEST_DB_USER -p$HZ_TEST_DB_PASS $HZ_TEST_DB_NAME < ./install/schema_mysql.sql + mysql -v -u $HZ_TEST_DB_ROOT_USER -p$HZ_TEST_DB_ROOT_PASS -Ns -e "show databases" + mysql -v -u $HZ_TEST_DB_USER -p$HZ_TEST_DB_PASS $HZ_TEST_DB_NAME -Ns -e "show tables" +fi diff --git a/tests/create_test_db_pgsql.sh b/tests/create_test_db_pgsql.sh deleted file mode 100755 index 8ec2bc371..000000000 --- a/tests/create_test_db_pgsql.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env bash - -# -# Copyright (c) 2016 Hubzilla -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# - -# Exit if anything fails -set -e - -# -# Initialize some defaults if they're not set by the environment -# -: ${DB_ROOT_USER:=postgres} -: ${DB_TEST_USER:=test_user} -: ${DB_TEST_DB:=hubzilla_test_db} - -echo "Creating test db for PostgreSQL..." - -if [[ "$POSTGRESQL_VERSION" == "10" ]]; then - echo "Using PostgreSQL in Docker container, need to use TCP" - export PROTO="-h localhost" -fi - -# Print out some PostgreSQL information -psql --version -# Why does this hang further execution of the job? -psql $PROTO -U $DB_ROOT_USER -c "SELECT VERSION();" - -# Create Hubzilla database -psql $PROTO -U $DB_ROOT_USER -v ON_ERROR_STOP=1 <<-EOSQL - DROP DATABASE IF EXISTS $DB_TEST_DB; - DROP USER IF EXISTS $DB_TEST_USER; - CREATE USER $DB_TEST_USER WITH PASSWORD 'hubzilla'; - CREATE DATABASE $DB_TEST_DB WITH OWNER $DB_TEST_USER; -EOSQL - -export PGPASSWORD=hubzilla - -# Import table structure -echo "Importing schema..." -psql $PROTO -U $DB_TEST_USER -v ON_ERROR_STOP=1 $DB_TEST_DB < ./install/schema_postgres.sql - -# Show databases and tables -psql $PROTO -U $DB_TEST_USER -l -psql $PROTO -U $DB_TEST_USER -d $DB_TEST_DB -c "\dt;" diff --git a/tests/phpunit.xml b/tests/phpunit.xml index a8ff7b395..a92dd530f 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -7,10 +7,10 @@ > - .. - + .. + - + diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php index 18467d91e..a4ea94b13 100644 --- a/tests/unit/UnitTestCase.php +++ b/tests/unit/UnitTestCase.php @@ -77,14 +77,14 @@ class UnitTestCase extends TestCase { protected function connect_to_test_db() : void { if ( !\DBA::$dba ) { \DBA::dba_factory( - getenv('HZ_TEST_DB_HOST') ?: 'db', + getenv('HZ_TEST_DB_HOST') ?: 'localhost', // Use default port for db type if none specified getenv('HZ_TEST_DB_PORT'), getenv('HZ_TEST_DB_USER') ?: 'test_user', getenv('HZ_TEST_DB_PASS') ?: 'hubzilla', getenv('HZ_TEST_DB_DATABASE') ?: 'hubzilla_test_db', - Self::dbtype(getenv('HZ_TEST_DB_TYPE')), + self::dbtype(getenv('HZ_TEST_DB_TYPE')), getenv('HZ_TEST_DB_CHARSET') ?: 'UTF8', false); -- cgit v1.2.3