autotest.sh 6.2 KB

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