#!/bin/bash # test_cache_matrix.sh — 2026-03-23 (v3) # Комплексный тест кэша registry: # Phase 1 — полный деплой всех 24 ресурсов (kaniko builds, т.к. нет образов) # Phase 2 — destroy sless_* + re-apply (все образы из кэша) # Phase 3 — одновременно: удаление 2, смена кода 2, смена параметров 2 # ВАЖНО: nubes_postgres.npg и nubes_postgres_database НЕ уничтожаются. # Причина: пересоздание БД ротирует пароль user0 в k8s secret; # vault_secrets на это не реагирует сразу → FunctionJob получает старый пароль. set -euo pipefail DIR="$(cd "$(dirname "$0")" && pwd)" LOG="$DIR/test_cache_matrix_$(date +%Y%m%d_%H%M%S).log" TIMINGS="$LOG.timings" PASS=0 FAIL=0 log() { echo "[$(date +%H:%M:%S)] $*" | tee -a "$LOG"; } sep() { log "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"; } timed_op() { local label="$1"; shift log "▶ START: $label" local t0; t0=$(date +%s%3N) "$@" 2>&1 | tee -a "$LOG" local rc=${PIPESTATUS[0]} local t1; t1=$(date +%s%3N) local elapsed=$(( (t1 - t0) / 1000 )) if [[ $rc -eq 0 ]]; then log "✓ DONE: $label — ${elapsed}s" PASS=$((PASS+1)) else log "✗ FAIL: $label — ${elapsed}s (exit $rc)" FAIL=$((FAIL+1)) fi echo "$label: ${elapsed}s" >> "$TIMINGS" return $rc } destroy_sless_only() { local label="$1" local targets targets=$(terraform state list 2>/dev/null | grep -E '^(sless_service|sless_job)' | sed 's/^/-target=/' | tr ' ' ' ' || true) if [[ -z "$targets" ]]; then log " (nothing to destroy for $label — state empty)" return 0 fi local n; n=$(terraform state list 2>/dev/null | grep -cE '^(sless_service|sless_job)' || true) log " Targets: $n resources" timed_op "$label" terraform destroy $targets -auto-approve } cd "$DIR" sep log "PHASE 1: Полный начальный деплой" sep destroy_sless_only "phase1-pre-clean" timed_op "phase1-apply-all" terraform apply -auto-approve log "--- Образы в registry после Phase 1 ---" kubectl exec -n sless deployment/sless-registry -- sh -c 'find /var/lib/registry -name "*.json" -path "*/tags/*" 2>/dev/null | sed "s|.*repository/||;s|/_manifests.*||" | sort | uniq -c | sort -rn' 2>/dev/null | head -30 | tee -a "$LOG" || log "(registry inspect failed)" sep log "PHASE 2: Destroy sless_* → Re-apply (ожидаем cache hits)" sep destroy_sless_only "phase2-destroy" timed_op "phase2-apply-cached" terraform apply -auto-approve sep log "PHASE 3: Mixed ops (delete+code+params)" sep log "--- 3a: destroy targets: stress_divzero, chaos_echo ---" timed_op "phase3a-destroy-targets" terraform destroy -target=sless_service.stress_divzero -target=sless_service.chaos_echo -auto-approve log "--- 3b: code changes (new sha256 → kaniko) ---" echo "" >> "$DIR/code/pg-counter/pg_counter.py" echo "# cache-test-$(date +%s)" >> "$DIR/code/pg-counter/pg_counter.py" echo "" >> "$DIR/code/stress-js-async/stress_js_async.js" echo "// cache-test-$(date +%s)" >> "$DIR/code/stress-js-async/stress_js_async.js" log " changed: pg_counter.py, stress_js_async.js" log "--- 3c: param changes (same sha256 → no kaniko) ---" python3 - <<'PYEOF' import re, sys with open("stress.tf") as f: content = f.read() orig = content content = re.sub( r'(resource "sless_service" "stress_slow" \{[^}]*?)memory_mb\s*=\s*\d+', lambda m: m.group(1) + 'memory_mb = 192', content, flags=re.DOTALL ) content = re.sub( r'(resource "sless_service" "pg_stats" \{[^}]*?)timeout_sec\s*=\s*\d+', lambda m: m.group(1) + 'timeout_sec = 20', content, flags=re.DOTALL ) if content == orig: print(" stress.tf: no changes (already patched?)", file=sys.stderr) else: with open("stress.tf", "w") as f: f.write(content) print(" stress.tf: stress_slow→memory_mb=192, pg_stats→timeout_sec=20") PYEOF log "--- 3d: apply всех mixed изменений ---" log " Expected: stress_divzero+chaos_echo=cache_hit, pg_counter+stress_js_async=kaniko, stress_slow+pg_stats=k8s_only" timed_op "phase3d-mixed-apply" terraform apply -auto-approve sep log "ИТОГ" sep log "Timings:" cat "$TIMINGS" 2>/dev/null | tee -a "$LOG" log "Pass: $PASS | Fail: $FAIL" log "Лог: $LOG"