Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autotest.sh 9.3KB

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