1
0

autotest.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. DATABASENAME=oc_autotest
  16. DATABASEUSER=oc_autotest
  17. DATABASEHOST=localhost
  18. ADMINLOGIN=admin
  19. BASEDIR=$PWD
  20. PRIMARY_STORAGE_CONFIGS="local swift"
  21. DBCONFIGS="sqlite mysql mariadb pgsql oci mysqlmb4"
  22. # $PHP_EXE is run through 'which' and as such e.g. 'php' or 'hhvm' is usually
  23. # sufficient. Due to the behaviour of 'which', $PHP_EXE may also be a path
  24. # (absolute or not) to an executable, e.g. ./code/projects/php-src/sapi/cli/php.
  25. if [ -z "$PHP_EXE" ]; then
  26. PHP_EXE=php
  27. fi
  28. PHP=$(which "$PHP_EXE")
  29. PHPUNIT=$(which phpunit)
  30. set -e
  31. _XDEBUG_CONFIG=$XDEBUG_CONFIG
  32. unset XDEBUG_CONFIG
  33. function print_syntax {
  34. echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
  35. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  36. echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
  37. echo -e "\nExample: ./autotest.sh sqlite lib/template.php" >&2
  38. echo "will run the test suite from \"tests/lib/template.php\"" >&2
  39. echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
  40. }
  41. if [ -x "$PHP" ]; then
  42. echo "Using PHP executable $PHP"
  43. else
  44. echo "Could not find PHP executable $PHP_EXE" >&2
  45. exit 3
  46. fi
  47. if ! [ -x "$PHPUNIT" ]; then
  48. echo "phpunit executable not found, please install phpunit version >= 4.8" >&2
  49. exit 3
  50. fi
  51. # PHPUnit might also be installed via a facade binary script
  52. if [[ "$PHPUNIT" =~ \.phar$ ]]; then
  53. PHPUNIT=( "$PHP" "$PHPUNIT" )
  54. else
  55. PHPUNIT=( "$PHPUNIT" )
  56. fi
  57. PHPUNIT_VERSION=$($PHPUNIT --version | cut -d" " -f2)
  58. PHPUNIT_MAJOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f1)
  59. PHPUNIT_MINOR_VERSION=$(echo "$PHPUNIT_VERSION" | cut -d"." -f2)
  60. if ! [ "$PHPUNIT_MAJOR_VERSION" -gt 4 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 4 -a "$PHPUNIT_MINOR_VERSION" -ge 8 \) ]; then
  61. echo "phpunit version >= 4.8 required. Version found: $PHPUNIT_VERSION" >&2
  62. exit 4
  63. fi
  64. if ! [ \( -w config -a ! -f config/config.php \) -o \( -f config/config.php -a -w config/config.php \) ]; then
  65. echo "Please enable write permissions on config and config/config.php" >&2
  66. exit 1
  67. fi
  68. if [ "$1" ]; then
  69. FOUND=0
  70. for DBCONFIG in $DBCONFIGS; do
  71. if [ "$1" = "$DBCONFIG" ]; then
  72. FOUND=1
  73. break
  74. fi
  75. done
  76. if [ $FOUND = 0 ]; then
  77. echo -e "Unknown database config name \"$1\"\n" >&2
  78. print_syntax
  79. exit 2
  80. fi
  81. fi
  82. if [ "$PRIMARY_STORAGE_CONFIG" ]; then
  83. FOUND=0
  84. for PSC in $PRIMARY_STORAGE_CONFIGS; do
  85. if [ "$PRIMARY_STORAGE_CONFIG" = "$PSC" ]; then
  86. FOUND=1
  87. break
  88. fi
  89. done
  90. if [ $FOUND = 0 ]; then
  91. echo -e "Unknown primary storage config name \"$PRIMARY_STORAGE_CONFIG\"\n" >&2
  92. print_syntax
  93. exit 2
  94. fi
  95. else
  96. PRIMARY_STORAGE_CONFIG="local"
  97. fi
  98. # check for the presence of @since in all OCP methods
  99. $PHP build/OCPSinceChecker.php
  100. # Back up existing (dev) config if one exists and backup not already there
  101. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  102. mv config/config.php config/config-autotest-backup.php
  103. fi
  104. function cleanup_config {
  105. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  106. echo "Kill the docker $DOCKER_CONTAINER_ID"
  107. docker stop "$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. # Remove autotest redis config
  128. if [ -f config/redis.config.php ]; then
  129. rm config/redis.config.php
  130. fi
  131. # Remove mysqlmb4.config.php
  132. rm -f config/mysqlmb4.config.php
  133. }
  134. # restore config on exit
  135. trap cleanup_config EXIT
  136. # use tmpfs for datadir - should speedup unit test execution
  137. if [ -d /dev/shm ]; then
  138. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  139. else
  140. DATADIR=$BASEDIR/data-autotest
  141. fi
  142. echo "Using database $DATABASENAME"
  143. function execute_tests {
  144. DB=$1
  145. echo "Setup environment for $DB testing on $PRIMARY_STORAGE_CONFIG storage ..."
  146. # back to root folder
  147. cd "$BASEDIR"
  148. # revert changes to tests/data
  149. git checkout tests/data
  150. # reset data directory
  151. rm -rf "$DATADIR"
  152. mkdir "$DATADIR"
  153. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  154. tests/objectstore/start-swift-ceph.sh
  155. cp tests/objectstore/swift.config.php config/autotest-storage-swift.config.php
  156. fi
  157. cp tests/preseed-config.php config/config.php
  158. if [ "$ENABLE_REDIS" == "true" ] ; then
  159. cp tests/redis.config.php config/redis.config.php
  160. elif [ "$ENABLE_REDIS_CLUSTER" == "true" ] ; then
  161. cp tests/redis-cluster.config.php config/redis.config.php
  162. fi
  163. _DB=$DB
  164. # drop database
  165. if [ "$DB" == "mysql" ] ; then
  166. if [ ! -z "$USEDOCKER" ] ; then
  167. echo "Fire up the mysql docker"
  168. DOCKER_CONTAINER_ID=$(docker run \
  169. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  170. -e MYSQL_ROOT_PASSWORD=owncloud \
  171. -e MYSQL_USER="$DATABASEUSER" \
  172. -e MYSQL_PASSWORD=owncloud \
  173. -e MYSQL_DATABASE="$DATABASENAME" \
  174. -d mysql)
  175. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  176. else
  177. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  178. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  179. echo "Your mysql binary is not provided by mysql"
  180. echo "To use the docker container set the USEDOCKER environment variable"
  181. exit -1
  182. fi
  183. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  184. else
  185. DATABASEHOST=mysql
  186. fi
  187. fi
  188. echo "Waiting for MySQL initialisation ..."
  189. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  190. echo "[ERROR] Waited 600 seconds, no response" >&2
  191. exit 1
  192. fi
  193. fi
  194. if [ "$DB" == "mysqlmb4" ] ; then
  195. if [ ! -z "$USEDOCKER" ] ; then
  196. echo "Fire up the mysql docker"
  197. DOCKER_CONTAINER_ID=$(docker run \
  198. -v $BASEDIR/tests/docker/mysqlmb4:/etc/mysql/conf.d \
  199. -e MYSQL_ROOT_PASSWORD=owncloud \
  200. -e MYSQL_USER="$DATABASEUSER" \
  201. -e MYSQL_PASSWORD=owncloud \
  202. -e MYSQL_DATABASE="$DATABASENAME" \
  203. -d mysql:5.7 \
  204. --innodb_large_prefix=true \
  205. --innodb_file_format=barracuda \
  206. --innodb_file_per_table=true)
  207. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  208. else
  209. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  210. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  211. echo "Your mysql binary is not provided by mysql"
  212. echo "To use the docker container set the USEDOCKER environment variable"
  213. exit -1
  214. fi
  215. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  216. else
  217. DATABASEHOST=mysqlmb4
  218. fi
  219. fi
  220. echo "Waiting for MySQL(utf8mb4) initialisation ..."
  221. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  222. echo "[ERROR] Waited 600 seconds, no response" >&2
  223. exit 1
  224. fi
  225. sleep 1
  226. echo "MySQL(utf8mb4) is up."
  227. _DB="mysql"
  228. cp tests/docker/mysqlmb4.config.php config
  229. fi
  230. if [ "$DB" == "mariadb" ] ; then
  231. if [ ! -z "$USEDOCKER" ] ; then
  232. echo "Fire up the mariadb docker"
  233. DOCKER_CONTAINER_ID=$(docker run \
  234. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  235. -e MYSQL_ROOT_PASSWORD=owncloud \
  236. -e MYSQL_USER="$DATABASEUSER" \
  237. -e MYSQL_PASSWORD=owncloud \
  238. -e MYSQL_DATABASE="$DATABASENAME" \
  239. -d mariadb)
  240. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  241. echo "Waiting for MariaDB initialisation ..."
  242. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  243. echo "[ERROR] Waited 600 seconds, no response" >&2
  244. exit 1
  245. fi
  246. echo "MariaDB is up."
  247. else
  248. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  249. echo "Your mysql binary is not provided by MariaDB"
  250. echo "To use the docker container set the USEDOCKER environment variable"
  251. exit -1
  252. fi
  253. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  254. fi
  255. #Reset _DB to mysql since that is what we use internally
  256. _DB="mysql"
  257. fi
  258. if [ "$DB" == "pgsql" ] ; then
  259. if [ ! -z "$USEDOCKER" ] ; then
  260. echo "Fire up the postgres docker"
  261. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  262. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  263. echo "Waiting for Postgres initialisation ..."
  264. # grep exits on the first match and then the script continues
  265. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  266. echo "Postgres is up."
  267. else
  268. if [ ! -z "$DRONE" ] ; then
  269. DATABASEHOST="postgres-$POSTGRES"
  270. fi
  271. echo "Waiting for Postgres to be available ..."
  272. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 5432 60; then
  273. echo "[ERROR] Waited 60 seconds for $DATABASEHOST, no response" >&2
  274. exit 1
  275. fi
  276. echo "Give it 10 additional seconds ..."
  277. sleep 10
  278. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  279. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  280. fi
  281. fi
  282. fi
  283. if [ "$DB" == "oci" ] ; then
  284. echo "Fire up the oracle docker"
  285. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  286. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  287. echo "Waiting for Oracle initialization ... "
  288. # Try to connect to the OCI host via sqlplus to ensure that the connection is already running
  289. for i in {1..48}
  290. do
  291. if sqlplus "autotest/owncloud@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$DATABASEHOST)(Port=1521))(CONNECT_DATA=(SID=XE)))" < /dev/null | grep 'Connected to'; then
  292. break;
  293. fi
  294. sleep 5
  295. done
  296. DATABASEUSER=autotest
  297. DATABASENAME='XE'
  298. fi
  299. # trigger installation
  300. echo "Installing ...."
  301. "$PHP" ./occ maintenance:install -vvv --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"
  302. #test execution
  303. echo "Testing with $DB ..."
  304. cd tests
  305. rm -rf "coverage-html-$DB"
  306. mkdir "coverage-html-$DB"
  307. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  308. if [[ "$_XDEBUG_CONFIG" ]]; then
  309. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  310. fi
  311. GROUP=''
  312. if [ "$TEST_SELECTION" == "QUICKDB" ]; then
  313. GROUP='--group DB --exclude-group=SLOWDB'
  314. fi
  315. if [ "$TEST_SELECTION" == "DB" ]; then
  316. GROUP='--group DB,SLOWDB'
  317. fi
  318. if [ "$TEST_SELECTION" == "NODB" ]; then
  319. GROUP='--exclude-group DB,SLOWDB'
  320. fi
  321. if [ "$TEST_SELECTION" == "PRIMARY-s3" ]; then
  322. GROUP='--group PRIMARY-s3'
  323. fi
  324. COVER=''
  325. if [ -z "$NOCOVERAGE" ]; then
  326. COVER="--coverage-clover autotest-clover-$DB.xml --coverage-html coverage-html-$DB"
  327. else
  328. echo "No coverage"
  329. fi
  330. if [ -d "$2" ]; then
  331. for f in $(find "$2" -name '*Test.php'); do
  332. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" / "$f" "$3"
  333. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$f" "$3"
  334. RESULT=$?
  335. done;
  336. else
  337. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  338. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  339. RESULT=$?
  340. fi
  341. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  342. cd ..
  343. echo "Kill the swift docker"
  344. tests/objectstore/stop-swift-ceph.sh
  345. fi
  346. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  347. echo "Kill the docker $DOCKER_CONTAINER_ID"
  348. docker stop $DOCKER_CONTAINER_ID
  349. docker rm -f $DOCKER_CONTAINER_ID
  350. unset DOCKER_CONTAINER_ID
  351. fi
  352. }
  353. #
  354. # start test execution
  355. #
  356. if [ -z "$1" ]
  357. then
  358. # run all known database configs
  359. for DBCONFIG in $DBCONFIGS; do
  360. execute_tests "$DBCONFIG"
  361. done
  362. else
  363. FILENAME="$2"
  364. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
  365. FILENAME="../$FILENAME"
  366. fi
  367. execute_tests "$1" "$FILENAME" "$3"
  368. fi
  369. #
  370. # NOTES on mysql:
  371. # - CREATE DATABASE oc_autotest;
  372. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  373. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  374. #
  375. # - for parallel executor support with EXECUTOR_NUMBER=0:
  376. # - CREATE DATABASE oc_autotest0;
  377. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  378. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  379. #
  380. # NOTES on pgsql:
  381. # - su - postgres
  382. # - createuser -P oc_autotest (enter password and enable superuser)
  383. # - to enable dropdb I decided to add following line to pg_hba.conf
  384. # (this is not the safest way but I don't care for the testing machine):
  385. # local all all trust
  386. #
  387. # - for parallel executor support with EXECUTOR_NUMBER=0:
  388. # - createuser -P oc_autotest0 (enter password and enable superuser)
  389. #
  390. # NOTES on oci:
  391. # - it's a pure nightmare to install Oracle on a Linux-System
  392. # - DON'T TRY THIS AT HOME!
  393. # - if you really need it: we feel sorry for you
  394. #