From e8ea204b8ad7d550ee36c7d554137a1768feef9a Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Mon, 15 Jul 2024 13:21:18 +0200 Subject: [PATCH] Fix for failing tests (#343) --- .../ServiceLogsClientIp/build.gradle.kts | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/nginx/tests/ServiceLogsClientIp/build.gradle.kts b/nginx/tests/ServiceLogsClientIp/build.gradle.kts index 2018ffb7..739c7eb2 100644 --- a/nginx/tests/ServiceLogsClientIp/build.gradle.kts +++ b/nginx/tests/ServiceLogsClientIp/build.gradle.kts @@ -3,7 +3,7 @@ import java.io.ByteArrayOutputStream tasks.named("test") { doLast { - // get the docker logs from our nginx service + // Get the docker logs from our nginx service. val outputStream = ByteArrayOutputStream() project.exec { commandLine = baseArguments + listOf("logs") @@ -12,15 +12,28 @@ tasks.named("test") { } val output = outputStream.toString() - // see if the log has a match for the IP we set in the test.sh cURL -H command - val pattern = "nginx-1 | 1.2.3.4" - val matchingLines = output.lines().filter { line -> - line.startsWith(pattern) + // See if the log has a match for the IP we set in the test.sh cURL -H command + // Output is expected for follow the log_format specified in: + // + // nginx/rootfs/etc/confd/templates/nginx.conf.tmpl + // + // Which is: + // log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + // '$status $body_bytes_sent "$http_referer" ' + // '"$http_user_agent" "$http_x_forwarded_for"'; + // + // A literal example: + // + // ::1 - - [14/Jul/2024:11:26:36 +0000] "GET / HTTP/1.1" 404 146 "-" "curl/8.5.0" "1.2.3.4" + val logEntryPattern = """::1 - - \[\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4}\] "GET / HTTP/1\.1" \d{3} \d+ "-" "curl/\d+\.\d+\.\d+" "(?\d+\.\d+\.\d+\.\d+)"""".toRegex() + + val found = output.lines().any { logEntry -> + logEntryPattern.find(logEntry)?.groups?.get("ip")?.value == "1.2.3.4" } - // fail the test if we didn't find any logs with the IP - if (matchingLines.isEmpty()) { - throw GradleException("No lines found starting with '$pattern'") + // Fail the test if we didn't find any logs with the IP. + if (!found) { + throw GradleException("No log entry found where http_x_forwarded_for is set to 1.2.3.4") } } }