]> git.mxchange.org Git - friendica.git/blob - autotest.sh
Merge pull request #7988 from friendica/MrPetovan-notice
[friendica.git] / autotest.sh
1 #!/usr/bin/env bash
2 #
3 # This script is used for autotesting the Friendica codebase with different
4 # types of tests and environments.
5 #
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
10 #
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
17
18 DATABASENAME=${MYSQL_DATABASE:-test}
19 DATABASEUSER=${MYSQL_USERNAME:-friendica}
20 DATABASEHOST=${MYSQL_HOST:-localhost}
21 BASEDIR=$PWD
22
23 DBCONFIGS="mysql mariadb"
24 TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
25
26 export MYSQL_DATABASE="$DATABASENAME"
27 export MYSQL_USERNAME="$DATABASEUSER"
28 export MYSQL_PASSWORD="friendica"
29
30 if [ -z "$PHP_EXE" ]; then
31   PHP_EXE=php
32 fi
33 PHP=$(which "$PHP_EXE")
34 # Use the Friendica internal composer
35 COMPOSER="$BASEDIR/bin/composer.phar"
36
37 set -e
38
39 _XDEBUG_CONFIG=$XDEBUG_CONFIG
40 unset XDEBUG_CONFIG
41
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
58 }
59
60 if [ -x "$PHP" ]; then
61   echo "Using PHP executable $PHP"
62 else
63   echo "Could not find PHP executable $PHP_EXE" >&2
64   exit 3
65 fi
66
67 echo "Installing depdendencies"
68 $PHP "$COMPOSER" install
69
70 PHPUNIT="$BASEDIR/vendor/bin/phpunit"
71
72 if [ -x "$PHPUNIT" ]; then
73   echo "Using PHPUnit executable $PHPUNIT"
74 else
75   echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
76   exit 3
77 fi
78
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
81   exit 1
82 fi
83
84 if [ "$1" ]; then
85   FOUND=0
86   for DBCONFIG in $DBCONFIGS; do
87     if [ "$1" = "$DBCONFIG" ]; then
88       FOUND=1
89       break
90     fi
91   done
92   if [ $FOUND = 0 ]; then
93     echo -e "Unknown database config name \"$1\"\n" >&2
94     show_syntax
95     exit 2
96   fi
97 fi
98
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
102 fi
103
104 function cleanup_config() {
105
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"
110   fi
111
112   cd "$BASEDIR"
113
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
117   fi
118 }
119
120 # restore config on exit
121 trap cleanup_config EXIT
122
123 function execute_tests() {
124   DB=$1
125   echo "Setup environment for $DB testing ..."
126   # back to root folder
127   cd "$BASEDIR"
128
129   # backup current config
130   if [ -f config/local.config.php ]; then
131     mv config/local.config.php config/local.config-autotest-backup.php
132   fi
133
134   if [ -z "$NOINSTALL" ]; then
135     #drop database
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" \
144           -d mysql)
145         DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
146
147       else
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"
152             exit 3
153           fi
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
156         else
157           DATABASEHOST=mysql
158         fi
159       fi
160
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
164         exit 1
165       fi
166
167       echo "MySQL is up."
168     fi
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" \
177           -d mariadb)
178         DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
179
180       else
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"
185             exit 3
186           fi
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
189         else
190           DATABASEHOST=mariadb
191         fi
192       fi
193
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
197         exit 1
198       fi
199
200       echo "MariaDB is up."
201     fi
202
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;'
206     fi
207
208     export MYSQL_HOST="$DATABASEHOST"
209
210     #call installer
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
213   fi
214
215   #test execution
216   echo "Testing..."
217   rm -fr "coverage-html"
218   mkdir "coverage-html"
219   if [[ "$_XDEBUG_CONFIG" ]]; then
220     export XDEBUG_CONFIG=$_XDEBUG_CONFIG
221   fi
222
223   COVER=''
224   if [ -z "$NOCOVERAGE" ]; then
225     COVER="--coverage-clover tests/autotest-clover.xml"
226   else
227     echo "No coverage"
228   fi
229
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"
234   fi
235   if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
236     GROUP="--group MEMCACHE"
237   fi
238   if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
239     GROUP="--group MEMCACHED"
240   fi
241   if [ "$TEST_SELECTION" == "APCU" ]; then
242     GROUP="--group APCU"
243   fi
244   if [ "$TEST_SELECTION" == "NODB" ]; then
245     GROUP="--exclude-group DB,SLOWDB"
246   fi
247
248   INPUT="$BASEDIR/tests"
249   if [ -n "$2" ]; then
250     INPUT="$INPUT/$2"
251   fi
252
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"
255   RESULT=$?
256
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
262   fi
263 }
264
265 #
266 # Start the test execution
267 #
268 if [ -z "$1" ] && [ -n "$TEST_SELECTION" ]; then
269   # run all known database configs
270   for DBCONFIG in $DBCONFIGS; do
271     execute_tests "$DBCONFIG"
272   done
273 else
274   FILENAME="$2"
275   if [ -n "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
276     FILENAME="../$FILENAME"
277   fi
278   execute_tests "$1" "$FILENAME" "$3"
279 fi