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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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' 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 >= 6.5" >&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 6 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 6 -a "$PHPUNIT_MINOR_VERSION" -ge 5 \) ]; then
  61. echo "phpunit version >= 6.5 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. # Back up existing (dev) config if one exists and backup not already there
  99. if [ -f config/config.php ] && [ ! -f config/config-autotest-backup.php ]; then
  100. mv config/config.php config/config-autotest-backup.php
  101. fi
  102. function cleanup_config {
  103. if [ ! -z "$DOCKER_CONTAINER_ID" ]; then
  104. echo "Kill the docker $DOCKER_CONTAINER_ID"
  105. docker stop "$DOCKER_CONTAINER_ID"
  106. docker rm -f "$DOCKER_CONTAINER_ID"
  107. fi
  108. cd "$BASEDIR"
  109. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  110. echo "Kill the swift docker"
  111. tests/objectstore/stop-swift-ceph.sh
  112. fi
  113. # Restore existing config
  114. if [ -f config/config-autotest-backup.php ]; then
  115. mv config/config-autotest-backup.php config/config.php
  116. fi
  117. # Remove autotest config
  118. if [ -f config/autoconfig.php ]; then
  119. rm config/autoconfig.php
  120. fi
  121. # Remove autotest swift storage config
  122. if [ -f config/autotest-storage-swift.config.php ]; then
  123. rm config/autotest-storage-swift.config.php
  124. fi
  125. # Remove autotest redis config
  126. if [ -f config/redis.config.php ]; then
  127. rm config/redis.config.php
  128. fi
  129. # Remove mysqlmb4.config.php
  130. rm -f config/mysqlmb4.config.php
  131. }
  132. # restore config on exit
  133. trap cleanup_config EXIT
  134. # use tmpfs for datadir - should speedup unit test execution
  135. if [ -d /dev/shm ]; then
  136. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  137. else
  138. DATADIR=$BASEDIR/data-autotest
  139. fi
  140. echo "Using database $DATABASENAME"
  141. function execute_tests {
  142. DB=$1
  143. echo "Setup environment for $DB testing on $PRIMARY_STORAGE_CONFIG storage ..."
  144. # back to root folder
  145. cd "$BASEDIR"
  146. # revert changes to tests/data
  147. git checkout tests/data
  148. # reset data directory
  149. rm -rf "$DATADIR"
  150. mkdir "$DATADIR"
  151. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  152. tests/objectstore/start-swift-ceph.sh
  153. cp tests/objectstore/swift.config.php config/autotest-storage-swift.config.php
  154. fi
  155. cp tests/preseed-config.php config/config.php
  156. if [ "$ENABLE_REDIS" == "true" ] ; then
  157. cp tests/redis.config.php config/redis.config.php
  158. elif [ "$ENABLE_REDIS_CLUSTER" == "true" ] ; then
  159. cp tests/redis-cluster.config.php config/redis.config.php
  160. fi
  161. _DB=$DB
  162. # drop database
  163. if [ "$DB" == "mysql" ] ; then
  164. if [ ! -z "$USEDOCKER" ] ; then
  165. echo "Fire up the mysql docker"
  166. DOCKER_CONTAINER_ID=$(docker run \
  167. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  168. -e MYSQL_ROOT_PASSWORD=owncloud \
  169. -e MYSQL_USER="$DATABASEUSER" \
  170. -e MYSQL_PASSWORD=owncloud \
  171. -e MYSQL_DATABASE="$DATABASENAME" \
  172. -d mysql)
  173. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  174. else
  175. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  176. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  177. echo "Your mysql binary is not provided by mysql"
  178. echo "To use the docker container set the USEDOCKER environment variable"
  179. exit -1
  180. fi
  181. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  182. else
  183. DATABASEHOST=mysql
  184. fi
  185. fi
  186. echo "Waiting for MySQL initialisation ..."
  187. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 300; then
  188. echo "[ERROR] Waited 300 seconds, no response" >&2
  189. exit 1
  190. fi
  191. fi
  192. if [ "$DB" == "mysqlmb4" ] ; then
  193. if [ ! -z "$USEDOCKER" ] ; then
  194. echo "Fire up the mysql docker"
  195. DOCKER_CONTAINER_ID=$(docker run \
  196. -v $BASEDIR/tests/docker/mysqlmb4:/etc/mysql/conf.d \
  197. -e MYSQL_ROOT_PASSWORD=owncloud \
  198. -e MYSQL_USER="$DATABASEUSER" \
  199. -e MYSQL_PASSWORD=owncloud \
  200. -e MYSQL_DATABASE="$DATABASENAME" \
  201. -d mysql:5.7 \
  202. --innodb_large_prefix=true \
  203. --innodb_file_format=barracuda \
  204. --innodb_file_per_table=true)
  205. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  206. else
  207. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  208. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  209. echo "Your mysql binary is not provided by mysql"
  210. echo "To use the docker container set the USEDOCKER environment variable"
  211. exit -1
  212. fi
  213. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  214. else
  215. DATABASEHOST=mysqlmb4
  216. fi
  217. fi
  218. echo "Waiting for MySQL(utf8mb4) initialisation ..."
  219. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 300; then
  220. echo "[ERROR] Waited 300 seconds, no response" >&2
  221. exit 1
  222. fi
  223. sleep 1
  224. echo "MySQL(utf8mb4) is up."
  225. _DB="mysql"
  226. cp tests/docker/mysqlmb4.config.php config
  227. fi
  228. if [ "$DB" == "mariadb" ] ; then
  229. if [ ! -z "$USEDOCKER" ] ; then
  230. echo "Fire up the mariadb docker"
  231. DOCKER_CONTAINER_ID=$(docker run \
  232. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  233. -e MYSQL_ROOT_PASSWORD=owncloud \
  234. -e MYSQL_USER="$DATABASEUSER" \
  235. -e MYSQL_PASSWORD=owncloud \
  236. -e MYSQL_DATABASE="$DATABASENAME" \
  237. -d mariadb)
  238. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  239. echo "Waiting for MariaDB initialisation ..."
  240. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 300; then
  241. echo "[ERROR] Waited 300 seconds, no response" >&2
  242. exit 1
  243. fi
  244. echo "MariaDB is up."
  245. else
  246. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  247. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  248. echo "Your mysql binary is not provided by MariaDB"
  249. echo "To use the docker container set the USEDOCKER environment variable"
  250. exit -1
  251. fi
  252. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  253. else
  254. DATABASEHOST=mariadb
  255. fi
  256. fi
  257. echo "Waiting for MariaDB initialisation ..."
  258. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 300; then
  259. echo "[ERROR] Waited 300 seconds, no response" >&2
  260. exit 1
  261. fi
  262. #Reset _DB to mysql since that is what we use internally
  263. _DB="mysql"
  264. fi
  265. if [ "$DB" == "pgsql" ] ; then
  266. if [ ! -z "$USEDOCKER" ] ; then
  267. echo "Fire up the postgres docker"
  268. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  269. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  270. echo "Waiting for Postgres initialisation ..."
  271. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 5432 60; then
  272. echo "[ERROR] Waited 60 seconds for $DATABASEHOST, no response" >&2
  273. exit 1
  274. fi
  275. echo "Postgres is up."
  276. else
  277. if [ ! -z "$DRONE" ] ; then
  278. DATABASEHOST="postgres-$POSTGRES"
  279. fi
  280. echo "Waiting for Postgres to be available ..."
  281. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 5432 60; then
  282. echo "[ERROR] Waited 60 seconds for $DATABASEHOST, no response" >&2
  283. exit 1
  284. fi
  285. echo "Give it 10 additional seconds ..."
  286. sleep 10
  287. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  288. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  289. fi
  290. fi
  291. fi
  292. if [ "$DB" == "oci" ] ; then
  293. echo "Fire up the oracle docker"
  294. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  295. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  296. echo "Waiting for Oracle initialization ... "
  297. # Try to connect to the OCI host via sqlplus to ensure that the connection is already running
  298. for i in {1..48}
  299. do
  300. if sqlplus "autotest/owncloud@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$DATABASEHOST)(Port=1521))(CONNECT_DATA=(SID=XE)))" < /dev/null | grep 'Connected to'; then
  301. break;
  302. fi
  303. sleep 5
  304. done
  305. DATABASEUSER=autotest
  306. DATABASENAME='XE'
  307. fi
  308. # trigger installation
  309. echo "Installing ...."
  310. "$PHP" ./occ maintenance:install -vvv --database="$_DB" --database-name="$DATABASENAME" --database-host="$DATABASEHOST" --database-user="$DATABASEUSER" --database-pass=owncloud --admin-user="$ADMINLOGIN" --admin-pass=admin --data-dir="$DATADIR"
  311. #test execution
  312. echo "Testing with $DB ..."
  313. cd tests
  314. rm -rf "coverage-html-$DB"
  315. mkdir "coverage-html-$DB"
  316. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  317. if [[ "$_XDEBUG_CONFIG" ]]; then
  318. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  319. fi
  320. GROUP=''
  321. if [ "$TEST_SELECTION" == "QUICKDB" ]; then
  322. GROUP='--group DB --exclude-group=SLOWDB'
  323. fi
  324. if [ "$TEST_SELECTION" == "DB" ]; then
  325. GROUP='--group DB,SLOWDB'
  326. fi
  327. if [ "$TEST_SELECTION" == "NODB" ]; then
  328. GROUP='--exclude-group DB,SLOWDB'
  329. fi
  330. if [ "$TEST_SELECTION" == "PRIMARY-s3" ]; then
  331. GROUP='--group PRIMARY-s3'
  332. fi
  333. if [ "$TEST_SELECTION" == "PRIMARY-azure" ]; then
  334. GROUP='--group PRIMARY-azure'
  335. fi
  336. if [ "$TEST_SELECTION" == "PRIMARY-swift" ]; then
  337. GROUP='--group PRIMARY-swift'
  338. fi
  339. COVER=''
  340. if [ -z "$NOCOVERAGE" ]; then
  341. COVER="--coverage-clover autotest-clover-$DB.xml --coverage-html coverage-html-$DB"
  342. else
  343. echo "No coverage"
  344. fi
  345. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  346. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  347. RESULT=$?
  348. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  349. cd ..
  350. echo "Kill the swift docker"
  351. tests/objectstore/stop-swift-ceph.sh
  352. fi
  353. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  354. echo "Kill the docker $DOCKER_CONTAINER_ID"
  355. docker stop $DOCKER_CONTAINER_ID
  356. docker rm -f $DOCKER_CONTAINER_ID
  357. unset DOCKER_CONTAINER_ID
  358. fi
  359. }
  360. #
  361. # start test execution
  362. #
  363. if [ -z "$1" ]
  364. then
  365. # run all known database configs
  366. for DBCONFIG in $DBCONFIGS; do
  367. execute_tests "$DBCONFIG"
  368. done
  369. else
  370. FILENAME="$2"
  371. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
  372. FILENAME="../$FILENAME"
  373. fi
  374. execute_tests "$1" "$FILENAME" "$3"
  375. fi
  376. #
  377. # NOTES on mysql:
  378. # - CREATE DATABASE oc_autotest;
  379. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  380. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  381. #
  382. # - for parallel executor support with EXECUTOR_NUMBER=0:
  383. # - CREATE DATABASE oc_autotest0;
  384. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  385. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  386. #
  387. # NOTES on pgsql:
  388. # - su - postgres
  389. # - createuser -P oc_autotest (enter password and enable superuser)
  390. # - to enable dropdb I decided to add following line to pg_hba.conf
  391. # (this is not the safest way but I don't care for the testing machine):
  392. # local all all trust
  393. #
  394. # - for parallel executor support with EXECUTOR_NUMBER=0:
  395. # - createuser -P oc_autotest0 (enter password and enable superuser)
  396. #
  397. # NOTES on oci:
  398. # - it's a pure nightmare to install Oracle on a Linux-System
  399. # - DON'T TRY THIS AT HOME!
  400. # - if you really need it: we feel sorry for you
  401. #