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 DATABASENAME=${MYSQL_DATABASE:-test}
19 DATABASEUSER=${MYSQL_USERNAME:-friendica}
20 DATABASEHOST=${MYSQL_HOST:-localhost}
23 DBCONFIGS="mysql mariadb"
24 TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
26 export MYSQL_DATABASE="$DATABASENAME"
27 export MYSQL_USERNAME="$DATABASEUSER"
28 export MYSQL_PASSWORD="friendica"
30 if [ -z "$PHP_EXE" ]; then
33 PHP=$(which "$PHP_EXE")
34 # Use the Friendica internal composer
35 COMPOSER="$BASEDIR/bin/composer.phar"
39 _XDEBUG_CONFIG=$XDEBUG_CONFIG
42 function show_syntax() {
43 echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
44 echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
45 echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
46 echo -e "\nDatabase environment variables:\n" >&2
47 echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
48 echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
49 echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
50 echo -e "\nOther environment variables:\n" >&2
51 echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
52 echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
53 echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
54 echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
55 echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
56 echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
57 echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
60 if [ -x "$PHP" ]; then
61 echo "Using PHP executable $PHP"
63 echo "Could not find PHP executable $PHP_EXE" >&2
67 echo "Installing depdendencies"
68 $PHP "$COMPOSER" install
70 PHPUNIT="$BASEDIR/vendor/bin/phpunit"
72 if [ -x "$PHPUNIT" ]; then
73 echo "Using PHPUnit executable $PHPUNIT"
75 echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
79 if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
80 echo "Please enable write permissions on config and config/config.php" >&2
86 for DBCONFIG in $DBCONFIGS; do
87 if [ "$1" = "$DBCONFIG" ]; then
92 if [ $FOUND = 0 ]; then
93 echo -e "Unknown database config name \"$1\"\n" >&2
99 # Back up existing (dev) config if one exists and backup not already there
100 if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
101 mv config/local.config.php config/local.config-autotest-backup.php
104 function cleanup_config() {
106 if [ -n "$DOCKER_CONTAINER_ID" ]; then
107 echo "Kill the docker $DOCKER_CONTAINER_ID"
108 docker stop "$DOCKER_CONTAINER_ID"
109 docker rm -f "$DOCKER_CONTAINER_ID"
114 # Restore existing config
115 if [ -f config/local.config-autotest-backup.php ]; then
116 mv config/local.config-autotest-backup.php config/local.config.php
120 # restore config on exit
121 trap cleanup_config EXIT
123 function execute_tests() {
125 echo "Setup environment for $DB testing ..."
126 # back to root folder
129 # backup current config
130 if [ -f config/local.config.php ]; then
131 mv config/local.config.php config/local.config-autotest-backup.php
134 if [ -z "$NOINSTALL" ]; then
136 if [ "$DB" == "mysql" ]; then
137 if [ -n "$USEDOCKER" ]; then
138 echo "Fire up the mysql docker"
139 DOCKER_CONTAINER_ID=$(docker run \
140 -e MYSQL_ROOT_PASSWORD=friendica \
141 -e MYSQL_USER="$DATABASEUSER" \
142 -e MYSQL_PASSWORD=friendica \
143 -e MYSQL_DATABASE="$DATABASENAME" \
145 DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
148 if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
149 if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
150 echo "Your mysql binary is not provided by mysql"
151 echo "To use the docker container set the USEDOCKER environment variable"
154 mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
155 mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
161 echo "Waiting for MySQL $DATABASEHOST initialization..."
162 if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
163 echo "[ERROR] Waited 300 seconds, no response" >&2
169 if [ "$DB" == "mariadb" ]; then
170 if [ -n "$USEDOCKER" ]; then
171 echo "Fire up the mariadb docker"
172 DOCKER_CONTAINER_ID=$(docker run \
173 -e MYSQL_ROOT_PASSWORD=friendica \
174 -e MYSQL_USER="$DATABASEUSER" \
175 -e MYSQL_PASSWORD=friendica \
176 -e MYSQL_DATABASE="$DATABASENAME" \
178 DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
181 if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
182 if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
183 echo "Your mysql binary is not provided by mysql"
184 echo "To use the docker container set the USEDOCKER environment variable"
187 mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
188 mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
194 echo "Waiting for MariaDB $DATABASEHOST initialization..."
195 if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
196 echo "[ERROR] Waited 300 seconds, no response" >&2
200 echo "MariaDB is up."
203 if [ -n "$USEDOCKER" ]; then
204 echo "Initialize database..."
205 docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
208 export MYSQL_HOST="$DATABASEHOST"
211 echo "Installing Friendica..."
212 "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
217 rm -fr "coverage-html"
218 mkdir "coverage-html"
219 if [[ "$_XDEBUG_CONFIG" ]]; then
220 export XDEBUG_CONFIG=$_XDEBUG_CONFIG
224 if [ -z "$NOCOVERAGE" ]; then
225 COVER="--coverage-clover tests/autotest-clover.xml"
230 # per default, there is no cache installed
231 GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
232 if [ "$TEST_SELECTION" == "REDIS" ]; then
233 GROUP="--group REDIS"
235 if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
236 GROUP="--group MEMCACHE"
238 if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
239 GROUP="--group MEMCACHED"
241 if [ "$TEST_SELECTION" == "APCU" ]; then
244 if [ "$TEST_SELECTION" == "NODB" ]; then
245 GROUP="--exclude-group DB,SLOWDB"
248 INPUT="$BASEDIR/tests"
253 echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
254 "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
257 if [ -n "$DOCKER_CONTAINER_ID" ]; then
258 echo "Kill the docker $DOCKER_CONTAINER_ID"
259 docker stop $DOCKER_CONTAINER_ID
260 docker rm -f $DOCKER_CONTAINER_ID
261 unset $DOCKER_CONTAINER_ID
266 # Start the test execution
268 if [ -z "$1" ] && [ -n "$TEST_SELECTION" ]; then
269 # run all known database configs
270 for DBCONFIG in $DBCONFIGS; do
271 execute_tests "$DBCONFIG"
275 if [ -n "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
276 FILENAME="../$FILENAME"
278 execute_tests "$1" "$FILENAME" "$3"