Skip to content

Commit

Permalink
Wait for traefik before fetching it's IP
Browse files Browse the repository at this point in the history
On Windows get_host() gets the IP address from the traefik service,
and will fail with an empty return value if the service is not yet
ready.

The assert failures in get_host() also don't cause a direct test failure
if $(get_host) is just interpolated into a string, so it needs to be
called by itself.

Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed May 16, 2024
1 parent 34c3c66 commit 68fe53e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 63 deletions.
22 changes: 22 additions & 0 deletions bats/tests/helpers/kubernetes.bash
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ wait_for_kube_deployment_available() {
try assert_kube_deployment_available "$@"
}

traefik_ip() {
local jsonpath='jsonpath={.status.loadBalancer.ingress[0].ip}'
run --separate-stderr kubectl get service traefik --namespace kube-system --output "$jsonpath"
assert_success || return
assert_output || return
echo "$output"
}

traefik_hostname() {
if is_windows; then
local ip
ip=$(traefik_ip) || return
echo "${ip}.sslip.io"
else
echo "localhost"
fi
}

wait_for_traefik() {
try traefik_ip
}

get_k3s_versions() {
if [[ $RD_K3S_VERSIONS == "all" ]]; then
# filter out duplicates; RD only supports the latest of +k3s1, +k3s2, etc.
Expand Down
21 changes: 7 additions & 14 deletions bats/tests/k8s/helm-install-rancher.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ local_setup() {
helm repo update
}

get_host() {
if is_windows; then
local LB_IP
local output='jsonpath={.status.loadBalancer.ingress[0].ip}'
LB_IP=$(kubectl get service traefik --namespace kube-system --output "$output")
assert [ -n "$LB_IP" ] || return
echo "$LB_IP.sslip.io"
else
echo "localhost"
fi
}

deploy_rancher() {
helm upgrade \
--install cert-manager jetstack/cert-manager \
Expand All @@ -34,7 +22,8 @@ deploy_rancher() {
--create-namespace

local host
host=$(get_host) || return
host=$(traefik_hostname) || return

helm upgrade \
--install rancher rancher-latest/rancher \
--version "${RD_RANCHER_IMAGE_TAG#v}" \
Expand All @@ -46,7 +35,10 @@ deploy_rancher() {
}

verify_rancher() {
run try --max 9 --delay 10 curl --insecure --silent --show-error "https://$(get_host)/dashboard/auth/login"
local host
host=$(traefik_hostname) || return

run try --max 9 --delay 10 curl --insecure --silent --show-error "https://${host}/dashboard/auth/login"
assert_success
assert_output --partial "Rancher Dashboard"
run kubectl get secret --namespace cattle-system bootstrap-secret -o json
Expand All @@ -65,6 +57,7 @@ foreach_k3s_version \
factory_reset \
start_kubernetes \
wait_for_kubelet \
wait_for_traefik \
deploy_rancher \
verify_rancher \
uninstall_rancher
24 changes: 9 additions & 15 deletions bats/tests/k8s/spinkube-npm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,13 @@ local_setup() {
MY_APP_IMAGE="ttl.sh/${MY_APP_NAME}:15m"
}

# Get the host name to use to reach Traefik
get_host() {
if is_windows; then
local jsonpath='jsonpath={.status.loadBalancer.ingress[0].ip}'
run --separate-stderr kubectl get service traefik --namespace kube-system --output "$jsonpath"
assert_success || return
assert_output || return
echo "${output}.sslip.io"
else
echo "localhost"
fi
}

@test 'start k8s with spinkube' {
factory_reset
start_kubernetes \
--experimental.container-engine.web-assembly.enabled \
--experimental.kubernetes.options.spinkube
wait_for_kubelet
wait_for_traefik
}

@test 'create sample application' {
Expand All @@ -58,6 +46,9 @@ get_host() {

# TODO replace ingress with port-forwarding
@test 'deploy ingress' {
local host
host=$(traefik_hostname)

kubectl apply --filename - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
Expand All @@ -67,7 +58,7 @@ metadata:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- host: "$(get_host)"
- host: "${host}"
http:
paths:
- path: /
Expand All @@ -81,7 +72,10 @@ EOF
}

@test 'connect to app on localhost' {
run --separate-stderr try curl --connect-timeout 5 --fail "http://$(get_host)"
local host
host=$(traefik_hostname)

run --separate-stderr try curl --connect-timeout 5 --fail "http://${host}"
assert_success
assert_output "Hello from JS-SDK"
}
24 changes: 9 additions & 15 deletions bats/tests/k8s/spinkube.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,13 @@ local_setup() {
needs_port 80
}

# Get the host name to use to reach Traefik
get_host() {
if is_windows; then
local jsonpath='jsonpath={.status.loadBalancer.ingress[0].ip}'
run --separate-stderr kubectl get service traefik --namespace kube-system --output "$jsonpath"
assert_success || return
assert_output || return
echo "${output}.sslip.io"
else
echo "localhost"
fi
}

@test 'start k8s with spinkube' {
factory_reset
start_kubernetes \
--experimental.container-engine.web-assembly.enabled \
--experimental.kubernetes.options.spinkube
wait_for_kubelet
wait_for_traefik
}

@test 'wait for spinkube operator' {
Expand All @@ -38,6 +26,9 @@ get_host() {

# TODO replace ingress with port-forwarding
@test 'deploy ingress' {
local host
host=$(traefik_hostname)

kubectl apply --filename - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
Expand All @@ -47,7 +38,7 @@ metadata:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- host: "$(get_host)"
- host: "${host}"
http:
paths:
- path: /
Expand All @@ -61,7 +52,10 @@ EOF
}

@test 'connect to app on localhost' {
run --separate-stderr try curl --connect-timeout 5 --fail "http://$(get_host)/hello"
local host
host=$(traefik_hostname)

run --separate-stderr try curl --connect-timeout 5 --fail "http://${host}/hello"
assert_success
assert_output "Hello world from Spin!"
}
29 changes: 10 additions & 19 deletions bats/tests/k8s/wasm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ get_runtime_classes() {
fi
}

# Get the host name to use to reach Traefik
get_host() {
if is_windows; then
local jsonpath='jsonpath={.status.loadBalancer.ingress[0].ip}'
run --separate-stderr kubectl get service traefik --namespace kube-system --output "$jsonpath"
assert_success || return
assert_output || return
echo "$output.sslip.io"
else
echo "localhost"
fi
}

@test 'start k8s without wasm support' {
factory_reset
start_kubernetes
Expand All @@ -63,6 +50,8 @@ get_host() {
factory_reset
start_kubernetes --experimental.container-engine.web-assembly.enabled
wait_for_kubelet
wait_for_traefik

try assert_traefik_crd_established
}

Expand Down Expand Up @@ -102,11 +91,10 @@ spec:
EOF
}

@test 'wait for traefik to get IP' {
try get_host
}

@test 'deploy ingress' {
local host
host=$(traefik_hostname)

kubectl apply --filename - <<EOF
apiVersion: v1
kind: Service
Expand All @@ -127,7 +115,7 @@ metadata:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- host: $(get_host)
- host: "$host"
http:
paths:
- path: /
Expand All @@ -141,8 +129,11 @@ EOF
}

@test 'connect to the service' {
local host
host=$(traefik_hostname)

# This can take 100s with old versions of traefik, and 15s with newer ones.
run try curl --silent --fail "http://$(get_host)/hello"
run try curl --silent --fail "http://${host}/hello"
assert_success
assert_output "Hello world from Spin!"
}

0 comments on commit 68fe53e

Please sign in to comment.