#!/bin/bash # Teste de Stress para verificar resiliência do Ingress # Uso: ./teste-stress.sh [URL] [NUM_REQUESTS] # Cores RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' URL="${1:-http://localhost}" TOTAL="${2:-50}" TIMEOUT=120 # ============================================================================= # VERIFICACAO DO NGINX INGRESS CONTROLLER # ============================================================================= echo -e "${YELLOW}[INFO]${NC} Verificando NGINX Ingress Controller..." echo "" echo "O teste de stress da aula-04 valida o Keep Request (Lua) via Ingress." echo "" # Verifica se o ingress-nginx está rodando if ! kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx 2>/dev/null | grep -q "Running"; then echo -e "${RED}[ERRO]${NC} NGINX Ingress Controller NAO esta rodando!" echo "" echo "Para resolver, execute:" echo "" echo " 1. Setup completo:" echo " ./setup.sh" echo "" echo " 2. Ou apenas o Ingress Controller:" echo " helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx" echo " helm install nginx-ingress ingress-nginx/ingress-nginx \\" echo " --namespace ingress-nginx --create-namespace \\" echo " --set controller.allowSnippetAnnotations=true \\" echo " --set controller.config.annotations-risk-level=Critical" echo "" exit 1 fi echo -e "${GREEN}[OK]${NC} NGINX Ingress Controller esta rodando" echo "" # ============================================================================= # TESTE DE STRESS # ============================================================================= echo "============================================" echo " Teste de Stress - Alta Disponibilidade" echo "============================================" echo "URL: $URL" echo "Requisições: $TOTAL" echo "Timeout: ${TIMEOUT}s" echo "============================================" echo "" SUCCESS=0 FAIL=0 for i in $(seq 1 $TOTAL); do RESULT=$(curl -s -m $TIMEOUT "$URL" 2>&1) if echo "$RESULT" | grep -q "Req ->"; then SUCCESS=$((SUCCESS + 1)) echo -e "[$i/$TOTAL] ${GREEN}OK${NC} - $RESULT" else FAIL=$((FAIL + 1)) # Extrai apenas o título do erro se for HTML if echo "$RESULT" | grep -q ""; then ERROR=$(echo "$RESULT" | grep -o "<title>[^<]*" | sed 's/<[^>]*>//g') else ERROR="$RESULT" fi echo -e "[$i/$TOTAL] ${RED}FALHA${NC} - $ERROR" fi sleep 0.2 done echo "" echo "============================================" echo " Resultado Final" echo "============================================" PERCENT=$((SUCCESS * 100 / TOTAL)) echo -e "Sucesso: ${GREEN}$SUCCESS${NC} / $TOTAL ($PERCENT%)" echo -e "Falhas: ${RED}$FAIL${NC} / $TOTAL" echo "============================================" if [ $FAIL -eq 0 ]; then echo -e "${GREEN}*** ZERO FALHAS! ***${NC}" elif [ $PERCENT -ge 95 ]; then echo -e "${YELLOW}Bom resultado (>= 95%)${NC}" else echo -e "${RED}Resultado abaixo do esperado${NC}" fi