improve test
This commit is contained in:
@@ -17,11 +17,11 @@ tasks:
|
|||||||
Example: task init
|
Example: task init
|
||||||
cmds:
|
cmds:
|
||||||
- |
|
- |
|
||||||
PROJECT_ID="${1:-{{.DEFAULT_PROJECT_ID}}}"
|
PROJECT_ID="${1:-{{ .DEFAULT_PROJECT_ID }}}"
|
||||||
gcloud auth login --update-adc
|
gcloud auth login --update-adc
|
||||||
gcloud config set project "$PROJECT_ID"
|
gcloud config set project "$PROJECT_ID"
|
||||||
gcloud config set compute/region {{.REGION}}
|
gcloud config set compute/region {{ .REGION }}
|
||||||
gcloud config set compute/zone {{.REGION}}-c
|
gcloud config set compute/zone {{ .REGION }}-c
|
||||||
gcloud auth application-default set-quota-project "$PROJECT_ID"
|
gcloud auth application-default set-quota-project "$PROJECT_ID"
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
@@ -40,9 +40,9 @@ tasks:
|
|||||||
Example: task deploy
|
Example: task deploy
|
||||||
cmds:
|
cmds:
|
||||||
- |
|
- |
|
||||||
gcloud run deploy {{.SERVICE_NAME}} \
|
gcloud run deploy {{ .SERVICE_NAME }} \
|
||||||
--source . \
|
--source . \
|
||||||
--region {{.REGION}} \
|
--region {{ .REGION }} \
|
||||||
--allow-unauthenticated
|
--allow-unauthenticated
|
||||||
|
|
||||||
map-domain:
|
map-domain:
|
||||||
@@ -55,9 +55,9 @@ tasks:
|
|||||||
cmds:
|
cmds:
|
||||||
- |
|
- |
|
||||||
gcloud beta run domain-mappings create \
|
gcloud beta run domain-mappings create \
|
||||||
--service {{.SERVICE_NAME }} \
|
--service {{ .SERVICE_NAME }} \
|
||||||
--domain {{ .SERVICE_NAME }}.{{ .DOMAIN }} \
|
--domain {{ .SERVICE_NAME }}.{{ .DOMAIN }} \
|
||||||
--region {{.REGION}}
|
--region {{ .REGION }}
|
||||||
|
|
||||||
logs:
|
logs:
|
||||||
desc: Tail the real-time logs for the service
|
desc: Tail the real-time logs for the service
|
||||||
@@ -68,8 +68,8 @@ tasks:
|
|||||||
Example: task logs
|
Example: task logs
|
||||||
cmds:
|
cmds:
|
||||||
- |
|
- |
|
||||||
gcloud beta run services logs tail {{.SERVICE_NAME}} \
|
gcloud beta run services logs tail {{ .SERVICE_NAME }} \
|
||||||
--region {{.REGION}}
|
--region {{ .REGION }}
|
||||||
|
|
||||||
test:
|
test:
|
||||||
desc: Test the deployed service
|
desc: Test the deployed service
|
||||||
@@ -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
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
|
||||||
# Explicitly return 0 so the task does not fail if a test fails
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user