autotest-external.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 >= 4.8" >&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 9 -o \( $PHPUNIT_MAJOR_VERSION -eq 9 -a $PHPUNIT_MINOR_VERSION -ge 0 \) ]; then
  36. echo "phpunit version >= 9.0 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 --admin-user=$ADMINLOGIN --admin-pass=admin --data-dir=$DATADIR
  130. ./occ config:system:set --value true --type boolean allow_local_remote_servers
  131. #test execution
  132. echo "Testing with $1 ..."
  133. if [ -n "$2" ]; then
  134. echo "Run only $2 ..."
  135. fi
  136. cd tests
  137. rm -rf "coverage-external-html-$1"
  138. mkdir "coverage-external-html-$1"
  139. # just enable files_external
  140. php ../occ app:enable -vvv files_external
  141. if [[ "$_XDEBUG_CONFIG" ]]; then
  142. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  143. fi
  144. if [ -z "$NOCOVERAGE" ]; then
  145. "$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"
  146. else
  147. echo "No coverage"
  148. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1.xml"
  149. fi
  150. if [[ $? -ne 0 ]]; then
  151. echo "Error during phpunit execution ... terminating"
  152. exit 1
  153. fi
  154. if [ -n "$2" -a "$2" == "common-tests" ]; then
  155. return;
  156. fi
  157. FILES_EXTERNAL_BACKEND_PATH=../apps/files_external/tests/Storage
  158. FILES_EXTERNAL_BACKEND_ENV_PATH=../apps/files_external/tests/env
  159. for startFile in `ls -1 $FILES_EXTERNAL_BACKEND_ENV_PATH | grep start`; do
  160. name=`echo $startFile | sed 's/start-//' | sed 's/\.sh//'`
  161. if [ -n "$2" -a "$2" != "$name" ]; then
  162. echo "skip: $startFile"
  163. continue;
  164. fi
  165. echo "start: $startFile"
  166. echo "name: $name"
  167. # execute start file
  168. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$startFile
  169. if [ $? -eq 0 ]; then
  170. # getting backend to test from filename
  171. # it's the part between the dots startSomething.TestToRun.sh
  172. testToRun=`echo $startFile | cut -d '-' -f 2`
  173. # capitalize first letter
  174. testToRun="${testToRun^}"
  175. testToRun="${testToRun}Test.php"
  176. # run the specific test
  177. if [ -z "$NOCOVERAGE" ]; then
  178. rm -rf "coverage-external-html-$1-$name"
  179. mkdir "coverage-external-html-$1-$name"
  180. "$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"
  181. else
  182. echo "No coverage"
  183. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1-$name.xml" "$FILES_EXTERNAL_BACKEND_PATH/$testToRun"
  184. fi
  185. else
  186. DOEXIT=1
  187. fi
  188. if [[ $? -ne 0 ]]; then
  189. echo "Error during phpunit execution ... terminating"
  190. exit 1
  191. fi
  192. # calculate stop file
  193. stopFile=`echo "$startFile" | sed 's/start/stop/'`
  194. echo "stop: $stopFile"
  195. if [ -f $FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile ]; then
  196. # execute stop file if existent
  197. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile
  198. fi
  199. if [ "$DOEXIT" ]; then
  200. echo "Error during start file execution ... terminating"
  201. exit $DOEXIT
  202. fi
  203. done;
  204. }
  205. #
  206. # start test execution
  207. #
  208. if [ -z "$1" ]; then
  209. # run all known database configs
  210. for DBCONFIG in $DBCONFIGS; do
  211. execute_tests $DBCONFIG "$2"
  212. done
  213. else
  214. execute_tests "$1" "$2"
  215. fi
  216. #
  217. # NOTES on mysql:
  218. # - CREATE DATABASE oc_autotest;
  219. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  220. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  221. #
  222. # - for parallel executor support with EXECUTOR_NUMBER=0:
  223. # - CREATE DATABASE oc_autotest0;
  224. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  225. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  226. #
  227. # NOTES on pgsql:
  228. # - su - postgres
  229. # - createuser -P oc_autotest (enter password and enable superuser)
  230. # - 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):
  231. # local all all trust
  232. #
  233. # - for parallel executor support with EXECUTOR_NUMBER=0:
  234. # - createuser -P oc_autotest0 (enter password and enable superuser)
  235. #
  236. # NOTES on oci:
  237. # - it's a pure nightmare to install Oracle on a Linux-System
  238. # - DON'T TRY THIS AT HOME!
  239. # - if you really need it: we feel sorry for you
  240. #