Files
mtusi2026lab5/tests/pipeline_retention.py
2026-11-06 21:15:31 +03:00

141 lines
4.8 KiB
Python

from client import Client, check, run
from pipeline_support import compose, sql, broker, ready_api, wait_ready, wait_for, metadata, job
import hashlib
import json
import uuid
def execute(code, data=None):
return compose("exec", "-T", "dispatcher", "python", "-c", code, data=data)
def main():
ready_api()
c = Client(base_url="http://127.0.0.1:18085", timeout=30)
a, b, e, chat = c.bootstrap()
protected, original = c.attachment(a, chat)
wait_ready(c, a, protected)
key = sql("select object_key from attachments where id='" + protected["id"] + "';")
status = compose(
"exec",
"-T",
"minio",
"curl",
"-s",
"-o",
"/dev/null",
"-w",
"%{http_code}",
"http://localhost:9000/messenger-attachments/" + key,
)
check(status == "403", "bucket permits anonymous access")
print("PASS anonymous direct S3 GET of existing original returns 403", flush=True)
compose("stop", "worker")
attachment, _ = c.attachment(a, chat)
code = r"""import hashlib,struct,zlib
from pipeline.storage import Storage
from pipeline.config import Settings
from pipeline.repository import Repository
s=Settings();storage=Storage(s);repo=Repository(s)
def chunk(kind,data):return struct.pack('>I',len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data)&0xffffffff)
compressor=zlib.compressobj();parts=[];row=b'\0'+b'\0'*(7000*3)
for _ in range(7000):parts.append(compressor.compress(row))
parts.append(compressor.flush())
data=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',7000,7000,8,2,0,0,0))+chunk(b'IDAT',b''.join(parts))+chunk(b'IEND',b'')
with repo.connection() as db:
item=db.execute('select staging_key from attachments where id=%s',('ATTACHMENT',)).fetchone()
storage.client.put_object(Bucket=s.bucket,Key=item['staging_key'],Body=data,ContentType='image/png')
db.execute('update attachments set size_bytes=%s,checksum=%s where id=%s',(len(data),hashlib.sha256(data).hexdigest(),'ATTACHMENT'))
print('injected 49-million-pixel complete PNG into trusted staging with matching metadata')
""".replace(
"ATTACHMENT", attachment["id"]
)
print(execute(code), flush=True)
compose("start", "worker")
wait_for(lambda: metadata(c, a, attachment)["status"] == "failed", 60)
check(
metadata(c, a, attachment)["error_code"] == "invalid_image",
"worker pixel guard did not reject",
)
check(job(attachment)["attempt"] == 1, "permanent pixel error retried")
check(
sql(
"select count(*) from attachment_variants where attachment_id='"
+ attachment["id"]
+ "';"
)
== "0",
"invalid image published variants",
)
print(
"PASS worker independently rejects >40 million pixels before full decode; FAILED on first attempt",
flush=True,
)
sql(
"update attachments set updated_at=now()-interval '2 days' where id='"
+ attachment["id"]
+ "';"
)
fresh = "staging/fresh-proof-" + uuid.uuid4().hex
code = r"""from pipeline.storage import Storage
from pipeline.repository import Repository
from pipeline.cleanup import Cleanup
from pipeline.config import Settings
s=Settings();storage=Storage(s);repo=Repository(s)
storage.client.put_object(Bucket=s.bucket,Key='FRESH',Body=b'fresh-proof')
Cleanup(repo,storage).run()
storage.client.head_object(Bucket=s.bucket,Key='FRESH')
print('fresh orphan retained by grace')
storage.client.delete_object(Bucket=s.bucket,Key='FRESH')
""".replace(
"FRESH", fresh
)
print(execute(code), flush=True)
check(
metadata(c, a, attachment)["error_code"] == "staging_expired",
"expired failed staging retained",
)
check(
sql("select staging_key IS NULL from attachments where id='" + attachment["id"] + "';")
== "t",
"staging reference retained",
)
command = [
"docker",
"compose",
"-f",
"docker-compose.yml",
"-f",
"compose.local.yaml",
"-f",
"compose.test.yaml",
"exec",
"-T",
"dispatcher",
"python",
"-m",
"pipeline.operator",
"replay",
attachment["id"],
]
import subprocess
response = subprocess.run(command, capture_output=True, text=True, timeout=30)
check(
response.returncode != 0 and "staging_expired" in response.stderr, "expired replay accepted"
)
check(metadata(c, a, attachment)["status"] == "failed", "unsafe replay changed status")
check(
c.call("GET", "/api/v1/attachments/" + protected["id"] + "/content", actor=b)["bytes"]
== original,
"cleanup deleted referenced original",
)
print(
"PASS expired failed staging cleaned; unsafe replay refused; referenced ready bytes retained",
flush=True,
)
if __name__ == "__main__":
run(main)