Skip to content

Commit f60d7b5

Browse files
committed
Capture pod logs for helm-install-rancher.bats
Also add more `|| return` clauses in functions called via `try` and `run`. Signed-off-by: Jan Dubois <[email protected]>
1 parent d59616f commit f60d7b5

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

bats/tests/k8s/helm-install-rancher.bats

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ local_setup_file() {
88

99
local_setup() {
1010
needs_port 443
11+
12+
LOG_NAMESPACE=cattle-system
13+
LOG_APPNAME=rancher
1114
}
1215

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

104-
# Given namespace and app name, assert that a log line contains the given string.
105-
assert_pod_log_line() {
106-
local namespace="$1"
107-
local selector="app=$2"
108-
shift 2
109-
local expect="$*"
110-
run kubectl get pod --namespace "$namespace" --selector "$selector" --output=jsonpath='{.items[0].metadata.name}'
111-
assert_success
107+
# Fetch pod log using LOG_NAMESPACE and LOG_APPNAME
108+
get_pod_log() {
109+
run kubectl get pod --namespace "$LOG_NAMESPACE" --selector "app=$LOG_APPNAME" \
110+
--output=jsonpath='{.items[0].metadata.name}'
111+
assert_success || return
112112
assert_output || return
113113
local name="$output"
114114

115-
run kubectl logs --namespace "$namespace" "$name"
115+
run kubectl logs --namespace "$LOG_NAMESPACE" "$name"
116116
assert_success || return
117+
echo "$output"
118+
}
119+
120+
# Given LOG_NAMESPACE and LOG_APPNAME, assert that a log line contains the given string.
121+
assert_pod_log_line() {
122+
local expect="$*"
123+
run get_pod_log || return
117124
assert_output --partial "$expect" || return
118125
}
119126

127+
capture_pod_log() {
128+
if [[ $status -eq 0 ]]; then
129+
return 0
130+
fi
131+
if capturing_logs; then
132+
local logfile
133+
logfile=$(unique_filename "${PATH_BATS_LOGS}/${BATS_SUITE_TEST_NUMBER}-${LOG_NAMESPACE}-${LOG_APPNAME}" .log)
134+
trace "capturing ${logfile}"
135+
get_pod_log >"$logfile"
136+
fi
137+
return 1
138+
}
139+
120140
# Pull down the image manually first so we are less likely to time out when
121141
# deploying rancher
122142
pull_rancher_image() {
@@ -128,22 +148,33 @@ pull_rancher_image() {
128148
try ctrctl pull --quiet "rancher/rancher:v$rancher_chart_version"
129149
}
130150

151+
_wait_for_rancher_pod() {
152+
try assert_pod_log_line 'Listening on :443' || return
153+
try assert_pod_log_line 'Starting catalog controller' || return
154+
try --max 60 --delay 10 assert_pod_log_line 'Watching metadata for rke-machine-config.cattle.io/v1' || return
155+
try --max 60 --delay 10 assert_pod_log_line 'Creating clusterRole for roleTemplate Cluster Owner (cluster-owner).' || return
156+
try assert_pod_log_line 'Rancher startup complete' || return
157+
try assert_pod_log_line 'Created machine for node' || return
158+
}
159+
131160
wait_for_rancher_pod() {
132-
try assert_pod_log_line cattle-system rancher Listening on :443
133-
try assert_pod_log_line cattle-system rancher Starting catalog controller
134-
try --max 60 --delay 10 assert_pod_log_line cattle-system rancher Watching metadata for rke-machine-config.cattle.io/v1
135-
try --max 60 --delay 10 assert_pod_log_line cattle-system rancher 'Creating clusterRole for roleTemplate Cluster Owner (cluster-owner).'
136-
try assert_pod_log_line cattle-system rancher Rancher startup complete
137-
try assert_pod_log_line cattle-system rancher Created machine for node
161+
_wait_for_rancher_pod
162+
capture_pod_log
138163
}
139164

140-
wait_for_webhook_pod() {
141-
try assert_pod_log_line cattle-system rancher-webhook Rancher-webhook version
142-
try assert_pod_log_line cattle-system rancher-webhook Listening on :9443
165+
_wait_for_webhook_pod() {
166+
local LOG_APPNAME=rancher-webhook
167+
try assert_pod_log_line 'Rancher-webhook version' || return
168+
try assert_pod_log_line 'Listening on :9443' || return
143169
# Depending on version, this is either "cattle-webhook-tls" or "cattle-system/cattle-webhook-tls"
144-
try assert_pod_log_line cattle-system rancher-webhook Creating new TLS secret for cattle-
145-
try assert_pod_log_line cattle-system rancher-webhook Active TLS secret cattle-
146-
try assert_pod_log_line cattle-system rancher-webhook 'Sleeping for 15 seconds then applying webhook config'
170+
try assert_pod_log_line 'Creating new TLS secret for cattle-' || return
171+
try assert_pod_log_line 'Active TLS secret cattle-' || return
172+
try assert_pod_log_line 'Sleeping for 15 seconds then applying webhook config' || return
173+
}
174+
175+
wait_for_webhook_pod() {
176+
_wait_for_webhook_pod
177+
LOG_APPNAME=rancher-webhook capture_pod_log
147178
}
148179

149180
deploy_rancher() {

0 commit comments

Comments
 (0)