You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autotest.sh 6.8KB

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