[+] lab 2
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit local dependency fault experiment. Restores stopped services in finally."""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.run(['docker', 'compose', *args], cwd=ROOT, check=True, capture_output=True, text=True).stdout.strip()
|
||||
|
||||
|
||||
def wait_ready(c):
|
||||
deadline = time.monotonic() + 60
|
||||
while True:
|
||||
try:
|
||||
c.call('GET', '/health/ready', schema='Health')
|
||||
return
|
||||
except Failure:
|
||||
if time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
def main():
|
||||
c = Client(timeout=6)
|
||||
actor = c.user('fault')
|
||||
key = 'user:' + actor['id']
|
||||
compose('exec', '-T', 'redis', 'redis-cli', 'DEL', key)
|
||||
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
||||
ttl = int(compose('exec', '-T', 'redis', 'redis-cli', 'TTL', key))
|
||||
check(0 < ttl <= 60, 'profile was not cached with TTL')
|
||||
def hits():
|
||||
stats = compose('exec', '-T', 'redis', 'redis-cli', 'INFO', 'stats')
|
||||
return int(next(line.split(':')[1] for line in stats.splitlines() if line.startswith('keyspace_hits:')))
|
||||
before = hits()
|
||||
check(c.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'] == actor['user'], 'cached profile differs')
|
||||
check(hits() > before, 'second profile read did not hit Redis')
|
||||
results = {'cache_ttl_seconds': ttl, 'cache_miss_hit': 'PASS'}
|
||||
try:
|
||||
compose('stop', '-t', '2', 'redis')
|
||||
response = c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
||||
check(response['json'] == actor['user'] and response['elapsed'] < 3, 'Redis fallback failed or unbounded')
|
||||
c.call('GET', '/health/ready', schema='Health')
|
||||
c.call('GET', '/api/v1/users/me', 401)
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=actor)
|
||||
results['redis_outage'] = {'result': 'PASS', 'fallback_seconds': round(response['elapsed'], 4),
|
||||
'readiness': 200, 'anonymous': 401, 'logout': 204, 'revoked': 401}
|
||||
finally:
|
||||
compose('start', 'redis')
|
||||
# Restored cache contains the old public profile, but may never bypass session revocation.
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=actor)
|
||||
actor = c.user('dbfault')
|
||||
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
||||
try:
|
||||
compose('stop', '-t', '2', 'postgres')
|
||||
c.call('GET', '/health/live', schema='Health')
|
||||
ready = c.call('GET', '/health/ready', 503)
|
||||
response = c.call('GET', '/api/v1/users/me', 503, actor=actor)
|
||||
check(response['elapsed'] < 5 and ready['elapsed'] < 5, 'Database timeout exceeded bound')
|
||||
c.call('GET', '/api/v1/users/me', 401)
|
||||
results['postgres_outage'] = {'result': 'PASS', 'authenticated_status':503,
|
||||
'response_seconds':round(response['elapsed'], 4),
|
||||
'readiness':503, 'liveness':200, 'anonymous':401}
|
||||
finally:
|
||||
compose('start', 'postgres')
|
||||
recovered = time.monotonic()
|
||||
wait_ready(c)
|
||||
check(c.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'] == actor['user'], 'session lost after DB recovery')
|
||||
results['postgres_recovery_seconds'] = round(time.monotonic()-recovered, 4)
|
||||
results['result'] = 'PASS'
|
||||
print(json.dumps(results, ensure_ascii=False, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user