|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Usage: |
| 17 | +# test-localhost.sh deployment-type path/to/project -- [maven arguments] |
| 18 | +# |
| 19 | +# This script runs a localhost server Maven plugin and verifies that a request |
| 20 | +# to http://localhost:8080/ does not return an error code. |
| 21 | + |
| 22 | +print_usage () { |
| 23 | + echo "Usage:" >&2 |
| 24 | + echo " $0 server-type path/to/project [-- maven arguments]" >&2 |
| 25 | + echo >&2 |
| 26 | + echo "server-type can be any of the following:" >&2 |
| 27 | + echo " appengine" >&2 |
| 28 | + echo " jetty" >&2 |
| 29 | + echo " spring-boot" >&2 |
| 30 | +} |
| 31 | + |
| 32 | +if [[ -z "$1" ]]; then |
| 33 | + echo "Missing server-type parameter." >&2 |
| 34 | + print_usage |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | +case $1 in |
| 38 | + appengine) |
| 39 | + mvn_plugin="appengine:devserver" |
| 40 | + server_started_message="localhost:8080" |
| 41 | + ;; |
| 42 | + jetty) |
| 43 | + mvn_plugin="jetty:run-exploded" |
| 44 | + server_started_message="Started Jetty Server" |
| 45 | + ;; |
| 46 | + spring-boot) |
| 47 | + mvn_plugin="spring-boot:run" |
| 48 | + server_started_message="Tomcat started on port(s): 8080 (http)" |
| 49 | + ;; |
| 50 | + *) |
| 51 | + print_usage |
| 52 | + exit 1 |
| 53 | + ;; |
| 54 | +esac |
| 55 | + |
| 56 | +if [[ -z "$2" ]]; then |
| 57 | + echo "Missing directory parameter." >&2 |
| 58 | + print_usage |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | +code_path=$2 |
| 62 | + |
| 63 | +mvn_command="mvn --batch-mode clean ${mvn_plugin} -DskipTests" |
| 64 | +if [[ "$3" == "--" ]]; then |
| 65 | + shift 3 |
| 66 | + for mvn_arg in "${@}"; do |
| 67 | + mvn_command="${mvn_command} ${mvn_arg}" |
| 68 | + done |
| 69 | +elif [[ -n "$3" ]]; then |
| 70 | + echo "Got unexpected third argument" >&2 |
| 71 | + print_usage |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +set -e |
| 76 | +set -x |
| 77 | + |
| 78 | +( |
| 79 | +cd "$code_path" |
| 80 | +expect -c " |
| 81 | + spawn ${mvn_command} |
| 82 | + set timeout 600 |
| 83 | + expect \"${server_started_message}\" |
| 84 | + "'sleep 10 |
| 85 | + spawn curl --silent --output /dev/stderr --write-out "%{http_code}" http://localhost:8080/ |
| 86 | + expect { |
| 87 | + "200" { |
| 88 | + exit |
| 89 | + } |
| 90 | + } |
| 91 | + exit 1 |
| 92 | + ' |
| 93 | +) |
| 94 | + |
0 commit comments