autotest-external.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/bin/bash
  2. #
  3. # ownCloud
  4. #
  5. # @author Thomas Müller
  6. # @author Morris Jobke
  7. # @copyright 2012, 2013 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. }
  25. if ! [ -x "$PHPUNIT" ]; then
  26. echo "phpunit executable not found, please install phpunit version >= 3.7" >&2
  27. exit 3
  28. fi
  29. PHPUNIT_VERSION=$("$PHPUNIT" --version | cut -d" " -f2)
  30. PHPUNIT_MAJOR_VERSION=$(echo $PHPUNIT_VERSION | cut -d"." -f1)
  31. PHPUNIT_MINOR_VERSION=$(echo $PHPUNIT_VERSION | cut -d"." -f2)
  32. if ! [ $PHPUNIT_MAJOR_VERSION -gt 3 -o \( $PHPUNIT_MAJOR_VERSION -eq 3 -a $PHPUNIT_MINOR_VERSION -ge 7 \) ]; then
  33. echo "phpunit version >= 3.7 required. Version found: $PHPUNIT_VERSION" >&2
  34. exit 4
  35. fi
  36. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  37. echo "Please enable write permissions on config and config/config.php" >&2
  38. exit 1
  39. fi
  40. if [ "$1" ]; then
  41. FOUND=0
  42. for DBCONFIG in $DBCONFIGS; do
  43. if [ "$1" = $DBCONFIG ]; then
  44. FOUND=1
  45. break
  46. fi
  47. done
  48. if [ $FOUND = 0 ]; then
  49. echo -e "Unknown database config name \"$1\"\n" >&2
  50. print_syntax
  51. exit 2
  52. fi
  53. fi
  54. # Back up existing (dev) config if one exists
  55. if [ -f config/config.php ]; then
  56. mv config/config.php config/config-autotest-backup.php
  57. fi
  58. function restore_config {
  59. # Restore existing config
  60. if [ -f config/config-autotest-backup.php ]; then
  61. mv config/config-autotest-backup.php config/config.php
  62. fi
  63. }
  64. # restore config on exit, even when killed
  65. trap restore_config SIGINT SIGTERM
  66. # use tmpfs for datadir - should speedup unit test execution
  67. if [ -d /dev/shm ]; then
  68. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  69. else
  70. DATADIR=$BASEDIR/data-autotest
  71. fi
  72. echo "Using database $DATABASENAME"
  73. # create autoconfig for sqlite, mysql and postgresql
  74. cat > ./tests/autoconfig-sqlite.php <<DELIM
  75. <?php
  76. \$AUTOCONFIG = array (
  77. 'installed' => false,
  78. 'dbtype' => 'sqlite',
  79. 'dbtableprefix' => 'oc_',
  80. 'adminlogin' => '$ADMINLOGIN',
  81. 'adminpass' => 'admin',
  82. 'directory' => '$DATADIR',
  83. );
  84. DELIM
  85. cat > ./tests/autoconfig-mysql.php <<DELIM
  86. <?php
  87. \$AUTOCONFIG = array (
  88. 'installed' => false,
  89. 'dbtype' => 'mysql',
  90. 'dbtableprefix' => 'oc_',
  91. 'adminlogin' => '$ADMINLOGIN',
  92. 'adminpass' => 'admin',
  93. 'directory' => '$DATADIR',
  94. 'dbuser' => '$DATABASEUSER',
  95. 'dbname' => '$DATABASENAME',
  96. 'dbhost' => 'localhost',
  97. 'dbpass' => 'owncloud',
  98. );
  99. DELIM
  100. cat > ./tests/autoconfig-pgsql.php <<DELIM
  101. <?php
  102. \$AUTOCONFIG = array (
  103. 'installed' => false,
  104. 'dbtype' => 'pgsql',
  105. 'dbtableprefix' => 'oc_',
  106. 'adminlogin' => '$ADMINLOGIN',
  107. 'adminpass' => 'admin',
  108. 'directory' => '$DATADIR',
  109. 'dbuser' => '$DATABASEUSER',
  110. 'dbname' => '$DATABASENAME',
  111. 'dbhost' => 'localhost',
  112. 'dbpass' => 'owncloud',
  113. );
  114. DELIM
  115. cat > ./tests/autoconfig-oci.php <<DELIM
  116. <?php
  117. \$AUTOCONFIG = array (
  118. 'installed' => false,
  119. 'dbtype' => 'oci',
  120. 'dbtableprefix' => 'oc_',
  121. 'adminlogin' => '$ADMINLOGIN',
  122. 'adminpass' => 'admin',
  123. 'directory' => '$DATADIR',
  124. 'dbuser' => '$DATABASENAME',
  125. 'dbname' => 'XE',
  126. 'dbhost' => 'localhost',
  127. 'dbpass' => 'owncloud',
  128. );
  129. DELIM
  130. function execute_tests {
  131. echo "Setup environment for $1 testing ..."
  132. # back to root folder
  133. cd "$BASEDIR"
  134. # revert changes to tests/data
  135. git checkout tests/data
  136. # reset data directory
  137. rm -rf "$DATADIR"
  138. mkdir "$DATADIR"
  139. # remove the old config file
  140. #rm -rf config/config.php
  141. cp tests/preseed-config.php config/config.php
  142. # drop database
  143. if [ "$1" == "mysql" ] ; then
  144. mysql -u $DATABASEUSER -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" || true
  145. fi
  146. if [ "$1" == "pgsql" ] ; then
  147. dropdb -U $DATABASEUSER $DATABASENAME || true
  148. fi
  149. if [ "$1" == "oci" ] ; then
  150. echo "drop the database"
  151. sqlplus -s -l / as sysdba <<EOF
  152. drop user $DATABASENAME cascade;
  153. EOF
  154. echo "create the database"
  155. sqlplus -s -l / as sysdba <<EOF
  156. create user $DATABASENAME identified by owncloud;
  157. alter user $DATABASENAME default tablespace users
  158. temporary tablespace temp
  159. quota unlimited on users;
  160. grant create session
  161. , create table
  162. , create procedure
  163. , create sequence
  164. , create trigger
  165. , create view
  166. , create synonym
  167. , alter session
  168. to $DATABASENAME;
  169. exit;
  170. EOF
  171. fi
  172. # copy autoconfig
  173. cp "$BASEDIR/tests/autoconfig-$1.php" "$BASEDIR/config/autoconfig.php"
  174. # trigger installation
  175. echo "INDEX"
  176. php -f index.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  177. echo "END INDEX"
  178. #test execution
  179. echo "Testing with $1 ..."
  180. if [ -n "$2" ]; then
  181. echo "Run only $2 ..."
  182. fi
  183. cd tests
  184. rm -rf "coverage-external-html-$1"
  185. mkdir "coverage-external-html-$1"
  186. # just enable files_external
  187. php ../occ app:enable files_external
  188. if [ -z "$NOCOVERAGE" ]; then
  189. #"$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"
  190. RESULT=$?
  191. else
  192. echo "No coverage"
  193. #"$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1.xml"
  194. RESULT=$?
  195. fi
  196. FILES_EXTERNAL_BACKEND_PATH=../apps/files_external/tests/backends
  197. FILES_EXTERNAL_BACKEND_ENV_PATH=../apps/files_external/tests/env
  198. for startFile in `ls -1 $FILES_EXTERNAL_BACKEND_ENV_PATH | grep start`; do
  199. name=`echo $startFile | replace "start-" "" | replace ".sh" ""`
  200. if [ -n "$2" -a "$2" != "$name" ]; then
  201. echo "skip: $startFile"
  202. continue;
  203. fi
  204. echo "start: $startFile"
  205. echo "name: $name"
  206. # execute start file
  207. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$startFile
  208. # getting backend to test from filename
  209. # it's the part between the dots startSomething.TestToRun.sh
  210. testToRun=`echo $startFile | cut -d '-' -f 2`
  211. # run the specific test
  212. if [ -z "$NOCOVERAGE" ]; then
  213. rm -rf "coverage-external-html-$1-$name"
  214. mkdir "coverage-external-html-$1-$name"
  215. "$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"
  216. RESULT=$?
  217. else
  218. echo "No coverage"
  219. "$PHPUNIT" --configuration phpunit-autotest-external.xml --log-junit "autotest-external-results-$1-$name.xml" "$FILES_EXTERNAL_BACKEND_PATH/$testToRun.php"
  220. RESULT=$?
  221. fi
  222. # calculate stop file
  223. stopFile=`echo "$startFile" | replace start stop`
  224. echo "stop: $stopFile"
  225. if [ -f $FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile ]; then
  226. # execute stop file if existant
  227. ./$FILES_EXTERNAL_BACKEND_ENV_PATH/$stopFile
  228. fi
  229. done;
  230. }
  231. #
  232. # start test execution
  233. #
  234. if [ -z "$1" ]
  235. then
  236. # run all known database configs
  237. for DBCONFIG in $DBCONFIGS; do
  238. execute_tests $DBCONFIG "$2"
  239. done
  240. else
  241. execute_tests "$1" "$2"
  242. fi
  243. cd "$BASEDIR"
  244. restore_config
  245. #
  246. # NOTES on mysql:
  247. # - CREATE DATABASE oc_autotest;
  248. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  249. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  250. #
  251. # - for parallel executor support with EXECUTOR_NUMBER=0:
  252. # - CREATE DATABASE oc_autotest0;
  253. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  254. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  255. #
  256. # NOTES on pgsql:
  257. # - su - postgres
  258. # - createuser -P oc_autotest (enter password and enable superuser)
  259. # - 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):
  260. # local all all trust
  261. #
  262. # - for parallel executor support with EXECUTOR_NUMBER=0:
  263. # - createuser -P oc_autotest0 (enter password and enable superuser)
  264. #
  265. # NOTES on oci:
  266. # - it's a pure nightmare to install Oracle on a Linux-System
  267. # - DON'T TRY THIS AT HOME!
  268. # - if you really need it: we feel sorry for you
  269. #