autotest.sh 14 KB

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