forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-common.sh
184 lines (164 loc) · 4.16 KB
/
verify-common.sh
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
181
182
183
184
#!/bin/bash -e
DELAY="${DELAY:-0}"
DOCKER_NO_PULL="${DOCKER_NO_PULL:-}"
MANUAL="${MANUAL:-}"
NAME="${NAME:-}"
PATHS="${PATHS:-.}"
UPARGS="${UPARGS:-}"
DOCKER_COMPOSE="${DOCKER_COMPOSE:-docker-compose}"
run_log () {
echo -e "\n> [${NAME}] ${*}"
}
bring_up_example_stack () {
local args path up_args
args=("${UPARGS[@]}")
path="$1"
read -ra up_args <<< "up --quiet-pull --build -d ${args[*]}"
if [[ -z "$DOCKER_NO_PULL" ]]; then
run_log "Pull the images ($path)"
"$DOCKER_COMPOSE" pull -q
echo
fi
run_log "Bring up services ($path)"
"$DOCKER_COMPOSE" "${up_args[@]}" || return 1
echo
}
bring_up_example () {
local path paths
read -ra paths <<< "$(echo "$PATHS" | tr ',' ' ')"
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
bring_up_example_stack "$path" || {
echo "ERROR: starting ${NAME} ${path}" >&2
return 1
}
popd > /dev/null || return 1
done
if [[ "$DELAY" -ne "0" ]]; then
run_log "Snooze for ${DELAY} while ${NAME} gets started"
sleep "$DELAY"
fi
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
"$DOCKER_COMPOSE" ps
"$DOCKER_COMPOSE" logs
popd > /dev/null || return 1
done
}
cleanup_stack () {
local path
path="$1"
run_log "Cleanup ($path)"
"$DOCKER_COMPOSE" down --remove-orphans
}
cleanup () {
local path paths
read -ra paths <<< "$(echo "$PATHS" | tr ',' ' ')"
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
cleanup_stack "$path" || {
echo "ERROR: cleanup ${NAME} ${path}" >&2
return 1
}
popd > /dev/null
done
}
_curl () {
local arg curl_command
curl_command=(curl -s)
if [[ ! "$*" =~ "-X" ]]; then
curl_command+=(-X GET)
fi
for arg in "${@}"; do
curl_command+=("$arg")
done
"${curl_command[@]}" || {
echo "ERROR: curl (${curl_command[*]})" >&2
return 1
}
}
responds_with () {
local expected response
expected="$1"
shift
response=$(_curl "${@}")
grep -s "$expected" <<< "$response" || {
echo "ERROR: curl (${*})" >&2
echo "EXPECTED: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_without () {
local expected response
expected="$1"
shift
response=$(_curl "${@}")
# shellcheck disable=2266
grep -s "$expected" <<< "$response" | [[ "$(wc -l)" -eq 0 ]] || {
echo "ERROR: curl (${*})" >&2
echo "DID NOT EXPECT: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_with_header () {
local expected response
expected="$1"
shift
response=$(_curl --head "${@}")
grep -s "$expected" <<< "$response" || {
echo "ERROR: curl (${*})" >&2
echo "EXPECTED HEADER: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_without_header () {
local expected response
expected="$1"
shift
response=$(_curl --head "${@}")
# shellcheck disable=2266
grep -s "$expected" <<< "$response" | [[ "$(wc -l)" -eq 0 ]] || {
echo "ERROR: curl (${*})" >&2
echo "DID NOT EXPECT HEADER: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
wait_for () {
local i=1 returns=1 seconds="$1"
shift
while ((i<=seconds)); do
if "${@}" &> /dev/null; then
returns=0
break
else
sleep 1
((i++))
fi
done
if [[ "$returns" != 0 ]]; then
echo "Wait (${seconds}) failed: ${*}" >&2
fi
return "$returns"
}
trap 'cleanup' EXIT
if [[ -z "$NAME" ]]; then
echo "ERROR: You must set the '$NAME' variable before sourcing this script" >&2
exit 1
fi
if [[ -z "$MANUAL" ]]; then
bring_up_example
fi
# These allow the functions to be used in subshells, e.g. in `wait_for`
export -f responds_with
export -f responds_without
export -f responds_with_header
export -f responds_without_header
export -f _curl