Files
mtusi2026lab5/scripts/lab4/test_upload_memory.py
T

165 lines
8.1 KiB
Python

"""Offline checks for reproducible PNGs and Docker memory evidence."""
import importlib.util
import json
from pathlib import Path
import struct
import unittest
from types import SimpleNamespace
import io
from email.message import Message
from unittest.mock import Mock
import threading
import zlib
class UploadMemoryTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
spec = importlib.util.spec_from_file_location('upload_memory', Path(__file__).with_name('upload-memory.py'))
cls.module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cls.module)
def inspect_png(self, payload, width, height):
self.assertEqual(payload[:8], b'\x89PNG\r\n\x1a\n')
pos, chunks, compressed = 8, [], bytearray()
while pos < len(payload):
size = struct.unpack_from('>I', payload, pos)[0]
kind = payload[pos + 4:pos + 8]
data = payload[pos + 8:pos + 8 + size]
crc = struct.unpack_from('>I', payload, pos + 8 + size)[0]
self.assertEqual(crc, zlib.crc32(kind + data) & 0xffffffff)
if kind == b'IHDR':
self.assertEqual(struct.unpack('>IIBBBBB', data), (width, height, 8, 2, 0, 0, 0))
if kind == b'IDAT':
compressed.extend(data)
chunks.append(kind)
pos += size + 12
self.assertEqual(pos, len(payload))
self.assertEqual(chunks[0], b'IHDR')
self.assertEqual(chunks[-1], b'IEND')
self.assertIn(b'npAD', chunks)
rows = zlib.decompress(compressed)
self.assertEqual(len(rows), height * (width * 3 + 1))
self.assertTrue(all(rows[row * (width * 3 + 1)] == 0 for row in range(height)))
def test_seeded_png_has_correct_crc_pixels_and_exact_size(self):
first = self.module.make_png(32, 24, 4096, 123)
self.assertEqual(len(first), 4096)
self.assertEqual(first, self.module.make_png(32, 24, 4096, 123))
self.assertNotEqual(first, self.module.make_png(32, 24, 4096, 124))
self.inspect_png(first, 32, 24)
def test_both_experiment_payloads_decode_and_reach_exact_limits(self):
for name, width, height, size, seed in self.module.CASES:
with self.subTest(name=name):
payload = self.module.make_png(width, height, size, seed)
self.assertEqual(len(payload), size)
self.assertLessEqual(len(payload), 10 * 1024 * 1024)
self.assertLessEqual(width * height, 40_000_000)
self.inspect_png(payload, width, height)
def test_impossible_png_size_is_rejected(self):
with self.assertRaises(ValueError):
self.module.make_png(32, 32, 64, 1)
def test_docker_memory_uses_binary_and_decimal_units(self):
for raw, expected in [('1.5MiB / 512MiB', 1572864), ('2MB / 512MB', 2000000), ('4096B / 1GiB', 4096)]:
self.assertEqual(self.module.memory_bytes(raw), expected)
with self.assertRaises(ValueError):
self.module.memory_bytes('not available')
def test_memory_summary_separates_baseline_and_load_for_each_replica(self):
samples = [
{'container_id': 'a', 'phase': 'baseline', 'used_bytes': 10},
{'container_id': 'b', 'phase': 'baseline', 'used_bytes': 20},
{'container_id': 'a', 'phase': 'baseline', 'used_bytes': 12},
{'container_id': 'a', 'phase': 'load', 'used_bytes': 35},
{'container_id': 'a', 'phase': 'load', 'used_bytes': 30},
{'container_id': 'b', 'phase': 'load', 'used_bytes': 50},
]
result = self.module.summarize_memory(samples, ['a', 'b'])
self.assertEqual(result['a']['baseline_bytes'], 12)
self.assertEqual(result['a']['sampled_peak_bytes'], 35)
self.assertEqual(result['a']['delta_bytes'], 23)
self.assertEqual(result['b']['sampled_peak_bytes'], 50)
with self.assertRaises(RuntimeError):
self.module.summarize_memory(samples, ['a', 'b', 'missing'])
def test_latency_percentiles_and_overlap_are_explicit(self):
self.assertEqual(self.module.latency_summary(list(range(1, 11))),
{'p50_ms': 5.5, 'p95_ms': 10, 'max_ms': 10})
self.assertEqual(self.module.max_inflight([(0, 4), (1, 3), (2, 5)]), 3)
self.assertEqual(self.module.max_inflight([(0, 1), (1, 2)]), 1)
def test_docker_stream_ignores_ansi_only_lines_between_samples(self):
cid = 'a' * 64
row = json.dumps({'ID': cid, 'Name': 'lab4-api-1', 'MemUsage': '12MiB / 512MiB'})
stats = self.module.DockerStats([cid])
stats.process = SimpleNamespace(stdout=iter(['\x1b[H' + row + '\n', ' \x1b[K\n', row + '\n']))
stats.mark('small', 'baseline')
stats._read()
self.assertIsNone(stats.error)
self.assertEqual(len(stats.samples), 2)
def test_rgba64_profile_has_valid_header_crc_and_zero_scanlines(self):
payload = self.module.make_rgba64_png(8, 5)
pos, compressed = 8, bytearray()
while pos < len(payload):
size = struct.unpack_from('>I', payload, pos)[0]
kind, data = payload[pos + 4:pos + 8], payload[pos + 8:pos + 8 + size]
self.assertEqual(struct.unpack_from('>I', payload, pos + 8 + size)[0], zlib.crc32(kind + data) & 0xffffffff)
if kind == b'IHDR':
self.assertEqual(struct.unpack('>IIBBBBB', data), (8, 5, 16, 6, 0, 0, 0))
if kind == b'IDAT':
compressed.extend(data)
pos += size + 12
self.assertEqual(zlib.decompress(compressed), bytes(5 * (1 + 8 * 8)))
self.assertEqual(pos, len(payload))
def test_max_pixels_profile_fits_byte_limit_without_full_raw_buffer(self):
payload = self.module.make_rgba64_png(8000, 5000)
self.assertLess(len(payload), 10 * 1024 * 1024)
self.assertEqual(struct.unpack_from('>IIBB', payload, 16), (8000, 5000, 16, 6))
def test_upload_uses_requested_jpeg_mime(self):
captured = []
class Opener:
def open(self, request, timeout):
captured.append(request)
response = io.BytesIO(b'')
response.code, response.headers = 415, {}
return response
client = SimpleNamespace(base='http://localhost', opener=Opener(), timeout=1)
result = self.module.upload(None, client, {'token': 'synthetic'}, {'id': 'chat'}, b'jpeg', 'hash',
threading.Barrier(1), 0, 'decoder', mime='image/jpeg')
self.assertEqual(captured[0].get_header('Content-type'), 'image/jpeg')
self.assertTrue(captured[0].full_url.endswith('decoder-0.jpg'))
self.assertEqual(result['status'], 415)
def test_upload_counts_the_queued_snapshot_as_accepted(self):
attachment = {'id': 'a1', 'sha256': 'hash', 'size_bytes': 3, 'status': 'queued',
'owner_id': 'user', 'chat_id': 'chat'}
response = io.BytesIO(json.dumps(attachment).encode())
response.code, response.headers = 202, {}
client = SimpleNamespace(base='http://localhost', opener=Mock(), timeout=1)
client.opener.open.return_value = response
result = self.module.upload(Mock(), client, {'id': 'user', 'token': 'synthetic'},
{'id': 'chat'}, b'png', 'hash', threading.Barrier(1), 0, 'small')
self.assertTrue(result['ok'], result)
self.assertEqual(result['attachment_id'], 'a1')
def test_sample_download_waits_for_publication(self):
client = Mock()
headers = Message()
headers['Content-Type'] = 'image/jpeg'
client.call.return_value = {'bytes': b'jpeg', 'headers': headers, 'status': 200}
result = self.module.verify_download(client, 'reader', 'a1', b'jpeg', 'image/jpeg')
self.assertEqual([call[0] for call in client.method_calls], ['wait_ready', 'call'])
client.wait_ready.assert_called_once_with('reader', {'id': 'a1'})
client.call.assert_called_once_with('GET', '/api/v1/attachments/a1/content', actor='reader')
self.assertTrue(result['ok'])
if __name__ == '__main__':
unittest.main()