Add syntax explanation
[friendica.git/.git] / autotest.sh
1 #!/usr/bin/env bash
2
3 DATABASENAME=${MYSQL_DATABASE:-test}
4 DATABASEUSER=${MYSQL_USERNAME:-friendica}
5 DATABASEHOST=${MYSQL_HOST:-localhost}
6 BASEDIR=$PWD
7
8 DBCONFIGS="mysql mariadb"
9 TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
10
11 export MYSQL_DATABASE="$DATABASENAME"
12 export MYSQL_USERNAME="$DATABASEUSER"
13 export MYSQL_PASSWORD="friendica"
14
15 if [ -z "$PHP_EXE" ]; then
16   PHP_EXE=php
17 fi
18 PHP=$(which "$PHP_EXE")
19 # Use the Friendica internal composer
20 COMPOSER="$BASEDIR/bin/composer.phar"
21
22 set -e
23
24 _XDEBUG_CONFIG=$XDEBUG_CONFIG
25 unset XDEBUG_CONFIG
26
27 function show_syntax() {
28   echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
29   echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
30   echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
31   echo -e "\nDatabase environment variables:\n" >&2
32   echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
33   echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
34   echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
35   echo -e "\nOther environment variables:\n" >&2
36   echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
37   echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
38   echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
39   echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
40   echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
41   echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
42   echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
43 }
44
45 if [ -x "$PHP" ]; then
46   echo "Using PHP executable $PHP"
47 else
48   echo "Could not find PHP executable $PHP_EXE" >&2
49   exit 3
50 fi
51
52 echo "Installing depdendencies"
53 $PHP "$COMPOSER" install
54
55 PHPUNIT="$BASEDIR/vendor/bin/phpunit"
56
57 if [ -x "$PHPUNIT" ]; then
58   echo "Using PHPUnit executable $PHPUNIT"
59 else
60   echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
61   exit 3
62 fi
63
64 if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
65   echo "Please enable write permissions on config and config/config.php" >&2
66   exit 1
67 fi
68
69 if [ "$1" ]; then
70   FOUND=0
71   for DBCONFIG in $DBCONFIGS; do
72     if [ "$1" = "$DBCONFIG" ]; then
73       FOUND=1
74       break
75     fi
76   done
77   if [ $FOUND = 0 ]; then
78     echo -e "Unknown database config name \"$1\"\n" >&2
79     show_syntax
80     exit 2
81   fi
82 fi
83
84 # Back up existing (dev) config if one exists and backup not already there
85 if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
86   mv config/local.config.php config/local.config-autotest-backup.php
87 fi
88
89 function cleanup_config() {
90
91   if [ -n "$DOCKER_CONTAINER_ID" ]; then
92     echo "Kill the docker $DOCKER_CONTAINER_ID"
93     docker stop "$DOCKER_CONTAINER_ID"
94     docker rm -f "$DOCKER_CONTAINER_ID"
95   fi
96
97   cd "$BASEDIR"
98
99   # Restore existing config
100   if [ -f config/local.config-autotest-backup.php ]; then
101     mv config/local.config-autotest-backup.php config/local.config.php
102   fi
103 }
104
105 # restore config on exit
106 trap cleanup_config EXIT
107
108 function execute_tests() {
109   DB=$1
110   echo "Setup environment for $DB testing ..."
111   # back to root folder
112   cd "$BASEDIR"
113
114   # backup current config
115   if [ -f config/local.config.php ]; then
116     mv config/local.config.php config/local.config-autotest-backup.php
117   fi
118
119   if [ -z "$NOINSTALL" ]; then
120     #drop database
121     if [ "$DB" == "mysql" ]; then
122       if [ -n "$USEDOCKER" ]; then
123         echo "Fire up the mysql docker"
124         DOCKER_CONTAINER_ID=$(docker run \
125           -e MYSQL_ROOT_PASSWORD=friendica \
126           -e MYSQL_USER="$DATABASEUSER" \
127           -e MYSQL_PASSWORD=friendica \
128           -e MYSQL_DATABASE="$DATABASENAME" \
129           -d mysql)
130         DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
131
132       else
133         if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
134           if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
135             echo "Your mysql binary is not provided by mysql"
136             echo "To use the docker container set the USEDOCKER environment variable"
137             exit 3
138           fi
139           mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
140           mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
141         else
142           DATABASEHOST=mysql
143         fi
144       fi
145
146       echo "Waiting for MySQL $DATABASEHOST initialization..."
147       if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
148         echo "[ERROR] Waited 300 seconds, no response" >&2
149         exit 1
150       fi
151
152       echo "MySQL is up."
153     fi
154     if [ "$DB" == "mariadb" ]; then
155       if [ -n "$USEDOCKER" ]; then
156         echo "Fire up the mariadb docker"
157         DOCKER_CONTAINER_ID=$(docker run \
158           -e MYSQL_ROOT_PASSWORD=friendica \
159           -e MYSQL_USER="$DATABASEUSER" \
160           -e MYSQL_PASSWORD=friendica \
161           -e MYSQL_DATABASE="$DATABASENAME" \
162           -d mariadb)
163         DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
164
165       else
166         if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
167           if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
168             echo "Your mysql binary is not provided by mysql"
169             echo "To use the docker container set the USEDOCKER environment variable"
170             exit 3
171           fi
172           mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
173           mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
174         else
175           DATABASEHOST=mariadb
176         fi
177       fi
178
179       echo "Waiting for MariaDB $DATABASEHOST initialization..."
180       if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
181         echo "[ERROR] Waited 300 seconds, no response" >&2
182         exit 1
183       fi
184
185       echo "MariaDB is up."
186     fi
187
188     if [ -n "$USEDOCKER" ]; then
189       echo "Initialize database..."
190       docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
191     fi
192
193     export MYSQL_HOST="$DATABASEHOST"
194
195     #call installer
196     echo "Installing Friendica..."
197     "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
198   fi
199
200   #test execution
201   echo "Testing..."
202   rm -fr "coverage-html"
203   mkdir "coverage-html"
204   if [[ "$_XDEBUG_CONFIG" ]]; then
205     export XDEBUG_CONFIG=$_XDEBUG_CONFIG
206   fi
207
208   COVER=''
209   if [ -z "$NOCOVERAGE" ]; then
210     COVER="--coverage-clover tests/autotest-clover.xml"
211   else
212     echo "No coverage"
213   fi
214
215   # per default, there is no cache installed
216   GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
217   if [ "$TEST_SELECTION" == "REDIS" ]; then
218     GROUP="--group REDIS"
219   fi
220   if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
221     GROUP="--group MEMCACHE"
222   fi
223   if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
224     GROUP="--group MEMCACHED"
225   fi
226   if [ "$TEST_SELECTION" == "APCU" ]; then
227     GROUP="--group APCU"
228   fi
229   if [ "$TEST_SELECTION" == "NODB" ]; then
230     GROUP="--exclude-group DB,SLOWDB"
231   fi
232
233   INPUT="$BASEDIR/tests"
234   if [ -n "$2" ]; then
235     INPUT="$INPUT/$2"
236   fi
237
238   echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
239   "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
240   RESULT=$?
241
242   if [ -n "$DOCKER_CONTAINER_ID" ]; then
243     echo "Kill the docker $DOCKER_CONTAINER_ID"
244     docker stop $DOCKER_CONTAINER_ID
245     docker rm -f $DOCKER_CONTAINER_ID
246     unset $DOCKER_CONTAINER_ID
247   fi
248 }
249
250 #
251 # Start the test execution
252 #
253 if [ -z "$1" ] && [ -n "$TEST_SELECTION" ]; then
254   # run all known database configs
255   for DBCONFIG in $DBCONFIGS; do
256     execute_tests "$DBCONFIG"
257   done
258 else
259   FILENAME="$2"
260   if [ -n "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
261     FILENAME="../$FILENAME"
262   fi
263   execute_tests "$1" "$FILENAME" "$3"
264 fi