Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for failing Nginx tests #343

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions nginx/tests/ServiceLogsClientIp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.io.ByteArrayOutputStream

tasks.named<DockerComposeUp>("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")
Expand All @@ -12,15 +12,28 @@ tasks.named<DockerComposeUp>("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+" "(?<ip>\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")
}
}
}
Loading