autotest.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 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. function print_syntax {
  31. echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
  32. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  33. echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
  34. echo -e "\nExample: ./autotest.sh sqlite lib/template.php" >&2
  35. echo "will run the test suite from \"tests/lib/template.php\"" >&2
  36. echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
  37. }
  38. if [ -x "$PHP" ]; then
  39. echo "Using PHP executable $PHP"
  40. else
  41. echo "Could not find PHP executable $PHP_EXE" >&2
  42. exit 3
  43. fi
  44. if ! [ -x "$PHPUNIT" ]; then
  45. echo "phpunit executable not found, please install phpunit version >= 3.7" >&2
  46. exit 3
  47. fi
  48. # PHPUnit might also be installed via a facade binary script
  49. if [[ "$PHPUNIT" =~ \.phar$ ]]; then
  50. PHPUNIT=( "$PHP" "$PHPUNIT" )
  51. else
  52. PHPUNIT=( "$PHPUNIT" )
  53. fi
  54. PHPUNIT_VERSION=$($PHPUNIT --version | cut -d" " -f2)
  55. PHPUNIT_MAJOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f1)
  56. PHPUNIT_MINOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f2)
  57. if ! [ "$PHPUNIT_MAJOR_VERSION" -gt 3 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 3 -a "$PHPUNIT_MINOR_VERSION" -ge 7 \) ]; then
  58. echo "phpunit version >= 3.7 required. Version found: $PHPUNIT_VERSION" >&2
  59. exit 4
  60. fi
  61. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  62. echo "Please enable write permissions on config and config/config.php" >&2
  63. exit 1
  64. fi
  65. if [ "$1" ]; then
  66. FOUND=0
  67. for DBCONFIG in $DBCONFIGS; do
  68. if [ "$1" = "$DBCONFIG" ]; then
  69. FOUND=1
  70. break
  71. fi
  72. done
  73. if [ $FOUND = 0 ]; then
  74. echo -e "Unknown database config name \"$1\"\n" >&2
  75. print_syntax
  76. exit 2
  77. fi
  78. fi
  79. # Back up existing (dev) config if one exists and backup not already there
  80. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  81. mv config/config.php config/config-autotest-backup.php
  82. fi
  83. function cleanup_config {
  84. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  85. echo "Kill the docker $DOCKER_CONTAINER_ID"
  86. docker rm -f "$DOCKER_CONTAINER_ID"
  87. fi
  88. cd "$BASEDIR"
  89. # Restore existing config
  90. if [ -f config/config-autotest-backup.php ]; then
  91. mv config/config-autotest-backup.php config/config.php
  92. fi
  93. # Remove autotest config
  94. if [ -f config/autoconfig.php ]; then
  95. rm config/autoconfig.php
  96. fi
  97. }
  98. # restore config on exit
  99. trap cleanup_config EXIT
  100. # use tmpfs for datadir - should speedup unit test execution
  101. if [ -d /dev/shm ]; then
  102. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  103. else
  104. DATADIR=$BASEDIR/data-autotest
  105. fi
  106. echo "Using database $DATABASENAME"
  107. function execute_tests {
  108. echo "Setup environment for $1 testing ..."
  109. # back to root folder
  110. cd "$BASEDIR"
  111. # revert changes to tests/data
  112. git checkout tests/data
  113. # reset data directory
  114. rm -rf "$DATADIR"
  115. mkdir "$DATADIR"
  116. cp tests/preseed-config.php config/config.php
  117. # drop database
  118. if [ "$1" == "mysql" ] ; then
  119. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  120. fi
  121. if [ "$1" == "pgsql" ] ; then
  122. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  123. fi
  124. if [ "$1" == "oci" ] ; then
  125. echo "Fire up the oracle docker"
  126. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  127. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  128. echo "Waiting 60 seconds for Oracle initialization ... "
  129. sleep 60
  130. DATABASEUSER=autotest
  131. DATABASENAME='XE'
  132. fi
  133. # trigger installation
  134. echo "Installing ...."
  135. "$PHP" ./occ maintenance:install --database="$1" --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"
  136. #test execution
  137. echo "Testing with $1 ..."
  138. cd tests
  139. rm -rf "coverage-html-$1"
  140. mkdir "coverage-html-$1"
  141. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  142. if [ -z "$NOCOVERAGE" ]; then
  143. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" --coverage-clover "autotest-clover-$1.xml" --coverage-html "coverage-html-$1" "$2" "$3"
  144. RESULT=$?
  145. else
  146. echo "No coverage"
  147. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" "$2" "$3"
  148. RESULT=$?
  149. fi
  150. }
  151. #
  152. # start test execution
  153. #
  154. if [ -z "$1" ]
  155. then
  156. # run all known database configs
  157. for DBCONFIG in $DBCONFIGS; do
  158. execute_tests "$DBCONFIG"
  159. done
  160. else
  161. FILENAME="$2"
  162. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  163. FILENAME="../$FILENAME"
  164. fi
  165. execute_tests "$1" "$FILENAME" "$3"
  166. fi
  167. #
  168. # NOTES on mysql:
  169. # - CREATE DATABASE oc_autotest;
  170. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  171. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  172. #
  173. # - for parallel executor support with EXECUTOR_NUMBER=0:
  174. # - CREATE DATABASE oc_autotest0;
  175. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  176. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  177. #
  178. # NOTES on pgsql:
  179. # - su - postgres
  180. # - createuser -P oc_autotest (enter password and enable superuser)
  181. # - 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):
  182. # local all all trust
  183. #
  184. # - for parallel executor support with EXECUTOR_NUMBER=0:
  185. # - createuser -P oc_autotest0 (enter password and enable superuser)
  186. #
  187. # NOTES on oci:
  188. # - it's a pure nightmare to install Oracle on a Linux-System
  189. # - DON'T TRY THIS AT HOME!
  190. # - if you really need it: we feel sorry for you
  191. #