| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import filecmp |
| 16 |
import os |
| 17 |
import subprocess |
| 18 |
import time |
| 19 |
|
| 20 |
from rsyncfns import ( |
| 21 |
RSYNC, SCRATCHDIR, RSYNC_PEER, SRCDIR, TMPDIR, |
| 22 |
make_data_file, rsync_argv, test_fail, rsync_path_arg, rsh_cmd, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
os.environ['RSYNC_RSH'] = rsh_cmd() |
| 32 |
|
| 33 |
|
| 34 |
srcbase = TMPDIR / 'src_files' |
| 35 |
srcbase.mkdir(parents=True, exist_ok=True) |
| 36 |
home = SCRATCHDIR |
| 37 |
|
| 38 |
|
| 39 |
def make_testfile(path) -> None: |
| 40 |
"""~32 KiB of non-trivial content -- large enough to trigger rsync's |
| 41 |
block-matching delta path.""" |
| 42 |
make_data_file(path, 32768) |
| 43 |
|
| 44 |
|
| 45 |
def push(*args, label: str) -> None: |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
saved = os.getcwd() |
| 50 |
os.chdir(srcbase) |
| 51 |
try: |
| 52 |
proc = subprocess.run( |
| 53 |
rsync_argv(f'--rsync-path={rsync_path_arg()}', *args), |
| 54 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 55 |
) |
| 56 |
print(proc.stdout, end='') |
| 57 |
if proc.returncode != 0: |
| 58 |
test_fail(f"{label}: rsync exited {proc.returncode}") |
| 59 |
finally: |
| 60 |
os.chdir(saved) |
| 61 |
|
| 62 |
|
| 63 |
def assert_same(label: str, a, b) -> None: |
| 64 |
if not filecmp.cmp(a, b, shallow=False): |
| 65 |
test_fail(f"{label}: content mismatch between {a} and {b}") |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
(home / 'real-dir').mkdir() |
| 70 |
os.symlink('real-dir', home / 'dir') |
| 71 |
(srcbase / 'dir').mkdir() |
| 72 |
make_testfile(srcbase / 'dir' / 'file') |
| 73 |
|
| 74 |
push('-KRlptv', 'dir/file', 'localhost:', label="test 1 initial") |
| 75 |
if not (home / 'real-dir' / 'file').is_file(): |
| 76 |
test_fail("test 1: initial transfer did not create file through symlink") |
| 77 |
assert_same("test 1 initial", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 78 |
|
| 79 |
|
| 80 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 81 |
f.write(b"appended update\n") |
| 82 |
time.sleep(1) |
| 83 |
(srcbase / 'dir' / 'file').touch() |
| 84 |
|
| 85 |
push('-KRlptv', 'dir/file', 'localhost:', label="test 1 update") |
| 86 |
assert_same("test 1 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 91 |
f.write(b"another line\n") |
| 92 |
time.sleep(1) |
| 93 |
(srcbase / 'dir' / 'file').touch() |
| 94 |
|
| 95 |
push('-KRlptzv', 'dir/file', 'localhost:', label="test 2") |
| 96 |
assert_same("test 2", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
(home / 'nested_real' / 'sub').mkdir(parents=True) |
| 101 |
os.symlink('nested_real', home / 'nested') |
| 102 |
|
| 103 |
(srcbase / 'nested' / 'sub').mkdir(parents=True) |
| 104 |
make_testfile(srcbase / 'nested' / 'sub' / 'data.txt') |
| 105 |
|
| 106 |
push('-KRlptv', 'nested/sub/data.txt', 'localhost:', label="test 3 initial") |
| 107 |
|
| 108 |
with open(srcbase / 'nested' / 'sub' / 'data.txt', 'ab') as f: |
| 109 |
f.write(b"appended nested\n") |
| 110 |
time.sleep(1) |
| 111 |
(srcbase / 'nested' / 'sub' / 'data.txt').touch() |
| 112 |
|
| 113 |
push('-KRlptv', 'nested/sub/data.txt', 'localhost:', label="test 3 update") |
| 114 |
assert_same("test 3 update", |
| 115 |
srcbase / 'nested' / 'sub' / 'data.txt', |
| 116 |
home / 'nested_real' / 'sub' / 'data.txt') |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
(home / 'real-dir' / 'file').unlink() |
| 121 |
(home / 'real-dir' / 'file~').unlink(missing_ok=True) |
| 122 |
make_testfile(srcbase / 'dir' / 'file') |
| 123 |
|
| 124 |
push('-KRlptv', 'dir/file', 'localhost:', label="test 4 initial") |
| 125 |
|
| 126 |
old_content = (srcbase / 'dir' / 'file').read_bytes() |
| 127 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 128 |
f.write(b"backup update\n") |
| 129 |
time.sleep(1) |
| 130 |
(srcbase / 'dir' / 'file').touch() |
| 131 |
|
| 132 |
push('-KRlptv', '--backup', 'dir/file', 'localhost:', label="test 4 update") |
| 133 |
assert_same("test 4 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 134 |
if not (home / 'real-dir' / 'file~').is_file(): |
| 135 |
test_fail("test 4: backup file was not created") |
| 136 |
|
| 137 |
if (home / 'real-dir' / 'file~').read_bytes() != old_content: |
| 138 |
test_fail("test 4: backup file~ does not hold the pre-update content") |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
(home / 'real-dir' / 'file').unlink() |
| 143 |
(home / 'real-dir' / 'file~').unlink(missing_ok=True) |
| 144 |
make_testfile(srcbase / 'dir' / 'file') |
| 145 |
|
| 146 |
push('-KRlptv', '--inplace', 'dir/file', 'localhost:', label="test 5 initial") |
| 147 |
|
| 148 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 149 |
f.write(b"inplace update\n") |
| 150 |
time.sleep(1) |
| 151 |
(srcbase / 'dir' / 'file').touch() |
| 152 |
|
| 153 |
push('-KRlptv', '--inplace', 'dir/file', 'localhost:', label="test 5 update") |
| 154 |
assert_same("test 5 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
make_testfile(srcbase / 'topfile') |
| 159 |
home.mkdir(parents=True, exist_ok=True) |
| 160 |
|
| 161 |
push('-Rlptv', 'topfile', 'localhost:', label="test 6 initial") |
| 162 |
|
| 163 |
with open(srcbase / 'topfile', 'ab') as f: |
| 164 |
f.write(b"toplevel update\n") |
| 165 |
time.sleep(1) |
| 166 |
(srcbase / 'topfile').touch() |
| 167 |
|
| 168 |
push('-Rlptv', 'topfile', 'localhost:', label="test 6 update") |
| 169 |
assert_same("test 6 update", srcbase / 'topfile', home / 'topfile') |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
(home / 'real-dir' / 'file').unlink(missing_ok=True) |
| 174 |
make_testfile(srcbase / 'dir' / 'file') |
| 175 |
|
| 176 |
push('-KRlptv', '--protocol=28', '--partial-dir=.rsync-partial', |
| 177 |
'dir/file', 'localhost:', label="test 7 initial") |
| 178 |
|
| 179 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 180 |
f.write(b"partial-dir update\n") |
| 181 |
time.sleep(1) |
| 182 |
(srcbase / 'dir' / 'file').touch() |
| 183 |
|
| 184 |
push('-KRlptv', '--protocol=28', '--partial-dir=.rsync-partial', |
| 185 |
'dir/file', 'localhost:', label="test 7 update") |
| 186 |
assert_same("test 7 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
(home / 'real-dir' / 'file').unlink(missing_ok=True) |
| 191 |
make_testfile(srcbase / 'dir' / 'file') |
| 192 |
|
| 193 |
push('-KRlptv', '--protocol=28', 'dir/file', 'localhost:', label="test 8 initial") |
| 194 |
|
| 195 |
with open(srcbase / 'dir' / 'file', 'ab') as f: |
| 196 |
f.write(b"proto28 update\n") |
| 197 |
time.sleep(1) |
| 198 |
(srcbase / 'dir' / 'file').touch() |
| 199 |
|
| 200 |
push('-KRlptv', '--protocol=28', 'dir/file', 'localhost:', label="test 8 update") |
| 201 |
assert_same("test 8 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file') |