forked from cloudendpoints/esp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinux-start-local-test
executable file
·180 lines (156 loc) · 6.39 KB
/
linux-start-local-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
#
# Copyright (C) Extensible Service Proxy Authors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
################################################################################
#
[[ "$(uname)" == 'Linux' ]] || { echo 'Run on Linux only.'; exit 1; }
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
. "${ROOT}/script/jenkins-utilities" || { echo 'Cannot load jenkins utilities'; exit 1; }
TEST_ENV=""
# This variable can be set by -m option. If set to a positive value, the nginx
# and the backend will be kept alive for that much time for manual test. And
# this script will skip running the loadtest client esp_client.py.
MANUAL_TEST_DURATION_MINUTES=0
function quit_nginx {
echo "Shutting down Nginx."
"${NGINX}" "${NGINX_ARGS[@]}" -s quit
}
function tar_logs() {
local tar_file="${1}"
local path_to_tar="${2}"
echo "Taring local logs from ${path_to_tar}."
tar czf "${tar_file}" "${path_to_tar}" \
|| echo "Unable to tar {path_to_tar}."
}
while getopts :b:m:t: arg; do
case ${arg} in
b) BUCKET="${OPTARG}";;
m) MANUAL_TEST_DURATION_MINUTES="${OPTARG}";;
t) TEST_ENV="$(realpath "${OPTARG}")";;
esac
done
echo 'Tweaking Linux net interface'
${SUDO} sh -c 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse'
${SUDO} sh -c 'echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle'
${SUDO} sh -c 'echo "1024 65000" > /proc/sys/net/ipv4/ip_local_port_range'
NGINX=${ROOT}/nginx-esp
gsutil cp gs://client-secret-files/esp-load-test-08fcbb64ace1.json \
"${ROOT}/test/echo/gce-vm/client-secret.json" \
|| error_exit "Failed to download credentials."
set_api_keys
API_KEY="${ESP_LOAD_TEST_API_KEY}"
[[ -n "${API_KEY}" ]] || error_exit 'Could not set api key.'
NGINX_CONFIG="${ROOT}/test/echo/gce-vm/nginx.conf"
PREFIX="${ROOT}/.nginx"
# clean up the logs before the run
rm "${PREFIX}/logs/error.log"
rm "${PREFIX}/logs/backend.log"
mkdir --parents --mode=777 "${PREFIX}/logs" \
|| error_exit 'Cannot create NGINX logs directory.'
echo 'Starting backend.'
pushd test/echo
npm install
node echo.js 2>&1 | tee "${PREFIX}/logs/backend.log" &
trap 'kill %node' EXIT
popd
NGINX_BAZEL_BIN="${ROOT}/bazel-bin/src/nginx/main/nginx-esp"
if ! [[ -e "${NGINX_BAZEL_BIN}" ]]; then
(
echo 'Building Endpoints Runtime.'
cd "${ROOT}" \
&& retry -n 3 ${BAZEL} ${BAZEL_ARGS} fetch //src/nginx/main:nginx-esp \
&& retry -n 2 ${BAZEL} ${BAZEL_ARGS} build ${BAZEL_BUILD_ARGS} \
--config=release //src/nginx/main:nginx-esp
) || error_exit 'Failed to build //src/nginx/main:nginx-esp'
fi
cp -f "${NGINX_BAZEL_BIN}" "${NGINX}" \
|| error_exit 'Error copying nginx-esp, aborting.'
echo 'Creating service.json'
# Hard code Config_ID.
SERVICE_NAME='echo-dot-esp-load-test.appspot.com'
CONFIG_ID='2017-07-12r0'
activate_service_account "esp-load-test"
${GCLOUD} service-management configs describe --service ${SERVICE_NAME} ${CONFIG_ID} \
--project=esp-load-test --format=json > "${ROOT}/test/echo/gce-vm/service.json"
cat ${ROOT}/test/echo/gce-vm/service.json
echo 'Starting Endpoints Runtime.'
NGINX_ARGS=(-p "${PREFIX}" -c "${NGINX_CONFIG}" -g "pid ${PREFIX}/.nginx.pid;")
"${NGINX}" "${NGINX_ARGS[@]}" -T \
|| error_exit 'Nginx configuration test failed.'
"${NGINX}" "${NGINX_ARGS[@]}"
echo 'Constructing test data.'
JWT_TOKEN="$("${ROOT}/client/custom/gen-auth-token.sh" \
-s "$(get_test_client_key ${ROOT}/test/echo/esp-echo-client-secret-jwk.json)" \
-a "${SERVICE_NAME}")"
POST_FILE="${ROOT}/test/data/8k.json"
HOST='http://127.0.0.1:8090'
#Status server always run in port 8090 and use HTTP protocol.
STATUS_SERVER=${HOST/8080/8090}
STATUS_SERVER=${STATUS_SERVER/443/8090}
STATUS_SERVER=${STATUS_SERVER/https/http}
trap quit_nginx EXIT
echo "ESP status: ${STATUS_SERVER}/endpoints_status"
retry -s 1 -t 5 \
check_http_service ${STATUS_SERVER}/endpoints_status 200 \
|| error_exit "ESP is not running"
if [[ ${MANUAL_TEST_DURATION_MINUTES} -gt 0 ]]; then
echo 'ESP listens at port 8090. You can start your manual test against it.'
echo "ESP will quit after ${DURATION_MINUTES} minutes or by Ctrl-C."
echo "NGINX error log can be found here: ${PREFIX}/logs/error.log"
sleep `expr "${MANUAL_TEST_DURATION_MINUTES}" \* 60`
else
echo 'Start basic testing'
cat "${TEST_ENV}"
pushd "${ROOT}/test/client"
./esp_client.py \
--test_env="${TEST_ENV}" \
--host=${HOST} \
--api_key=${API_KEY} \
--auth_token=${JWT_TOKEN} \
--post_file=${POST_FILE}
EXIT_STATUS=${?}
popd
fi
# Quitting nginx before gathering logs
quit_nginx
if [[ -n "${BUCKET}" ]] ; then
RUN_ID="$(extract_key_from_test_env_file 'run_id' "${TEST_ENV}")" \
|| error_exit "Could not extract run_id from ${TEST_ENV}"
TEST_ID="$(extract_key_from_test_env_file 'test' "${TEST_ENV}")" \
|| error_exit "Could not extract test from ${TEST_ENV}"
LOG_DIRECTORY="$(mktemp -d /tmp/log_dir_XXXX)"
JSON_FILE="${LOG_DIRECTORY}/${TEST_ID}.json"
create_status_file \
-f "${JSON_FILE}" \
-s ${EXIT_STATUS} \
-t "${TEST_ID}" \
-r "${RUN_ID}" \
|| error_exit "Could not create ${JSON_FILE}."
tar_logs "${LOG_DIRECTORY}/${RUN_ID}.tar.gz" "${PREFIX}/logs/"
gsutil -h 'Content-Type:text/plain' -m cp -r "${LOG_DIRECTORY}" "${BUCKET}/${RUN_ID}" \
|| echo 'Cannot upload logs.'
fi
exit ${EXIT_STATUS}