test/docker: Use gerritcodereview docker image

Use a 2.14.21-ubuntu16 gerritcodereview image tag as the plugin
currently is compatible with 2.14 gerrit version.

Retain the options for specifying a gerrit.war and/or events.jar
but convert them to use local paths since the use cases for those
now are to specify locally-built artifacts.

As gerritcodereview image has reviewnotes plugin installed, update
the change-merged test in test_events_plugin.sh to account for an
extra event of type 'ref-updated' on 'refs/notes/review' which gets
fired when reviewnotes is installed. Also, fix the test script to
exit with the correct status.

Change-Id: I11c079c93dffb8703a4ae4e3cfb00e485385f58e
diff --git a/test/docker/docker-compose.yaml b/test/docker/docker-compose.yaml
index 70112f6..7014537 100755
--- a/test/docker/docker-compose.yaml
+++ b/test/docker/docker-compose.yaml
@@ -10,7 +10,6 @@
     networks:
       - gerrit-net
     volumes:
-      - "./:/docker"
       - "gerrit-site-etc:/var/gerrit/etc"
 
   run_tests:
diff --git a/test/docker/gerrit/Dockerfile b/test/docker/gerrit/Dockerfile
index 9c12ffa..156d9d4 100755
--- a/test/docker/gerrit/Dockerfile
+++ b/test/docker/gerrit/Dockerfile
@@ -1,30 +1,13 @@
-FROM openjdk:8
-ARG GERRIT_WAR
-ARG EVENTS_PLUGIN_JAR
-ARG UID=1000
-ARG GID=1000
+FROM gerritcodereview/gerrit:2.14.21-ubuntu16
 
-ENV GERRIT_USER gerrit
+USER root
+
 ENV GERRIT_SITE /var/gerrit
-ENV USER_HOME /home/$GERRIT_USERENV USER_HOME /home/$GERRIT_USER
-RUN mkdir -p $GERRIT_SITE/bin $GERRIT_SITE/plugins $GERRIT_SITE/etc $USER_HOME/.ssh
-COPY $GERRIT_WAR $GERRIT_SITE/bin/gerrit.war
-COPY $EVENTS_PLUGIN_JAR $GERRIT_SITE/plugins/events.jar
+RUN git config -f "$GERRIT_SITE/etc/gerrit.config" auth.type \
+    DEVELOPMENT_BECOME_ANY_ACCOUNT
 
-RUN touch $GERRIT_SITE/etc/gerrit.config && \
-    git config -f $GERRIT_SITE/etc/gerrit.config auth.type DEVELOPMENT_BECOME_ANY_ACCOUNT && \
-    git config -f "$GERRIT_SITE"/etc/secure.config ssh-alias.stream-events "events stream"
+COPY artifacts /tmp/
+RUN cp /tmp/events.jar "$GERRIT_SITE/plugins/events.jar"
+RUN { [ -e /tmp/gerrit.war ] && cp /tmp/gerrit.war "$GERRIT_SITE/bin/gerrit.war" ; } || true
 
-EXPOSE 29418 8080
-RUN groupadd -f -g $GID users2 && \
-  useradd -u $UID -g $GID $GERRIT_USER && \
-  chown -R $GERRIT_USER $GERRIT_SITE $USER_HOME
-
-USER $GERRIT_USER
-
-RUN echo "Initializing Gerrit site ..." && \
-    java -jar "$GERRIT_SITE"/bin/gerrit.war init --batch -d "$GERRIT_SITE" && \
-    java -jar "$GERRIT_SITE"/bin/gerrit.war reindex -d "$GERRIT_SITE"
-
-RUN echo "Running Gerrit ..."
-ENTRYPOINT "$GERRIT_SITE/bin/gerrit.sh" run
+USER gerrit
diff --git a/test/docker/run.sh b/test/docker/run.sh
index 442b874..d4ae3fa 100755
--- a/test/docker/run.sh
+++ b/test/docker/run.sh
@@ -23,14 +23,20 @@
     local prog=$(basename "$0")
     cat <<EOF
 Usage:
-    "$prog" --gerrit-war|-g <WAR URL or file path>
-            --events-plugin-jar|-e <events plugin JAR URL or file path>
+    $prog [--events-plugin-jar|-t <FILE_PATH>] [--gerrit-war|-g <FILE_PATH>]
 
+    This tool runs the plugin functional tests in a Docker environment built
+    from the gerritcodereview/gerrit base Docker image.
+
+    The events plugin JAR and optionally a Gerrit WAR are expected to be in the
+    $ARTIFACTS dir;
+    however, the --events-plugin-jar and --gerrit-war switches may be used as
+    helpers to specify which files to copy there.
+
+    Options:
     --help|-h
-    --gerrit-war|-g             gerrit WAR URL or the file path in local workspace
-                                eg: file:///path/to/gerrit.war
-    --events-plugin-jar|-e      events plugin JAR URL or the file path in local workspace
-                                eg: file:///path/to/events.jar
+    --gerrit-war|-g             path to Gerrit WAR file
+    --events-plugin-jar|-e      path to events plugin JAR file
 
 EOF
 
@@ -43,30 +49,14 @@
     docker-compose --version > /dev/null || die "docker-compose is not installed"
 }
 
-fetch_artifact() { # source_location output_path
-    curl --silent --fail --netrc "$1" --output "$2" --create-dirs || die "unable to fetch $1"
-}
-
-fetch_artifacts() {
-    fetch_artifact "$GERRIT_WAR" "$ARTIFACTS/gerrit.war"
-    fetch_artifact "$EVENTS_PLUGIN_JAR" "$ARTIFACTS/events.jar"
-}
-
 build_images() {
-    local build_args=(--build-arg GERRIT_WAR="/artifacts/gerrit.war" \
-        --build-arg EVENTS_PLUGIN_JAR="/artifacts/events.jar" \
-        --build-arg UID="$(id -u)" --build-arg GID="$(id -g)")
-
-    docker-compose "${COMPOSE_ARGS[@]}" build "${build_args[@]}" --quiet
-    rm -r "$ARTIFACTS"
+    docker-compose "${COMPOSE_ARGS[@]}" build --quiet
 }
 
 run_events_plugin_tests() {
-    docker-compose "${COMPOSE_ARGS[@]}" up -d
-    local runtests_container=$(docker ps | grep "$PROJECT_NAME"_run_tests | \
-        awk '{print $1}')
-    docker exec --user=gerrit_admin "$runtests_container" \
-            '/events/test/docker/run_tests/start.sh'
+    docker-compose "${COMPOSE_ARGS[@]}" up --detach
+    docker-compose "${COMPOSE_ARGS[@]}" exec -T --user=gerrit_admin run_tests \
+        '/events/test/docker/run_tests/start.sh'
 }
 
 cleanup() {
@@ -83,14 +73,18 @@
     shift
 done
 
-[ -n "$GERRIT_WAR" ] || usage "'--gerrit-war' not set"
-[ -n "$EVENTS_PLUGIN_JAR" ] || usage "'--events-plugin-jar' not set "
-
 PROJECT_NAME="events_$$"
 COMPOSE_YAML="$MYDIR/docker-compose.yaml"
 COMPOSE_ARGS=(--project-name "$PROJECT_NAME" -f "$COMPOSE_YAML")
 check_prerequisite
-progress "fetching artifacts" fetch_artifacts
+mkdir -p -- "$ARTIFACTS"
+[ -n "$EVENTS_PLUGIN_JAR" ] && cp -f "$EVENTS_PLUGIN_JAR" "$ARTIFACTS/events.jar"
+if [ ! -e "$ARTIFACTS/events.jar" ] ; then
+    MISSING="Missing $ARTIFACTS/events.jar"
+    [ -n "$EVENTS_PLUGIN_JAR" ] && die "$MISSING, check for copy failure?"
+    usage "$MISSING, did you forget --events-plugin-jar?"
+fi
+[ -n "$GERRIT_WAR" ] && cp -f "$GERRIT_WAR" "$ARTIFACTS/gerrit.war"
 progress "Building docker images" build_images
 run_events_plugin_tests ; RESULT=$?
 cleanup
diff --git a/test/test_events_plugin.sh b/test/test_events_plugin.sh
index 5651c41..88f96a5 100755
--- a/test/test_events_plugin.sh
+++ b/test/test_events_plugin.sh
@@ -4,6 +4,9 @@
 gssh() { ssh -p 29418 -x "$SERVER" "$@" 2>&1 ; } # run a gerrit ssh command
 mygit() { git --work-tree="$REPO_DIR" --git-dir="$GIT_DIR" "$@" ; } # [args...]
 
+# plugin_name
+is_plugin_installed() { gssh gerrit plugin ls | awk '{print $1}' | grep -q "^$1$"; }
+
 cleanup() {
     wait_event
     (kill_captures ; sleep 1 ; kill_captures -9 ) &
@@ -198,6 +201,8 @@
 
 setup_captures
 
+RESULT=0
+
 # ------------------------- Individual Event Tests ---------------------------
 GROUP=visible-events
 type=patchset-created
@@ -227,10 +232,14 @@
 result_type "$GROUP" "$type"
 
 type=change-merged
-capture_events 2
+events_count=2
+# If reviewnotes plugin is installed, an extra event of type 'ref-updated'
+# on 'refs/notes/review' is fired when a change is merged.
+is_plugin_installed reviewnotes && events_count=3
+capture_events "$events_count"
 submit "$ch1,1"
 result_type "$GROUP $type" "ref-updated"
-result_type "$GROUP" "$type" 2
+result_type "$GROUP" "$type" "$events_count"
 
 # reviewer-added needs to be tested via Rest-API
 
@@ -239,3 +248,4 @@
 out=$(diff "$EVENTS_CORE" "$EVENTS_PLUGIN")
 result "core/plugin diff" "$out"
 
+exit $RESULT