2019-09-16 08:47:49 -04:00
|
|
|
#!/usr/bin/env bash
|
2019-09-30 08:33:49 -04:00
|
|
|
#
|
|
|
|
# This script is used for autotesting the Friendica codebase with different
|
|
|
|
# types of tests and environments.
|
|
|
|
#
|
|
|
|
# Currently, there are three types of autotesting possibilities:
|
|
|
|
# - "USEDOCKER=true ./autotest.sh" will start a database docker container for testing
|
|
|
|
# - "./autotest.sh" on the Drone CI environment will use the database container of the drone CI pipeline
|
|
|
|
# - "./autotest.sh" on a local environment will try to use the local database instance for testing
|
|
|
|
#
|
|
|
|
# You can specify a database (mysql, mariadb currently) for the db backend of Friendica ("./autotest.sh mysql")
|
|
|
|
# And you can specify some parameters for the test, like:
|
|
|
|
# - NOCOVERAGE=true ... Don't create a coverage XML (this is only useful if you will send coverage to codecov.io)
|
|
|
|
# - NOINSTALL=true ... Skip the whole Friendica installation process (e.g. you just test Caching drivers)
|
|
|
|
# - TEST_SELECTION= ... Specify which tests are used to run (based on the test-labeling)
|
|
|
|
# - XDEBUG_CONFIG= ... Set some XDEBUG specific environment settings for development
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
DATABASE_NAME=${FRIENDICA_MYSQL_DATABASE:-test}
|
|
|
|
DATABASE_USER=${FRIENDICA_MYSQL_USERNAME:-friendica}
|
|
|
|
DATABASE_HOST=${FRIENDICA_MYSQL_HOST:-localhost}
|
|
|
|
DATABASE_PASSWORD=${FRIENDICA_MYSQL_PASSWORD:-friendica}
|
|
|
|
BASEDIR=${PWD}
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-24 10:29:19 -04:00
|
|
|
DBCONFIGS="mysql mariadb"
|
2019-09-27 06:24:48 -04:00
|
|
|
TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
|
2019-09-24 10:29:19 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
export MYSQL_DATABASE="${DATABASE_NAME}"
|
|
|
|
export MYSQL_USERNAME="${DATABASE_USER}"
|
|
|
|
export MYSQL_PASSWORD="${DATABASE_PASSWORD}"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "${PHP_EXE}" ]; then
|
2019-09-16 08:47:49 -04:00
|
|
|
PHP_EXE=php
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
PHP=$(which "${PHP_EXE}")
|
2019-09-16 08:47:49 -04:00
|
|
|
# Use the Friendica internal composer
|
2022-06-21 13:38:56 -04:00
|
|
|
COMPOSER="${BASEDIR}/bin/composer.phar"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
_XDEBUG_CONFIG=${XDEBUG_CONFIG}
|
2019-09-16 08:47:49 -04:00
|
|
|
unset XDEBUG_CONFIG
|
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
function show_syntax() {
|
|
|
|
echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
|
2022-06-21 13:38:56 -04:00
|
|
|
echo -e "\t\"dbconfigname\" can be one of: ${DBCONFIGS}" >&2
|
2019-09-27 06:24:48 -04:00
|
|
|
echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
|
|
|
|
echo -e "\nDatabase environment variables:\n" >&2
|
|
|
|
echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
|
|
|
|
echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
|
|
|
|
echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
|
|
|
|
echo -e "\nOther environment variables:\n" >&2
|
|
|
|
echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
|
|
|
|
echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
|
|
|
|
echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
|
|
|
|
echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
|
|
|
|
echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
|
|
|
|
echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
|
|
|
|
echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
|
|
|
|
}
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -x "${PHP}" ]; then
|
|
|
|
echo "Using PHP executable ${PHP}"
|
2019-09-16 08:47:49 -04:00
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "Could not find PHP executable ${PHP_EXE}" >&2
|
2019-09-16 08:47:49 -04:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Installing depdendencies"
|
2022-06-21 13:38:56 -04:00
|
|
|
${PHP} "$COMPOSER" install
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
PHPUNIT="${BASEDIR}/vendor/bin/phpunit"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -x "${PHPUNIT}" ]; then
|
|
|
|
echo "Using PHPUnit executable ${PHPUNIT}"
|
2019-09-16 08:47:49 -04:00
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "Could not find PHPUnit executable after composer ${PHPUNIT}" >&2
|
2019-09-16 08:47:49 -04:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "Please enable write permissions on config and config/config.php" >&2
|
|
|
|
exit 1
|
2019-09-16 08:47:49 -04:00
|
|
|
fi
|
|
|
|
|
2019-09-24 10:29:19 -04:00
|
|
|
if [ "$1" ]; then
|
|
|
|
FOUND=0
|
2022-06-21 13:38:56 -04:00
|
|
|
for DBCONFIG in ${DBCONFIGS}; do
|
|
|
|
if [ "$1" = "${DBCONFIG}" ]; then
|
2019-09-24 10:29:19 -04:00
|
|
|
FOUND=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ $FOUND = 0 ]; then
|
|
|
|
echo -e "Unknown database config name \"$1\"\n" >&2
|
2019-09-27 06:24:48 -04:00
|
|
|
show_syntax
|
2019-09-24 10:29:19 -04:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-09-16 08:47:49 -04:00
|
|
|
# Back up existing (dev) config if one exists and backup not already there
|
|
|
|
if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
|
|
|
|
mv config/local.config.php config/local.config-autotest-backup.php
|
|
|
|
fi
|
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
function cleanup_config() {
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -n "${DOCKER_CONTAINER_ID}" ]; then
|
|
|
|
echo "Kill the docker ${DOCKER_CONTAINER_ID}"
|
|
|
|
docker stop "${DOCKER_CONTAINER_ID}"
|
|
|
|
docker rm -f "${DOCKER_CONTAINER_ID}"
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
cd "${BASEDIR}"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
# Restore existing config
|
|
|
|
if [ -f config/local.config-autotest-backup.php ]; then
|
|
|
|
mv config/local.config-autotest-backup.php config/local.config.php
|
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# restore config on exit
|
|
|
|
trap cleanup_config EXIT
|
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
function execute_tests() {
|
|
|
|
DB=$1
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "Setup environment for ${DB} testing ..."
|
2019-09-27 06:24:48 -04:00
|
|
|
# back to root folder
|
2022-06-21 13:38:56 -04:00
|
|
|
cd "${BASEDIR}"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
# backup current config
|
|
|
|
if [ -f config/local.config.php ]; then
|
|
|
|
mv config/local.config.php config/local.config-autotest-backup.php
|
|
|
|
fi
|
2019-09-24 10:29:19 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "${NOINSTALL}" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
#drop database
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${DB}" == "mysql" ]; then
|
|
|
|
if [ -n "${USEDOCKER}" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "Fire up the mysql docker"
|
|
|
|
DOCKER_CONTAINER_ID=$(docker run \
|
|
|
|
-e MYSQL_ROOT_PASSWORD=friendica \
|
2022-06-21 13:38:56 -04:00
|
|
|
-e MYSQL_USER="${DATABASE_USER}" \
|
2019-09-27 06:24:48 -04:00
|
|
|
-e MYSQL_PASSWORD=friendica \
|
2022-06-21 13:38:56 -04:00
|
|
|
-e MYSQL_DATABASE="${DATABASE_NAME}" \
|
2019-09-27 06:24:48 -04:00
|
|
|
-d mysql)
|
2022-06-21 13:38:56 -04:00
|
|
|
DATABASE_HOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "${DOCKER_CONTAINER_ID}")
|
2019-09-27 06:24:48 -04:00
|
|
|
|
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "${DRONE}" ]; then # no need to drop the DB when we are on CI
|
2019-09-27 06:24:48 -04:00
|
|
|
if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
|
|
|
|
echo "Your mysql binary is not provided by mysql"
|
|
|
|
echo "To use the docker container set the USEDOCKER environment variable"
|
|
|
|
exit 3
|
2019-09-24 10:29:19 -04:00
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
mysql -u "${DATABASE_USER}" -pfriendica -e "DROP DATABASE IF EXISTS ${DATABASE_NAME}" -h ${DATABASE_HOST} || true
|
|
|
|
mysql -u "${DATABASE_USER}" -pfriendica -e "CREATE DATABASE ${DATABASE_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h ${DATABASE_HOST}
|
2019-09-27 06:24:48 -04:00
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
DATABASE_HOST=mysql
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
fi
|
2019-09-24 10:29:19 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "Waiting for MySQL ${DATABASE_HOST} initialization..."
|
|
|
|
if ! bin/wait-for-connection ${DATABASE_HOST} 3306 300; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "[ERROR] Waited 300 seconds, no response" >&2
|
|
|
|
exit 1
|
2019-09-23 09:23:09 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "MySQL is up."
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${DB}" == "mariadb" ]; then
|
|
|
|
if [ -n "${USEDOCKER}" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "Fire up the mariadb docker"
|
|
|
|
DOCKER_CONTAINER_ID=$(docker run \
|
|
|
|
-e MYSQL_ROOT_PASSWORD=friendica \
|
2022-06-21 13:38:56 -04:00
|
|
|
-e MYSQL_USER="${DATABASE_USER}" \
|
2019-09-27 06:24:48 -04:00
|
|
|
-e MYSQL_PASSWORD=friendica \
|
2022-06-21 13:38:56 -04:00
|
|
|
-e MYSQL_DATABASE="${DATABASE_NAME}" \
|
2019-09-27 06:24:48 -04:00
|
|
|
-d mariadb)
|
2022-06-21 13:38:56 -04:00
|
|
|
DATABASE_HOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "${DOCKER_CONTAINER_ID}")
|
2019-09-27 06:24:48 -04:00
|
|
|
|
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "${DRONE}" ]; then # no need to drop the DB when we are on CI
|
2019-09-27 06:24:48 -04:00
|
|
|
if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
|
|
|
|
echo "Your mysql binary is not provided by mysql"
|
|
|
|
echo "To use the docker container set the USEDOCKER environment variable"
|
|
|
|
exit 3
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
mysql -u "${DATABASE_USER}" -pfriendica -e "DROP DATABASE IF EXISTS ${DATABASE_NAME}" -h ${DATABASE_HOST} || true
|
|
|
|
mysql -u "${DATABASE_USER}" -pfriendica -e "CREATE DATABASE ${DATABASE_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h ${DATABASE_HOST}
|
2019-09-27 06:24:48 -04:00
|
|
|
else
|
2022-06-21 13:38:56 -04:00
|
|
|
DATABASE_HOST=mariadb
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-23 09:23:09 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "Waiting for MariaDB ${DATABASE_HOST} initialization..."
|
|
|
|
if ! bin/wait-for-connection ${DATABASE_HOST} 3306 300; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "[ERROR] Waited 300 seconds, no response" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "MariaDB is up."
|
2019-09-23 09:23:09 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -n "${USEDOCKER}" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
echo "Initialize database..."
|
2022-06-21 13:38:56 -04:00
|
|
|
docker exec ${DOCKER_CONTAINER_ID} mysql -u root -pfriendica -e "CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME};"
|
2019-09-16 08:47:49 -04:00
|
|
|
fi
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
export MYSQL_HOST="${DATABASE_HOST}"
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
#call installer
|
|
|
|
echo "Installing Friendica..."
|
2022-06-21 13:38:56 -04:00
|
|
|
"${PHP}" ./bin/console.php autoinstall --dbuser="${DATABASE_USER}" --dbpass=friendica --dbdata="${DATABASE_NAME}" --dbhost="${DATABASE_HOST}" --url=https://friendica.local --admin=admin@friendica.local
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-23 08:31:13 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
#test execution
|
|
|
|
echo "Testing..."
|
|
|
|
rm -fr "coverage-html"
|
|
|
|
mkdir "coverage-html"
|
2022-06-21 13:38:56 -04:00
|
|
|
if [[ "${_XDEBUG_CONFIG}" ]]; then
|
|
|
|
export XDEBUG_CONFIG=${_XDEBUG_CONFIG}
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
COVER=''
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "${NOCOVERAGE}" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
COVER="--coverage-clover tests/autotest-clover.xml"
|
|
|
|
else
|
|
|
|
echo "No coverage"
|
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
|
2019-09-27 06:24:48 -04:00
|
|
|
# per default, there is no cache installed
|
|
|
|
GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${TEST_SELECTION}" == "REDIS" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
GROUP="--group REDIS"
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${TEST_SELECTION}" == "MEMCACHE" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
GROUP="--group MEMCACHE"
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${TEST_SELECTION}" == "MEMCACHED" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
GROUP="--group MEMCACHED"
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${TEST_SELECTION}" == "APCU" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
GROUP="--group APCU"
|
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ "${TEST_SELECTION}" == "NODB" ]; then
|
2019-09-27 06:24:48 -04:00
|
|
|
GROUP="--exclude-group DB,SLOWDB"
|
|
|
|
fi
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
INPUT="${BASEDIR}/tests"
|
2019-09-27 06:24:48 -04:00
|
|
|
if [ -n "$2" ]; then
|
2022-06-21 13:38:56 -04:00
|
|
|
INPUT="${INPUT}/$2"
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml ${GROUP} ${COVER} --log-junit "autotest-results.xml" "${INPUT}" "$3"
|
|
|
|
"${PHPUNIT[@]}" --configuration tests/phpunit.xml ${GROUP} ${COVER} --log-junit "autotest-results.xml" "${INPUT}" "$3"
|
2019-09-27 06:24:48 -04:00
|
|
|
RESULT=$?
|
|
|
|
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -n "${DOCKER_CONTAINER_ID}" ]; then
|
|
|
|
echo "Kill the docker ${DOCKER_CONTAINER_ID}"
|
|
|
|
docker stop ${DOCKER_CONTAINER_ID}
|
|
|
|
docker rm -f ${DOCKER_CONTAINER_ID}
|
|
|
|
unset ${DOCKER_CONTAINER_ID}
|
2019-09-27 06:24:48 -04:00
|
|
|
fi
|
2019-09-16 08:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Start the test execution
|
|
|
|
#
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -z "$1" ] && [ -n "${TEST_SELECTION}" ]; then
|
2019-09-24 10:29:19 -04:00
|
|
|
# run all known database configs
|
2022-06-21 13:38:56 -04:00
|
|
|
for DBCONFIG in ${DBCONFIGS}; do
|
|
|
|
execute_tests "${DBCONFIG}"
|
2019-09-24 10:29:19 -04:00
|
|
|
done
|
2019-09-16 08:47:49 -04:00
|
|
|
else
|
2019-09-24 10:29:19 -04:00
|
|
|
FILENAME="$2"
|
2022-06-21 13:38:56 -04:00
|
|
|
if [ -n "$2" ] && [ ! -f "tests/${FILENAME}" ] && [ "${FILENAME:0:2}" != "--" ]; then
|
|
|
|
FILENAME="../${FILENAME}"
|
2019-09-24 10:29:19 -04:00
|
|
|
fi
|
2022-06-21 13:38:56 -04:00
|
|
|
execute_tests "$1" "${FILENAME}" "$3"
|
2019-09-16 08:47:49 -04:00
|
|
|
fi
|