|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +# Point staging at a temp dir for tests |
| 7 | +@pytest.fixture(autouse=True) |
| 8 | +def isolated_staging(tmp_path, monkeypatch): |
| 9 | + monkeypatch.setattr( |
| 10 | + "transformerlab.services.upload_service.STAGING_ROOT", |
| 11 | + str(tmp_path / "staging"), |
| 12 | + ) |
| 13 | + yield |
| 14 | + |
| 15 | + |
| 16 | +def test_init_creates_staging_dir(): |
| 17 | + import asyncio |
| 18 | + from transformerlab.services.upload_service import init_upload, STAGING_ROOT |
| 19 | + |
| 20 | + result = asyncio.run(init_upload("model.zip", 1024 * 1024)) |
| 21 | + |
| 22 | + assert "upload_id" in result |
| 23 | + assert result["chunk_size"] == 64 * 1024 * 1024 |
| 24 | + staging = os.path.join(STAGING_ROOT, result["upload_id"]) |
| 25 | + assert os.path.isdir(staging) |
| 26 | + with open(os.path.join(staging, "meta.json")) as f: |
| 27 | + meta = json.load(f) |
| 28 | + assert meta["filename"] == "model.zip" |
| 29 | + assert meta["total_size"] == 1024 * 1024 |
| 30 | + |
| 31 | + |
| 32 | +def test_save_chunk_writes_file(): |
| 33 | + import asyncio |
| 34 | + from transformerlab.services.upload_service import init_upload, save_chunk, STAGING_ROOT |
| 35 | + |
| 36 | + r = asyncio.run(init_upload("data.bin", 200)) |
| 37 | + uid = r["upload_id"] |
| 38 | + |
| 39 | + received = asyncio.run(save_chunk(uid, 0, b"hello chunk 0")) |
| 40 | + |
| 41 | + chunk_path = os.path.join(STAGING_ROOT, uid, "0") |
| 42 | + assert os.path.isfile(chunk_path) |
| 43 | + assert open(chunk_path, "rb").read() == b"hello chunk 0" |
| 44 | + assert received == [0] |
| 45 | + |
| 46 | + |
| 47 | +def test_save_chunk_idempotent(): |
| 48 | + import asyncio |
| 49 | + from transformerlab.services.upload_service import init_upload, save_chunk |
| 50 | + |
| 51 | + r = asyncio.run(init_upload("data.bin", 200)) |
| 52 | + uid = r["upload_id"] |
| 53 | + |
| 54 | + asyncio.run(save_chunk(uid, 0, b"first write")) |
| 55 | + asyncio.run(save_chunk(uid, 0, b"second write")) |
| 56 | + |
| 57 | + from transformerlab.services.upload_service import STAGING_ROOT |
| 58 | + |
| 59 | + chunk_path = os.path.join(STAGING_ROOT, uid, "0") |
| 60 | + assert open(chunk_path, "rb").read() == b"second write" |
| 61 | + |
| 62 | + |
| 63 | +def test_get_status_returns_received_chunks(): |
| 64 | + import asyncio |
| 65 | + from transformerlab.services.upload_service import init_upload, save_chunk, get_status |
| 66 | + |
| 67 | + r = asyncio.run(init_upload("data.bin", 300)) |
| 68 | + uid = r["upload_id"] |
| 69 | + asyncio.run(save_chunk(uid, 0, b"a")) |
| 70 | + asyncio.run(save_chunk(uid, 2, b"c")) |
| 71 | + |
| 72 | + status = asyncio.run(get_status(uid)) |
| 73 | + |
| 74 | + assert status["upload_id"] == uid |
| 75 | + assert status["received"] == [0, 2] |
| 76 | + assert status["complete"] is False |
| 77 | + |
| 78 | + |
| 79 | +def test_assemble_upload_concatenates_chunks(): |
| 80 | + import asyncio |
| 81 | + from transformerlab.services.upload_service import init_upload, save_chunk, assemble_upload_sync, STAGING_ROOT |
| 82 | + |
| 83 | + r = asyncio.run(init_upload("data.bin", 6)) |
| 84 | + uid = r["upload_id"] |
| 85 | + asyncio.run(save_chunk(uid, 0, b"ABC")) |
| 86 | + asyncio.run(save_chunk(uid, 1, b"DEF")) |
| 87 | + |
| 88 | + path = assemble_upload_sync(uid, total_chunks=2) |
| 89 | + |
| 90 | + assert open(path, "rb").read() == b"ABCDEF" |
| 91 | + # _staging_dir returns os.path.realpath(), so compare against the resolved path. |
| 92 | + assert path == os.path.realpath(os.path.join(STAGING_ROOT, uid, "assembled")) |
| 93 | + |
| 94 | + |
| 95 | +def test_assemble_upload_raises_on_missing_chunks(): |
| 96 | + import asyncio |
| 97 | + from transformerlab.services.upload_service import init_upload, save_chunk, assemble_upload_sync |
| 98 | + |
| 99 | + r = asyncio.run(init_upload("data.bin", 9)) |
| 100 | + uid = r["upload_id"] |
| 101 | + asyncio.run(save_chunk(uid, 0, b"AAA")) |
| 102 | + # chunk 1 is missing |
| 103 | + |
| 104 | + with pytest.raises(ValueError, match="Missing chunks"): |
| 105 | + assemble_upload_sync(uid, total_chunks=2) |
| 106 | + |
| 107 | + |
| 108 | +def test_delete_upload_removes_dir(): |
| 109 | + import asyncio |
| 110 | + from transformerlab.services.upload_service import init_upload, delete_upload, STAGING_ROOT |
| 111 | + |
| 112 | + r = asyncio.run(init_upload("data.bin", 10)) |
| 113 | + uid = r["upload_id"] |
| 114 | + |
| 115 | + asyncio.run(delete_upload(uid)) |
| 116 | + |
| 117 | + assert not os.path.isdir(os.path.join(STAGING_ROOT, uid)) |
| 118 | + |
| 119 | + |
| 120 | +def test_get_assembled_path_returns_path_when_assembled(): |
| 121 | + import asyncio |
| 122 | + from transformerlab.services.upload_service import init_upload, save_chunk, assemble_upload_sync, get_assembled_path |
| 123 | + |
| 124 | + r = asyncio.run(init_upload("data.bin", 3)) |
| 125 | + uid = r["upload_id"] |
| 126 | + asyncio.run(save_chunk(uid, 0, b"XYZ")) |
| 127 | + assembled = assemble_upload_sync(uid, total_chunks=1) |
| 128 | + |
| 129 | + result = asyncio.run(get_assembled_path(uid)) |
| 130 | + |
| 131 | + assert result == assembled |
| 132 | + |
| 133 | + |
| 134 | +def test_get_assembled_path_raises_when_not_assembled(): |
| 135 | + import asyncio |
| 136 | + from transformerlab.services.upload_service import init_upload, get_assembled_path |
| 137 | + |
| 138 | + r = asyncio.run(init_upload("data.bin", 3)) |
| 139 | + uid = r["upload_id"] |
| 140 | + |
| 141 | + with pytest.raises(ValueError, match="has not been assembled"): |
| 142 | + asyncio.run(get_assembled_path(uid)) |
| 143 | + |
| 144 | + |
| 145 | +def test_save_chunk_raises_on_unknown_upload_id(): |
| 146 | + import asyncio |
| 147 | + from transformerlab.services.upload_service import save_chunk |
| 148 | + |
| 149 | + # Must be a valid 32-char hex UUID format but refer to an upload that doesn't exist. |
| 150 | + with pytest.raises(ValueError, match="not found"): |
| 151 | + asyncio.run(save_chunk("a" * 32, 0, b"data")) |
| 152 | + |
| 153 | + |
| 154 | +def test_save_chunk_raises_on_invalid_upload_id(): |
| 155 | + import asyncio |
| 156 | + from transformerlab.services.upload_service import save_chunk |
| 157 | + |
| 158 | + with pytest.raises(ValueError, match="Invalid upload_id"): |
| 159 | + asyncio.run(save_chunk("../../etc/passwd", 0, b"data")) |
| 160 | + |
| 161 | + |
| 162 | +def test_get_filename_returns_filename(): |
| 163 | + import asyncio |
| 164 | + from transformerlab.services.upload_service import init_upload, get_filename |
| 165 | + |
| 166 | + r = asyncio.run(init_upload("my_model.zip", 500)) |
| 167 | + uid = r["upload_id"] |
| 168 | + |
| 169 | + result = asyncio.run(get_filename(uid)) |
| 170 | + |
| 171 | + assert result == "my_model.zip" |
| 172 | + |
| 173 | + |
| 174 | +def test_sweep_removes_old_uploads(tmp_path, monkeypatch): |
| 175 | + import asyncio |
| 176 | + from transformerlab.services import upload_service as svc |
| 177 | + |
| 178 | + monkeypatch.setattr(svc, "STAGING_ROOT", str(tmp_path / "staging")) |
| 179 | + |
| 180 | + r = asyncio.run(svc.init_upload("old.bin", 10)) |
| 181 | + uid = r["upload_id"] |
| 182 | + |
| 183 | + # Backdating meta.json created_at by 25 hours |
| 184 | + from datetime import datetime, timezone, timedelta |
| 185 | + |
| 186 | + meta_path = os.path.join(svc.STAGING_ROOT, uid, "meta.json") |
| 187 | + with open(meta_path) as f: |
| 188 | + meta = json.load(f) |
| 189 | + meta["created_at"] = (datetime.now(timezone.utc) - timedelta(hours=25)).isoformat() |
| 190 | + with open(meta_path, "w") as f: |
| 191 | + json.dump(meta, f) |
| 192 | + |
| 193 | + count = svc.sweep_expired_uploads(max_age_hours=24) |
| 194 | + |
| 195 | + assert count == 1 |
| 196 | + assert not os.path.isdir(os.path.join(svc.STAGING_ROOT, uid)) |
0 commit comments