autotest-external.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env bash
  2. #
  3. # ownCloud
  4. #
  5. # @author Thomas Müller
  6. # @author Morris Jobke
  7. # @copyright 2012-2015 Thomas Müller thomas.mueller@tmit.eu
  8. # @copyright 2014 Morris Jobke hey@morrisjobke.de
  9. #
  10. #$EXECUTOR_NUMBER is set by Jenkins and allows us to run autotest in parallel
  11. DATABASENAME=oc_autotest$EXECUTOR_NUMBER
  12. DATABASEUSER=oc_autotest$EXECUTOR_NUMBER
  13. ADMINLOGIN=admin$EXECUTOR_NUMBER
  14. BASEDIR=$PWD
  15. DBCONFIGS="sqlite mysql pgsql oci"
  16. PHPUNIT=$(which phpunit)
  17. _XDEBUG_CONFIG=$XDEBUG_CONFIG
  18. unset XDEBUG_CONFIG
  19. function print_syntax {
  20. echo -e "Syntax: ./autotest-external.sh [dbconfigname] [startfile]\n" >&2
  21. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  22. echo -e "\t\"startfile\" is the name of a start file inside the env/ folder in the files_external tests" >&2
  23. echo -e "\nExample: ./autotest.sh sqlite webdav-ownCloud" >&2
  24. echo "will run the external suite from \"apps/files_external/tests/env/start-webdav-ownCloud.sh\"" >&2
  25. echo -e "\nIf no arguments are specified, all available external backends will be run with all database configs" >&2
  26. echo -e "\nIf you specify 'common-tests' as startfile it will just run the tests that are independent from the backends" >&2
  27. }
  28. if ! [ -x "$PHPUNIT" ]; then
  29. echo "phpunit executable not found, please install phpunit version >= 3.7" >&2
  30. exit 3
  31. fi
  32. PHPUNIT_VERSION=$("$PHPUNIT" --version | cut -d" " -f2)
  33. PHPUNIT_MAJOR_VERSION=$(echo $PHPUNIT_VERSION | cut -d"." -f1)
  34. PHPUNIT_MINOR_VERSION=$(echo $PHPUNIT_VERSION | cut -d"." -f2)
  35. if ! [ $PHPUNIT_MAJOR_VERSION -gt 4 -o \( $PHPUNIT_MAJOR_VERSION -eq 4 -a $PHPUNIT_MINOR_VERSION -ge 4 \) ]; then
  36. echo "phpunit version >= 4.4 required. Version found: $PHPUNIT_VERSION" >&2
  37. exit 4
  38. fi
  39. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  40. echo "Please enable write permissions on config and config/config.php" >&2
  41. exit 1
  42. fi
  43. if [ "$1" ]; then
  44. FOUND=0
  45. for DBCONFIG in $DBCONFIGS; do
  46. if [ "$1" = $DBCONFIG ]; then
  47. FOUND=1
  48. break
  49. fi
  50. done
  51. if [ $FOUND = 0 ]; then
  52. echo -e "Unknown database config name \"$1\"\n" >&2
  53. print_syntax
  54. exit 2
  55. fi
  56. fi
  57. # Back up existing (dev) config if one exists and backup not already there
  58. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  59. mv config/config.php config/config-autotest-backup.php
  60. fi
  61. function cleanup_config {
  62. cd "$BASEDIR"
  63. # Restore existing config
  64. if [ -f config/config-autotest-backup.php ]; then
  65. mv config/config-autotest-backup.php config/config.php
  66. fi
  67. # Remove autotest config
  68. if [ -f config/autoconfig.php ]; then
  69. rm config/autoconfig.php
  70. fi
  71. }
  72. # restore config on exit
  73. trap cleanup_config EXIT
  74. # use tmpfs for datadir - should speedup unit test execution
  75. if [ -d /dev/shm ]; then
  76. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  77. else
  78. DATADIR=$BASEDIR/data-autotest
  79. fi
  80. echo "Using database $DATABASENAME"
  81. function execute_tests {
  82. echo "Setup environment for $1 testing ..."
  83. # back to root folder
  84. cd "$BASEDIR"
  85. # revert changes to tests/data
  86. git checkout tests/data
  87. # reset data directory
  88. rm -rf "$DATADIR"
  89. mkdir "$DATADIR"
  90. # remove the old config file
  91. #rm -rf config/config.php
  92. cp tests/preseed-config.php config/config.php
  93. # drop database
  94. if [ "$1" == "mysql" ] ; then
  95. mysql -u $DATABASEUSER -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" || true
  96. fi
  97. if [ "$1" == "pgsql" ] ; then
  98. dropdb -U $DATABASEUSER $DATABASENAME || true
  99. fi
  100. if [ "$1" == "oci" ] ; then
  101. echo "drop the database"
  102. sqlplus -s -l / as sysdba <<EOF
  103. drop user $DATABASENAME cascade;
  104. EOF
  105. echo "create the database"
  106. sqlplus -s -l / as sysdba <<EOF
  107. create user $DATABASENAME identified by owncloud;
  108. alter user $DATABASENAME default tablespace users
  109. temporary tablespace temp
  110. quota unlimited on users;
  111. grant create session
  112. , create table
  113. , create procedure
  114. , create sequence
  115. , create trigger
  116. , create view
  117. , create synonym
  118. , alter session
  119. to $DATABASENAME;
  120. exit;
  121. EOF
  122. DATABASEUSER=$DATABASENAME
  123. DATABASENAME='XE'
  124. fi
  125. # copy autoconfig
  126. cp "$BASEDIR/tests/autoconfig-$1.php" "$BASEDIR/config/autoconfig.php"
  127. # trigger installation
  128. echo "Installing ...."
  129. ./occ maintenance:install -vvv --database=$1 --database-name=$DATABASENAME --database-host=localhost --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. if [ -n "$2" ]; then
  133. echo "Run only $2 ..."
  134. fi
  135. cd tests
  136. rm -rf "coverage-external-html-$1"
  137. mkdir "coverage-external-html-$1"
  138. # just enable files_external
  139. php ../occ app:enable -vvv files_external
  140. if [[ "$_XDEBUG_CONFIG" ]]; then
  141. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  142. fi
  143. if [ -z "$NOCOVERAGE" ]; then
  144. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1.xml" --coverage-clover "autotest-external-clover-$1.xml" --coverage-html "coverage-external-html-$1"
  145. RESULT=$?
  146. else
  147. echo "No coverage"
  148. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1.xml"
  149. RESULT=$?
  150. fi
  151. if [ -n "$2" -a "$2" == "common-tests" ]; then
  152. return;
  153. fi
  154. FILES_EXTERNAL_BACKEND_PATH=../apps/files_external/tests/storage
  155. FILES_EXTERNAL_BACKEND_ENV_PATH=../apps/files_external/tests/env
  156. for startFile in `ls -1 $FILES_EXTERNAL_BACKEND_ENV_PATH | grep start`; do
  157. name=`echo $startFile | sed 's/start-//' | sed 's/\.sh//'`
  158. if [ -n "$2" -a "$2" != "$name" ]; then
  159. echo "skip: $startFile"
  160. continue;
  161. fi
  162. echo "start: $startFile"
  163. echo "name: $name"
  164. # execute start file
  165. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$startFile
  166. if [ $? -eq 0 ]; then
  167. # getting backend to test from filename
  168. # it's the part between the dots startSomething.TestToRun.sh
  169. testToRun=`echo $startFile | cut -d '-' -f 2`
  170. testToRun="${testToRun}test.php"
  171. # run the specific test
  172. if [ -z "$NOCOVERAGE" ]; then
  173. rm -rf "coverage-external-html-$1-$name"
  174. mkdir "coverage-external-html-$1-$name"
  175. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1-$name.xml" --coverage-clover "autotest-external-clover-$1-$name.xml" --coverage-html "coverage-external-html-$1-$name" "$FILES_EXTERNAL_BACKEND_PATH/$testToRun"
  176. RESULT=$?
  177. else
  178. echo "No coverage"
  179. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1-$name.xml" "$FILES_EXTERNAL_BACKEND_PATH/$testToRun"
  180. RESULT=$?
  181. fi
  182. else
  183. DOEXIT=1
  184. fi
  185. # calculate stop file
  186. stopFile=`echo "$startFile" | sed 's/start/stop/'`
  187. echo "stop: $stopFile"
  188. if [ -f $FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile ]; then
  189. # execute stop file if existent
  190. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile
  191. fi
  192. if [ "$DOEXIT" ]; then
  193. echo "Error during start file execution ... terminating"
  194. exit $DOEXIT
  195. fi
  196. done;
  197. }
  198. #
  199. # start test execution
  200. #
  201. if [ -z "$1" ]; then
  202. # run all known database configs
  203. for DBCONFIG in $DBCONFIGS; do
  204. execute_tests $DBCONFIG "$2"
  205. done
  206. else
  207. execute_tests "$1" "$2"
  208. fi
  209. #
  210. # NOTES on mysql:
  211. # - CREATE DATABASE oc_autotest;
  212. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  213. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  214. #
  215. # - for parallel executor support with EXECUTOR_NUMBER=0:
  216. # - CREATE DATABASE oc_autotest0;
  217. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  218. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  219. #
  220. # NOTES on pgsql:
  221. # - su - postgres
  222. # - createuser -P oc_autotest (enter password and enable superuser)
  223. # - 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):
  224. # local all all trust
  225. #
  226. # - for parallel executor support with EXECUTOR_NUMBER=0:
  227. # - createuser -P oc_autotest0 (enter password and enable superuser)
  228. #
  229. # NOTES on oci:
  230. # - it's a pure nightmare to install Oracle on a Linux-System
  231. # - DON'T TRY THIS AT HOME!
  232. # - if you really need it: we feel sorry for you
  233. #