autotest.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # check for the presence of @since in all OCP methods
  80. $PHP build/OCPSinceChecker.php
  81. # Back up existing (dev) config if one exists and backup not already there
  82. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  83. mv config/config.php config/config-autotest-backup.php
  84. fi
  85. function cleanup_config {
  86. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  87. echo "Kill the docker $DOCKER_CONTAINER_ID"
  88. docker rm -f "$DOCKER_CONTAINER_ID"
  89. fi
  90. cd "$BASEDIR"
  91. # Restore existing config
  92. if [ -f config/config-autotest-backup.php ]; then
  93. mv config/config-autotest-backup.php config/config.php
  94. fi
  95. # Remove autotest config
  96. if [ -f config/autoconfig.php ]; then
  97. rm config/autoconfig.php
  98. fi
  99. }
  100. # restore config on exit
  101. trap cleanup_config EXIT
  102. # use tmpfs for datadir - should speedup unit test execution
  103. if [ -d /dev/shm ]; then
  104. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  105. else
  106. DATADIR=$BASEDIR/data-autotest
  107. fi
  108. echo "Using database $DATABASENAME"
  109. function execute_tests {
  110. echo "Setup environment for $1 testing ..."
  111. # back to root folder
  112. cd "$BASEDIR"
  113. # revert changes to tests/data
  114. git checkout tests/data
  115. # reset data directory
  116. rm -rf "$DATADIR"
  117. mkdir "$DATADIR"
  118. cp tests/preseed-config.php config/config.php
  119. # drop database
  120. if [ "$1" == "mysql" ] ; then
  121. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  122. fi
  123. if [ "$1" == "pgsql" ] ; then
  124. if [ ! -z "$USEDOCKER" ] ; then
  125. echo "Fire up the postgres docker"
  126. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  127. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  128. echo "Waiting for Postgres initialisation ..."
  129. # grep exits on the first match and then the script continues
  130. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  131. echo "Postgres is up."
  132. else
  133. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  134. fi
  135. fi
  136. if [ "$1" == "oci" ] ; then
  137. echo "Fire up the oracle docker"
  138. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  139. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  140. echo "Waiting for Oracle initialization ... "
  141. # grep exits on the first match and then the script continues - times out after 2 minutes
  142. timeout 120 docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "Grant succeeded."
  143. DATABASEUSER=autotest
  144. DATABASENAME='XE'
  145. fi
  146. # trigger installation
  147. echo "Installing ...."
  148. "$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"
  149. #test execution
  150. echo "Testing with $1 ..."
  151. cd tests
  152. rm -rf "coverage-html-$1"
  153. mkdir "coverage-html-$1"
  154. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  155. if [ -z "$NOCOVERAGE" ]; then
  156. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" --coverage-clover "autotest-clover-$1.xml" --coverage-html "coverage-html-$1" "$2" "$3"
  157. RESULT=$?
  158. else
  159. echo "No coverage"
  160. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" "$2" "$3"
  161. RESULT=$?
  162. fi
  163. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  164. echo "Kill the docker $DOCKER_CONTAINER_ID"
  165. docker rm -f $DOCKER_CONTAINER_ID
  166. unset DOCKER_CONTAINER_ID
  167. fi
  168. }
  169. #
  170. # start test execution
  171. #
  172. if [ -z "$1" ]
  173. then
  174. # run all known database configs
  175. for DBCONFIG in $DBCONFIGS; do
  176. execute_tests "$DBCONFIG"
  177. done
  178. else
  179. FILENAME="$2"
  180. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  181. FILENAME="../$FILENAME"
  182. fi
  183. execute_tests "$1" "$FILENAME" "$3"
  184. fi
  185. #
  186. # NOTES on mysql:
  187. # - CREATE DATABASE oc_autotest;
  188. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  189. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  190. #
  191. # - for parallel executor support with EXECUTOR_NUMBER=0:
  192. # - CREATE DATABASE oc_autotest0;
  193. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  194. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  195. #
  196. # NOTES on pgsql:
  197. # - su - postgres
  198. # - createuser -P oc_autotest (enter password and enable superuser)
  199. # - 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):
  200. # local all all trust
  201. #
  202. # - for parallel executor support with EXECUTOR_NUMBER=0:
  203. # - createuser -P oc_autotest0 (enter password and enable superuser)
  204. #
  205. # NOTES on oci:
  206. # - it's a pure nightmare to install Oracle on a Linux-System
  207. # - DON'T TRY THIS AT HOME!
  208. # - if you really need it: we feel sorry for you
  209. #