2017-04-04 14:31:23 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
|
|
|
|
#
|
|
|
|
# @license GNU AGPL version 3 or any later version
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Helper script to run the acceptance tests, which test a running Nextcloud
|
|
|
|
# instance from the point of view of a real user.
|
|
|
|
#
|
2017-04-19 05:59:25 +00:00
|
|
|
# The acceptance tests are run in its own Docker container; the grandparent
|
|
|
|
# directory of the acceptance tests directory (that is, the root directory of
|
|
|
|
# the Nextcloud server) is copied to the container and the acceptance tests are
|
|
|
|
# run inside it. Once the tests end the container is stopped. The acceptance
|
|
|
|
# tests also use the Selenium server to control a web browser, so the Selenium
|
|
|
|
# server is also launched before the tests start in its own Docker container (it
|
|
|
|
# will be stopped automatically too once the tests end).
|
2017-04-04 14:31:23 +00:00
|
|
|
#
|
2017-04-19 05:59:25 +00:00
|
|
|
# To perform its job, the script requires the "docker" command to be available.
|
2017-04-04 14:31:23 +00:00
|
|
|
#
|
|
|
|
# The Docker Command Line Interface (the "docker" command) requires special
|
|
|
|
# permissions to talk to the Docker daemon, and those permissions are typically
|
2017-04-19 05:59:25 +00:00
|
|
|
# available only to the root user. Please see the Docker documentation to find
|
|
|
|
# out how to give access to a regular user to the Docker daemon:
|
2017-04-04 14:31:23 +00:00
|
|
|
# https://docs.docker.com/engine/installation/linux/linux-postinstall/
|
|
|
|
#
|
|
|
|
# Note, however, that being able to communicate with the Docker daemon is the
|
|
|
|
# same as being able to get root privileges for the system. Therefore, you must
|
|
|
|
# give access to the Docker daemon (and thus run this script as) ONLY to trusted
|
|
|
|
# and secure users:
|
|
|
|
# https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
|
|
|
|
#
|
|
|
|
# Finally, take into account that this script will automatically remove the
|
2017-04-19 05:59:25 +00:00
|
|
|
# Docker containers named "selenium-nextcloud-local-test-acceptance" and
|
|
|
|
# "nextcloud-local-test-acceptance", even if the script did not create them
|
|
|
|
# (probably you will not have containers nor images with those names, but just
|
|
|
|
# in case).
|
2017-04-04 14:31:23 +00:00
|
|
|
|
2017-04-22 15:46:20 +00:00
|
|
|
# Sets the variables that abstract the differences in command names and options
|
|
|
|
# between operating systems.
|
|
|
|
#
|
|
|
|
# Switches between timeout on GNU/Linux and gtimeout on macOS (same for mktemp
|
|
|
|
# and gmktemp).
|
|
|
|
function setOperatingSystemAbstractionVariables() {
|
|
|
|
case "$OSTYPE" in
|
|
|
|
darwin*)
|
|
|
|
if [ "$(which gtimeout)" == "" ]; then
|
|
|
|
echo "Please install coreutils (brew install coreutils)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
MKTEMP=gmktemp
|
|
|
|
TIMEOUT=gtimeout
|
|
|
|
DOCKER_OPTIONS="-e no_proxy=localhost "
|
|
|
|
;;
|
|
|
|
linux*)
|
|
|
|
MKTEMP=mktemp
|
|
|
|
TIMEOUT=timeout
|
|
|
|
DOCKER_OPTIONS=" "
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Operating system ($OSTYPE) not supported"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2017-04-21 19:11:56 +00:00
|
|
|
|
2017-04-16 18:31:52 +00:00
|
|
|
# Launches the Selenium server in a Docker container.
|
2017-04-04 14:31:23 +00:00
|
|
|
#
|
|
|
|
# The acceptance tests use Firefox by default but, unfortunately, Firefox >= 48
|
|
|
|
# does not provide yet the same level of support as earlier versions for certain
|
2017-04-16 18:31:52 +00:00
|
|
|
# features related to automated testing. Therefore, the Docker image used is not
|
|
|
|
# the latest one, but an older version known to work.
|
|
|
|
#
|
|
|
|
# The acceptance tests expect the Selenium server to be accessible at
|
2017-04-19 05:59:25 +00:00
|
|
|
# "127.0.0.1:4444"; as the Selenium server container and the container in which
|
|
|
|
# the acceptance tests are run share the same network nothing else needs to be
|
|
|
|
# done for the acceptance tests to access the Selenium server and for the
|
|
|
|
# Selenium server to access the Nextcloud server. However, in order to ensure
|
|
|
|
# from this script that the Selenium server was started the 4444 port of its
|
|
|
|
# container is mapped to the 4444 port of the host.
|
2017-04-16 18:31:52 +00:00
|
|
|
#
|
|
|
|
# Besides the Selenium server, the Docker image also provides a VNC server, so
|
|
|
|
# the 5900 port of the container is also mapped to the 5900 port of the host.
|
|
|
|
#
|
|
|
|
# The Docker container started here will be automatically stopped when the
|
2017-04-04 14:31:23 +00:00
|
|
|
# script exits (see cleanUp). If the Selenium server can not be started then the
|
|
|
|
# script will be exited immediately with an error state; the most common cause
|
|
|
|
# for the Selenium server to fail to start is that another server is already
|
2017-04-19 05:59:25 +00:00
|
|
|
# using the mapped ports in the host.
|
2017-04-04 14:31:23 +00:00
|
|
|
#
|
2017-04-16 18:31:52 +00:00
|
|
|
# As the web browser is run inside the Docker container it is not visible by
|
|
|
|
# default. However, it can be viewed using VNC (for example,
|
|
|
|
# "vncviewer 127.0.0.1:5900"); when asked for the password use "secret".
|
2017-04-04 14:31:23 +00:00
|
|
|
function prepareSelenium() {
|
2017-04-16 18:31:52 +00:00
|
|
|
SELENIUM_CONTAINER=selenium-nextcloud-local-test-acceptance
|
2017-04-04 14:31:23 +00:00
|
|
|
|
|
|
|
echo "Starting Selenium server"
|
2017-04-22 15:46:20 +00:00
|
|
|
docker run --detach --name=$SELENIUM_CONTAINER --publish 4444:4444 --publish 5900:5900 $DOCKER_OPTIONS selenium/standalone-firefox-debug:2.53.1-beryllium
|
2017-04-04 14:31:23 +00:00
|
|
|
|
2017-04-16 18:31:52 +00:00
|
|
|
echo "Waiting for Selenium server to be ready"
|
2017-04-21 19:11:56 +00:00
|
|
|
if ! $TIMEOUT 10s bash -c "while ! curl 127.0.0.1:4444 >/dev/null 2>&1; do sleep 1; done"; then
|
2017-04-16 18:31:52 +00:00
|
|
|
echo "Could not start Selenium server; running" \
|
2017-04-22 15:46:20 +00:00
|
|
|
"\"docker run --rm --publish 4444:4444 --publish 5900:5900 $DOCKER_OPTIONS selenium/standalone-firefox-debug:2.53.1-beryllium\"" \
|
2017-04-16 18:31:52 +00:00
|
|
|
"could give you a hint of the problem"
|
2017-04-04 14:31:23 +00:00
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:59:25 +00:00
|
|
|
# Creates a Docker container to run both the acceptance tests and the Nextcloud
|
|
|
|
# server used by them.
|
2017-04-04 14:31:23 +00:00
|
|
|
#
|
2017-04-19 05:59:25 +00:00
|
|
|
# This function starts a Docker container with a copy the Nextcloud code from
|
|
|
|
# the grandparent directory, although ignoring any configuration or data that it
|
|
|
|
# may provide (for example, if that directory was used directly to deploy a
|
|
|
|
# Nextcloud instance in a web server). As the Nextcloud code is copied to the
|
|
|
|
# container instead of referenced the original code can be modified while the
|
|
|
|
# acceptance tests are running without interfering in them.
|
2017-04-04 14:31:23 +00:00
|
|
|
function prepareDocker() {
|
|
|
|
NEXTCLOUD_LOCAL_CONTAINER=nextcloud-local-test-acceptance
|
|
|
|
|
2017-04-19 05:59:25 +00:00
|
|
|
echo "Starting the Nextcloud container"
|
|
|
|
# As the Nextcloud server container uses the network of the Selenium server
|
|
|
|
# container the Nextcloud server can be accessed at "127.0.0.1" from the
|
|
|
|
# Selenium server.
|
|
|
|
# The container exits immediately if no command is given, so a Bash session
|
|
|
|
# is created to prevent that.
|
2017-10-31 09:38:54 +00:00
|
|
|
docker run --detach --name=$NEXTCLOUD_LOCAL_CONTAINER --network=container:$SELENIUM_CONTAINER --interactive --tty nextcloudci/php7.1:php7.1-15 bash
|
2017-04-04 14:31:23 +00:00
|
|
|
|
|
|
|
# Use the $TMPDIR or, if not set, fall back to /tmp.
|
2017-04-21 19:11:56 +00:00
|
|
|
NEXTCLOUD_LOCAL_TAR="$($MKTEMP --tmpdir="${TMPDIR:-/tmp}" --suffix=.tar nextcloud-local-XXXXXXXXXX)"
|
2017-04-04 14:31:23 +00:00
|
|
|
|
|
|
|
# Setting the user and group of files in the tar would be superfluous, as
|
|
|
|
# "docker cp" does not take them into account (the extracted files are set
|
|
|
|
# to root).
|
|
|
|
echo "Copying local Git working directory of Nextcloud to the container"
|
2017-04-21 12:35:19 +00:00
|
|
|
tar --create --file="$NEXTCLOUD_LOCAL_TAR" --exclude=".git" --exclude="./build" --exclude="./config/config.php" --exclude="./data" --exclude="./data-autotest" --exclude="./tests" --directory=../../ .
|
|
|
|
tar --append --file="$NEXTCLOUD_LOCAL_TAR" --directory=../../ tests/acceptance/
|
2017-04-04 14:31:23 +00:00
|
|
|
|
2017-04-19 05:59:25 +00:00
|
|
|
docker exec $NEXTCLOUD_LOCAL_CONTAINER mkdir /nextcloud
|
|
|
|
docker cp - $NEXTCLOUD_LOCAL_CONTAINER:/nextcloud/ < "$NEXTCLOUD_LOCAL_TAR"
|
2017-04-04 14:31:23 +00:00
|
|
|
|
2017-04-19 05:59:25 +00:00
|
|
|
# run-local.sh expects a Git repository to be available in the root of the
|
|
|
|
# Nextcloud server, but it was excluded when the Git working directory was
|
|
|
|
# copied to the container to avoid copying the large and unneeded history of
|
|
|
|
# the repository.
|
|
|
|
docker exec $NEXTCLOUD_LOCAL_CONTAINER bash -c "cd nextcloud && git init"
|
2017-04-04 14:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Removes/stops temporal elements created/started by this script.
|
|
|
|
function cleanUp() {
|
|
|
|
# Disable (yes, "+" disables) exiting immediately on errors to ensure that
|
|
|
|
# all the cleanup commands are executed (well, no errors should occur during
|
|
|
|
# the cleanup anyway, but just in case).
|
|
|
|
set +o errexit
|
|
|
|
|
|
|
|
echo "Cleaning up"
|
|
|
|
|
|
|
|
if [ -f "$NEXTCLOUD_LOCAL_TAR" ]; then
|
|
|
|
echo "Removing $NEXTCLOUD_LOCAL_TAR"
|
|
|
|
rm $NEXTCLOUD_LOCAL_TAR
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The name filter must be specified as "^/XXX$" to get an exact match; using
|
|
|
|
# just "XXX" would match every name that contained "XXX".
|
|
|
|
if [ -n "$(docker ps --all --quiet --filter name="^/$NEXTCLOUD_LOCAL_CONTAINER$")" ]; then
|
|
|
|
echo "Removing Docker container $NEXTCLOUD_LOCAL_CONTAINER"
|
|
|
|
docker rm --volumes --force $NEXTCLOUD_LOCAL_CONTAINER
|
|
|
|
fi
|
|
|
|
|
2017-04-16 18:31:52 +00:00
|
|
|
if [ -n "$(docker ps --all --quiet --filter name="^/$SELENIUM_CONTAINER$")" ]; then
|
|
|
|
echo "Removing Docker container $SELENIUM_CONTAINER"
|
|
|
|
docker rm --volumes --force $SELENIUM_CONTAINER
|
2017-04-04 14:31:23 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Exit immediately on errors.
|
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
# Execute cleanUp when the script exits, either normally or due to an error.
|
|
|
|
trap cleanUp EXIT
|
|
|
|
|
2017-04-19 05:59:25 +00:00
|
|
|
# Ensure working directory is script directory, as some actions (like copying
|
|
|
|
# the Git working directory to the container) expect that.
|
2017-04-04 14:31:23 +00:00
|
|
|
cd "$(dirname $0)"
|
|
|
|
|
2017-05-03 15:24:30 +00:00
|
|
|
# "--timeout-multiplier N" option can be provided before the specific scenario
|
|
|
|
# to run, if any, to set the timeout multiplier to be used in the acceptance
|
|
|
|
# tests.
|
|
|
|
TIMEOUT_MULTIPLIER_OPTION=""
|
|
|
|
if [ "$1" = "--timeout-multiplier" ]; then
|
|
|
|
if [[ ! "$2" =~ ^[0-9]+$ ]]; then
|
|
|
|
echo "--timeout-multiplier must be followed by a positive integer"
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TIMEOUT_MULTIPLIER_OPTION="--timeout-multiplier $2"
|
|
|
|
|
|
|
|
shift 2
|
|
|
|
fi
|
|
|
|
|
2017-04-16 16:12:12 +00:00
|
|
|
# If no parameter is provided to this script all the acceptance tests are run.
|
|
|
|
SCENARIO_TO_RUN=$1
|
|
|
|
|
2017-04-22 15:46:20 +00:00
|
|
|
setOperatingSystemAbstractionVariables
|
|
|
|
|
2017-04-04 14:31:23 +00:00
|
|
|
prepareSelenium
|
|
|
|
prepareDocker
|
|
|
|
|
2017-04-16 16:12:12 +00:00
|
|
|
echo "Running tests"
|
2017-05-03 15:24:30 +00:00
|
|
|
docker exec $NEXTCLOUD_LOCAL_CONTAINER bash -c "cd nextcloud && tests/acceptance/run-local.sh $TIMEOUT_MULTIPLIER_OPTION allow-git-repository-modifications $SCENARIO_TO_RUN"
|