forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdr-test-runner.sh
More file actions
170 lines (148 loc) · 6.3 KB
/
Copy pathdr-test-runner.sh
File metadata and controls
170 lines (148 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
set -euo pipefail
PRIMARY_REGION="${PRIMARY_REGION:-us-east-1}"
DR_REGION="${DR_REGION:-us-west-2}"
NAMESPACE="${NAMESPACE:-gistpin}"
RTO_TARGET_SECONDS="${RTO_TARGET_SECONDS:-900}"
RPO_TARGET_SECONDS="${RPO_TARGET_SECONDS:-300}"
TEST_REPORT="/tmp/dr-test-runner-$(date +%Y%m%d-%H%M%S).txt"
PASS=0
FAIL=0
log() { echo "[$(date +%H:%M:%S)] $*" | tee -a "${TEST_REPORT}"; }
pass() { log " ✓ PASS: $*"; ((PASS++)); }
fail_check() { log " ✗ FAIL: $*"; ((FAIL++)); }
log "=============================================="
log " GistPin Disaster Recovery Test Runner"
log "=============================================="
log "Primary Region: ${PRIMARY_REGION}"
log "DR Region: ${DR_REGION}"
log "Namespace: ${NAMESPACE}"
log "RTO Target: ${RTO_TARGET_SECONDS}s (${RTO_TARGET_SECONDS}s)"
log "RPO Target: ${RPO_TARGET_SECONDS}s (${RPO_TARGET_SECONDS}s)"
log "Started: $(date)"
log ""
# ---------------------------------------------------------------------------
# Phase 1: Simulate primary region failure
# ---------------------------------------------------------------------------
log "[Phase 1] Simulating primary region failure"
log ""
log " Blocking traffic to primary region endpoints..."
FAILOVER_START=$(date +%s)
# Simulate primary region unavailability by checking endpoint health
if PRIMARY_HEALTH=$(curl -sf -o /dev/null -w "%{http_code}" \
--connect-timeout 5 --max-time 10 \
"https://api.${PRIMARY_REGION}.gistpin.internal/health" 2>/dev/null); then
log " Primary region appears reachable (HTTP ${PRIMARY_HEALTH}) — simulating failure"
else
log " Primary region confirmed unreachable (expected during failure simulation)"
fi
log " Primary region failure simulation complete"
log ""
# ---------------------------------------------------------------------------
# Phase 2: Measure failover time (RTO)
# ---------------------------------------------------------------------------
log "[Phase 2] Measuring failover time (RTO target: ${RTO_TARGET_SECONDS}s)"
log ""
log " Initiating failover to DR region..."
# Record when DR endpoint becomes healthy
FAILOVER_END=""
for i in $(seq 1 60); do
if DR_HEALTH=$(curl -sf -o /dev/null -w "%{http_code}" \
--connect-timeout 5 --max-time 10 \
"https://api.${DR_REGION}.gistpin.internal/health" 2>/dev/null); then
FAILOVER_END=$(date +%s)
log " DR region healthy after ${i}s (HTTP ${DR_HEALTH})"
break
fi
sleep 5
done
if [[ -z "${FAILOVER_END}" ]]; then
fail_check "DR region did not become healthy within timeout"
RTO_ACTUAL="TIMEOUT"
else
RTO_ACTUAL=$(( FAILOVER_END - FAILOVER_START ))
if (( RTO_ACTUAL <= RTO_TARGET_SECONDS )); then
pass "Failover completed in ${RTO_ACTUAL}s (target: ${RTO_TARGET_SECONDS}s)"
else
fail_check "Failover took ${RTO_ACTUAL}s (exceeds target: ${RTO_TARGET_SECONDS}s)"
fi
fi
log ""
# ---------------------------------------------------------------------------
# Phase 3: Verify data completeness (RPO)
# ---------------------------------------------------------------------------
log "[Phase 3] Verifying data completeness (RPO target: ${RPO_TARGET_SECONDS}s)"
log ""
log " Checking DR database replication lag..."
RDS_LAG=$(aws cloudwatch get-metric-statistics \
--region "${DR_REGION}" \
--namespace AWS/RDS \
--metric-name ReplicaLag \
--dimensions Name=DBInstanceIdentifier,Value=gistpin-db-dr \
--statistics Average \
--period 60 \
--start-time "$(date -u -d '5 minutes ago' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-5M +%Y-%m-%dT%H:%M:%SZ)" \
--end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--query 'Datapoints[0].Average' --output text 2>/dev/null || echo "N/A")
if [[ "${RDS_LAG}" != "N/A" ]] && (( $(echo "${RDS_LAG} < ${RPO_TARGET_SECONDS}" | bc -l 2>/dev/null || echo 0) )); then
pass "RDS replication lag: ${RDS_LAG}s (target: ${RPO_TARGET_SECONDS}s)"
else
fail_check "RDS replication lag: ${RDS_LAG}s (exceeds target: ${RPO_TARGET_SECONDS}s)"
fi
log " Verifying S3 cross-region replication..."
TEST_KEY="dr-test-runner/probe-$(date +%s).txt"
echo "dr-probe-${DR_REGION}" | aws s3 cp - "s3://gistpin-backups/${TEST_KEY}" 2>/dev/null || true
sleep 10
if aws s3 ls "s3://gistpin-backups-dr/${TEST_KEY}" --region "${DR_REGION}" 2>/dev/null; then
pass "S3 cross-region replication confirmed"
else
fail_check "S3 cross-region replication not confirmed"
fi
aws s3 rm "s3://gistpin-backups/${TEST_KEY}" 2>/dev/null || true
log " Checking backup freshness..."
LATEST_BACKUP=$(aws s3 ls "s3://gistpin-backups/db/" 2>/dev/null | sort | tail -1 | awk '{print $4}' || echo "")
if [[ -n "${LATEST_BACKUP}" ]]; then
pass "Latest backup available: ${LATEST_BACKUP}"
else
fail_check "No backups found in bucket"
fi
log ""
# ---------------------------------------------------------------------------
# Phase 4: Verify DR workload health
# ---------------------------------------------------------------------------
log "[Phase 4] Verifying DR workload health"
log ""
DR_CONTEXT="arn:aws:eks:${DR_REGION}:$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo '000000000000'):cluster/gistpin-dr"
READY_PODS=$(kubectl --context "${DR_CONTEXT}" -n "${NAMESPACE}" \
get pods --field-selector=status.phase=Running --no-headers 2>/dev/null | wc -l || echo 0)
if (( READY_PODS >= 2 )); then
pass "DR pods running: ${READY_PODS}"
else
fail_check "DR pods running: ${READY_PODS} (expected >= 2)"
fi
if kubectl --context "${DR_CONTEXT}" -n "${NAMESPACE}" get services --no-headers 2>/dev/null | grep -q "backend"; then
pass "Backend service found in DR cluster"
else
fail_check "Backend service not found in DR cluster"
fi
log ""
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
log "=============================================="
log " DR Test Results Summary"
log "=============================================="
log "RTO: ${RTO_ACTUAL}s (target: ${RTO_TARGET_SECONDS}s)"
log "RPO: ${RDS_LAG}s (target: ${RPO_TARGET_SECONDS}s)"
log "Passed: ${PASS}"
log "Failed: ${FAIL}"
log "Report: ${TEST_REPORT}"
log "Completed: $(date)"
log "=============================================="
if (( FAIL > 0 )); then
log "Result: FAILED — ${FAIL} checks did not meet targets"
exit 1
else
log "Result: PASSED — all DR readiness checks passed"
exit 0
fi