forked from mtusi2026/mtusi2026lab6
107 lines
3.9 KiB
Python
107 lines
3.9 KiB
Python
"""Run cleanup scenarios against a disposable PostgreSQL database."""
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import psycopg2
|
|
from psycopg2 import sql
|
|
from psycopg2.extensions import make_dsn
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src'))
|
|
from messenger.config import Config
|
|
from messenger.db import Database
|
|
from messenger.errors import Unavailable
|
|
from messenger.store import Store
|
|
|
|
GRACE_SECONDS = 900
|
|
|
|
|
|
def unavailable(_key):
|
|
raise Unavailable('synthetic S3 failure')
|
|
|
|
|
|
def expect_unavailable(operation):
|
|
try:
|
|
operation()
|
|
except Unavailable:
|
|
return
|
|
raise AssertionError('Expected Unavailable')
|
|
|
|
|
|
def exercise(database):
|
|
store = Store(None, database, None)
|
|
user, chat = str(uuid4()), str(uuid4())
|
|
with database.cursor() as cur:
|
|
cur.execute("INSERT INTO users VALUES (%s,'cleanup','unused','Cleanup')", (user,))
|
|
cur.execute("INSERT INTO chats VALUES (%s,'Cleanup')", (chat,))
|
|
|
|
def reserve(expired=False):
|
|
aid = str(uuid4())
|
|
key = 'attachments/' + aid
|
|
store.reserve_attachment(aid, chat, user, 'x.png', 'image/png', 1, 'a' * 64, key)
|
|
if expired:
|
|
with database.cursor() as cur:
|
|
cur.execute("UPDATE attachments SET updated_at=now()-interval '16 minutes' WHERE id=%s", (aid,))
|
|
return aid, key
|
|
|
|
objects = set()
|
|
fresh, fresh_key = reserve()
|
|
objects.add(fresh_key)
|
|
ready, ready_key = reserve(expired=True)
|
|
store.finish_attachment(ready, objects.add)
|
|
with database.cursor() as cur:
|
|
cur.execute("UPDATE attachments SET updated_at=now()-interval '16 minutes' WHERE id=%s", (ready,))
|
|
|
|
active, active_key = reserve(expired=True)
|
|
def active_put(key):
|
|
objects.add(key)
|
|
assert not store.clean_pending_attachment(GRACE_SECONDS, objects.discard)
|
|
store.finish_attachment(active, active_put)
|
|
|
|
failed, failed_key = reserve(expired=True)
|
|
def put_then_fail(key):
|
|
objects.add(key)
|
|
# Inject a failure after the object exists and before the ready update.
|
|
raise Unavailable('synthetic failure after PUT')
|
|
expect_unavailable(lambda: store.finish_attachment(failed, put_then_fail))
|
|
assert failed_key in objects
|
|
expect_unavailable(lambda: store.clean_pending_attachment(GRACE_SECONDS, unavailable))
|
|
with database.cursor() as cur:
|
|
cur.execute('SELECT status FROM attachments WHERE id=%s', (failed,))
|
|
assert cur.fetchone()['status'] == 'pending'
|
|
assert store.clean_pending_attachment(GRACE_SECONDS, objects.discard)
|
|
assert objects == {fresh_key, ready_key, active_key}
|
|
|
|
reserve(expired=True) # Reservation whose PUT never created an object.
|
|
assert store.clean_pending_attachment(GRACE_SECONDS, objects.discard)
|
|
assert not store.clean_pending_attachment(GRACE_SECONDS, objects.discard)
|
|
with database.cursor() as cur:
|
|
cur.execute('SELECT id::text AS id, status FROM attachments')
|
|
assert {row['id']: row['status'] for row in cur.fetchall()} == {
|
|
fresh: 'pending', ready: 'ready', active: 'ready',
|
|
}
|
|
print('PASS: PostgreSQL row lock protects active PUT; fresh and ready attachments preserved')
|
|
print('PASS: interrupted PUT and failure after PUT cleaned; S3 failure retries safely')
|
|
|
|
|
|
def main():
|
|
dsn = os.environ['LAB4_TEST_DATABASE_URL']
|
|
schema = 'cleanup_' + uuid4().hex
|
|
with psycopg2.connect(dsn) as conn, conn.cursor() as cur:
|
|
cur.execute(sql.SQL('CREATE SCHEMA {}').format(sql.Identifier(schema)))
|
|
config = Config.from_env({'DATABASE_URL': make_dsn(dsn, options='-csearch_path=' + schema)})
|
|
database = Database(config)
|
|
try:
|
|
database.open()
|
|
database.run_migrations()
|
|
exercise(database)
|
|
finally:
|
|
database.close()
|
|
with psycopg2.connect(dsn) as conn, conn.cursor() as cur:
|
|
cur.execute(sql.SQL('DROP SCHEMA {} CASCADE').format(sql.Identifier(schema)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|