|
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 |
|
9 | | -from ...archive import ChunkBuffer, ArchiveChecker |
| 9 | +from ...archive import ArchiveChecker, ChunkBuffer |
10 | 10 | from ...constants import * # NOQA |
11 | | -from ...helpers import bin_to_hex, msgpack, CommandError, IntegrityError |
12 | | -from ...manifest import Manifest |
13 | | -from ...repository import Repository |
| 11 | +from ...helpers import bin_to_hex, msgpack, CommandError, Error, IntegrityError, sig_int |
| 12 | +from ...manifest import Archives, Manifest |
| 13 | +from ...repository import PackTracker, Repository |
14 | 14 | from ..repository_test import fchunk, corrupt_chunk_on_disk |
15 | 15 | from . import ( |
16 | 16 | cmd, |
@@ -75,6 +75,138 @@ def test_check_usage(archivers, request): |
75 | 75 | assert "archive2" in output |
76 | 76 |
|
77 | 77 |
|
| 78 | +def test_check_soft_interrupt(archivers, request, monkeypatch): |
| 79 | + """A mid-run Ctrl-C stops both check phases at a safe boundary (#7893): the repository check persists |
| 80 | + its checked packs for a later partial check to resume, and the archive check runs finish() and then |
| 81 | + raises. The check is read-only, so a normal check still passes afterwards.""" |
| 82 | + archiver = request.getfixturevalue(archivers) |
| 83 | + check_cmd_setup(archiver) # produces many packs |
| 84 | + |
| 85 | + # repository check: interrupt after the first pack. |
| 86 | + with Repository(archiver.repository_path, exclusive=True) as repository: |
| 87 | + orig_hash = repository.store.hash |
| 88 | + pack_checks = [] |
| 89 | + |
| 90 | + def hash_then_interrupt(key): |
| 91 | + result = orig_hash(key) |
| 92 | + if key.startswith("packs/"): # count pack checks, not the index files hashed first |
| 93 | + pack_checks.append(key) |
| 94 | + if len(pack_checks) == 1: # one Ctrl-C after the first pack is checked |
| 95 | + sig_int._sig_int_triggered = True |
| 96 | + return result |
| 97 | + |
| 98 | + monkeypatch.setattr(repository.store, "hash", hash_then_interrupt) |
| 99 | + try: |
| 100 | + repository.check() |
| 101 | + finally: |
| 102 | + sig_int._sig_int_triggered = False |
| 103 | + assert len(PackTracker.load(repository.store)) == 1 # the pack checked before the break persisted |
| 104 | + |
| 105 | + # a partial check resumes from the saved record (the one pack checked before the interrupt). |
| 106 | + output = cmd(archiver, "check", "-v", "--repository-only", "--max-duration=600", exit_code=0) |
| 107 | + assert "1 pack check results on record" in output |
| 108 | + |
| 109 | + # archive check: interrupt verify_data after 3 chunks. |
| 110 | + with Repository(archiver.repository_path, exclusive=True) as repository: |
| 111 | + orig_get = repository.get |
| 112 | + get_calls = 0 |
| 113 | + |
| 114 | + def get_then_interrupt(*args, **kwargs): |
| 115 | + nonlocal get_calls |
| 116 | + get_calls += 1 |
| 117 | + if get_calls == 3: # trip mid-loop, after 3 chunks |
| 118 | + sig_int._sig_int_triggered = True |
| 119 | + return orig_get(*args, **kwargs) |
| 120 | + |
| 121 | + monkeypatch.setattr(repository, "get", get_then_interrupt) |
| 122 | + try: |
| 123 | + with pytest.raises(Error, match="Got Ctrl-C"): |
| 124 | + ArchiveChecker().check(repository, verify_data=True, sort_by="ts", format="{archive} {time} {id}") |
| 125 | + finally: |
| 126 | + sig_int._sig_int_triggered = False |
| 127 | + # verify_data breaks at the chunk it interrupted on, and the skipped scans issue no more get()s. |
| 128 | + assert get_calls == 3 |
| 129 | + |
| 130 | + # nothing changed, so a normal check passes. |
| 131 | + cmd(archiver, "check", exit_code=0) |
| 132 | + |
| 133 | + |
| 134 | +def test_check_repair_soft_interrupt(archivers, request, monkeypatch): |
| 135 | + """A Ctrl-C after the first archive of a --repair archive check stops at the archive boundary, runs |
| 136 | + finish() (dropping the chunk index, writing the manifest), then raises. No archive is lost, and a |
| 137 | + second --repair finishes the job so a following check reports the repository consistent.""" |
| 138 | + archiver = request.getfixturevalue(archivers) |
| 139 | + check_cmd_setup(archiver) # two archives |
| 140 | + |
| 141 | + orig_create = Archives.create |
| 142 | + |
| 143 | + def create_then_interrupt(self, *args, **kwargs): |
| 144 | + orig_create(self, *args, **kwargs) |
| 145 | + sig_int._sig_int_triggered = True # one Ctrl-C after the first archive was rebuilt |
| 146 | + |
| 147 | + monkeypatch.setattr(Archives, "create", create_then_interrupt) |
| 148 | + try: |
| 149 | + with Repository(archiver.repository_path, exclusive=True) as repository: |
| 150 | + with pytest.raises(Error, match="Got Ctrl-C"): |
| 151 | + ArchiveChecker().check(repository, repair=True, sort_by="ts", format="{archive} {time} {id}") |
| 152 | + finally: |
| 153 | + sig_int._sig_int_triggered = False # reset the global flag for the following tests |
| 154 | + # restore the real method; monkeypatch.undo() would also drop the autouse env (BORG_TESTONLY_WEAKEN_KDF). |
| 155 | + monkeypatch.setattr(Archives, "create", orig_create) |
| 156 | + |
| 157 | + # both archives survive the interrupt between archives. |
| 158 | + output = cmd(archiver, "repo-list", exit_code=0) |
| 159 | + assert "archive1" in output |
| 160 | + assert "archive2" in output |
| 161 | + |
| 162 | + # a second --repair finishes the job; a plain check then finds no problems. |
| 163 | + cmd(archiver, "check", "--repair", exit_code=0) |
| 164 | + cmd(archiver, "check", exit_code=0) |
| 165 | + |
| 166 | + |
| 167 | +def test_check_interrupt_skips_archive_check(archivers, request, monkeypatch): |
| 168 | + """A Ctrl-C during the repository check makes a full `borg check` skip the archive check. do_check |
| 169 | + raises at the sig_int guard, which sits before the archive_checker.check() call, so the raise itself |
| 170 | + is the skip. Exercises the do_check path (the other soft-interrupt tests call check() directly).""" |
| 171 | + archiver = request.getfixturevalue(archivers) |
| 172 | + if archiver.EXE: # a class-level monkeypatch cannot reach the borg.exe subprocess |
| 173 | + pytest.skip("in-process store patch does not apply to the binary") |
| 174 | + check_cmd_setup(archiver) # produces many packs |
| 175 | + |
| 176 | + from borgstore.store import Store |
| 177 | + |
| 178 | + orig_hash = Store.hash |
| 179 | + pack_checks = [] |
| 180 | + |
| 181 | + def hash_then_interrupt(self, key): |
| 182 | + result = orig_hash(self, key) |
| 183 | + if key.startswith("packs/"): # count pack checks, not the index files hashed first |
| 184 | + pack_checks.append(key) |
| 185 | + if len(pack_checks) == 1: # one Ctrl-C after the first pack is checked |
| 186 | + sig_int._sig_int_triggered = True |
| 187 | + return result |
| 188 | + |
| 189 | + # spy on the archive check: "Got Ctrl-C" is also raised inside ArchiveChecker.check(), so matching the |
| 190 | + # message alone would not prove the skip. Recording that check() never runs is the load-bearing assertion. |
| 191 | + orig_check = ArchiveChecker.check |
| 192 | + archive_check_ran = False |
| 193 | + |
| 194 | + def spy_check(self, *args, **kwargs): |
| 195 | + nonlocal archive_check_ran |
| 196 | + archive_check_ran = True |
| 197 | + return orig_check(self, *args, **kwargs) |
| 198 | + |
| 199 | + monkeypatch.setattr(Store, "hash", hash_then_interrupt) |
| 200 | + monkeypatch.setattr(ArchiveChecker, "check", spy_check) |
| 201 | + try: |
| 202 | + # exec_cmd calls Archiver.run() directly; only main() maps Error to an exit code, so it propagates. |
| 203 | + with pytest.raises(Error, match="Got Ctrl-C"): |
| 204 | + cmd(archiver, "check", "-v") |
| 205 | + finally: |
| 206 | + sig_int._sig_int_triggered = False |
| 207 | + assert archive_check_ran is False # do_check raised at the sig_int guard, before archive_checker.check() |
| 208 | + |
| 209 | + |
78 | 210 | def test_check_max_age(archivers, request): |
79 | 211 | archiver = request.getfixturevalue(archivers) |
80 | 212 | check_cmd_setup(archiver) |
|
0 commit comments