From a90594d5acc8f33df3f4ec7f67f5a8361888cdcf Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 9 Jun 2016 18:18:04 -0700 Subject: [PATCH 1/2] Support App Engine or Jetty for local testing. This script will allow us to run tests against a local server for testing. Since the App Engine and Jetty server running and testing is so similar, I combined the scripts rather than copying. The only differences are which plugin to use and what string to wait for before running curl. Since the bookshelf sample also needs a `-Plocal` profile argument, I extended the script to allow passing through arbitrary arguments to Maven. --- .../test-localhost.sh} | 65 +++++++++++++++---- 1 file changed, 53 insertions(+), 12 deletions(-) rename java-repo-tools/{test-devserver.sh => scripts/test-localhost.sh} (51%) diff --git a/java-repo-tools/test-devserver.sh b/java-repo-tools/scripts/test-localhost.sh similarity index 51% rename from java-repo-tools/test-devserver.sh rename to java-repo-tools/scripts/test-localhost.sh index 62b3dfefe..9615333c1 100755 --- a/java-repo-tools/test-devserver.sh +++ b/java-repo-tools/scripts/test-localhost.sh @@ -14,7 +14,7 @@ # limitations under the License. # Usage: -# test-devserver.sh path/to/project +# test-localhost.sh deployment-type path/to/project -- [maven arguments] # # This script runs the local appengine:devserver Maven plugin and verifies that # a request to http://localhost:8080/ does not return an error code. @@ -23,23 +23,64 @@ # correct (only if autoGenerate=false and the / handler does all queries used), # as an example. -set -e -set -x +print_usage () { + echo "Usage:" >&2 + echo " $0 server-type path/to/project [-- maven arguments]" >&2 + echo >&2 + echo "server-type can be any of the following:" >&2 + echo " appengine" >&2 + echo " jetty" >&2 +} + +if [[ -z "$1" ]]; then + echo "Missing server-type parameter." >&2 + print_usage + exit 1 +fi +case $1 in + appengine) + mvn_plugin="appengine:devserver" + server_started_message="localhost:8080" + ;; + jetty) + mvn_plugin="jetty:run-exploded" + server_started_message="Started Jetty Server" + ;; + *) + print_usage + exit 1 + ;; +esac -if [ -z "$1" ]; then - echo "Missing directory parameter." - echo "Usage:" - echo " $0 path/to/project" +if [[ -z "$2" ]]; then + echo "Missing directory parameter." >&2 + print_usage exit 1 fi +code_path=$2 + +mvn_command="mvn --batch-mode clean ${mvn_plugin} -DskipTests" +if [[ "$3" == "--" ]]; then + shift 3 + for mvn_arg in "${@}"; do + mvn_command="${mvn_command} ${mvn_arg}" + done +elif [[ -n "$3" ]]; then + echo "Got unexpected third argument" >&2 + print_usage + exit 1 +fi + +set -e +set -x ( -cd "$1" -expect -c ' - spawn mvn --batch-mode clean appengine:devserver -DskipTests +cd "$code_path" +expect -c " + spawn ${mvn_command} set timeout 600 - expect localhost:8080 - sleep 10 + expect \"${server_started_message}\" + "'sleep 10 spawn curl --silent --output /dev/stderr --write-out "%{http_code}" http://localhost:8080/ expect { "200" { From 435248f85853da657dc92591b051cd7e66cb5bd6 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 13 Jun 2016 13:03:59 -0700 Subject: [PATCH 2/2] Add localhost tests to Travis. Run the local servers and verify they respond with code 200 to the "/" path. Note: bookshelf does not yet run in Travis, since we'll need to set up client secrets credentials. --- .travis.yml | 11 +++++++- java-repo-tools/scripts/test-localhost.sh | 13 ++++----- travis.sh | 32 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100755 travis.sh diff --git a/.travis.yml b/.travis.yml index d3dd9d5f4..4f54445a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,16 @@ language: java jdk: - oraclejdk8 -script: mvn --batch-mode clean verify | egrep -v "(^\[INFO\] Download|^\[INFO\].*skipping)" +addons: + apt: + packages: + - expect +# Skip the install step, since Maven will download the dependencies we need +# when the test build runs. +# http://stackoverflow.com/q/31945809/101923 +# https://docs.travis-ci.com/user/customizing-the-build/#Skipping-the-Installation-Step +install: true +script: ./travis.sh after_success: - bash <(curl -s https://codecov.io/bash) branches: diff --git a/java-repo-tools/scripts/test-localhost.sh b/java-repo-tools/scripts/test-localhost.sh index 9615333c1..742e0df3a 100755 --- a/java-repo-tools/scripts/test-localhost.sh +++ b/java-repo-tools/scripts/test-localhost.sh @@ -16,12 +16,8 @@ # Usage: # test-localhost.sh deployment-type path/to/project -- [maven arguments] # -# This script runs the local appengine:devserver Maven plugin and verifies that -# a request to http://localhost:8080/ does not return an error code. -# -# As an example, this is useful for verifying that datastore-indexes.xml is -# correct (only if autoGenerate=false and the / handler does all queries used), -# as an example. +# This script runs a localhost server Maven plugin and verifies that a request +# to http://localhost:8080/ does not return an error code. print_usage () { echo "Usage:" >&2 @@ -30,6 +26,7 @@ print_usage () { echo "server-type can be any of the following:" >&2 echo " appengine" >&2 echo " jetty" >&2 + echo " spring-boot" >&2 } if [[ -z "$1" ]]; then @@ -46,6 +43,10 @@ case $1 in mvn_plugin="jetty:run-exploded" server_started_message="Started Jetty Server" ;; + spring-boot) + mvn_plugin="spring-boot:run" + server_started_message="Tomcat started on port(s): 8080 (http)" + ;; *) print_usage exit 1 diff --git a/travis.sh b/travis.sh new file mode 100755 index 000000000..c2cfd07c8 --- /dev/null +++ b/travis.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e +set -x +# Set pipefail so that `egrep` does not eat the exit code. +set -o pipefail + +mvn --batch-mode clean verify | egrep -v "(^\[INFO\] Download|^\[INFO\].*skipping)" + +# Test running samples on localhost. +if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then + # The bookshelf sample requires Cloud Datastore access. Enable when + # credentials are available (such as branch PRs). + ./java-repo-tools/scripts/test-localhost.sh jetty bookshelf -- -Plocal +fi +./java-repo-tools/scripts/test-localhost.sh jetty helloworld-jsp +./java-repo-tools/scripts/test-localhost.sh jetty helloworld-servlet +./java-repo-tools/scripts/test-localhost.sh spring-boot helloworld-springboot +