autotest.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. # 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 300; then
  190. echo "[ERROR] Waited 300 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 300; then
  222. echo "[ERROR] Waited 300 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 300; then
  243. echo "[ERROR] Waited 300 seconds, no response" >&2
  244. exit 1
  245. fi
  246. echo "MariaDB is up."
  247. else
  248. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  249. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  250. echo "Your mysql binary is not provided by MariaDB"
  251. echo "To use the docker container set the USEDOCKER environment variable"
  252. exit -1
  253. fi
  254. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  255. else
  256. DATABASEHOST=mariadb
  257. fi
  258. fi
  259. echo "Waiting for MariaDB initialisation ..."
  260. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 300; then
  261. echo "[ERROR] Waited 300 seconds, no response" >&2
  262. exit 1
  263. fi
  264. #Reset _DB to mysql since that is what we use internally
  265. _DB="mysql"
  266. fi
  267. if [ "$DB" == "pgsql" ] ; then
  268. if [ ! -z "$USEDOCKER" ] ; then
  269. echo "Fire up the postgres docker"
  270. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  271. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  272. echo "Waiting for Postgres initialisation ..."
  273. # grep exits on the first match and then the script continues
  274. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  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. #