summaryrefslogtreecommitdiffstatshomepage
path: root/tests/scanner/test_scanner.py
blob: 6fc6650fa07d15d6343c041efb029cfa1f3211ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import configparser
import os
import shutil
from datetime import datetime, timezone
from pathlib import Path
from zipfile import ZipFile

import pytest
from conftest import DB

import hircine.thumbnailer
from hircine.config import DirectoryStructure
from hircine.db.models import Archive, Image, Page
from hircine.scanner import Scanner, Status
from hircine.thumbnailer import object_path


def pageset(pages):
    return set([(page.path, page.archive_id, page.image.hash) for page in pages])


@pytest.fixture
def archive(data):
    stat = os.stat(data("contents/archive.zip"))
    mtime = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc)

    cover = Image(
        id=1,
        hash="4ac228082aaf8bedc0fbd4859c5324c2acf0d1c63f9097d55e9be88d0804eaa4",
        width=0,
        height=0,
    )

    archive = Archive(
        id=1,
        path=data("contents/archive.zip"),
        hash="8aa2fd72954fb9103776114172d345ad4446babf292e876a892cfbed1c283523",
        size=stat.st_size,
        mtime=mtime,
        cover=cover,
        pages=[
            Page(
                id=1,
                archive_id=1,
                index=1,
                path="01.png",
                image=cover,
            ),
            Page(
                id=2,
                archive_id=1,
                index=2,
                path="02.png",
                image=Image(
                    id=2,
                    hash="9b2c7a9c1f3d1c5a07fa1492d9d91ace5122262559c7f513e3b97464d2edb753",
                    width=0,
                    height=0,
                ),
            ),
            Page(
                id=3,
                archive_id=1,
                index=3,
                path="03.png",
                image=Image(
                    id=3,
                    hash="ed132e79daf9e93970d14d9443b7870f1aefd12aa9d3fba8cab0096984754ff5",
                    width=0,
                    height=0,
                ),
            ),
        ],
        page_count=3,
    )

    yield archive


@pytest.fixture
def scanner(data, monkeypatch):
    monkeypatch.setattr(
        hircine.thumbnailer.Thumbnailer, "process", lambda s, a, b: (0, 0)
    )

    dirs = DirectoryStructure(scan=data("contents/"), objects=data("objects/"))
    yield Scanner(configparser.ConfigParser(), dirs)


@pytest.mark.anyio
async def test_scanner_adds_new_archive(archive, scanner, capsys):
    await scanner.scan()
    added_archive = await DB.get(Archive, 1, full=True)

    assert added_archive.hash == archive.hash
    assert pageset(added_archive.pages) == pageset(archive.pages)

    captured = capsys.readouterr()
    assert captured.out == "[+] archive.zip\n"


@pytest.mark.anyio
async def test_scanner_dedups_archive_contents(archive, scanner, capsys):
    archive = await DB.add(archive)

    dedup_path = archive.path + ".dedup"
    with ZipFile(archive.path, "r") as zin, ZipFile(dedup_path, "w") as zout:
        for info in zin.infolist():
            base, ext = os.path.splitext(info.filename)

            if base == "03":
                continue

            if ext == ".png":
                zout.writestr(f"0{base}.png", zin.read(info))
            else:
                zout.writestr(info.filename, zin.read(info))

    await scanner.scan()
    added_archive = await DB.get(Archive, 2, full=True)

    assert (
        added_archive.hash
        == "fc2ea810eddc231824aef44db62d5f3de89b3747e4aea6b5728c1532aabdeccd"
    )

    pages = set()
    for page in archive.pages:
        if page.path == "03.png":
            continue

        pages.add((f"0{page.path}", 2, page.image.hash))

    assert pageset(added_archive.pages) == pages

    captured = capsys.readouterr()
    assert captured.out == "[+] archive.zip.dedup\n"


@pytest.mark.anyio
async def test_scanner_skips_same_mtime(archive, scanner, capsys):
    archive = await DB.add(archive)
    await scanner.scan()

    captured = capsys.readouterr()
    assert captured.out == ""


@pytest.mark.anyio
async def test_scanner_finds_existing_before_duplicate(archive, scanner, capsys):
    stat = os.stat(archive.path)
    mtime = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc)

    before = await DB.add(archive)

    copy_path = before.path + ".copy"
    shutil.copyfile(Path(before.path), copy_path)

    await scanner.scan()

    after = await DB.get(Archive, before.id, full=True)
    assert after.hash == before.hash
    assert after.path == before.path
    assert after.mtime == mtime
    assert pageset(after.pages) == pageset(before.pages)

    captured = capsys.readouterr()
    assert captured.out == "[I] archive.zip.copy\n"


@pytest.mark.anyio
async def test_scanner_skips_non_zip(data, scanner, capsys):
    Path(data("contents/archive.zip")).unlink()
    Path(data("contents/non_zip.txt")).touch()
    await scanner.scan()

    captured = capsys.readouterr()
    assert captured.out == ""


@pytest.mark.anyio
async def test_scanner_skips_link(data, scanner, capsys):
    Path(data("contents/archive.zip")).rename(data("archive.zip"))
    os.symlink(data("archive.zip"), data("contents/archive.zip"))
    await scanner.scan()

    captured = capsys.readouterr()
    assert captured.out == ""


@pytest.mark.anyio
async def test_scanner_updates_mtime(archive, scanner, capsys):
    Path(archive.path).touch()
    stat = os.stat(archive.path)
    mtime = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc)

    archive = await DB.add(archive)
    await scanner.scan()

    updated_archive = await DB.get(Archive, archive.id, full=True)
    assert updated_archive.hash == archive.hash
    assert updated_archive.path == archive.path
    assert updated_archive.mtime == mtime
    assert pageset(updated_archive.pages) == pageset(archive.pages)

    captured = capsys.readouterr()
    assert captured.out == "[*] archive.zip\n"


@pytest.mark.anyio
async def test_scanner_updates_path(archive, scanner, capsys):
    new_path = archive.path + ".new"

    Path(archive.path).rename(new_path)
    stat = os.stat(new_path)
    mtime = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc)

    archive = await DB.add(archive)
    await scanner.scan()

    updated_archive = await DB.get(Archive, archive.id, full=True)
    assert updated_archive.hash == archive.hash
    assert updated_archive.path == new_path
    assert updated_archive.mtime == mtime
    assert pageset(updated_archive.pages) == pageset(archive.pages)

    captured = capsys.readouterr()
    assert captured.out == "[>] archive.zip -> archive.zip.new\n"


@pytest.mark.anyio
async def test_scanner_reports_missing(archive, scanner):
    archive = await DB.add(archive)
    Path(archive.path).unlink()
    await scanner.scan()

    assert scanner.registry.orphans == {archive.hash: (archive.id, archive.path)}


@pytest.mark.anyio
async def test_scanner_reports_duplicate(archive, scanner, capsys):
    archive = await DB.add(archive)
    copy_path = archive.path + ".copy"
    shutil.copyfile(Path(archive.path), copy_path)
    await scanner.scan()

    assert list(scanner.registry.duplicates) == [
        [
            (archive.path, Status.UNCHANGED),
            (copy_path, Status.IGNORED),
        ]
    ]

    captured = capsys.readouterr()
    assert captured.out == "[I] archive.zip.copy\n"


@pytest.mark.anyio
async def test_scanner_ignores_empty_archive(archive, scanner, capsys):
    Path(archive.path).unlink()

    empty_path = archive.path + ".empty"
    ZipFile(empty_path, "w").close()

    await scanner.scan()

    assert scanner.registry.marked == {}

    captured = capsys.readouterr()
    assert captured.out == ""


@pytest.mark.anyio
async def test_scanner_reports_conflict(archive, scanner, capsys):
    archive = await DB.add(archive)
    ZipFile(archive.path, "w").close()

    await scanner.scan()

    assert scanner.registry.conflicts == {
        archive.path: (
            archive.hash,
            "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
        )
    }

    captured = capsys.readouterr()
    assert captured.out == "[!] archive.zip\n"


@pytest.mark.anyio
async def test_scanner_reprocess(archive, data, scanner, capsys):
    await scanner.scan()

    captured = capsys.readouterr()
    assert captured.out == "[+] archive.zip\n"

    old_stat = os.stat(data(object_path("objects/", archive.cover.hash, "full")))
    old_mtime = datetime.fromtimestamp(old_stat.st_mtime, tz=timezone.utc)

    scanner.reprocess = True

    await scanner.scan()

    new_stat = os.stat(data(object_path("objects/", archive.cover.hash, "full")))
    new_mtime = datetime.fromtimestamp(new_stat.st_mtime, tz=timezone.utc)

    assert new_mtime > old_mtime

    captured = capsys.readouterr()
    assert captured.out == "[~] archive.zip\n"