run-docker.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/bin/env bash
  2. # SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # Helper script to run the integration tests on a fresh Nextcloud server through
  5. # Docker.
  6. #
  7. # The integration tests are run in its own Docker container; the grandparent
  8. # directory of the integration tests directory (that is, the root directory of
  9. # the Nextcloud server) is copied to the container and the integration tests are
  10. # run inside it; in the container the configuration/data from the original
  11. # Nextcloud server is ignored, and a new server installation is performed inside
  12. # the container instead. Once the tests end the container is stopped.
  13. #
  14. # To perform its job, the script requires the "docker" command to be available.
  15. #
  16. # The Docker Command Line Interface (the "docker" command) requires special
  17. # permissions to talk to the Docker daemon, and those permissions are typically
  18. # available only to the root user. Please see the Docker documentation to find
  19. # out how to give access to a regular user to the Docker daemon:
  20. # https://docs.docker.com/engine/installation/linux/linux-postinstall/
  21. #
  22. # Note, however, that being able to communicate with the Docker daemon is the
  23. # same as being able to get root privileges for the system. Therefore, you must
  24. # give access to the Docker daemon (and thus run this script as) ONLY to trusted
  25. # and secure users:
  26. # https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
  27. #
  28. # Finally, take into account that this script will automatically remove the
  29. # Docker containers named "database-nextcloud-local-test-integration" and
  30. # "nextcloud-local-test-integration", even if the script did not create them
  31. # (probably you will not have containers nor images with that name, but just in
  32. # case).
  33. # Sets the variables that abstract the differences in command names and options
  34. # between operating systems.
  35. #
  36. # Switches between mktemp on GNU/Linux and gmktemp on macOS.
  37. function setOperatingSystemAbstractionVariables() {
  38. case "$OSTYPE" in
  39. darwin*)
  40. if [ "$(which gtimeout)" == "" ]; then
  41. echo "Please install coreutils (brew install coreutils)"
  42. exit 1
  43. fi
  44. MKTEMP=gmktemp
  45. TIMEOUT=gtimeout
  46. ;;
  47. linux*)
  48. MKTEMP=mktemp
  49. TIMEOUT=timeout
  50. ;;
  51. *)
  52. echo "Operating system ($OSTYPE) not supported"
  53. exit 1
  54. ;;
  55. esac
  56. }
  57. # Launches the database server in a Docker container.
  58. #
  59. # No server is started if "SQLite" is being used; in other cases the database
  60. # is set up as needed and generic "$DATABASE_NAME/USER/PASSWORD" variables
  61. # (independent of the database type) are set to be used when installing the
  62. # Nextcloud server.
  63. #
  64. # The Docker container started here will be automatically stopped when the
  65. # script exits (see cleanUp). If the database server can not be started then the
  66. # script will be exited immediately with an error state.
  67. function prepareDatabase() {
  68. if [ "$DATABASE" = "sqlite" ]; then
  69. return
  70. fi
  71. DATABASE_CONTAINER=database-nextcloud-local-test-integration
  72. DATABASE_NAME=oc_autotest
  73. DATABASE_USER=oc_autotest
  74. DATABASE_PASSWORD=nextcloud
  75. DATABASE_CONTAINER_OPTIONS="--env MYSQL_ROOT_PASSWORD=nextcloud_root --env MYSQL_USER=$DATABASE_USER --env MYSQL_PASSWORD=$DATABASE_PASSWORD --env MYSQL_DATABASE=$DATABASE_NAME"
  76. if [ "$DATABASE" = "pgsql" ]; then
  77. DATABASE_CONTAINER_OPTIONS=" --env POSTGRES_USER=$DATABASE_USER --env POSTGRES_PASSWORD=$DATABASE_PASSWORD --env POSTGRES_DB=${DATABASE_NAME}_dummy"
  78. fi
  79. echo "Starting database server"
  80. docker run --detach --name=$DATABASE_CONTAINER $DATABASE_CONTAINER_OPTIONS $DATABASE_IMAGE
  81. DATABASE_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $DATABASE_CONTAINER)
  82. DATABASE_PORT=3306
  83. if [ "$DATABASE" = "pgsql" ]; then
  84. DATABASE_PORT=5432
  85. fi
  86. echo "Waiting for database server to be ready"
  87. if ! $TIMEOUT 600s bash -c "while ! (</dev/tcp/$DATABASE_IP/$DATABASE_PORT) >/dev/null 2>&1; do sleep 1; done"; then
  88. echo "Could not start database server after 600 seconds" >&2
  89. exit 1
  90. fi
  91. }
  92. # Creates a Docker container to run the integration tests.
  93. #
  94. # This function starts a Docker container with a copy of the Nextcloud code from
  95. # the grandparent directory, although ignoring any configuration or data that it
  96. # may provide (for example, if that directory was used directly to deploy a
  97. # Nextcloud instance in a web server). As the Nextcloud code is copied to the
  98. # container instead of referenced the original code can be modified while the
  99. # integration tests are running without interfering in them.
  100. function prepareDocker() {
  101. NEXTCLOUD_LOCAL_CONTAINER=nextcloud-local-test-integration
  102. NEXTCLOUD_LOCAL_CONTAINER_NETWORK_OPTIONS=""
  103. if [ -n "$DATABASE_CONTAINER" ]; then
  104. # The network stack is shared between the database and the Nextcloud
  105. # container, so the Nextcloud server can access the database directly on
  106. # 127.0.0.1.
  107. NEXTCLOUD_LOCAL_CONTAINER_NETWORK_OPTIONS="--network=container:$DATABASE_CONTAINER"
  108. fi
  109. echo "Starting the Nextcloud container"
  110. # When using "nextcloudci/phpX.Y" images the container exits immediately if
  111. # no command is given, so a Bash session is created to prevent that.
  112. docker run \
  113. --volume composer_cache:/root/.composer \
  114. --detach --name=$NEXTCLOUD_LOCAL_CONTAINER $NEXTCLOUD_LOCAL_CONTAINER_NETWORK_OPTIONS --interactive --tty $NEXTCLOUD_LOCAL_IMAGE bash
  115. # Use the $TMPDIR or, if not set, fall back to /tmp.
  116. NEXTCLOUD_LOCAL_TAR="$($MKTEMP --tmpdir="${TMPDIR:-/tmp}" --suffix=.tar nextcloud-local-XXXXXXXXXX)"
  117. # Setting the user and group of files in the tar would be superfluous, as
  118. # "docker cp" does not take them into account (the extracted files are set
  119. # to root).
  120. echo "Copying local Git working directory of Nextcloud to the container"
  121. tar --create --file="$NEXTCLOUD_LOCAL_TAR" \
  122. --exclude=".git" \
  123. --exclude="./config/config.php" \
  124. --exclude="./config/*.config.php" \
  125. --exclude="./data" \
  126. --exclude="./data-autotest" \
  127. --exclude="./tests" \
  128. --exclude="node_modules" \
  129. --directory=../../ \
  130. .
  131. docker exec $NEXTCLOUD_LOCAL_CONTAINER mkdir /nextcloud
  132. docker cp - $NEXTCLOUD_LOCAL_CONTAINER:/nextcloud/ < "$NEXTCLOUD_LOCAL_TAR"
  133. # Database options are needed only when a database other than SQLite is
  134. # used.
  135. NEXTCLOUD_LOCAL_CONTAINER_INSTALL_DATABASE_OPTIONS=""
  136. if [ -n "$DATABASE_CONTAINER" ]; then
  137. NEXTCLOUD_LOCAL_CONTAINER_INSTALL_DATABASE_OPTIONS="--database=$DATABASE --database-name=$DATABASE_NAME --database-user=$DATABASE_USER --database-pass=$DATABASE_PASSWORD --database-host=127.0.0.1"
  138. fi
  139. echo "Installing Nextcloud in the container"
  140. docker exec $NEXTCLOUD_LOCAL_CONTAINER bash -c "cd nextcloud && php occ maintenance:install --admin-pass=admin $NEXTCLOUD_LOCAL_CONTAINER_INSTALL_DATABASE_OPTIONS"
  141. }
  142. # Removes/stops temporal elements created/started by this script.
  143. function cleanUp() {
  144. # Disable (yes, "+" disables) exiting immediately on errors to ensure that
  145. # all the cleanup commands are executed (well, no errors should occur during
  146. # the cleanup anyway, but just in case).
  147. set +o errexit
  148. echo "Cleaning up"
  149. if [ -f "$NEXTCLOUD_LOCAL_TAR" ]; then
  150. echo "Removing $NEXTCLOUD_LOCAL_TAR"
  151. rm $NEXTCLOUD_LOCAL_TAR
  152. fi
  153. # The name filter must be specified as "^/XXX$" to get an exact match; using
  154. # just "XXX" would match every name that contained "XXX".
  155. if [ -n "$(docker ps --all --quiet --filter name="^/$NEXTCLOUD_LOCAL_CONTAINER$")" ]; then
  156. echo "Removing Docker container $NEXTCLOUD_LOCAL_CONTAINER"
  157. docker rm --volumes --force $NEXTCLOUD_LOCAL_CONTAINER
  158. fi
  159. if [ -n "$DATABASE_CONTAINER" -a -n "$(docker ps --all --quiet --filter name="^/$DATABASE_CONTAINER$")" ]; then
  160. echo "Removing Docker container $DATABASE_CONTAINER"
  161. docker rm --volumes --force $DATABASE_CONTAINER
  162. fi
  163. }
  164. # Exit immediately on errors.
  165. set -o errexit
  166. # Execute cleanUp when the script exits, either normally or due to an error.
  167. trap cleanUp EXIT
  168. # Ensure working directory is script directory, as some actions (like copying
  169. # the Git working directory to the container) expect that.
  170. cd "$(dirname $0)"
  171. # "--image XXX" option can be provided to set the Docker image to use to run
  172. # the integration tests (one of the "nextcloudci/phpX.Y:phpX.Y-Z" or
  173. # "ghcr.io/nextcloud/continuous-integration-integration-phpX.Y:latest" images).
  174. NEXTCLOUD_LOCAL_IMAGE="ghcr.io/nextcloud/continuous-integration-integration-php8.0:latest"
  175. if [ "$1" = "--image" ]; then
  176. NEXTCLOUD_LOCAL_IMAGE=$2
  177. shift 2
  178. fi
  179. # "--database XXX" option can be provided to set the database to use to run the
  180. # integration tests (one of "sqlite", "mysql" or "pgsql"; "sqlite" is used
  181. # by default).
  182. DATABASE="sqlite"
  183. if [ "$1" = "--database" ]; then
  184. DATABASE=$2
  185. shift 2
  186. fi
  187. if [ "$DATABASE" != "sqlite" ] && [ "$DATABASE" != "mysql" ] && [ "$DATABASE" != "pgsql" ]; then
  188. echo "--database must be followed by one of: sqlite, mysql or pgsql"
  189. exit 1
  190. fi
  191. # "--database-image XXX" option can be provided to set the Docker image to use
  192. # for the database container (ignored when using "sqlite").
  193. if [ "$DATABASE" = "mysql" ]; then
  194. DATABASE_IMAGE="mysql:5.7"
  195. elif [ "$DATABASE" = "pgsql" ]; then
  196. DATABASE_IMAGE="postgres:10"
  197. fi
  198. if [ "$1" = "--database-image" ]; then
  199. DATABASE_IMAGE=$2
  200. shift 2
  201. fi
  202. # If no parameter is provided to this script all the integration tests are run.
  203. SCENARIO_TO_RUN=$1
  204. setOperatingSystemAbstractionVariables
  205. prepareDatabase
  206. prepareDocker
  207. echo "Running tests"
  208. # --tty is needed to get colourful output.
  209. docker exec --tty $NEXTCLOUD_LOCAL_CONTAINER bash -c "cd nextcloud/build/integration && ./run.sh $SCENARIO_TO_RUN"