-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.sh
executable file
·290 lines (236 loc) · 6.3 KB
/
test.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
#
# Jenkins configuration test runner
#
# Andrew Martin, 2017-12-29 15:38:15
#
## Usage: %SCRIPT_NAME% [options] filename
##
## Options:
## -d, --description [string] Description
## -t, --type [bash] Template type to create
## --debug More debug
##
## -h --help Display this message
##
# exit on error or pipe failure
set -eo pipefail
# error on unset variable
set -o nounset
# error on clobber
set -o noclobber
# user defaults
DEBUG=0
# resolved directory and self
declare -r DIR=$(cd "$(dirname "$0")" && pwd)
declare -r THIS_SCRIPT="${DIR}/$(basename "$0")"
# required defaults
declare -a ARGUMENTS
EXPECTED_NUM_ARGUMENTS=0
ARGUMENTS=()
CONTAINER_ID=''
PORT="8080"
READY_LOG_TEXT="Jenkins provided DSL script setup complete"
FAILED_LOG_TEXT=(
"Failed to run script file"
"WARNING: GroovyInitScript.init failed"
)
export CONTAINER_TAG=latest
main() {
handle_arguments "$@"
trap cleanup EXIT
build_jenkins
info "Starting Jenkins..."
start_jenkins
wait_until_initialised
sleep 3
info "Running tests..."
run_tests
success "Test complete"
}
build_jenkins() {
make build-with-cache
}
start_jenkins() {
make test-run
info "Waiting for cid file..."
until [[ -f /tmp/jenkins-test.cid ]]; do
sleep 0.2
done
CONTAINER_ID=$(cat /tmp/jenkins-test.cid)
info "CONTAINER_ID: ${CONTAINER_ID}"
}
cleanup() {
docker ps -qf "id=${CONTAINER_ID}" \
| xargs --no-run-if-empty docker kill
}
wait_until_initialised() {
local IS_FAILED=0
local IS_OUTPUT=0
local START_TIME="${SECONDS}"
while IFS='' read LINE; do
if echo "${LINE}" | grep --color=always -E "${READY_LOG_TEXT}"; then
success "Jenkins is ready"
break
fi
IS_OUTPUT=0
OLD_IFS="${IFS}"
IFS=$'\n'
for THIS_FAILED_LOG_TEXT in "${FAILED_LOG_TEXT[@]}"; do
if echo "${LINE}" | grep --color=always -E "${THIS_FAILED_LOG_TEXT}"; then
warning "Error in log output"
IS_FAILED=$((IS_FAILED + 1))
IS_OUTPUT=1
fi
done
IFS="${OLD_IFS}"
if [[ "${IS_OUTPUT}" == 0 ]]; then
echo -e "${LINE}" | grep --color=always -E '^|INFO: !!!.*' || true
fi
if [[ "${IS_FAILED}" -gt 30 ]]; then
error "Failed"
break
elif [[ "${IS_FAILED}" -gt 0 ]]; then
IS_FAILED=$((IS_FAILED + 1))
fi
if [[ $(( SECONDS - START_TIME )) -gt 200 ]]; then
error "Timeout reached, test took over 200s"
fi
done < <(docker logs -f "${CONTAINER_ID}" 2>&1)
if [[ "${IS_FAILED}" == 0 ]]; then
return 0
else
return 1
fi
}
# TODO: bats suite
run_tests() {
local HTTP_STATUS_CODE
local HOST
local COUNT=0
HOST=$(docker inspect -f "{{.NetworkSettings.IPAddress}}" jenkins-test)
while HTTP_STATUS_CODE=$(curl -o /dev/null \
--silent \
--head \
--write-out '%{http_code}\n' \
${HOST}:${PORT} || true); do
[[ $(( COUNT++ )) -gt 10 ]] && break
[[ "${HTTP_STATUS_CODE}" = 403 ]] && break
printf '.'
sleep 1
done
if [[ "${HTTP_STATUS_CODE}" != 403 ]]; then
error "Expected 403 from UI. Got ${HTTP_STATUS_CODE}"
fi
success "Pass"
}
handle_arguments() {
[[ $# = 0 && "${EXPECTED_NUM_ARGUMENTS}" -gt 0 ]] && usage
parse_arguments "$@"
validate_arguments "$@"
}
parse_arguments() {
while [ $# -gt 0 ]; do
case $1 in
-h | --help) usage ;;
--debug)
DEBUG=1
set -xe
;;
-d | --description)
shift
not_empty_or_usage "${1:-}"
DESCRIPTION="${1}"
;;
-t | --type)
shift
not_empty_or_usage "${1:-}"
case $1 in
bash) FILETYPE=bash ;;
*) usage "Template type '${1}' not recognised" ;;
esac
;;
--)
shift
break
;;
-*) usage "${1}: unknown option" ;;
*) ARGUMENTS+=("$1") ;;
esac
shift
done
}
validate_arguments() {
[[ "${EXPECTED_NUM_ARGUMENTS}" -gt 0 && -z "${FILETYPE:-}" ]] && usage "Filetype required"
check_number_of_expected_arguments
[[ "${#ARGUMENTS[@]}" -gt 0 ]] && FILENAME="${ARGUMENTS[0]}" || true
}
# helper functions
usage() {
[ "$*" ] && echo "${THIS_SCRIPT}: ${COLOUR_RED}$*${COLOUR_RESET}" && echo
sed -n '/^##/,/^$/s/^## \{0,1\}//p' "${THIS_SCRIPT}" | sed "s/%SCRIPT_NAME%/$(basename "${THIS_SCRIPT}")/g"
exit 2
} 2>/dev/null
success() {
[ "${*:-}" ] && RESPONSE="$*" || RESPONSE="Unknown Success"
printf "%s\n" "$(log_message_prefix)${COLOUR_GREEN}${RESPONSE}${COLOUR_RESET}"
} 1>&2
info() {
[ "${*:-}" ] && INFO="$*" || INFO="Unknown Info"
printf "%s\n" "$(log_message_prefix)${COLOUR_WHITE}${INFO}${COLOUR_RESET}"
} 1>&2
warning() {
[ "${*:-}" ] && ERROR="$*" || ERROR="Unknown Warning"
printf "%s\n" "$(log_message_prefix)${COLOUR_RED}${ERROR}${COLOUR_RESET}"
} 1>&2
error() {
[ "${*:-}" ] && ERROR="$*" || ERROR="Unknown Error"
printf "%s\n" "$(log_message_prefix)${COLOUR_RED}${ERROR}${COLOUR_RESET}"
exit 3
} 1>&2
error_env_var() {
error "${1} environment variable required"
}
log_message_prefix() {
local TIMESTAMP="[$(date +'%Y-%m-%dT%H:%M:%S%z')]"
local THIS_SCRIPT_SHORT=${THIS_SCRIPT/$DIR/.}
tput bold 2>/dev/null
echo -n "${TIMESTAMP} ${THIS_SCRIPT_SHORT}: "
}
is_empty() {
[[ -z ${1-} ]] && return 0 || return 1
}
not_empty_or_usage() {
is_empty "${1-}" && usage "Non-empty value required" || return 0
}
check_number_of_expected_arguments() {
[[ "${EXPECTED_NUM_ARGUMENTS}" != "${#ARGUMENTS[@]}" ]] && {
ARGUMENTS_STRING="argument"
[[ "${EXPECTED_NUM_ARGUMENTS}" -gt 1 ]] && ARGUMENTS_STRING="${ARGUMENTS_STRING}"s
usage "${EXPECTED_NUM_ARGUMENTS} ${ARGUMENTS_STRING} expected, ${#ARGUMENTS[@]} found"
}
return 0
}
hr() {
printf '=%.0s' $(seq $(tput cols))
echo
}
wait_safe() {
local PIDS="${1}"
for JOB in ${PIDS}; do
wait "${JOB}"
done
}
export CLICOLOR=1
export TERM="xterm-color"
export COLOUR_BLACK=$(tput setaf 0 :-"" 2>/dev/null)
export COLOUR_RED=$(tput setaf 1 :-"" 2>/dev/null)
export COLOUR_GREEN=$(tput setaf 2 :-"" 2>/dev/null)
export COLOUR_YELLOW=$(tput setaf 3 :-"" 2>/dev/null)
export COLOUR_BLUE=$(tput setaf 4 :-"" 2>/dev/null)
export COLOUR_MAGENTA=$(tput setaf 5 :-"" 2>/dev/null)
export COLOUR_CYAN=$(tput setaf 6 :-"" 2>/dev/null)
export COLOUR_WHITE=$(tput setaf 7 :-"" 2>/dev/null)
export COLOUR_RESET=$(tput sgr0 :-"" 2>/dev/null)
main "$@"