forked from mtusi2026/mtusi2026lab2
106 lines
3.0 KiB
Python
Executable File
106 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Verify that Redis outage degrades to PostgreSQL without bypassing auth."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import uuid
|
|
|
|
from client import Client, Failure, arguments, check, run
|
|
|
|
|
|
def execute_hook(script, action):
|
|
env = dict(os.environ)
|
|
env['CACHE_ACTION'] = action
|
|
try:
|
|
result = subprocess.run(
|
|
[str(script)],
|
|
env=env,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
timeout=90,
|
|
check=False,
|
|
)
|
|
except subprocess.TimeoutExpired:
|
|
raise Failure('Redis outage hook exceeded 90 seconds') from None
|
|
check(result.returncode == 0, 'Redis outage hook failed for action ' + action)
|
|
|
|
|
|
def main():
|
|
def configure(parser):
|
|
parser.add_argument(
|
|
'--action-script',
|
|
required=True,
|
|
help='Executable student hook controlled by CACHE_ACTION=stop|start',
|
|
)
|
|
|
|
args = arguments(
|
|
'Redis failure scenario; creates only synthetic API data',
|
|
configure,
|
|
)
|
|
script = Path(args.action_script).expanduser().resolve()
|
|
check(
|
|
script.is_file() and os.access(script, os.X_OK),
|
|
'action script must exist and be executable (chmod +x)',
|
|
)
|
|
|
|
client = Client(args.base_url, args.ca_file, timeout=3)
|
|
alice, bob, _, original_chat = client.bootstrap()
|
|
path = '/api/v1/chats/' + original_chat['id']
|
|
|
|
warmed = client.call(
|
|
'GET',
|
|
path,
|
|
actor=bob,
|
|
schema='Chat',
|
|
)['json']
|
|
check(warmed == original_chat, 'warm read changed chat data')
|
|
|
|
print('Stopping only Redis; hook output is suppressed.')
|
|
execute_hook(script, 'stop')
|
|
|
|
created_during_outage = None
|
|
try:
|
|
client.call('GET', '/health/ready', schema='Health')
|
|
|
|
fallback = client.call(
|
|
'GET',
|
|
path,
|
|
actor=bob,
|
|
schema='Chat',
|
|
)['json']
|
|
check(fallback == original_chat, 'PostgreSQL fallback changed chat data')
|
|
|
|
created_during_outage = client.call(
|
|
'POST',
|
|
'/api/v1/chats',
|
|
201,
|
|
actor=alice,
|
|
body={
|
|
'title': 'Redis outage ' + uuid.uuid4().hex[:8],
|
|
'member_ids': [alice['id'], bob['id']],
|
|
},
|
|
schema='Chat',
|
|
)['json']
|
|
|
|
client.call('GET', path, 401, actor={'id': 'unused', 'token': 'invalid'})
|
|
finally:
|
|
execute_hook(script, 'start')
|
|
|
|
check(created_during_outage is not None, 'chat was not created during outage')
|
|
recovered = client.call(
|
|
'GET',
|
|
'/api/v1/chats/' + created_during_outage['id'],
|
|
actor=bob,
|
|
schema='Chat',
|
|
)['json']
|
|
check(recovered == created_during_outage, 'cache recovery changed chat data')
|
|
|
|
print(
|
|
'PASS: Redis outage used PostgreSQL fallback; readiness, session, '
|
|
'authorization and writes remained correct.'
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|