-
Notifications
You must be signed in to change notification settings - Fork 288
/
test-lib-nodejs.sh
608 lines (527 loc) · 18.5 KB
/
test-lib-nodejs.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/bin/bash
#
# Functions for tests for the Node.js image.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
THISDIR=$(dirname ${BASH_SOURCE[0]})
source "${THISDIR}/test-lib.sh"
source "${THISDIR}/test-lib-openshift.sh"
test_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
info() {
echo -e "\n\e[1m[INFO] $@...\e[0m\n"
}
check_prep_result() {
if [ $1 -ne 0 ]; then
ct_update_test_result "[FAILED]" "$2" "preparation"
TESTSUITE_RESULT=1
return $1
fi
}
image_exists() {
docker inspect $1 &>/dev/null
}
container_exists() {
image_exists $(cat $cid_file)
}
container_ip() {
docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
}
container_logs() {
docker logs $(cat $cid_file)
}
run_s2i_build() {
ct_s2i_build_as_df file://${test_dir}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} $(ct_build_s2i_npm_variables) $1
}
run_s2i_build_proxy() {
ct_s2i_build_as_df file://${test_dir}/test-hw ${IMAGE_NAME} ${IMAGE_NAME}-testhw ${s2i_args} $(ct_build_s2i_npm_variables) -e HTTP_PROXY=$1 -e http_proxy=$1 -e HTTPS_PROXY=$2 -e https_proxy=$2
}
run_s2i_build_client() {
ct_s2i_build_as_df_build_args \
"file://${test_dir}/$1" "${IMAGE_NAME}" "${IMAGE_NAME}-$1" "--ulimit nofile=4096:4096" \
${s2i_args} \
$(ct_build_s2i_npm_variables) -e NODE_ENV=development
}
run_s2i_build_binary() {
ct_s2i_build_as_df file://${test_dir}/test-binary ${IMAGE_NAME} ${IMAGE_NAME}-testbinary ${s2i_args} $(ct_build_s2i_npm_variables) $1
}
run_s2i_multistage_build() {
ct_s2i_multistage_build file://${test_dir}/test-app ${FULL_IMAGE} ${IMAGE_NAME} ${IMAGE_NAME}-testapp $(ct_build_s2i_npm_variables)
}
prepare_dummy_git_repo() {
git init
for key in "${!gitconfig[@]}"; do
git config --local "$key" "${gitconfig[$key]}"
done
git add --all
git commit -m "Sample commit"
}
prepare_client_repo() {
git clone \
--config advice.detachedHead="false" \
--branch "$3" --depth 1 \
"$2" "$1"
pushd "$1" >/dev/null || return
for key in "${!gitconfig[@]}"; do
git config --local "$key" "${gitconfig[$key]}"
done
popd >/dev/null || return
}
prepare_minimal_build() {
suffix=$1
# Build the app using the full assemble-capable image
case "$suffix" in
testapp)
run_s2i_multistage_build #>/tmp/build-log 2>&1
;;
testhw)
IMAGE_NAME=$FULL_IMAGE run_s2i_build_proxy http://[email protected]:8000 https://[email protected]:8000 >/tmp/build-log 2>&1
# Get the application from the assembled image and into the minimal
tempdir=$(mktemp -d)
chown 1001:0 "$tempdir"
docker run -u 0 --rm -ti -v "$tempdir:$tempdir:Z" "$FULL_IMAGE-$suffix" bash -c "cp -ar /opt/app-root/src $tempdir"
pushd "$tempdir" >/dev/null || return
cat <<EOF >Dockerfile
FROM $IMAGE_NAME
ADD src/* /opt/app-root/src
CMD /usr/libexec/s2i/run
EOF
# Check if CA autority is present on host and add it into Dockerfile
[ -f "$(full_ca_file_path)" ] && cat <<EOF >>Dockerfile
USER 0
RUN cd /etc/pki/ca-trust/source/anchors && update-ca-trust extract
USER 1001
EOF
docker build -t "$IMAGE_NAME-$suffix" $(ct_build_s2i_npm_variables | grep -o -e '\(-v\)[[:space:]]\.*\S*') .
popd >/dev/null || return
;;
*)
echo "Please specify a valid test application"
return 1
;;
esac
}
prepare() {
if ! image_exists ${IMAGE_NAME}; then
echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
return 1
fi
case "$1" in
# TODO: STI build require the application is a valid 'GIT' repository, we
# should remove this restriction in the future when a file:// is used.
app|hw|express-webapp|binary)
pushd "${test_dir}/test-${1}" >/dev/null
prepare_dummy_git_repo
popd >/dev/null
;;
*)
if [[ "$TEST_LIST_CLIENTS" == *"${test_case}"* ]];
then
PREFIX=$1
PREFIX=${PREFIX//-/}
REPO="${PREFIX^^}"_REPO
REVISION="${PREFIX^^}"_REVISION
prepare_client_repo "${test_dir}/$1" "${!REPO}" "${!REVISION}"
else
echo "Please specify a valid test application"
return 1
fi
;;
esac
}
run_test_application() {
case "$1" in
app|hw|express-webapp|binary)
cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix=.cid)
docker run -d --user=100001 $(ct_mount_ca_file) --rm --cidfile=${cid_file} $2 ${IMAGE_NAME}-test$1
;;
*)
echo "No such test application"
return 1
;;
esac
}
run_client_test_suite() {
cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix=.cid)
local cmd="npm test"
# Skip style check tests
[ "$1" == "prom-client" ] && cmd="sed -i.bak 's/&& npm run check-prettier //g' package.json && $cmd"
docker run --user=100001 $(ct_mount_ca_file) --rm --cidfile=${cid_file} ${IMAGE_NAME}-$1 bash -c "$cmd"
}
kill_test_application() {
docker kill $(cat $cid_file)
rm $cid_file
}
wait_for_cid() {
local max_attempts=20
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
[ -f $cid_file ] && [ -s $cid_file ] && break
echo "Waiting for container start..."
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
}
test_s2i_usage() {
echo "Testing 's2i usage'..."
ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
ct_check_testcase_result $?
}
test_docker_run_usage() {
echo "Testing 'docker run' usage..."
docker run --rm ${IMAGE_NAME} &>/dev/null
ct_check_testcase_result $?
}
test_connection() {
echo "Testing HTTP connection..."
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
echo "Sending GET request to http://$(container_ip):${test_port}/"
response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):${test_port}/)
status=$?
if [ $status -eq 0 ]; then
if [ $response_code -eq 200 ]; then
result=0
fi
break
fi
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
scl_usage() {
# Verify the 'usage' script is working properly when running the base image with 's2i usage ...'
local run_cmd="$1"
local expected="$2"
echo "Testing the image SCL enable ..."
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(cat ${cid_file}) /bin/sh -ic "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
}
function test_scl_usage() {
scl_usage "node --version" "v${VERSION//-minimal/}."
ct_check_testcase_result $?
}
validate_default_value() {
local label=$1
IFS=':' read -a label_vals <<< $(docker inspect -f "{{index .Config.Labels \"$label\"}}" ${IMAGE_NAME})
label_var=${label_vals[0]}
default_label_val=${label_vals[1]}
actual_label_val=$(docker run --rm $IMAGE_NAME /bin/bash -c "echo $"$label_var)
if [ "$actual_label_val" != "$default_label_val" ]; then
echo "ERROR default value for $label with environment variable $label_var; Expected $default_label_val, got $actual_label_val"
return 1
fi
}
# Gets the NODE_ENV environment variable from the container.
get_node_env_from_container() {
local dev_mode="$1"
local node_env="$2"
IFS=':' read -a label_val <<< $(docker inspect -f '{{index .Config.Labels "com.redhat.dev-mode"}}' $IMAGE_NAME)
dev_mode_label_var="${label_val[0]}"
echo $(docker run --rm --env $dev_mode_label_var=$dev_mode --env NODE_ENV=$node_env $IMAGE_NAME /bin/bash -c 'echo "$NODE_ENV"')
}
# Ensures that a docker container run with '--env NODE_ENV=$current_val' produces a NODE_ENV value of $expected when
# DEV_MODE=dev_mode.
validate_node_env() {
local current_val="$1"
local dev_mode_val="$2"
local expected="$3"
actual=$(get_node_env_from_container "$dev_mode_val" "$current_val")
if [ "$actual" != "$expected" ]; then
echo "ERROR default value for NODE_ENV when development mode is $dev_mode_val; should be $expected but is $actual"
return 1
fi
}
test_dev_mode() {
local app=$1
local dev_mode=$2
local node_env=$3
echo "Testing $app DEV_MODE=$dev_mode NODE_ENV=$node_env"
run_test_application $app "-e DEV_MODE=$dev_mode"
wait_for_cid
test_connection
ct_check_testcase_result $?
logs=$(container_logs)
echo ${logs} | grep -q DEV_MODE=$dev_mode
ct_check_testcase_result $?
echo ${logs} | grep -q DEBUG_PORT=5858
ct_check_testcase_result $?
echo ${logs} | grep -q NODE_ENV=$node_env
ct_check_testcase_result $?
kill_test_application
}
test_incremental_build() {
npm_variables=$(ct_build_s2i_npm_variables)
build_log1=$(ct_s2i_build_as_df file://${test_dir}/test-incremental ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} ${npm_variables})
ct_check_testcase_result $?
build_log2=$(ct_s2i_build_as_df file://${test_dir}/test-incremental ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} ${npm_variables} --incremental)
ct_check_testcase_result $?
if [ "$VERSION" == "6" ]; then
# Different npm output for version 6
if echo "$build_log2" | grep -e "\-\- yarn@[0-9\.]*"; then
echo "ERROR Incremental build failed: yarn package is getting installed in incremental build"
ct_check_testcase_result 1
fi
else
first=$(echo "$build_log1" | grep -o -e "added [0-9]* packages" | awk '{ print $2 }')
second=$(echo "$build_log2" | grep -o -e "added [0-9]* packages" | awk '{ print $2 }')
if [ "$first" == "$second" ]; then
echo "ERROR Incremental build failed: both builds installed $first packages"
ct_check_testcase_result 1
fi
fi
}
# test express webapp
run_s2i_build_express_webapp() {
local result
prepare express-webapp
check_prep_result $? express-webapp || return
ct_s2i_build_as_df file://${test_dir}/test-express-webapp ${IMAGE_NAME} ${IMAGE_NAME}-testexpress-webapp ${s2i_args} $(ct_build_s2i_npm_variables)
run_test_application express-webapp
wait_for_cid
ct_test_response http://$(container_ip):${test_port} 200 'Welcome to Express Testing Application'
result=$?
kill_test_application
return $result
}
function test_build_express_webapp() {
echo "Running express webapp test"
run_s2i_build_express_webapp
ct_check_testcase_result $?
}
function test_running_client_js {
echo "Running $1 test suite"
prepare "$1"
check_prep_result $? $1 || return
run_s2i_build_client "$1"
ct_check_testcase_result $?
run_client_test_suite "$1"
ct_check_testcase_result $?
}
function test_client_express() {
echo "Running express client test"
test_running_client_js express
}
function test_client_pino() {
echo "Running pino client test"
test_running_client_js pino
}
function test_client_prom() {
echo "Running prom-client test"
test_running_client_js prom-client
}
function test_client_opossum() {
echo "Running opossum client test"
test_running_client_js opossum
}
function test_client_kube() {
echo "Running kube-service-bindings client test"
test_running_client_js kube-service-bindings
}
function test_client_faas() {
echo "Running faas-js-runtime client test"
test_running_client_js faas-js-runtime
}
function test_client_cloudevents() {
echo "Running CloudEvents client test"
test_running_client_js cloudevents
}
function test_client_fastify() {
if [[ "${VERSION}" == *"minimal"* ]]; then
VERSION=$(echo "${VERSION}" | cut -d "-" -f 1)
fi
if [[ "$VERSION" == "18" ]]; then
if [ "$OS" == "rhel8" ] || [ "$OS" == "rhel9" ]; then
echo "Fastify is not supported in $VERSION and rhel8 and rhel9"
return
fi
fi
echo "Running fastify client test"
test_running_client_js fastify
}
function test_check_build_using_dockerfile() {
info "Check building using a Dockerfile"
ct_test_app_dockerfile ${THISDIR}/examples/from-dockerfile/Dockerfile 'https://github.com/sclorg/nodejs-ex.git' 'Node.js Crud Application' app-src "--ulimit nofile=4096:4096"
ct_check_testcase_result $?
ct_test_app_dockerfile ${THISDIR}/examples/from-dockerfile/Dockerfile.s2i 'https://github.com/sclorg/nodejs-ex.git' 'Node.js Crud Application' app-src "--ulimit nofile=4096:4096"
ct_check_testcase_result $?
}
function test_npm_functionality() {
echo "Testing npm availability"
ct_npm_works
ct_check_testcase_result $?
}
function test_nodemon_removed() {
# Test that the development dependencies (nodemon) have been removed (npm prune)
devdep=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "! test -d ./node_modules/nodemon")
ct_check_testcase_result "$?"
}
function test_nodemon_present() {
# Test that the development dependencies (nodemon) have been removed (npm prune)
devdep=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "test -d ./node_modules/nodemon")
ct_check_testcase_result "$?"
}
function test_npm_cache_cleared() {
# Test that the npm cache has been cleared
cache_loc=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "npm config get cache")
devdep=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "! test -d $cache_loc")
ct_check_testcase_result "$?"
}
function test_npm_cache_exists() {
# Test that the npm cache has been cleared
devdep=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "test -d \$(npm config get cache)")
ct_check_testcase_result "$?"
}
function test_npm_tmp_cleared() {
# Test that the npm tmp has been cleared
devdep=$(docker run --rm ${IMAGE_NAME}-testapp /bin/bash -c "! ls \$(npm config get tmp)/npm-* 2>/dev/null")
ct_check_testcase_result "$?"
}
function test_dev_mode_false_production() {
# DEV_MODE=false NODE_ENV=production
test_dev_mode app false production
}
function test_dev_mode_true_development() {
# DEV_MODE=true NODE_ENV=development
test_dev_mode app true development
}
function test_dev_mode_false_development() {
# DEV_MODE=false NODE_ENV=development
test_dev_mode app false development
}
function test_run_app_application() {
# Verify that the HTTP connection can be established to test application container
run_test_application app
# Wait for the container to write it's CID file
wait_for_cid
}
function test_run_hw_application() {
# Verify that the HTTP connection can be established to test application container
run_test_application hw
# Wait for the container to write it's CID file
wait_for_cid
ct_check_testcase_result $?
kill_test_application
}
function test_run_binary_application() {
# Test is suppressed because of https://github.com/Blizzard/node-rdkafka/issues/910
# The newest version of node-rdkafka works only with gcc-8 and higher
# On RHEL7 and CentOS7 is gcc-4.8
prepare binary
check_prep_result $? binary || return
run_s2i_build_binary
ct_check_testcase_result $?
# Verify that the HTTP connection can be established to test application container
run_test_application binary
# Wait for the container to write it's CID file
wait_for_cid
kill_test_application
}
function test_safe_logging() {
if [[ $(grep redacted /tmp/build-log | wc -l) -eq 4 ]]; then
grep redacted /tmp/build-log
ct_check_testcase_result 0
else
echo "Some proxy log-in credentials were left in log file"
grep Setting /tmp/build-log
ct_check_testcase_result 1
fi
}
function ct_pull_or_import_postgresql() {
postgresql_image="quay.io/sclorg/postgresql-15-c9s"
image_short="postgresql"
image_tag="postgresql:15-c9s"
# Variable CVP is set by CVP pipeline
if [ "${CVP:-0}" -eq "0" ]; then
# In case of container or OpenShift 4 tests
# Pull image before going through tests
# Exit in case of failure, because postgresql container is mandatory
ct_pull_image "${postgresql_image}" "true"
else
# Import postgresql-15-c9s image before running tests on CVP
oc import-image "${image_short}:latest" --from="${postgresql_image}:latest" --insecure=true --confirm
# Tag postgresql image to "postgresql:10" which is expected by test suite
oc tag "${image_short}:latest" "${image_tag}"
fi
}
# Check the imagestream
function test_nodejs_imagestream() {
local ret_val=0
if [[ "${VERSION}" == *"minimal"* ]]; then
VERSION=$(echo "${VERSION}" | cut -d "-" -f 1)
fi
ct_os_test_image_stream_quickstart \
"${THISDIR}/imagestreams/nodejs-${OS%[0-9]*}.json" \
"https://raw.githubusercontent.com/sclorg/nodejs-ex/${BRANCH_TO_TEST}/openshift/templates/nodejs-postgresql-persistent.json" \
"${IMAGE_NAME}" \
'nodejs' \
"Node.js Crud Application" \
8080 http 200 \
"-p SOURCE_REPOSITORY_REF=${BRANCH_TO_TEST} -p SOURCE_REPOSITORY_URL=https://github.com/sclorg/nodejs-ex.git -p NODEJS_VERSION=${VERSION} -p NAME=nodejs-testing
-p DATABASE_USER=testu \
-p DATABASE_PASSWORD=testpwd \
-p DATABASE_ADMIN_PASSWORD=testadminpwd" \
"quay.io/sclorg/postgresql-15-c9s|postgresql:15-c9s"|| ret_val=1
}
function test_nodejs_s2i_container() {
ct_os_test_s2i_app "${IMAGE_NAME}" \
"https://github.com/sclorg/s2i-nodejs-container.git" \
"test/test-app" \
"This is a node.js echo service"
}
function test_nodejs_s2i_app_ex() {
ct_os_test_s2i_app "${IMAGE_NAME}" \
"https://github.com/sclorg/nodejs-ex.git" \
"." \
"Node.js Crud Application"
}
function test_nodejs_s2i_templates() {
local ret_val=0
if [[ "${VERSION}" == *"minimal"* ]]; then
VERSION=$(echo "${VERSION}" | cut -d "-" -f 1)
fi
# TODO
# MongoDB is not supported at all.
# Let's disable it or replace it with mariadb
ct_os_test_template_app "${IMAGE_NAME}" \
"https://raw.githubusercontent.com/sclorg/nodejs-ex/${BRANCH_TO_TEST}/openshift/templates/nodejs-postgresql-persistent.json" \
nodejs \
"Node.js Crud Application" \
8080 http 200 \
"-p SOURCE_REPOSITORY_REF=${BRANCH_TO_TEST} -p SOURCE_REPOSITORY_URL=https://github.com/sclorg/nodejs-ex.git -p NODEJS_VERSION=${VERSION} -p NAME=nodejs-testing
-p DATABASE_USER=testu \
-p DATABASE_PASSWORD=testpwd \
-p DATABASE_ADMIN_PASSWORD=testadminpwd" \
"quay.io/sclorg/postgresql-15-c9s|postgresql:15-c9s" || ret_val=1
return $ret_val
}
function test_latest_imagestreams() {
local result=1
# Switch to root directory of a container
pushd "${test_dir}/.." >/dev/null || return 1
ct_check_latest_imagestreams
result=$?
popd >/dev/null || return 1
return $result
}
# vim: set tabstop=2:shiftwidth=2:expandtab: