| 1 |
|
| 2 |
"""Repeated backup-dir separators must not replace the backup directory.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import subprocess |
| 6 |
|
| 7 |
from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail |
| 8 |
|
| 9 |
|
| 10 |
OLD = b'old destination bytes\n' |
| 11 |
NEW = b'new source bytes with a different length\n' |
| 12 |
SENTINEL = b'unrelated backup history\n' |
| 13 |
NESTED = b'nested backup history\n' |
| 14 |
|
| 15 |
base = SCRATCHDIR / 'backup-dir-repeated-separator-delete' |
| 16 |
rmtree(base) |
| 17 |
source = base / 'source' |
| 18 |
destination = base / 'destination' |
| 19 |
backup = base / 'backup' |
| 20 |
makepath(source) |
| 21 |
makepath(destination) |
| 22 |
makepath(backup / 'nested') |
| 23 |
|
| 24 |
(source / 'file').write_bytes(NEW) |
| 25 |
(destination / 'file').write_bytes(OLD) |
| 26 |
(backup / 'sentinel').write_bytes(SENTINEL) |
| 27 |
(backup / 'nested' / 'history').write_bytes(NESTED) |
| 28 |
os.utime(source / 'file', (1_750_000_000, 1_750_000_000)) |
| 29 |
os.utime(destination / 'file', (1_700_000_000, 1_700_000_000)) |
| 30 |
|
| 31 |
proc = subprocess.run( |
| 32 |
rsync_argv('-a', '--backup', f'--backup-dir={backup}//', |
| 33 |
f'{source}/', f'{destination}/'), |
| 34 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 35 |
) |
| 36 |
if proc.returncode != 0: |
| 37 |
test_fail(f'repeated-separator transfer exited {proc.returncode}:\n{proc.stdout}') |
| 38 |
if not backup.is_dir(): |
| 39 |
test_fail('repeated separator replaced the backup directory itself') |
| 40 |
if not (backup / 'file').is_file() or (backup / 'file').read_bytes() != OLD: |
| 41 |
test_fail('old destination was not stored as backup/file') |
| 42 |
if not (backup / 'sentinel').is_file() or (backup / 'sentinel').read_bytes() != SENTINEL: |
| 43 |
test_fail('repeated separator deleted unrelated top-level backup history') |
| 44 |
if ((backup / 'nested' / 'history').read_bytes() != NESTED): |
| 45 |
test_fail('repeated separator deleted unrelated nested backup history') |
| 46 |
if (destination / 'file').read_bytes() != NEW: |
| 47 |
test_fail('new source content was not published at the destination') |
| 48 |
|
| 49 |
print('backup-dir-repeated-separator-delete: backup tree and history preserved') |