1
0

autotest.sh 14 KB

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