|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from borg.constants import * # noqa: F403 |
| 4 | +from borg.helpers import msgpack |
| 5 | +from borg.testsuite.archiver import exec_cmd |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def borg_env(tmp_path, monkeypatch): |
| 10 | + monkeypatch.setenv('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING', 'YES') |
| 11 | + monkeypatch.setenv('BORG_DELETE_I_KNOW_WHAT_I_AM_DOING', 'YES') |
| 12 | + monkeypatch.setenv('BORG_PASSPHRASE', 'waytooeasyonlyfortests') |
| 13 | + monkeypatch.setenv('BORG_SELFTEST', 'disabled') |
| 14 | + |
| 15 | + # Set up directories |
| 16 | + keys_path = tmp_path / 'keys' |
| 17 | + cache_path = tmp_path / 'cache' |
| 18 | + input_path = tmp_path / 'input' |
| 19 | + |
| 20 | + monkeypatch.setenv('BORG_KEYS_DIR', str(keys_path)) |
| 21 | + monkeypatch.setenv('BORG_CACHE_DIR', str(cache_path)) |
| 22 | + |
| 23 | + keys_path.mkdir() |
| 24 | + cache_path.mkdir() |
| 25 | + input_path.mkdir() |
| 26 | + |
| 27 | + # Create test file |
| 28 | + (input_path / 'file1').write_bytes(b'X' * 1024 * 80) |
| 29 | + |
| 30 | + cwd = os.getcwd() |
| 31 | + os.chdir(tmp_path) |
| 32 | + yield { |
| 33 | + 'repo': str(tmp_path / 'repository'), |
| 34 | + 'input': str(input_path), |
| 35 | + } |
| 36 | + os.chdir(cwd) |
| 37 | + |
| 38 | + |
| 39 | +def cmd(*args, **kw): |
| 40 | + kw.setdefault('fork', True) |
| 41 | + ret, output = exec_cmd(*args, **kw) |
| 42 | + if ret != 0: |
| 43 | + print(output) |
| 44 | + assert ret == 0 |
| 45 | + return output |
| 46 | + |
| 47 | + |
| 48 | +def test_missing_segment_in_hints(borg_env): |
| 49 | + """Test that compact handles missing segment files gracefully.""" |
| 50 | + repo = borg_env['repo'] |
| 51 | + |
| 52 | + cmd('init', '--encryption=none', repo) |
| 53 | + cmd('create', repo + '::archive1', 'input') |
| 54 | + cmd('delete', repo + '::archive1') |
| 55 | + |
| 56 | + # Find hints |
| 57 | + hints_files = sorted([f for f in os.listdir(repo) if f.startswith('hints.') and not f.endswith('.tmp')], |
| 58 | + key=lambda x: int(x.split('.')[1])) |
| 59 | + hints_file = os.path.join(repo, hints_files[-1]) |
| 60 | + |
| 61 | + with open(hints_file, 'rb') as f: |
| 62 | + hints = msgpack.unpack(f) |
| 63 | + |
| 64 | + # Find data segment |
| 65 | + target_segment = None |
| 66 | + for seg in hints[b'compact'].keys(): |
| 67 | + segment_file = os.path.join(repo, 'data', str(seg // 10000), str(seg)) |
| 68 | + if os.path.exists(segment_file) and os.path.getsize(segment_file) > 100: |
| 69 | + target_segment = seg |
| 70 | + break |
| 71 | + |
| 72 | + assert target_segment is not None |
| 73 | + |
| 74 | + # Delete segment file |
| 75 | + segment_file = os.path.join(repo, 'data', str(target_segment // 10000), str(target_segment)) |
| 76 | + os.unlink(segment_file) |
| 77 | + |
| 78 | + # Compact should succeed |
| 79 | + cmd('compact', repo) |
| 80 | + |
| 81 | + # Verify hints updated |
| 82 | + hints_files = sorted([f for f in os.listdir(repo) if f.startswith('hints.') and not f.endswith('.tmp')], |
| 83 | + key=lambda x: int(x.split('.')[1])) |
| 84 | + new_hints_file = os.path.join(repo, hints_files[-1]) |
| 85 | + |
| 86 | + with open(new_hints_file, 'rb') as f: |
| 87 | + new_hints = msgpack.unpack(f) |
| 88 | + |
| 89 | + assert target_segment not in new_hints[b'compact'] |
| 90 | + assert target_segment not in new_hints[b'segments'] |
| 91 | + |
| 92 | + |
| 93 | +def test_index_corruption_with_old_hints(borg_env): |
| 94 | + """Test that compact handles corrupted index (with old hints) gracefully.""" |
| 95 | + repo = borg_env['repo'] |
| 96 | + |
| 97 | + cmd('init', '--encryption=none', repo) |
| 98 | + cmd('create', repo + '::archive1', 'input') |
| 99 | + |
| 100 | + # Corrupt index |
| 101 | + index_files = sorted([f for f in os.listdir(repo) if f.startswith('index.') and not f.endswith('.tmp')], |
| 102 | + key=lambda x: int(x.split('.')[1])) |
| 103 | + index_path = os.path.join(repo, index_files[-1]) |
| 104 | + |
| 105 | + with open(index_path, 'wb') as f: |
| 106 | + f.write(b'corrupted') |
| 107 | + |
| 108 | + # Compact should succeed (with fix) |
| 109 | + cmd('compact', repo) |
0 commit comments