@@ -146,12 +146,32 @@ await_operator() {
146146 " ${COMMAND} " wait --for=condition=available deployment/" ${DEPLOYMENT_NAME} " -n " ${NAMESPACE} " --timeout=5m
147147}
148148
149+ # shellcheck disable=SC2329 # Function is invoked indirectly via trap
149150uninstall_operator () {
150151 echo " Uninstalling sail-operator (KUBECONFIG=${KUBECONFIG} )"
151152 helm uninstall sail-operator --namespace " ${NAMESPACE} "
152153 " ${COMMAND} " delete namespace " ${NAMESPACE} "
153154}
154155
156+ # Ensure cleanup always runs and that the original test exit code is preserved
157+ # shellcheck disable=SC2329 # Function is invoked indirectly via trap
158+ cleanup () {
159+ # Do not let cleanup errors affect the final exit code
160+ set +e
161+ if [ " ${OLM} " != " true" ] && [ " ${SKIP_DEPLOY} " != " true" ]; then
162+ if [ " ${MULTICLUSTER} " == true ]; then
163+ KUBECONFIG=" ${KUBECONFIG} " uninstall_operator || true
164+ # shellcheck disable=SC2153 # KUBECONFIG2 is set by multicluster setup scripts
165+ KUBECONFIG=" ${KUBECONFIG2} " uninstall_operator || true
166+ else
167+ uninstall_operator || true
168+ fi
169+ fi
170+ echo " JUnit report: ${ARTIFACTS} /report.xml"
171+ }
172+
173+ trap cleanup EXIT INT TERM
174+
155175# Main script flow
156176check_arguments " $@ "
157177parse_flags " $@ "
@@ -231,24 +251,12 @@ if [ "${OLM}" != "true" ] && [ "${SKIP_DEPLOY}" != "true" ]; then
231251fi
232252
233253set +e
234- # Disable to avoid fail the test run and not generate the report.xml
235- # We need to catch the exit code to be able to generate the report
254+ # Disable to avoid failing the test run before generating the report.xml
255+ # Capture the test exit code and allow cleanup via trap to run
236256# shellcheck disable=SC2086
237257IMAGE=" ${HUB} /${IMAGE_BASE} :${TAG} " \
238258go run github.com/onsi/ginkgo/v2/ginkgo -tags e2e \
239259--timeout 60m --junit-report=" ${ARTIFACTS} /report.xml" ${GINKGO_FLAGS:- } " ${WD} " /...
240260TEST_EXIT_CODE=$?
241- set -e
242-
243- if [ " ${OLM} " != " true" ] && [ " ${SKIP_DEPLOY} " != " true" ]; then
244- if [ " ${MULTICLUSTER} " == true ]; then
245- KUBECONFIG=" ${KUBECONFIG} " uninstall_operator
246- KUBECONFIG=" ${KUBECONFIG2} " uninstall_operator
247- else
248- uninstall_operator
249- fi
250- fi
251-
252261
253- echo " JUnit report: ${ARTIFACTS} /report.xml"
254- exit ${TEST_EXIT_CODE}
262+ exit " ${TEST_EXIT_CODE} "
0 commit comments