Skip to content

Commit

Permalink
Capture pod logs for helm-install-rancher.bats
Browse files Browse the repository at this point in the history
Also add more `|| return` clauses in functions called via `try` and `run`.

Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed Jun 28, 2024
1 parent d59616f commit f60d7b5
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions bats/tests/k8s/helm-install-rancher.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local_setup_file() {

local_setup() {
needs_port 443

LOG_NAMESPACE=cattle-system
LOG_APPNAME=rancher

Check failure on line 13 in bats/tests/k8s/helm-install-rancher.bats

View workflow job for this annotation

GitHub Actions / Check Spelling

`APPNAME` is not a recognized word. (unrecognized-spelling)
}

# Check that the rancher-latest/rancher helm chart at the given version is
Expand Down Expand Up @@ -101,22 +104,39 @@ assert_true() {
assert_output --regexp '^([Tt]rue|1)$' || return
}

# Given namespace and app name, assert that a log line contains the given string.
assert_pod_log_line() {
local namespace="$1"
local selector="app=$2"
shift 2
local expect="$*"
run kubectl get pod --namespace "$namespace" --selector "$selector" --output=jsonpath='{.items[0].metadata.name}'
assert_success
# Fetch pod log using LOG_NAMESPACE and LOG_APPNAME

Check failure on line 107 in bats/tests/k8s/helm-install-rancher.bats

View workflow job for this annotation

GitHub Actions / Check Spelling

`APPNAME` is not a recognized word. (unrecognized-spelling)
get_pod_log() {
run kubectl get pod --namespace "$LOG_NAMESPACE" --selector "app=$LOG_APPNAME" \

Check failure on line 109 in bats/tests/k8s/helm-install-rancher.bats

View workflow job for this annotation

GitHub Actions / Check Spelling

`APPNAME` is not a recognized word. (unrecognized-spelling)
--output=jsonpath='{.items[0].metadata.name}'
assert_success || return
assert_output || return
local name="$output"

run kubectl logs --namespace "$namespace" "$name"
run kubectl logs --namespace "$LOG_NAMESPACE" "$name"
assert_success || return
echo "$output"
}

# Given LOG_NAMESPACE and LOG_APPNAME, assert that a log line contains the given string.

Check failure on line 120 in bats/tests/k8s/helm-install-rancher.bats

View workflow job for this annotation

GitHub Actions / Check Spelling

`APPNAME` is not a recognized word. (unrecognized-spelling)
assert_pod_log_line() {
local expect="$*"
run get_pod_log || return
assert_output --partial "$expect" || return
}

capture_pod_log() {
if [[ $status -eq 0 ]]; then
return 0
fi
if capturing_logs; then
local logfile
logfile=$(unique_filename "${PATH_BATS_LOGS}/${BATS_SUITE_TEST_NUMBER}-${LOG_NAMESPACE}-${LOG_APPNAME}" .log)

Check failure on line 133 in bats/tests/k8s/helm-install-rancher.bats

View workflow job for this annotation

GitHub Actions / Check Spelling

`APPNAME` is not a recognized word. (unrecognized-spelling)
trace "capturing ${logfile}"
get_pod_log >"$logfile"
fi
return 1
}

# Pull down the image manually first so we are less likely to time out when
# deploying rancher
pull_rancher_image() {
Expand All @@ -128,22 +148,33 @@ pull_rancher_image() {
try ctrctl pull --quiet "rancher/rancher:v$rancher_chart_version"
}

_wait_for_rancher_pod() {
try assert_pod_log_line 'Listening on :443' || return
try assert_pod_log_line 'Starting catalog controller' || return
try --max 60 --delay 10 assert_pod_log_line 'Watching metadata for rke-machine-config.cattle.io/v1' || return
try --max 60 --delay 10 assert_pod_log_line 'Creating clusterRole for roleTemplate Cluster Owner (cluster-owner).' || return
try assert_pod_log_line 'Rancher startup complete' || return
try assert_pod_log_line 'Created machine for node' || return
}

wait_for_rancher_pod() {
try assert_pod_log_line cattle-system rancher Listening on :443
try assert_pod_log_line cattle-system rancher Starting catalog controller
try --max 60 --delay 10 assert_pod_log_line cattle-system rancher Watching metadata for rke-machine-config.cattle.io/v1
try --max 60 --delay 10 assert_pod_log_line cattle-system rancher 'Creating clusterRole for roleTemplate Cluster Owner (cluster-owner).'
try assert_pod_log_line cattle-system rancher Rancher startup complete
try assert_pod_log_line cattle-system rancher Created machine for node
_wait_for_rancher_pod
capture_pod_log
}

wait_for_webhook_pod() {
try assert_pod_log_line cattle-system rancher-webhook Rancher-webhook version
try assert_pod_log_line cattle-system rancher-webhook Listening on :9443
_wait_for_webhook_pod() {
local LOG_APPNAME=rancher-webhook
try assert_pod_log_line 'Rancher-webhook version' || return
try assert_pod_log_line 'Listening on :9443' || return
# Depending on version, this is either "cattle-webhook-tls" or "cattle-system/cattle-webhook-tls"
try assert_pod_log_line cattle-system rancher-webhook Creating new TLS secret for cattle-
try assert_pod_log_line cattle-system rancher-webhook Active TLS secret cattle-
try assert_pod_log_line cattle-system rancher-webhook 'Sleeping for 15 seconds then applying webhook config'
try assert_pod_log_line 'Creating new TLS secret for cattle-' || return
try assert_pod_log_line 'Active TLS secret cattle-' || return
try assert_pod_log_line 'Sleeping for 15 seconds then applying webhook config' || return
}

wait_for_webhook_pod() {
_wait_for_webhook_pod
LOG_APPNAME=rancher-webhook capture_pod_log
}

deploy_rancher() {
Expand Down

0 comments on commit f60d7b5

Please sign in to comment.