@@ -18,6 +18,7 @@ RELEASE_NAME="stac-fastapi-test"
1818NAMESPACE=" stac-fastapi"
1919BACKEND=${BACKEND:- " elasticsearch" }
2020MATRIX_MODE=${MATRIX_MODE:- false}
21+ DEBUG_BACKEND_LOGS=${DEBUG_BACKEND_LOGS:- false}
2122
2223# Functions
2324log_info () {
@@ -36,6 +37,51 @@ log_error() {
3637 echo -e " ${RED} [ERROR]${NC} $1 "
3738}
3839
40+ collect_backend_logs () {
41+ local backend=" $1 "
42+ local stage=" ${2:- post-install} "
43+
44+ log_info " Collecting ${backend} backend logs (${stage} )..."
45+
46+ set +e
47+
48+ kubectl get pods -n " $NAMESPACE " -o wide || log_warning " Failed to list pods in namespace $NAMESPACE "
49+
50+ local pod_resources
51+ pod_resources=$( kubectl get pods -n " $NAMESPACE " -l " release=$RELEASE_NAME " -o name 2> /dev/null | grep " $backend " || true)
52+
53+ if [[ -z " $pod_resources " ]]; then
54+ log_warning " No pods matching backend '$backend ' found for log collection"
55+ set -e
56+ return
57+ fi
58+
59+ while read -r pod_resource; do
60+ [[ -z " $pod_resource " ]] && continue
61+ local pod=${pod_resource# pod/ }
62+
63+ log_info " ----- Logs for pod $pod (all containers) -----"
64+ kubectl logs -n " $NAMESPACE " " $pod " --all-containers --tail=200 || log_warning " Failed to fetch logs for $pod "
65+
66+ log_info " ----- Describe pod $pod -----"
67+ kubectl describe pod " $pod " -n " $NAMESPACE " || log_warning " Failed to describe $pod "
68+ done <<< " $pod_resources"
69+
70+ log_info " ----- Recent events in namespace $NAMESPACE -----"
71+ kubectl get events -n " $NAMESPACE " --sort-by=.metadata.creationTimestamp | tail -n 50 || log_warning " Failed to fetch events"
72+
73+ set -e
74+ }
75+
76+ maybe_collect_backend_logs () {
77+ local backend=" $1 "
78+ local stage=" $2 "
79+
80+ if [[ " $DEBUG_BACKEND_LOGS " == " true" ]]; then
81+ collect_backend_logs " $backend " " $stage "
82+ fi
83+ }
84+
3985# Help function
4086show_help () {
4187 cat << EOF
@@ -111,6 +157,11 @@ while [[ $# -gt 0 ]]; do
111157 esac
112158done
113159
160+ # Enable debug logs automatically during matrix runs unless explicitly disabled
161+ if [[ " $MATRIX_MODE " == " true" && " $DEBUG_BACKEND_LOGS " == " false" ]]; then
162+ DEBUG_BACKEND_LOGS=true
163+ fi
164+
114165# Validate backend
115166if [[ " $BACKEND " != " elasticsearch" && " $BACKEND " != " opensearch" ]]; then
116167 log_error " Invalid backend: $BACKEND . Must be 'elasticsearch' or 'opensearch'"
@@ -512,27 +563,33 @@ run_backend_test() {
512563 log_info " Step 3: Installing chart for $backend ..."
513564 if ! install_chart; then
514565 log_error " Installation failed for $backend "
566+ collect_backend_logs " $backend " " install-failure"
515567 return 1
516568 fi
569+
570+ maybe_collect_backend_logs " $backend " " post-install"
517571
518572 # Step 4: Validate deployment
519573 log_info " Step 4: Validating deployment for $backend ..."
520574 if ! validate_deployment; then
521575 log_error " Deployment validation failed for $backend "
576+ collect_backend_logs " $backend " " validation-failure"
522577 test_failed=true
523578 fi
524579
525580 # Step 5: Load and test data
526581 log_info " Step 5: Testing data operations for $backend ..."
527582 if ! load_sample_data; then
528583 log_error " Data operations failed for $backend "
584+ collect_backend_logs " $backend " " data-operations-failure"
529585 test_failed=true
530586 fi
531587
532588 # Step 6: Test backend-specific functionality
533589 log_info " Step 6: Testing $backend -specific functionality..."
534590 if ! test_backend_specifics " $backend " ; then
535591 log_error " Backend-specific tests failed for $backend "
592+ collect_backend_logs " $backend " " backend-specific-failure"
536593 test_failed=true
537594 fi
538595
0 commit comments