forked from mtusi2026/mtusi2026lab6
fix(lab6): preserve lab5 resilience and reproducible recovery
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
"""Reconciliation must preserve variants, in-flight effects and durable staging."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from test_api import ROOT, call, register
|
||||
from test_attachments import PNG, download, upload
|
||||
from test_pipeline import wait_status
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.run(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT,
|
||||
check=True, capture_output=True, text=True, encoding='utf-8').stdout
|
||||
|
||||
|
||||
def reconcile(prefix, grace=0):
|
||||
return json.loads(compose('run', '--rm', '-T', 'api', 'python', '-m', 'app.reconcile',
|
||||
'--apply', '--grace', str(grace), '--prefix', prefix))
|
||||
|
||||
|
||||
def s3_action(action, key):
|
||||
code = ('import asyncio\nfrom app import config\nfrom app.s3 import S3\nasync def m():\n'
|
||||
' s=S3(); await s.start()\n'
|
||||
+ {'put': ' await s.put_bytes(%r, b"temporary", "application/octet-stream")\n',
|
||||
'head': ' print((await s.head(%r)) is not None)\n',
|
||||
'delete': ' await s.delete(%r)\n'}[action] % key
|
||||
+ ' await s.stop()\nasyncio.run(m())')
|
||||
return compose('exec', '-T', 'api', 'python', '-c', code).strip()
|
||||
|
||||
|
||||
class ReconcilePipelineTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.actor = register('reconcile')
|
||||
self.token = self.actor['token']
|
||||
self.chat = call('POST', '/api/v1/chats', {'title': 'reconcile', 'member_ids': [self.actor['user']['id']]}, self.token)[2]
|
||||
|
||||
def accept(self):
|
||||
status, _, att = upload(self.token, self.chat['id'], PNG)
|
||||
self.assertEqual(status, 202)
|
||||
return att
|
||||
|
||||
def test_ready_variants_and_uncommitted_side_effect_are_preserved(self):
|
||||
att = wait_status(self.token, self.accept()['id'])
|
||||
key = 'att/%s/v1/uncommitted' % att['id']
|
||||
s3_action('put', key)
|
||||
try:
|
||||
report = reconcile('att/%s/' % att['id'])
|
||||
self.assertEqual(report['fixed'], {'deleted_orphans': 0, 'marked_failed': 0})
|
||||
self.assertEqual(report['orphan_objects_count'], 0)
|
||||
self.assertEqual(report['owned_objects_skipped'], 1)
|
||||
self.assertEqual(s3_action('head', key), 'True')
|
||||
self.assertEqual(download(self.token, att['id'])[2], PNG)
|
||||
for variant in att['variants']:
|
||||
code, _, data = download(self.token, att['id'], '/content?variant=' + variant['name'])
|
||||
self.assertEqual(code, 200)
|
||||
self.assertEqual(hashlib.sha256(data).hexdigest(), variant['sha256'])
|
||||
finally:
|
||||
s3_action('delete', key)
|
||||
|
||||
def test_staging_is_protected_and_unowned_orphan_obeys_grace(self):
|
||||
compose('stop', 'worker')
|
||||
att = None
|
||||
orphan = 'staging/' + str(uuid.uuid4())
|
||||
try:
|
||||
att = self.accept()
|
||||
report = reconcile('staging/' + att['id'])
|
||||
self.assertEqual(report['fixed'], {'deleted_orphans': 0, 'marked_failed': 0})
|
||||
self.assertEqual(s3_action('head', 'staging/' + att['id']), 'True')
|
||||
s3_action('put', orphan)
|
||||
fresh = reconcile(orphan, grace=900)
|
||||
self.assertEqual(fresh['orphans_skipped_fresh'], 1)
|
||||
self.assertEqual(fresh['fixed']['deleted_orphans'], 0)
|
||||
self.assertEqual(reconcile(orphan)['fixed']['deleted_orphans'], 1)
|
||||
self.assertEqual(s3_action('head', orphan), 'False')
|
||||
self.assertEqual(reconcile(orphan)['fixed']['deleted_orphans'], 0)
|
||||
finally:
|
||||
s3_action('delete', orphan)
|
||||
compose('start', 'worker')
|
||||
self.assertEqual(wait_status(self.token, att['id'])['status'], 'ready')
|
||||
self.assertEqual(download(self.token, att['id'])[2], PNG)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user