autotest-external.sh 8.4 KB

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