autotest.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. 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. DB=$1
  111. echo "Setup environment for $DB testing ..."
  112. # back to root folder
  113. cd "$BASEDIR"
  114. # revert changes to tests/data
  115. git checkout tests/data
  116. # reset data directory
  117. rm -rf "$DATADIR"
  118. mkdir "$DATADIR"
  119. cp tests/preseed-config.php config/config.php
  120. _DB=$DB
  121. # drop database
  122. if [ "$DB" == "mysql" ] ; then
  123. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  124. fi
  125. if [ "$DB" == "mariadb" ] ; then
  126. if [ ! -z "$USEDOCKER" ] ; then
  127. echo "Fire up the mariadb docker"
  128. DOCKER_CONTAINER_ID=$(docker run \
  129. -e MYSQL_ROOT_PASSWORD=owncloud \
  130. -e MYSQL_USER="$DATABASEUSER" \
  131. -e MYSQL_PASSWORD=owncloud \
  132. -e MYSQL_DATABASE="$DATABASENAME" \
  133. -d rullzer/mariadb-owncloud)
  134. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  135. echo "Waiting for MariaDB initialisation ..."
  136. # grep exits on the first match and then the script continues
  137. timeout 30 docker logs -f $DOCKER_CONTAINER_ID 2>&1 | grep -q "mysqld: ready for connections."
  138. echo "MariaDB is up."
  139. else
  140. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  141. echo "Your mysql binary is not provided by MariaDB"
  142. echo "To use the docker container set the USEDOCKER enviroment variable"
  143. exit -1
  144. fi
  145. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  146. fi
  147. #Reset _DB to mysql since that is what we use internally
  148. _DB="mysql"
  149. fi
  150. if [ "$DB" == "pgsql" ] ; then
  151. if [ ! -z "$USEDOCKER" ] ; then
  152. echo "Fire up the postgres docker"
  153. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  154. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  155. echo "Waiting for Postgres initialisation ..."
  156. # grep exits on the first match and then the script continues
  157. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  158. echo "Postgres is up."
  159. else
  160. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  161. fi
  162. fi
  163. if [ "$DB" == "oci" ] ; then
  164. echo "Fire up the oracle docker"
  165. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  166. DATABASEHOST=$(docker inspect "$DOCKER_CONTAINER_ID" | grep IPAddress | cut -d '"' -f 4)
  167. echo "Waiting for Oracle initialization ... "
  168. # grep exits on the first match and then the script continues - times out after 2 minutes
  169. timeout 120 docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "Grant succeeded."
  170. DATABASEUSER=autotest
  171. DATABASENAME='XE'
  172. fi
  173. # trigger installation
  174. echo "Installing ...."
  175. "$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"
  176. #test execution
  177. echo "Testing with $DB ..."
  178. cd tests
  179. rm -rf "coverage-html-$DB"
  180. mkdir "coverage-html-$DB"
  181. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  182. if [ -z "$NOCOVERAGE" ]; then
  183. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$DB.xml" --coverage-clover "autotest-clover-$DB.xml" --coverage-html "coverage-html-$DB" "$2" "$3"
  184. RESULT=$?
  185. else
  186. echo "No coverage"
  187. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$DB.xml" "$2" "$3"
  188. RESULT=$?
  189. fi
  190. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  191. echo "Kill the docker $DOCKER_CONTAINER_ID"
  192. docker rm -f $DOCKER_CONTAINER_ID
  193. unset DOCKER_CONTAINER_ID
  194. fi
  195. }
  196. #
  197. # start test execution
  198. #
  199. if [ -z "$1" ]
  200. then
  201. # run all known database configs
  202. for DBCONFIG in $DBCONFIGS; do
  203. execute_tests "$DBCONFIG"
  204. done
  205. else
  206. FILENAME="$2"
  207. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  208. FILENAME="../$FILENAME"
  209. fi
  210. execute_tests "$1" "$FILENAME" "$3"
  211. fi
  212. #
  213. # NOTES on mysql:
  214. # - CREATE DATABASE oc_autotest;
  215. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  216. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  217. #
  218. # - for parallel executor support with EXECUTOR_NUMBER=0:
  219. # - CREATE DATABASE oc_autotest0;
  220. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  221. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  222. #
  223. # NOTES on pgsql:
  224. # - su - postgres
  225. # - createuser -P oc_autotest (enter password and enable superuser)
  226. # - 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):
  227. # local all all trust
  228. #
  229. # - for parallel executor support with EXECUTOR_NUMBER=0:
  230. # - createuser -P oc_autotest0 (enter password and enable superuser)
  231. #
  232. # NOTES on oci:
  233. # - it's a pure nightmare to install Oracle on a Linux-System
  234. # - DON'T TRY THIS AT HOME!
  235. # - if you really need it: we feel sorry for you
  236. #