Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

autotest.sh 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 120 seconds for Oracle initialization ... "
  141. sleep 120
  142. DATABASEUSER=autotest
  143. DATABASENAME='XE'
  144. fi
  145. # trigger installation
  146. echo "Installing ...."
  147. "$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"
  148. #test execution
  149. echo "Testing with $1 ..."
  150. cd tests
  151. rm -rf "coverage-html-$1"
  152. mkdir "coverage-html-$1"
  153. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  154. if [ -z "$NOCOVERAGE" ]; then
  155. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" --coverage-clover "autotest-clover-$1.xml" --coverage-html "coverage-html-$1" "$2" "$3"
  156. RESULT=$?
  157. else
  158. echo "No coverage"
  159. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml --log-junit "autotest-results-$1.xml" "$2" "$3"
  160. RESULT=$?
  161. fi
  162. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  163. echo "Kill the docker $DOCKER_CONTAINER_ID"
  164. docker rm -f $DOCKER_CONTAINER_ID
  165. unset DOCKER_CONTAINER_ID
  166. fi
  167. }
  168. #
  169. # start test execution
  170. #
  171. if [ -z "$1" ]
  172. then
  173. # run all known database configs
  174. for DBCONFIG in $DBCONFIGS; do
  175. execute_tests "$DBCONFIG"
  176. done
  177. else
  178. FILENAME="$2"
  179. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  180. FILENAME="../$FILENAME"
  181. fi
  182. execute_tests "$1" "$FILENAME" "$3"
  183. fi
  184. #
  185. # NOTES on mysql:
  186. # - CREATE DATABASE oc_autotest;
  187. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  188. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  189. #
  190. # - for parallel executor support with EXECUTOR_NUMBER=0:
  191. # - CREATE DATABASE oc_autotest0;
  192. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  193. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  194. #
  195. # NOTES on pgsql:
  196. # - su - postgres
  197. # - createuser -P oc_autotest (enter password and enable superuser)
  198. # - 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):
  199. # local all all trust
  200. #
  201. # - for parallel executor support with EXECUTOR_NUMBER=0:
  202. # - createuser -P oc_autotest0 (enter password and enable superuser)
  203. #
  204. # NOTES on oci:
  205. # - it's a pure nightmare to install Oracle on a Linux-System
  206. # - DON'T TRY THIS AT HOME!
  207. # - if you really need it: we feel sorry for you
  208. #