1
0

autotest.sh 5.7 KB

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