autotest.sh 14 KB

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