1
0

autotest.sh 14 KB

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