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