autotest-external.sh 7.8 KB

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