autotest.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  125. fi
  126. if [ "$1" == "oci" ] ; then
  127. echo "Fire up the oracle docker"
  128. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  129. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  130. echo "Waiting 120 seconds for Oracle initialization ... "
  131. sleep 120
  132. DATABASEUSER=autotest
  133. DATABASENAME='XE'
  134. fi
  135. # trigger installation
  136. echo "Installing ...."
  137. "$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"
  138. #test execution
  139. echo "Testing with $1 ..."
  140. cd tests
  141. rm -rf "coverage-html-$1"
  142. mkdir "coverage-html-$1"
  143. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  144. if [ -z "$NOCOVERAGE" ]; then
  145. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" --coverage-clover "autotest-clover-$1.xml" --coverage-html "coverage-html-$1" "$2" "$3"
  146. RESULT=$?
  147. else
  148. echo "No coverage"
  149. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" "$2" "$3"
  150. RESULT=$?
  151. fi
  152. }
  153. #
  154. # start test execution
  155. #
  156. if [ -z "$1" ]
  157. then
  158. # run all known database configs
  159. for DBCONFIG in $DBCONFIGS; do
  160. execute_tests "$DBCONFIG"
  161. done
  162. else
  163. FILENAME="$2"
  164. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  165. FILENAME="../$FILENAME"
  166. fi
  167. execute_tests "$1" "$FILENAME" "$3"
  168. fi
  169. #
  170. # NOTES on mysql:
  171. # - CREATE DATABASE oc_autotest;
  172. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  173. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  174. #
  175. # - for parallel executor support with EXECUTOR_NUMBER=0:
  176. # - CREATE DATABASE oc_autotest0;
  177. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  178. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  179. #
  180. # NOTES on pgsql:
  181. # - su - postgres
  182. # - createuser -P oc_autotest (enter password and enable superuser)
  183. # - 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):
  184. # local all all trust
  185. #
  186. # - for parallel executor support with EXECUTOR_NUMBER=0:
  187. # - createuser -P oc_autotest0 (enter password and enable superuser)
  188. #
  189. # NOTES on oci:
  190. # - it's a pure nightmare to install Oracle on a Linux-System
  191. # - DON'T TRY THIS AT HOME!
  192. # - if you really need it: we feel sorry for you
  193. #