3 # This script is used for autotesting the Friendica codebase with different
4 # types of tests and environments.
6 # Currently, there are three types of autotesting possibilities:
7 # - "USEDOCKER=true ./autotest.sh" will start a database docker container for testing
8 # - "./autotest.sh" on the Drone CI environment will use the database container of the drone CI pipeline
9 # - "./autotest.sh" on a local environment will try to use the local database instance for testing
11 # You can specify a database (mysql, mariadb currently) for the db backend of Friendica ("./autotest.sh mysql")
12 # And you can specify some parameters for the test, like:
13 # - NOCOVERAGE=true ... Don't create a coverage XML (this is only useful if you will send coverage to codecov.io)
14 # - NOINSTALL=true ... Skip the whole Friendica installation process (e.g. you just test Caching drivers)
15 # - TEST_SELECTION= ... Specify which tests are used to run (based on the test-labeling)
16 # - XDEBUG_CONFIG= ... Set some XDEBUG specific environment settings for development
18 DATABASE_NAME=${FRIENDICA_MYSQL_DATABASE:-test}
19 DATABASE_USER=${FRIENDICA_MYSQL_USERNAME:-friendica}
20 DATABASE_HOST=${FRIENDICA_MYSQL_HOST:-localhost}
21 DATABASE_PASSWORD=${FRIENDICA_MYSQL_PASSWORD:-friendica}
24 DBCONFIGS="mysql mariadb"
25 TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
27 export MYSQL_DATABASE="${DATABASE_NAME}"
28 export MYSQL_USERNAME="${DATABASE_USER}"
29 export MYSQL_PASSWORD="${DATABASE_PASSWORD}"
31 if [ -z "${PHP_EXE}" ]; then
34 PHP=$(which "${PHP_EXE}")
35 # Use the Friendica internal composer
36 COMPOSER="${BASEDIR}/bin/composer.phar"
40 _XDEBUG_CONFIG=${XDEBUG_CONFIG}
43 function show_syntax() {
44 echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
45 echo -e "\t\"dbconfigname\" can be one of: ${DBCONFIGS}" >&2
46 echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
47 echo -e "\nDatabase environment variables:\n" >&2
48 echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
49 echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
50 echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
51 echo -e "\nOther environment variables:\n" >&2
52 echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
53 echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
54 echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
55 echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
56 echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
57 echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
58 echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
61 if [ -x "${PHP}" ]; then
62 echo "Using PHP executable ${PHP}"
64 echo "Could not find PHP executable ${PHP_EXE}" >&2
68 echo "Installing depdendencies"
69 ${PHP} "$COMPOSER" install
71 PHPUNIT="${BASEDIR}/vendor/bin/phpunit"
73 if [ -x "${PHPUNIT}" ]; then
74 echo "Using PHPUnit executable ${PHPUNIT}"
76 echo "Could not find PHPUnit executable after composer ${PHPUNIT}" >&2
80 if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
81 echo "Please enable write permissions on config and config/config.php" >&2
87 for DBCONFIG in ${DBCONFIGS}; do
88 if [ "$1" = "${DBCONFIG}" ]; then
93 if [ $FOUND = 0 ]; then
94 echo -e "Unknown database config name \"$1\"\n" >&2
100 # Back up existing (dev) config if one exists and backup not already there
101 if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
102 mv config/local.config.php config/local.config-autotest-backup.php
105 function cleanup_config() {
107 if [ -n "${DOCKER_CONTAINER_ID}" ]; then
108 echo "Kill the docker ${DOCKER_CONTAINER_ID}"
109 docker stop "${DOCKER_CONTAINER_ID}"
110 docker rm -f "${DOCKER_CONTAINER_ID}"
115 # Restore existing config
116 if [ -f config/local.config-autotest-backup.php ]; then
117 mv config/local.config-autotest-backup.php config/local.config.php
121 # restore config on exit
122 trap cleanup_config EXIT
124 function execute_tests() {
126 echo "Setup environment for ${DB} testing ..."
127 # back to root folder
130 # backup current config
131 if [ -f config/local.config.php ]; then
132 mv config/local.config.php config/local.config-autotest-backup.php
135 if [ -z "${NOINSTALL}" ]; then
137 if [ "${DB}" == "mysql" ]; then
138 if [ -n "${USEDOCKER}" ]; then
139 echo "Fire up the mysql docker"
140 DOCKER_CONTAINER_ID=$(docker run \
141 -e MYSQL_ROOT_PASSWORD=friendica \
142 -e MYSQL_USER="${DATABASE_USER}" \
143 -e MYSQL_PASSWORD=friendica \
144 -e MYSQL_DATABASE="${DATABASE_NAME}" \
146 DATABASE_HOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "${DOCKER_CONTAINER_ID}")
149 if [ -z "${DRONE}" ]; then # no need to drop the DB when we are on CI
150 if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
151 echo "Your mysql binary is not provided by mysql"
152 echo "To use the docker container set the USEDOCKER environment variable"
155 mysql -u "${DATABASE_USER}" -pfriendica -e "DROP DATABASE IF EXISTS ${DATABASE_NAME}" -h ${DATABASE_HOST} || true
156 mysql -u "${DATABASE_USER}" -pfriendica -e "CREATE DATABASE ${DATABASE_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h ${DATABASE_HOST}
162 echo "Waiting for MySQL ${DATABASE_HOST} initialization..."
163 if ! bin/wait-for-connection ${DATABASE_HOST} 3306 300; then
164 echo "[ERROR] Waited 300 seconds, no response" >&2
170 if [ "${DB}" == "mariadb" ]; then
171 if [ -n "${USEDOCKER}" ]; then
172 echo "Fire up the mariadb docker"
173 DOCKER_CONTAINER_ID=$(docker run \
174 -e MYSQL_ROOT_PASSWORD=friendica \
175 -e MYSQL_USER="${DATABASE_USER}" \
176 -e MYSQL_PASSWORD=friendica \
177 -e MYSQL_DATABASE="${DATABASE_NAME}" \
179 DATABASE_HOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "${DOCKER_CONTAINER_ID}")
182 if [ -z "${DRONE}" ]; then # no need to drop the DB when we are on CI
183 if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
184 echo "Your mysql binary is not provided by mysql"
185 echo "To use the docker container set the USEDOCKER environment variable"
188 mysql -u "${DATABASE_USER}" -pfriendica -e "DROP DATABASE IF EXISTS ${DATABASE_NAME}" -h ${DATABASE_HOST} || true
189 mysql -u "${DATABASE_USER}" -pfriendica -e "CREATE DATABASE ${DATABASE_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h ${DATABASE_HOST}
191 DATABASE_HOST=mariadb
195 echo "Waiting for MariaDB ${DATABASE_HOST} initialization..."
196 if ! bin/wait-for-connection ${DATABASE_HOST} 3306 300; then
197 echo "[ERROR] Waited 300 seconds, no response" >&2
201 echo "MariaDB is up."
204 if [ -n "${USEDOCKER}" ]; then
205 echo "Initialize database..."
206 docker exec ${DOCKER_CONTAINER_ID} mysql -u root -pfriendica -e "CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME};"
209 export MYSQL_HOST="${DATABASE_HOST}"
212 echo "Installing Friendica..."
213 "${PHP}" ./bin/console.php autoinstall --dbuser="${DATABASE_USER}" --dbpass=friendica --dbdata="${DATABASE_NAME}" --dbhost="${DATABASE_HOST}" --url=https://friendica.local --admin=admin@friendica.local
218 rm -fr "coverage-html"
219 mkdir "coverage-html"
220 if [[ "${_XDEBUG_CONFIG}" ]]; then
221 export XDEBUG_CONFIG=${_XDEBUG_CONFIG}
225 if [ -z "${NOCOVERAGE}" ]; then
226 COVER="--coverage-clover tests/autotest-clover.xml"
231 # per default, there is no cache installed
232 GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
233 if [ "${TEST_SELECTION}" == "REDIS" ]; then
234 GROUP="--group REDIS"
236 if [ "${TEST_SELECTION}" == "MEMCACHE" ]; then
237 GROUP="--group MEMCACHE"
239 if [ "${TEST_SELECTION}" == "MEMCACHED" ]; then
240 GROUP="--group MEMCACHED"
242 if [ "${TEST_SELECTION}" == "APCU" ]; then
245 if [ "${TEST_SELECTION}" == "NODB" ]; then
246 GROUP="--exclude-group DB,SLOWDB"
249 INPUT="${BASEDIR}/tests"
254 echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml ${GROUP} ${COVER} --log-junit "autotest-results.xml" "${INPUT}" "$3"
255 "${PHPUNIT[@]}" --configuration tests/phpunit.xml ${GROUP} ${COVER} --log-junit "autotest-results.xml" "${INPUT}" "$3"
258 if [ -n "${DOCKER_CONTAINER_ID}" ]; then
259 echo "Kill the docker ${DOCKER_CONTAINER_ID}"
260 docker stop ${DOCKER_CONTAINER_ID}
261 docker rm -f ${DOCKER_CONTAINER_ID}
262 unset ${DOCKER_CONTAINER_ID}
267 # Start the test execution
269 if [ -z "$1" ] && [ -n "${TEST_SELECTION}" ]; then
270 # run all known database configs
271 for DBCONFIG in ${DBCONFIGS}; do
272 execute_tests "${DBCONFIG}"
276 if [ -n "$2" ] && [ ! -f "tests/${FILENAME}" ] && [ "${FILENAME:0:2}" != "--" ]; then
277 FILENAME="../${FILENAME}"
279 execute_tests "$1" "${FILENAME}" "$3"