autotest.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/usr/bin/env bash
  2. #
  3. # ownCloud
  4. #
  5. # @author Vincent Petry
  6. # @author Morris Jobke
  7. # @author Robin McCorkell
  8. # @author Thomas Müller
  9. # @author Andreas Fischer
  10. # @author Joas Schilling
  11. # @author Lukas Reschke
  12. # @author Jörn Friedrich Dreyer
  13. # @copyright 2012-2015 Thomas Müller thomas.mueller@tmit.eu
  14. #
  15. set -e
  16. #$EXECUTOR_NUMBER is set by Jenkins and allows us to run autotest in parallel
  17. DATABASENAME=oc_autotest$EXECUTOR_NUMBER
  18. DATABASEUSER=oc_autotest$EXECUTOR_NUMBER
  19. DATABASEHOST=localhost
  20. ADMINLOGIN=admin$EXECUTOR_NUMBER
  21. BASEDIR=$PWD
  22. PRIMARY_STORAGE_CONFIGS="local swift"
  23. DBCONFIGS="sqlite mysql mariadb pgsql oci"
  24. # $PHP_EXE is run through 'which' and as such e.g. 'php' or 'hhvm' is usually
  25. # sufficient. Due to the behaviour of 'which', $PHP_EXE may also be a path
  26. # (absolute or not) to an executable, e.g. ./code/projects/php-src/sapi/cli/php.
  27. if [ -z "$PHP_EXE" ]; then
  28. PHP_EXE=php
  29. fi
  30. PHP=$(which "$PHP_EXE")
  31. PHPUNIT=$(which phpunit)
  32. _XDEBUG_CONFIG=$XDEBUG_CONFIG
  33. unset XDEBUG_CONFIG
  34. function print_syntax {
  35. echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
  36. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  37. echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
  38. echo -e "\nExample: ./autotest.sh sqlite lib/template.php" >&2
  39. echo "will run the test suite from \"tests/lib/template.php\"" >&2
  40. echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
  41. }
  42. if [ -x "$PHP" ]; then
  43. echo "Using PHP executable $PHP"
  44. else
  45. echo "Could not find PHP executable $PHP_EXE" >&2
  46. exit 3
  47. fi
  48. if ! [ -x "$PHPUNIT" ]; then
  49. echo "phpunit executable not found, please install phpunit version >= 3.7" >&2
  50. exit 3
  51. fi
  52. # PHPUnit might also be installed via a facade binary script
  53. if [[ "$PHPUNIT" =~ \.phar$ ]]; then
  54. PHPUNIT=( "$PHP" "$PHPUNIT" )
  55. else
  56. PHPUNIT=( "$PHPUNIT" )
  57. fi
  58. PHPUNIT_VERSION=$($PHPUNIT --version | cut -d" " -f2)
  59. PHPUNIT_MAJOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f1)
  60. PHPUNIT_MINOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f2)
  61. if ! [ "$PHPUNIT_MAJOR_VERSION" -gt 3 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 3 -a "$PHPUNIT_MINOR_VERSION" -ge 7 \) ]; then
  62. echo "phpunit version >= 3.7 required. Version found: $PHPUNIT_VERSION" >&2
  63. exit 4
  64. fi
  65. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  66. echo "Please enable write permissions on config and config/config.php" >&2
  67. exit 1
  68. fi
  69. if [ "$1" ]; then
  70. FOUND=0
  71. for DBCONFIG in $DBCONFIGS; do
  72. if [ "$1" = "$DBCONFIG" ]; then
  73. FOUND=1
  74. break
  75. fi
  76. done
  77. if [ $FOUND = 0 ]; then
  78. echo -e "Unknown database config name \"$1\"\n" >&2
  79. print_syntax
  80. exit 2
  81. fi
  82. fi
  83. if [ "$PRIMARY_STORAGE_CONFIG" ]; then
  84. FOUND=0
  85. for PSC in $PRIMARY_STORAGE_CONFIGS; do
  86. if [ "$PRIMARY_STORAGE_CONFIG" = "$PSC" ]; then
  87. FOUND=1
  88. break
  89. fi
  90. done
  91. if [ $FOUND = 0 ]; then
  92. echo -e "Unknown primary storage config name \"$PRIMARY_STORAGE_CONFIG\"\n" >&2
  93. print_syntax
  94. exit 2
  95. fi
  96. else
  97. PRIMARY_STORAGE_CONFIG="local"
  98. fi
  99. # check for the presence of @since in all OCP methods
  100. $PHP build/OCPSinceChecker.php
  101. # Back up existing (dev) config if one exists and backup not already there
  102. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  103. mv config/config.php config/config-autotest-backup.php
  104. fi
  105. function cleanup_config {
  106. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  107. echo "Kill the docker $DOCKER_CONTAINER_ID"
  108. docker rm -f "$DOCKER_CONTAINER_ID"
  109. fi
  110. cd "$BASEDIR"
  111. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  112. echo "Kill the swift docker"
  113. tests/objectstore/stop-swift-ceph.sh
  114. fi
  115. # Restore existing config
  116. if [ -f config/config-autotest-backup.php ]; then
  117. mv config/config-autotest-backup.php config/config.php
  118. fi
  119. # Remove autotest config
  120. if [ -f config/autoconfig.php ]; then
  121. rm config/autoconfig.php
  122. fi
  123. # Remove autotest swift storage config
  124. if [ -f config/autotest-storage-swift.config.php ]; then
  125. rm config/autotest-storage-swift.config.php
  126. fi
  127. }
  128. # restore config on exit
  129. trap cleanup_config EXIT
  130. # use tmpfs for datadir - should speedup unit test execution
  131. if [ -d /dev/shm ]; then
  132. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  133. else
  134. DATADIR=$BASEDIR/data-autotest
  135. fi
  136. echo "Using database $DATABASENAME"
  137. function execute_tests {
  138. DB=$1
  139. echo "Setup environment for $DB testing on $PRIMARY_STORAGE_CONFIG storage ..."
  140. # back to root folder
  141. cd "$BASEDIR"
  142. # revert changes to tests/data
  143. git checkout tests/data
  144. # reset data directory
  145. rm -rf "$DATADIR"
  146. mkdir "$DATADIR"
  147. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  148. tests/objectstore/start-swift-ceph.sh
  149. cp tests/objectstore/swift.config.php config/autotest-storage-swift.config.php
  150. fi
  151. cp tests/preseed-config.php config/config.php
  152. _DB=$DB
  153. # drop database
  154. if [ "$DB" == "mysql" ] ; then
  155. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  156. fi
  157. if [ "$DB" == "mariadb" ] ; then
  158. if [ ! -z "$USEDOCKER" ] ; then
  159. echo "Fire up the mariadb docker"
  160. DOCKER_CONTAINER_ID=$(docker run \
  161. -e MYSQL_ROOT_PASSWORD=owncloud \
  162. -e MYSQL_USER="$DATABASEUSER" \
  163. -e MYSQL_PASSWORD=owncloud \
  164. -e MYSQL_DATABASE="$DATABASENAME" \
  165. -d rullzer/mariadb-owncloud)
  166. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  167. echo "Waiting for MariaDB initialisation ..."
  168. # grep exits on the first match and then the script continues
  169. timeout 30 docker logs -f $DOCKER_CONTAINER_ID 2>&1 | grep -q "mysqld: ready for connections."
  170. echo "MariaDB is up."
  171. else
  172. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  173. echo "Your mysql binary is not provided by MariaDB"
  174. echo "To use the docker container set the USEDOCKER environment variable"
  175. exit -1
  176. fi
  177. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  178. fi
  179. #Reset _DB to mysql since that is what we use internally
  180. _DB="mysql"
  181. fi
  182. if [ "$DB" == "pgsql" ] ; then
  183. if [ ! -z "$USEDOCKER" ] ; then
  184. echo "Fire up the postgres docker"
  185. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  186. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  187. echo "Waiting for Postgres initialisation ..."
  188. # grep exits on the first match and then the script continues
  189. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  190. echo "Postgres is up."
  191. else
  192. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  193. fi
  194. fi
  195. if [ "$DB" == "oci" ] ; then
  196. echo "Fire up the oracle docker"
  197. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  198. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  199. echo "Waiting for Oracle initialization ... "
  200. # Try to connect to the OCI host via sqlplus to ensure that the connection is already running
  201. for i in {1..48}
  202. do
  203. if sqlplus "system/oracle@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$DATABASEHOST)(Port=1521))(CONNECT_DATA=(SID=XE)))" < /dev/null | grep 'Connected to'; then
  204. break;
  205. fi
  206. sleep 5
  207. done
  208. DATABASEUSER=autotest
  209. DATABASENAME='XE'
  210. fi
  211. # trigger installation
  212. echo "Installing ...."
  213. "$PHP" ./occ maintenance:install --database="$_DB" --database-name="$DATABASENAME" --database-host="$DATABASEHOST" --database-user="$DATABASEUSER" --database-pass=owncloud --database-table-prefix=oc_ --admin-user="$ADMINLOGIN" --admin-pass=admin --data-dir="$DATADIR"
  214. #test execution
  215. echo "Testing with $DB ..."
  216. cd tests
  217. rm -rf "coverage-html-$DB"
  218. mkdir "coverage-html-$DB"
  219. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  220. if [[ "$_XDEBUG_CONFIG" ]]; then
  221. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  222. fi
  223. GROUP=''
  224. if [ "$TEST_SELECTION" == "DB" ]; then
  225. GROUP='--group DB'
  226. fi
  227. if [ "$TEST_SELECTION" == "NODB" ]; then
  228. GROUP='--exclude-group DB'
  229. fi
  230. COVER=''
  231. if [ -z "$NOCOVERAGE" ]; then
  232. COVER="--coverage-clover autotest-clover-$DB.xml --coverage-html coverage-html-$DB"
  233. else
  234. echo "No coverage"
  235. fi
  236. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  237. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  238. RESULT=$?
  239. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  240. cd ..
  241. echo "Kill the swift docker"
  242. tests/objectstore/stop-swift-ceph.sh
  243. fi
  244. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  245. echo "Kill the docker $DOCKER_CONTAINER_ID"
  246. docker rm -f $DOCKER_CONTAINER_ID
  247. unset DOCKER_CONTAINER_ID
  248. fi
  249. }
  250. #
  251. # start test execution
  252. #
  253. if [ -z "$1" ]
  254. then
  255. # run all known database configs
  256. for DBCONFIG in $DBCONFIGS; do
  257. execute_tests "$DBCONFIG"
  258. done
  259. else
  260. FILENAME="$2"
  261. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  262. FILENAME="../$FILENAME"
  263. fi
  264. execute_tests "$1" "$FILENAME" "$3"
  265. fi
  266. #
  267. # NOTES on mysql:
  268. # - CREATE DATABASE oc_autotest;
  269. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  270. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  271. #
  272. # - for parallel executor support with EXECUTOR_NUMBER=0:
  273. # - CREATE DATABASE oc_autotest0;
  274. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  275. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  276. #
  277. # NOTES on pgsql:
  278. # - su - postgres
  279. # - createuser -P oc_autotest (enter password and enable superuser)
  280. # - 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):
  281. # local all all trust
  282. #
  283. # - for parallel executor support with EXECUTOR_NUMBER=0:
  284. # - createuser -P oc_autotest0 (enter password and enable superuser)
  285. #
  286. # NOTES on oci:
  287. # - it's a pure nightmare to install Oracle on a Linux-System
  288. # - DON'T TRY THIS AT HOME!
  289. # - if you really need it: we feel sorry for you
  290. #