autotest.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/bin/bash
  2. #
  3. # ownCloud
  4. #
  5. # @author Vincent Petry
  6. # @author Morris Jobke
  7. # @author Robin McCorkell
  8. # @author Thomas Müller
  9. # @author Andreas Fischer
  10. # @author Joas Schilling
  11. # @author Lukas Reschke
  12. # @copyright 2012-2015 Thomas Müller thomas.mueller@tmit.eu
  13. #
  14. set -e
  15. #$EXECUTOR_NUMBER is set by Jenkins and allows us to run autotest in parallel
  16. DATABASENAME=oc_autotest$EXECUTOR_NUMBER
  17. DATABASEUSER=oc_autotest$EXECUTOR_NUMBER
  18. DATABASEHOST=localhost
  19. ADMINLOGIN=admin$EXECUTOR_NUMBER
  20. BASEDIR=$PWD
  21. DBCONFIGS="sqlite mysql mariadb pgsql oci"
  22. # $PHP_EXE is run through 'which' and as such e.g. 'php' or 'hhvm' is usually
  23. # sufficient. Due to the behaviour of 'which', $PHP_EXE may also be a path
  24. # (absolute or not) to an executable, e.g. ./code/projects/php-src/sapi/cli/php.
  25. if [ -z "$PHP_EXE" ]; then
  26. PHP_EXE=php
  27. fi
  28. PHP=$(which "$PHP_EXE")
  29. PHPUNIT=$(which phpunit)
  30. _XDEBUG_CONFIG=$XDEBUG_CONFIG
  31. unset XDEBUG_CONFIG
  32. function print_syntax {
  33. echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
  34. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  35. echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
  36. echo -e "\nExample: ./autotest.sh sqlite lib/template.php" >&2
  37. echo "will run the test suite from \"tests/lib/template.php\"" >&2
  38. echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
  39. }
  40. if [ -x "$PHP" ]; then
  41. echo "Using PHP executable $PHP"
  42. else
  43. echo "Could not find PHP executable $PHP_EXE" >&2
  44. exit 3
  45. fi
  46. if ! [ -x "$PHPUNIT" ]; then
  47. echo "phpunit executable not found, please install phpunit version >= 3.7" >&2
  48. exit 3
  49. fi
  50. # PHPUnit might also be installed via a facade binary script
  51. if [[ "$PHPUNIT" =~ \.phar$ ]]; then
  52. PHPUNIT=( "$PHP" "$PHPUNIT" )
  53. else
  54. PHPUNIT=( "$PHPUNIT" )
  55. fi
  56. PHPUNIT_VERSION=$($PHPUNIT --version | cut -d" " -f2)
  57. PHPUNIT_MAJOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f1)
  58. PHPUNIT_MINOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f2)
  59. if ! [ "$PHPUNIT_MAJOR_VERSION" -gt 3 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 3 -a "$PHPUNIT_MINOR_VERSION" -ge 7 \) ]; then
  60. echo "phpunit version >= 3.7 required. Version found: $PHPUNIT_VERSION" >&2
  61. exit 4
  62. fi
  63. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  64. echo "Please enable write permissions on config and config/config.php" >&2
  65. exit 1
  66. fi
  67. if [ "$1" ]; then
  68. FOUND=0
  69. for DBCONFIG in $DBCONFIGS; do
  70. if [ "$1" = "$DBCONFIG" ]; then
  71. FOUND=1
  72. break
  73. fi
  74. done
  75. if [ $FOUND = 0 ]; then
  76. echo -e "Unknown database config name \"$1\"\n" >&2
  77. print_syntax
  78. exit 2
  79. fi
  80. fi
  81. # check for the presence of @since in all OCP methods
  82. $PHP build/OCPSinceChecker.php
  83. # Back up existing (dev) config if one exists and backup not already there
  84. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  85. mv config/config.php config/config-autotest-backup.php
  86. fi
  87. function cleanup_config {
  88. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  89. echo "Kill the docker $DOCKER_CONTAINER_ID"
  90. docker rm -f "$DOCKER_CONTAINER_ID"
  91. fi
  92. cd "$BASEDIR"
  93. # Restore existing config
  94. if [ -f config/config-autotest-backup.php ]; then
  95. mv config/config-autotest-backup.php config/config.php
  96. fi
  97. # Remove autotest config
  98. if [ -f config/autoconfig.php ]; then
  99. rm config/autoconfig.php
  100. fi
  101. }
  102. # restore config on exit
  103. trap cleanup_config EXIT
  104. # use tmpfs for datadir - should speedup unit test execution
  105. if [ -d /dev/shm ]; then
  106. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  107. else
  108. DATADIR=$BASEDIR/data-autotest
  109. fi
  110. echo "Using database $DATABASENAME"
  111. function execute_tests {
  112. DB=$1
  113. echo "Setup environment for $DB testing ..."
  114. # back to root folder
  115. cd "$BASEDIR"
  116. # revert changes to tests/data
  117. git checkout tests/data
  118. # reset data directory
  119. rm -rf "$DATADIR"
  120. mkdir "$DATADIR"
  121. cp tests/preseed-config.php config/config.php
  122. _DB=$DB
  123. # drop database
  124. if [ "$DB" == "mysql" ] ; then
  125. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  126. fi
  127. if [ "$DB" == "mariadb" ] ; then
  128. if [ ! -z "$USEDOCKER" ] ; then
  129. echo "Fire up the mariadb docker"
  130. DOCKER_CONTAINER_ID=$(docker run \
  131. -e MYSQL_ROOT_PASSWORD=owncloud \
  132. -e MYSQL_USER="$DATABASEUSER" \
  133. -e MYSQL_PASSWORD=owncloud \
  134. -e MYSQL_DATABASE="$DATABASENAME" \
  135. -d rullzer/mariadb-owncloud)
  136. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  137. echo "Waiting for MariaDB initialisation ..."
  138. # grep exits on the first match and then the script continues
  139. timeout 30 docker logs -f $DOCKER_CONTAINER_ID 2>&1 | grep -q "mysqld: ready for connections."
  140. echo "MariaDB is up."
  141. else
  142. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  143. echo "Your mysql binary is not provided by MariaDB"
  144. echo "To use the docker container set the USEDOCKER enviroment variable"
  145. exit -1
  146. fi
  147. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  148. fi
  149. #Reset _DB to mysql since that is what we use internally
  150. _DB="mysql"
  151. fi
  152. if [ "$DB" == "pgsql" ] ; then
  153. if [ ! -z "$USEDOCKER" ] ; then
  154. echo "Fire up the postgres docker"
  155. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  156. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  157. echo "Waiting for Postgres initialisation ..."
  158. # grep exits on the first match and then the script continues
  159. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  160. echo "Postgres is up."
  161. else
  162. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  163. fi
  164. fi
  165. if [ "$DB" == "oci" ] ; then
  166. echo "Fire up the oracle docker"
  167. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  168. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  169. echo "Waiting for Oracle initialization ... "
  170. # grep exits on the first match and then the script continues - times out after 2 minutes
  171. timeout 120 docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "Grant succeeded."
  172. DATABASEUSER=autotest
  173. DATABASENAME='XE'
  174. fi
  175. # trigger installation
  176. echo "Installing ...."
  177. "$PHP" ./occ maintenance:install --database="$_DB" --database-name="$DATABASENAME" --database-host="$DATABASEHOST" --database-user="$DATABASEUSER" --database-pass=owncloud --database-table-prefix=oc_ --admin-user="$ADMINLOGIN" --admin-pass=admin --data-dir="$DATADIR"
  178. #test execution
  179. echo "Testing with $DB ..."
  180. cd tests
  181. rm -rf "coverage-html-$DB"
  182. mkdir "coverage-html-$DB"
  183. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  184. if [[ "$_XDEBUG_CONFIG" ]]; then
  185. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  186. fi
  187. if [ -z "$NOCOVERAGE" ]; then
  188. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$DB.xml" --coverage-clover "autotest-clover-$DB.xml" --coverage-html "coverage-html-$DB" "$2" "$3"
  189. RESULT=$?
  190. else
  191. echo "No coverage"
  192. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$DB.xml" "$2" "$3"
  193. RESULT=$?
  194. fi
  195. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  196. echo "Kill the docker $DOCKER_CONTAINER_ID"
  197. docker rm -f $DOCKER_CONTAINER_ID
  198. unset DOCKER_CONTAINER_ID
  199. fi
  200. }
  201. #
  202. # start test execution
  203. #
  204. if [ -z "$1" ]
  205. then
  206. # run all known database configs
  207. for DBCONFIG in $DBCONFIGS; do
  208. execute_tests "$DBCONFIG"
  209. done
  210. else
  211. FILENAME="$2"
  212. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  213. FILENAME="../$FILENAME"
  214. fi
  215. execute_tests "$1" "$FILENAME" "$3"
  216. fi
  217. #
  218. # NOTES on mysql:
  219. # - CREATE DATABASE oc_autotest;
  220. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  221. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  222. #
  223. # - for parallel executor support with EXECUTOR_NUMBER=0:
  224. # - CREATE DATABASE oc_autotest0;
  225. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  226. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  227. #
  228. # NOTES on pgsql:
  229. # - su - postgres
  230. # - createuser -P oc_autotest (enter password and enable superuser)
  231. # - to enable dropdb I decided to add following line to pg_hba.conf (this is not the safest way but I don't care for the testing machine):
  232. # local all all trust
  233. #
  234. # - for parallel executor support with EXECUTOR_NUMBER=0:
  235. # - createuser -P oc_autotest0 (enter password and enable superuser)
  236. #
  237. # NOTES on oci:
  238. # - it's a pure nightmare to install Oracle on a Linux-System
  239. # - DON'T TRY THIS AT HOME!
  240. # - if you really need it: we feel sorry for you
  241. #