You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autotest.sh 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #$EXECUTOR_NUMBER is set by Jenkins and allows us to run autotest in parallel
  16. DATABASENAME=oc_autotest$EXECUTOR_NUMBER
  17. DATABASEUSER=oc_autotest$EXECUTOR_NUMBER
  18. DATABASEHOST=localhost
  19. ADMINLOGIN=admin$EXECUTOR_NUMBER
  20. BASEDIR=$PWD
  21. PRIMARY_STORAGE_CONFIGS="local swift"
  22. DBCONFIGS="sqlite mysql mariadb pgsql oci mysqlmb4"
  23. # $PHP_EXE is run through 'which' and as such e.g. 'php' or 'hhvm' is usually
  24. # sufficient. Due to the behaviour of 'which', $PHP_EXE may also be a path
  25. # (absolute or not) to an executable, e.g. ./code/projects/php-src/sapi/cli/php.
  26. if [ -z "$PHP_EXE" ]; then
  27. PHP_EXE=php
  28. fi
  29. PHP=$(which "$PHP_EXE")
  30. PHPUNIT=$(which phpunit)
  31. set -e
  32. _XDEBUG_CONFIG=$XDEBUG_CONFIG
  33. unset XDEBUG_CONFIG
  34. function print_syntax {
  35. echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
  36. echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
  37. echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
  38. echo -e "\nExample: ./autotest.sh sqlite lib/template.php" >&2
  39. echo "will run the test suite from \"tests/lib/template.php\"" >&2
  40. echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
  41. }
  42. if [ -x "$PHP" ]; then
  43. echo "Using PHP executable $PHP"
  44. else
  45. echo "Could not find PHP executable $PHP_EXE" >&2
  46. exit 3
  47. fi
  48. if ! [ -x "$PHPUNIT" ]; then
  49. echo "phpunit executable not found, please install phpunit version >= 4.8" >&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 4 -o \( "$PHPUNIT_MAJOR_VERSION" -eq 4 -a "$PHPUNIT_MINOR_VERSION" -ge 8 \) ]; then
  62. echo "phpunit version >= 4.8 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. # check for the presence of @since in all OCP methods
  100. $PHP build/OCPSinceChecker.php
  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 mysqlmb4.config.php
  129. rm -f config/mysqlmb4.config.php
  130. }
  131. # restore config on exit
  132. trap cleanup_config EXIT
  133. # use tmpfs for datadir - should speedup unit test execution
  134. if [ -d /dev/shm ]; then
  135. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  136. else
  137. DATADIR=$BASEDIR/data-autotest
  138. fi
  139. echo "Using database $DATABASENAME"
  140. function execute_tests {
  141. DB=$1
  142. echo "Setup environment for $DB testing on $PRIMARY_STORAGE_CONFIG storage ..."
  143. # back to root folder
  144. cd "$BASEDIR"
  145. # revert changes to tests/data
  146. git checkout tests/data
  147. # reset data directory
  148. rm -rf "$DATADIR"
  149. mkdir "$DATADIR"
  150. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  151. tests/objectstore/start-swift-ceph.sh
  152. cp tests/objectstore/swift.config.php config/autotest-storage-swift.config.php
  153. fi
  154. cp tests/preseed-config.php config/config.php
  155. _DB=$DB
  156. # drop database
  157. if [ "$DB" == "mysql" ] ; then
  158. if [ ! -z "$USEDOCKER" ] ; then
  159. echo "Fire up the mysql docker"
  160. DOCKER_CONTAINER_ID=$(docker run \
  161. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  162. -e MYSQL_ROOT_PASSWORD=owncloud \
  163. -e MYSQL_USER="$DATABASEUSER" \
  164. -e MYSQL_PASSWORD=owncloud \
  165. -e MYSQL_DATABASE="$DATABASENAME" \
  166. -d mysql)
  167. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  168. else
  169. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  170. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  171. echo "Your mysql binary is not provided by mysql"
  172. echo "To use the docker container set the USEDOCKER environment variable"
  173. exit -1
  174. fi
  175. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  176. else
  177. DATABASEHOST=127.0.0.1
  178. fi
  179. fi
  180. echo "Waiting for MySQL initialisation ..."
  181. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  182. echo "[ERROR] Waited 600 seconds, no response" >&2
  183. exit 1
  184. fi
  185. fi
  186. if [ "$DB" == "mysqlmb4" ] ; then
  187. if [ ! -z "$USEDOCKER" ] ; then
  188. echo "Fire up the mysql docker"
  189. DOCKER_CONTAINER_ID=$(docker run \
  190. -v $BASEDIR/tests/docker/mysqlmb4:/etc/mysql/conf.d \
  191. -e MYSQL_ROOT_PASSWORD=owncloud \
  192. -e MYSQL_USER="$DATABASEUSER" \
  193. -e MYSQL_PASSWORD=owncloud \
  194. -e MYSQL_DATABASE="$DATABASENAME" \
  195. -d mysql:5.7
  196. --innodb_large_prefix=true
  197. --innodb_file_format=barracuda
  198. --innodb_file_per_table=true)
  199. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  200. else
  201. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  202. if [ "mysql" != "$(mysql --version | grep -o mysql)" ] ; then
  203. echo "Your mysql binary is not provided by mysql"
  204. echo "To use the docker container set the USEDOCKER environment variable"
  205. exit -1
  206. fi
  207. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  208. else
  209. DATABASEHOST=127.0.0.1
  210. fi
  211. fi
  212. echo "Waiting for MySQL(utf8mb4) initialisation ..."
  213. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  214. echo "[ERROR] Waited 600 seconds, no response" >&2
  215. exit 1
  216. fi
  217. sleep 1
  218. echo "MySQL(utf8mb4) is up."
  219. _DB="mysql"
  220. cp tests/docker/mysqlmb4.config.php config
  221. fi
  222. if [ "$DB" == "mariadb" ] ; then
  223. if [ ! -z "$USEDOCKER" ] ; then
  224. echo "Fire up the mariadb docker"
  225. DOCKER_CONTAINER_ID=$(docker run \
  226. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  227. -e MYSQL_ROOT_PASSWORD=owncloud \
  228. -e MYSQL_USER="$DATABASEUSER" \
  229. -e MYSQL_PASSWORD=owncloud \
  230. -e MYSQL_DATABASE="$DATABASENAME" \
  231. -d mariadb)
  232. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  233. echo "Waiting for MariaDB initialisation ..."
  234. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 600; then
  235. echo "[ERROR] Waited 600 seconds, no response" >&2
  236. exit 1
  237. fi
  238. echo "MariaDB is up."
  239. else
  240. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  241. echo "Your mysql binary is not provided by MariaDB"
  242. echo "To use the docker container set the USEDOCKER environment variable"
  243. exit -1
  244. fi
  245. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  246. fi
  247. #Reset _DB to mysql since that is what we use internally
  248. _DB="mysql"
  249. fi
  250. if [ "$DB" == "pgsql" ] ; then
  251. if [ ! -z "$USEDOCKER" ] ; then
  252. echo "Fire up the postgres docker"
  253. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  254. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  255. echo "Waiting for Postgres initialisation ..."
  256. # grep exits on the first match and then the script continues
  257. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  258. echo "Postgres is up."
  259. else
  260. if [ -z "$DRONE" ] ; then # no need to drop the DB when we are on CI
  261. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  262. fi
  263. fi
  264. fi
  265. if [ "$DB" == "oci" ] ; then
  266. echo "Fire up the oracle docker"
  267. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  268. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  269. echo "Waiting for Oracle initialization ... "
  270. # Try to connect to the OCI host via sqlplus to ensure that the connection is already running
  271. for i in {1..48}
  272. do
  273. if sqlplus "autotest/owncloud@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$DATABASEHOST)(Port=1521))(CONNECT_DATA=(SID=XE)))" < /dev/null | grep 'Connected to'; then
  274. break;
  275. fi
  276. sleep 5
  277. done
  278. DATABASEUSER=autotest
  279. DATABASENAME='XE'
  280. fi
  281. # trigger installation
  282. echo "Installing ...."
  283. "$PHP" ./occ maintenance:install -vvv --database="$_DB" --database-name="$DATABASENAME" --database-host="$DATABASEHOST" --database-user="$DATABASEUSER" --database-pass=owncloud --database-table-prefix=oc_ --admin-user="$ADMINLOGIN" --admin-pass=admin --data-dir="$DATADIR"
  284. #test execution
  285. echo "Testing with $DB ..."
  286. cd tests
  287. rm -rf "coverage-html-$DB"
  288. mkdir "coverage-html-$DB"
  289. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  290. if [[ "$_XDEBUG_CONFIG" ]]; then
  291. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  292. fi
  293. GROUP=''
  294. if [ "$TEST_SELECTION" == "QUICKDB" ]; then
  295. GROUP='--group DB --exclude-group=SLOWDB'
  296. fi
  297. if [ "$TEST_SELECTION" == "DB" ]; then
  298. GROUP='--group DB,SLOWDB'
  299. fi
  300. if [ "$TEST_SELECTION" == "NODB" ]; then
  301. GROUP='--exclude-group DB,SLOWDB'
  302. fi
  303. if [ "$TEST_SELECTION" == "PRIMARY-s3" ]; then
  304. GROUP='--group PRIMARY-s3'
  305. fi
  306. COVER=''
  307. if [ -z "$NOCOVERAGE" ]; then
  308. COVER="--coverage-clover autotest-clover-$DB.xml --coverage-html coverage-html-$DB"
  309. else
  310. echo "No coverage"
  311. fi
  312. if [ -d "$2" ]; then
  313. for f in $(find "$2" -name '*.php'); do
  314. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" / "$f" "$3"
  315. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$f" "$3"
  316. RESULT=$?
  317. done;
  318. else
  319. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  320. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  321. RESULT=$?
  322. fi
  323. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  324. cd ..
  325. echo "Kill the swift docker"
  326. tests/objectstore/stop-swift-ceph.sh
  327. fi
  328. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  329. echo "Kill the docker $DOCKER_CONTAINER_ID"
  330. docker stop $DOCKER_CONTAINER_ID
  331. docker rm -f $DOCKER_CONTAINER_ID
  332. unset DOCKER_CONTAINER_ID
  333. fi
  334. }
  335. #
  336. # start test execution
  337. #
  338. if [ -z "$1" ]
  339. then
  340. # run all known database configs
  341. for DBCONFIG in $DBCONFIGS; do
  342. execute_tests "$DBCONFIG"
  343. done
  344. else
  345. FILENAME="$2"
  346. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
  347. FILENAME="../$FILENAME"
  348. fi
  349. execute_tests "$1" "$FILENAME" "$3"
  350. fi
  351. #
  352. # NOTES on mysql:
  353. # - CREATE DATABASE oc_autotest;
  354. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  355. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  356. #
  357. # - for parallel executor support with EXECUTOR_NUMBER=0:
  358. # - CREATE DATABASE oc_autotest0;
  359. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  360. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  361. #
  362. # NOTES on pgsql:
  363. # - su - postgres
  364. # - createuser -P oc_autotest (enter password and enable superuser)
  365. # - to enable dropdb I decided to add following line to pg_hba.conf (this is not the safest way but I don't care for the testing machine):
  366. # local all all trust
  367. #
  368. # - for parallel executor support with EXECUTOR_NUMBER=0:
  369. # - createuser -P oc_autotest0 (enter password and enable superuser)
  370. #
  371. # NOTES on oci:
  372. # - it's a pure nightmare to install Oracle on a Linux-System
  373. # - DON'T TRY THIS AT HOME!
  374. # - if you really need it: we feel sorry for you
  375. #