PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/autotest.sh

https://gitlab.com/wuhang2003/core
Shell | 339 lines | 231 code | 44 blank | 64 comment | 63 complexity | 272271095864a2862cda06a29d3018fe MD5 | raw file
  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"
  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 >= 3.7" >&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 4 \) ]; then
  62. echo "phpunit version >= 4.4 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. }
  129. # restore config on exit
  130. trap cleanup_config EXIT
  131. # use tmpfs for datadir - should speedup unit test execution
  132. if [ -d /dev/shm ]; then
  133. DATADIR=/dev/shm/data-autotest$EXECUTOR_NUMBER
  134. else
  135. DATADIR=$BASEDIR/data-autotest
  136. fi
  137. echo "Using database $DATABASENAME"
  138. function execute_tests {
  139. DB=$1
  140. echo "Setup environment for $DB testing on $PRIMARY_STORAGE_CONFIG storage ..."
  141. # back to root folder
  142. cd "$BASEDIR"
  143. # revert changes to tests/data
  144. git checkout tests/data
  145. # reset data directory
  146. rm -rf "$DATADIR"
  147. mkdir "$DATADIR"
  148. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  149. tests/objectstore/start-swift-ceph.sh
  150. cp tests/objectstore/swift.config.php config/autotest-storage-swift.config.php
  151. fi
  152. cp tests/preseed-config.php config/config.php
  153. _DB=$DB
  154. # drop database
  155. if [ "$DB" == "mysql" ] ; then
  156. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  157. fi
  158. if [ "$DB" == "mariadb" ] ; then
  159. if [ ! -z "$USEDOCKER" ] ; then
  160. echo "Fire up the mariadb docker"
  161. DOCKER_CONTAINER_ID=$(docker run \
  162. -v $BASEDIR/tests/docker/mariadb:/etc/mysql/conf.d \
  163. -e MYSQL_ROOT_PASSWORD=owncloud \
  164. -e MYSQL_USER="$DATABASEUSER" \
  165. -e MYSQL_PASSWORD=owncloud \
  166. -e MYSQL_DATABASE="$DATABASENAME" \
  167. -d mariadb)
  168. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  169. echo "Waiting for MariaDB initialisation ..."
  170. if ! apps/files_external/tests/env/wait-for-connection $DATABASEHOST 3306 60; then
  171. echo "[ERROR] Waited 60 seconds, no response" >&2
  172. exit 1
  173. fi
  174. echo "MariaDB is up."
  175. else
  176. if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ] ; then
  177. echo "Your mysql binary is not provided by MariaDB"
  178. echo "To use the docker container set the USEDOCKER environment variable"
  179. exit -1
  180. fi
  181. mysql -u "$DATABASEUSER" -powncloud -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
  182. fi
  183. #Reset _DB to mysql since that is what we use internally
  184. _DB="mysql"
  185. fi
  186. if [ "$DB" == "pgsql" ] ; then
  187. if [ ! -z "$USEDOCKER" ] ; then
  188. echo "Fire up the postgres docker"
  189. DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
  190. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  191. echo "Waiting for Postgres initialisation ..."
  192. # grep exits on the first match and then the script continues
  193. docker logs -f "$DOCKER_CONTAINER_ID" 2>&1 | grep -q "database system is ready to accept connections"
  194. echo "Postgres is up."
  195. else
  196. dropdb -U "$DATABASEUSER" "$DATABASENAME" || true
  197. fi
  198. fi
  199. if [ "$DB" == "oci" ] ; then
  200. echo "Fire up the oracle docker"
  201. DOCKER_CONTAINER_ID=$(docker run -d deepdiver/docker-oracle-xe-11g)
  202. DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
  203. echo "Waiting for Oracle initialization ... "
  204. # Try to connect to the OCI host via sqlplus to ensure that the connection is already running
  205. for i in {1..48}
  206. do
  207. if sqlplus "system/oracle@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=$DATABASEHOST)(Port=1521))(CONNECT_DATA=(SID=XE)))" < /dev/null | grep 'Connected to'; then
  208. break;
  209. fi
  210. sleep 5
  211. done
  212. DATABASEUSER=autotest
  213. DATABASENAME='XE'
  214. fi
  215. # trigger installation
  216. echo "Installing ...."
  217. "$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"
  218. #test execution
  219. echo "Testing with $DB ..."
  220. cd tests
  221. rm -rf "coverage-html-$DB"
  222. mkdir "coverage-html-$DB"
  223. "$PHP" -f enable_all.php | grep -i -C9999 error && echo "Error during setup" && exit 101
  224. if [[ "$_XDEBUG_CONFIG" ]]; then
  225. export XDEBUG_CONFIG=$_XDEBUG_CONFIG
  226. fi
  227. GROUP=''
  228. if [ "$TEST_SELECTION" == "DB" ]; then
  229. GROUP='--group DB'
  230. fi
  231. if [ "$TEST_SELECTION" == "NODB" ]; then
  232. GROUP='--exclude-group DB'
  233. fi
  234. COVER=''
  235. if [ -z "$NOCOVERAGE" ]; then
  236. COVER="--coverage-clover autotest-clover-$DB.xml --coverage-html coverage-html-$DB"
  237. else
  238. echo "No coverage"
  239. fi
  240. echo "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  241. "${PHPUNIT[@]}" --configuration phpunit-autotest.xml $GROUP $COVER --log-junit "autotest-results-$DB.xml" "$2" "$3"
  242. RESULT=$?
  243. if [ "$PRIMARY_STORAGE_CONFIG" == "swift" ] ; then
  244. cd ..
  245. echo "Kill the swift docker"
  246. tests/objectstore/stop-swift-ceph.sh
  247. fi
  248. if [ ! -z "$DOCKER_CONTAINER_ID" ] ; then
  249. echo "Kill the docker $DOCKER_CONTAINER_ID"
  250. docker stop $DOCKER_CONTAINER_ID
  251. docker rm -f $DOCKER_CONTAINER_ID
  252. unset DOCKER_CONTAINER_ID
  253. fi
  254. }
  255. #
  256. # start test execution
  257. #
  258. if [ -z "$1" ]
  259. then
  260. # run all known database configs
  261. for DBCONFIG in $DBCONFIGS; do
  262. execute_tests "$DBCONFIG"
  263. done
  264. else
  265. FILENAME="$2"
  266. if [ ! -z "$2" ] && [ ! -f "tests/$FILENAME" ]; then
  267. FILENAME="../$FILENAME"
  268. fi
  269. execute_tests "$1" "$FILENAME" "$3"
  270. fi
  271. #
  272. # NOTES on mysql:
  273. # - CREATE DATABASE oc_autotest;
  274. # - CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY 'owncloud';
  275. # - grant all on oc_autotest.* to 'oc_autotest'@'localhost';
  276. #
  277. # - for parallel executor support with EXECUTOR_NUMBER=0:
  278. # - CREATE DATABASE oc_autotest0;
  279. # - CREATE USER 'oc_autotest0'@'localhost' IDENTIFIED BY 'owncloud';
  280. # - grant all on oc_autotest0.* to 'oc_autotest0'@'localhost';
  281. #
  282. # NOTES on pgsql:
  283. # - su - postgres
  284. # - createuser -P oc_autotest (enter password and enable superuser)
  285. # - 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):
  286. # local all all trust
  287. #
  288. # - for parallel executor support with EXECUTOR_NUMBER=0:
  289. # - createuser -P oc_autotest0 (enter password and enable superuser)
  290. #
  291. # NOTES on oci:
  292. # - it's a pure nightmare to install Oracle on a Linux-System
  293. # - DON'T TRY THIS AT HOME!
  294. # - if you really need it: we feel sorry for you
  295. #