|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright Mondoo, Inc. 2026 |
| 3 | +# SPDX-License-Identifier: BUSL-1.1 |
| 4 | + |
| 5 | +# OOM Stress Test Runner |
| 6 | +# |
| 7 | +# Sets up everything via Terraform, then watches for the scan pod to OOM. |
| 8 | +# |
| 9 | +# Prerequisites: |
| 10 | +# - Cluster running with mondoo-operator installed |
| 11 | +# - terraform, kubectl available |
| 12 | +# - MONDOO_API_TOKEN or ~/.config/mondoo/mondoo.yml configured |
| 13 | +# |
| 14 | +# Usage: |
| 15 | +# cd tests/integration/oom-stress |
| 16 | +# cp terraform/terraform.example.tfvars terraform/terraform.tfvars |
| 17 | +# # edit terraform.tfvars with your mondoo_org_id |
| 18 | +# ./run.sh [apply|watch|destroy|status] |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 23 | +TF_DIR="${SCRIPT_DIR}/terraform" |
| 24 | + |
| 25 | +TIMEOUT_SECONDS="${OOM_STRESS_TIMEOUT:-1500}" # 25 minutes |
| 26 | +POLL_INTERVAL=5 |
| 27 | + |
| 28 | +log() { echo "[$(date '+%H:%M:%S')] $*"; } |
| 29 | + |
| 30 | +cmd_apply() { |
| 31 | + log "Running terraform apply..." |
| 32 | + cd "$TF_DIR" |
| 33 | + terraform init -upgrade |
| 34 | + terraform apply -auto-approve |
| 35 | + |
| 36 | + OPERATOR_NS=$(terraform output -raw operator_namespace 2>/dev/null || echo "mondoo-operator") |
| 37 | + MEMORY_LIMIT=$(terraform output -raw scanner_memory_limit) |
| 38 | + IMAGE_COUNT=$(terraform output -raw stress_image_count) |
| 39 | + |
| 40 | + log "" |
| 41 | + log "Infrastructure ready:" |
| 42 | + log " Space: $(terraform output -raw mondoo_space_id)" |
| 43 | + log " Memory limit: ${MEMORY_LIMIT}" |
| 44 | + log " Images: ${IMAGE_COUNT}" |
| 45 | + log "" |
| 46 | + log "Waiting for target pods to be ready..." |
| 47 | + cd "$SCRIPT_DIR" |
| 48 | + |
| 49 | + TARGET_NS=$(cd "$TF_DIR" && terraform output -raw target_namespace) |
| 50 | + kubectl wait --for=condition=Ready pods \ |
| 51 | + -l app.kubernetes.io/part-of=oom-stress-test \ |
| 52 | + -n "$TARGET_NS" \ |
| 53 | + --timeout=300s 2>/dev/null || log "WARNING: some pods may not be ready yet" |
| 54 | + |
| 55 | + log "Run './run.sh watch' to monitor the scan, or it will start automatically." |
| 56 | +} |
| 57 | + |
| 58 | +cmd_watch() { |
| 59 | + cd "$TF_DIR" |
| 60 | + OPERATOR_NS="mondoo-operator" |
| 61 | + if [ -f terraform.tfstate ]; then |
| 62 | + OPERATOR_NS=$(terraform output -raw operator_namespace 2>/dev/null || echo "mondoo-operator") |
| 63 | + fi |
| 64 | + |
| 65 | + SCAN_LABEL="app=mondoo-container-scan,mondoo_cr=oom-stress-test" |
| 66 | + START_TIME=$(date +%s) |
| 67 | + |
| 68 | + log "Watching for scan pod (label: ${SCAN_LABEL})..." |
| 69 | + log "Timeout: ${TIMEOUT_SECONDS}s" |
| 70 | + log "" |
| 71 | + |
| 72 | + while true; do |
| 73 | + ELAPSED=$(( $(date +%s) - START_TIME )) |
| 74 | + if [ "$ELAPSED" -gt "$TIMEOUT_SECONDS" ]; then |
| 75 | + log "TIMEOUT: No scan pod terminated within ${TIMEOUT_SECONDS}s" |
| 76 | + cmd_status |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + POD_COUNT=$(kubectl get pods -n "$OPERATOR_NS" -l "$SCAN_LABEL" --no-headers 2>/dev/null | wc -l | tr -d ' ') |
| 81 | + |
| 82 | + if [ "$POD_COUNT" -eq 0 ]; then |
| 83 | + CJ_EXISTS=$(kubectl get cronjob -n "$OPERATOR_NS" -l "mondoo_cr=oom-stress-test" --no-headers 2>/dev/null | wc -l | tr -d ' ') |
| 84 | + if [ "$CJ_EXISTS" -eq 0 ]; then |
| 85 | + log " No CronJob yet... (${ELAPSED}s) — check operator logs if this persists" |
| 86 | + else |
| 87 | + log " CronJob exists, waiting for Job to spawn... (${ELAPSED}s)" |
| 88 | + fi |
| 89 | + sleep "$POLL_INTERVAL" |
| 90 | + continue |
| 91 | + fi |
| 92 | + |
| 93 | + while IFS= read -r POD_NAME; do |
| 94 | + [ -z "$POD_NAME" ] && continue |
| 95 | + POD_PHASE=$(kubectl get pod -n "$OPERATOR_NS" "$POD_NAME" -o jsonpath='{.status.phase}' 2>/dev/null || echo "Unknown") |
| 96 | + |
| 97 | + TERM_REASON=$(kubectl get pod -n "$OPERATOR_NS" "$POD_NAME" \ |
| 98 | + -o jsonpath='{.status.containerStatuses[0].state.terminated.reason}' 2>/dev/null || true) |
| 99 | + EXIT_CODE=$(kubectl get pod -n "$OPERATOR_NS" "$POD_NAME" \ |
| 100 | + -o jsonpath='{.status.containerStatuses[0].state.terminated.exitCode}' 2>/dev/null || true) |
| 101 | + LAST_REASON=$(kubectl get pod -n "$OPERATOR_NS" "$POD_NAME" \ |
| 102 | + -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}' 2>/dev/null || true) |
| 103 | + |
| 104 | + OOM_KILLED="false" |
| 105 | + if [ "$TERM_REASON" = "OOMKilled" ] || [ "$LAST_REASON" = "OOMKilled" ] || [ "$EXIT_CODE" = "137" ]; then |
| 106 | + OOM_KILLED="true" |
| 107 | + fi |
| 108 | + |
| 109 | + if [ "$OOM_KILLED" = "true" ]; then |
| 110 | + COMPLETED=$(kubectl logs -n "$OPERATOR_NS" "$POD_NAME" --tail=10000 2>/dev/null | grep -c "successfully uploaded" || echo 0) |
| 111 | + log "" |
| 112 | + log "=== OOM DETECTED ===" |
| 113 | + log "Pod: $POD_NAME" |
| 114 | + log "Phase: $POD_PHASE" |
| 115 | + log "Elapsed: ${ELAPSED}s" |
| 116 | + log "Images: $COMPLETED completed before OOM" |
| 117 | + log "" |
| 118 | + log "PASSED: scanner OOM-killed as expected." |
| 119 | + exit 0 |
| 120 | + fi |
| 121 | + |
| 122 | + if [ "$POD_PHASE" = "Succeeded" ]; then |
| 123 | + log "" |
| 124 | + log "=== SCAN COMPLETED WITHOUT OOM ===" |
| 125 | + log "Pod $POD_NAME completed successfully." |
| 126 | + log "Lower scanner_memory_limit or add more images." |
| 127 | + log "" |
| 128 | + log "FAILED: no OOM observed." |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | + |
| 132 | + if [ "$POD_PHASE" = "Failed" ] && [ "$OOM_KILLED" = "false" ]; then |
| 133 | + log "" |
| 134 | + log "=== SCAN FAILED (not OOM) ===" |
| 135 | + kubectl describe pod -n "$OPERATOR_NS" "$POD_NAME" 2>/dev/null | tail -15 || true |
| 136 | + log "" |
| 137 | + log "INCONCLUSIVE: pod failed for non-OOM reason." |
| 138 | + exit 2 |
| 139 | + fi |
| 140 | + done < <(kubectl get pods -n "$OPERATOR_NS" -l "$SCAN_LABEL" --no-headers -o custom-columns='NAME:.metadata.name' 2>/dev/null) |
| 141 | + |
| 142 | + log " Scan running... (${ELAPSED}s, phase: ${POD_PHASE:-unknown})" |
| 143 | + sleep "$POLL_INTERVAL" |
| 144 | + done |
| 145 | +} |
| 146 | + |
| 147 | +cmd_status() { |
| 148 | + cd "$TF_DIR" |
| 149 | + OPERATOR_NS="mondoo-operator" |
| 150 | + |
| 151 | + log "=== Cluster State ===" |
| 152 | + echo "" |
| 153 | + echo "--- CronJobs ---" |
| 154 | + kubectl get cronjobs -n "$OPERATOR_NS" -l "mondoo_cr=oom-stress-test" 2>/dev/null || echo "(none)" |
| 155 | + echo "" |
| 156 | + echo "--- Scan Pods ---" |
| 157 | + kubectl get pods -n "$OPERATOR_NS" -l "app=mondoo-container-scan,mondoo_cr=oom-stress-test" -o wide 2>/dev/null || echo "(none)" |
| 158 | + echo "" |
| 159 | + echo "--- Target Pods ---" |
| 160 | + TARGET_NS=$(terraform output -raw target_namespace 2>/dev/null || echo "oom-stress-targets") |
| 161 | + kubectl get pods -n "$TARGET_NS" -l "app.kubernetes.io/part-of=oom-stress-test" --no-headers 2>/dev/null | wc -l | xargs -I{} echo "{} target pods" |
| 162 | + echo "" |
| 163 | + echo "--- MondooAuditConfig ---" |
| 164 | + kubectl get mondooauditconfig -n "$OPERATOR_NS" oom-stress-test -o yaml 2>/dev/null | head -30 || echo "(not found)" |
| 165 | + echo "" |
| 166 | + echo "--- Operator Logs (last 10) ---" |
| 167 | + kubectl logs -n "$OPERATOR_NS" deploy/mondoo-operator-controller-manager --tail=10 2>/dev/null || echo "(no operator)" |
| 168 | +} |
| 169 | + |
| 170 | +cmd_destroy() { |
| 171 | + log "Destroying terraform resources..." |
| 172 | + cd "$TF_DIR" |
| 173 | + terraform destroy -auto-approve |
| 174 | + log "Done." |
| 175 | +} |
| 176 | + |
| 177 | +# --- Main --- |
| 178 | + |
| 179 | +case "${1:-apply-and-watch}" in |
| 180 | + apply) |
| 181 | + cmd_apply |
| 182 | + ;; |
| 183 | + watch) |
| 184 | + cmd_watch |
| 185 | + ;; |
| 186 | + apply-and-watch) |
| 187 | + cmd_apply |
| 188 | + cmd_watch |
| 189 | + ;; |
| 190 | + status) |
| 191 | + cmd_status |
| 192 | + ;; |
| 193 | + destroy) |
| 194 | + cmd_destroy |
| 195 | + ;; |
| 196 | + *) |
| 197 | + echo "Usage: $0 [apply|watch|apply-and-watch|status|destroy]" |
| 198 | + echo "" |
| 199 | + echo " apply-and-watch (default) Provision + watch for OOM" |
| 200 | + echo " apply Provision infrastructure only" |
| 201 | + echo " watch Watch for scan pod OOM (after apply)" |
| 202 | + echo " status Show current cluster state" |
| 203 | + echo " destroy Tear down all resources" |
| 204 | + exit 1 |
| 205 | + ;; |
| 206 | +esac |
0 commit comments