Skip to content

Commit 4c4ab60

Browse files
committed
Log testing
1 parent d2ec597 commit 4c4ab60

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

scripts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ build_test() {
190190
--build-arg RUNNER_BASE_IMAGE=${runnerBaseImage} \
191191
--build-arg PORT=${port} \
192192
--build-arg SSL_PORT=${sslPort} \
193+
--build-arg platform="linux/amd64" \
193194
${dockerArgs}
194195
}
195196

@@ -239,9 +240,16 @@ get_port() {
239240
endPort=$((startPort + 100))
240241

241242
for p in $(seq ${startPort} ${endPort}); do
242-
if ! ss -ln | grep -q ":${p} "; then
243-
echo ${p}
244-
break
243+
if [ "$(uname)" == "Darwin" ]; then
244+
if ! lsof -i -P | grep LISTEN | grep -q ":${p} "; then
245+
echo ${p}
246+
break
247+
fi
248+
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
249+
if ! ss -ln | grep -q ":${p} "; then
250+
echo ${p}
251+
break
252+
fi
245253
fi
246254
done
247255
}

test/etc/nginx/conf.d/test.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error_log /var/log/nginx/debug.log debug;
22
access_log /var/log/nginx/access.log;
33

4+
log_format extractTest 'Log extract test sub: $jwt_claim_sub';
5+
46
server {
57
listen %{PORT};
68
listen %{SSL_PORT} ssl;
@@ -449,4 +451,16 @@ vXjq39xtcIBRTO1c2zs=
449451
auth_jwt_enabled on;
450452
auth_jwt_redirect on;
451453
}
454+
455+
___location /log {
456+
auth_jwt_enabled on;
457+
auth_jwt_redirect off;
458+
auth_jwt_location HEADER=Authorization;
459+
auth_jwt_validate_sub on;
460+
auth_jwt_extract_var_claims sub;
461+
462+
access_log /var/log/nginx/test_access.log extractTest;
463+
464+
return 200 "logged sub";
465+
}
452466
}

test/test.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ run_test () {
2727
local curlCommand=
2828
local exitCode=
2929
local response=
30+
local expectedLogSub=
3031
local testNum="${GRAY}${NUM_TESTS}${NC}\t"
3132

32-
while getopts "n:asp:r:c:x:" option; do
33+
while getopts "n:asp:r:c:x:l:" option; do
3334
case $option in
3435
n)
3536
name=$OPTARG;;
@@ -44,6 +45,8 @@ run_test () {
4445
expectedResponseRegex=$OPTARG;;
4546
x)
4647
extraCurlOpts=$OPTARG;;
48+
l)
49+
expectedLogSub=$OPTARG;;
4750
\?) # Invalid option
4851
printf "Error: Invalid option\n";
4952
exit;;
@@ -81,6 +84,16 @@ run_test () {
8184
if [ "${okay}" == '1' ]; then
8285
printf "${GREEN}${name}";
8386
fi
87+
88+
if ["${expectedLogSub}" != ""]; then
89+
local logEntry=$(tail -n 1 /var/log/nginx/test_access.log)
90+
91+
if ["${logEntry}" != "Log extract test sub:${expectedLogSub}"]; then
92+
printf "${RED}${name} -- log extracted sub is not what is expected:${expectedLogSub}; logged: ${logEntry}"
93+
NUM_FAILED=$((${NUM_FAILED} + 1))
94+
okay=0
95+
fi
96+
fi
8497
fi
8598

8699
if [ "${DEBUG}" == "${NUM_TESTS}" ]; then
@@ -374,6 +387,13 @@ main() {
374387
run_test -n 'return_url includes query when redirected to login' \
375388
-p '/return-url?test=123' \
376389
-r '< Location: https://example\.com/login\?return_url=http://nginx.*/return-url%3Ftest=123'
390+
391+
run_test -n 'acess_log extract valid sub' \
392+
-p '/log' \
393+
-c 200 \
394+
-r 'logged sub' \
395+
-x '--header "Authorization: Bearer ${JWT_HS256_VALID}"' \
396+
-l 'some-long-uuid'
377397

378398
if [[ "${NUM_FAILED}" = '0' ]]; then
379399
printf "\nRan ${NUM_TESTS} tests successfully (skipped ${NUM_SKIPPED}).\n"

0 commit comments

Comments
 (0)