autotest-external.sh 8.4 KB

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