blob: 28dacca5e83a11ad46526dc5523c134607006e44 [file] [log] [blame]
#!/usr/bin/env bash
readlink -f / &> /dev/null || readlink() { greadlink "$@" ; } # for MacOS
MYDIR=$(dirname -- "$(readlink -f -- "$0")")
MYPROG=$(basename -- "$0")
GERRIT_ARTIFACTS=$MYDIR/gerrit/artifacts
REMOTE_GERRIT_ARTIFACTS=$MYDIR/remote-gerrit/artifacts
die() { echo -e "\nERROR:" "$@" ; kill $$ ; exit 1 ; } # error_message
progress() { # message cmd [args]...
local message=$1 ; shift
echo -n "$message"
"$@" &
local pid=$!
while kill -0 $pid 2> /dev/null ; do
echo -n "."
sleep 2
done
echo
wait "$pid"
}
usage() { # [error_message]
cat <<-EOF
Usage:
$MYPROG [--remote-gerrit-account-cache-jar|-r <FILE_PATH>]
[--gerrit-war|-g <FILE_PATH>]
This tool runs the functional tests in a Docker environment built
from the gerritcodereview/gerrit base Docker image.
The remote-gerrit-account-cache JAR and optionally a Gerrit WAR are
expected to be in the $GERRIT_ARTIFACTS dir; however, the
--remote-gerrit-account-cache-jar and --gerrit-war switches may be
used as helpers to specify which files to copy there.
Options:
--help|-h
--gerrit-war|-g path to Gerrit WAR file
--remote-gerrit-account-cache-jar|-r path to remote-gerrit-account-cache JAR file
--preserve To preserve the docker setup for debugging
EOF
[ -n "$1" ] && echo -e "\nERROR: $1" && exit 1
exit 0
}
check_prerequisite() {
docker --version > /dev/null || die "docker is not installed"
docker compose version > /dev/null || die "docker compose is not installed"
}
build_images() {
docker compose "${COMPOSE_ARGS[@]}" build --quiet
}
execute_tests() {
docker compose "${COMPOSE_ARGS[@]}" up --detach
local run_tests_container="$(docker compose "${COMPOSE_ARGS[@]}" ps -q run_tests | \
xargs docker inspect --format '{{.Name}}' | sed 's|/||')"
docker cp "$MYDIR"/../../test "$run_tests_container":/
docker compose "${COMPOSE_ARGS[@]}" exec -T --user=admin run_tests \
'/test/docker/run_tests/start.sh'
}
get_run_test_container() {
docker compose "${COMPOSE_ARGS[@]}" ps | grep run_tests | awk '{ print $1 }'
}
cleanup() {
if [ "$PRESERVE" = "true" ] ; then
echo "Preserving the following docker setup"
docker compose "${COMPOSE_ARGS[@]}" ps
echo ""
echo "To exec into run_tests container, use following command:"
echo "docker exec -it $(get_run_test_container) /bin/bash"
echo ""
echo "Run the following command to bring down the setup:"
echo "docker compose" "${COMPOSE_ARGS[@]}" "down -v --rmi local"
else
docker compose "${COMPOSE_ARGS[@]}" down -v --rmi local 2>/dev/null
fi
rm -rf "$GERRIT_ARTIFACTS" "$REMOTE_GERRIT_ARTIFACTS"
}
PRESERVE="false"
COMPOSE_ARGS=()
while (( "$#" )) ; do
case "$1" in
--help|-h) usage ;;
--gerrit-war|-g) shift ; GERRIT_WAR=$1 ;;
--remote-gerrit-account-cache-jar|-r) shift ; REMOTE_GERRIT_ACCOUNT_CACHE_JAR=$1 ;;
--preserve) PRESERVE="true" ;;
--compose-arg) shift ; COMPOSE_ARGS+=("$1") ;;
*) usage "invalid argument $1" ;;
esac
shift
done
PROJECT_NAME="remote-gerrit-account-cache_$$"
COMPOSE_YAML="$MYDIR/docker-compose.yaml"
COMPOSE_ARGS=(--project-name "$PROJECT_NAME" -f "$COMPOSE_YAML")
check_prerequisite
mkdir -p -- "$GERRIT_ARTIFACTS" "$REMOTE_GERRIT_ARTIFACTS"
[ -n "$REMOTE_GERRIT_ACCOUNT_CACHE_JAR" ] \
&& cp -f -- "$REMOTE_GERRIT_ACCOUNT_CACHE_JAR" "$GERRIT_ARTIFACTS/remote-gerrit-account-cache.jar"
if [ ! -e "$GERRIT_ARTIFACTS/remote-gerrit-account-cache.jar" ] ; then
MISSING="Missing $GERRIT_ARTIFACTS/remote-gerrit-account-cache.jar"
[ -n "$REMOTE_GERRIT_ACCOUNT_CACHE_JAR" ] && die "$MISSING, check for copy failure?"
usage "$MISSING, did you forget --remote-gerrit-account-cache-jar?"
fi
[ -n "$GERRIT_WAR" ] && cp -f -- "$GERRIT_WAR" "$GERRIT_ARTIFACTS/gerrit.war"
[ -n "$GERRIT_WAR" ] && cp -f -- "$GERRIT_WAR" "$REMOTE_GERRIT_ARTIFACTS/gerrit.war"
( trap cleanup EXIT SIGTERM
progress "Building docker images" build_images
execute_tests
)