improve test
This commit is contained in:
@@ -83,21 +83,29 @@ tasks:
|
|||||||
- |
|
- |
|
||||||
function test_service() {
|
function test_service() {
|
||||||
local path="$1"
|
local path="$1"
|
||||||
|
local url="https://{{.SERVICE_NAME}}.{{.DOMAIN}}/$path"
|
||||||
|
|
||||||
# Use curl to capture both HTTP status code and response body
|
# Use curl to capture response body and append the http_code at the end.
|
||||||
local response=$(curl -s -w "%{http_code}" https://{{.SERVICE_NAME}}.{{.DOMAIN}}/$path)
|
# Using `|| true` prevents Task from failing if curl encounters a network error.
|
||||||
|
local response=$(curl -s -w "%{http_code}" "$url") || true
|
||||||
|
|
||||||
# Extract status code (last 3 chars) and body (everything else)
|
# Extract status code (last 3 chars) and body (everything else)
|
||||||
local status_code="${response:${#response}-3}"
|
local status_code="${response:${#response}-3}"
|
||||||
|
local status_text="ERROR"
|
||||||
local body="${response:0:${#response}-3}"
|
local body="${response:0:${#response}-3}"
|
||||||
|
|
||||||
if [ "$status_code" -lt 399 ]; then
|
# Check if curl failed completely (e.g. DNS failure), resulting in empty output
|
||||||
printf "GET /%s -> [SUCCESS] Status: %s, Body: %s\n" "$path" "$status_code" "$body"
|
if [ -z "$response" ] || [ "${response:${#response}-3}" = "000" ]; then
|
||||||
else
|
body="[Network failure or unresolvable host]"
|
||||||
printf "GET /%s -> [FAILURE] Status: %s, Body: %s\n" "$path" "$status_code" "$body"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Explicitly return 0 so the task does not fail if a test fails
|
# Status code in the 200-399 range is considered a successful response
|
||||||
|
if [ "$status_code" -ge 200 ] && [ "$status_code" -lt 400 ]; then
|
||||||
|
status_text="SUCCESS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "GET /%s -> [%s] Status: %s, Body: %s\n" "$path" "$status_text" "$status_code" "$body"
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user