| 1 |
import os |
| 2 |
import pathlib |
| 3 |
import platform |
| 4 |
import sys |
| 5 |
import types |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import execnet |
| 10 |
from execnet import RSync |
| 11 |
from execnet.gateway import Gateway |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="module") |
| 15 |
def group(request: pytest.FixtureRequest) -> execnet.Group: |
| 16 |
group = execnet.Group() |
| 17 |
request.addfinalizer(group.terminate) |
| 18 |
return group |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture(scope="module") |
| 22 |
def gw1(request: pytest.FixtureRequest, group: execnet.Group) -> Gateway: |
| 23 |
gw = group.makegateway("popen//id=gw1") |
| 24 |
request.addfinalizer(gw.exit) |
| 25 |
return gw |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope="module") |
| 29 |
def gw2(request: pytest.FixtureRequest, group: execnet.Group) -> Gateway: |
| 30 |
gw = group.makegateway("popen//id=gw2") |
| 31 |
request.addfinalizer(gw.exit) |
| 32 |
return gw |
| 33 |
|
| 34 |
|
| 35 |
needssymlink = pytest.mark.skipif( |
| 36 |
not hasattr(os, "symlink") |
| 37 |
or (platform.python_implementation() == "PyPy" and sys.platform == "win32"), |
| 38 |
reason="os.symlink not available", |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
class _dirs(types.SimpleNamespace): |
| 43 |
source: pathlib.Path |
| 44 |
dest1: pathlib.Path |
| 45 |
dest2: pathlib.Path |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def dirs(tmp_path: pathlib.Path) -> _dirs: |
| 50 |
dirs = _dirs( |
| 51 |
source=tmp_path / "source", |
| 52 |
dest1=tmp_path / "dest1", |
| 53 |
dest2=tmp_path / "dest2", |
| 54 |
) |
| 55 |
dirs.source.mkdir() |
| 56 |
dirs.dest1.mkdir() |
| 57 |
dirs.dest2.mkdir() |
| 58 |
return dirs |
| 59 |
|
| 60 |
|
| 61 |
def are_paths_equal(path1: pathlib.Path, path2: pathlib.Path) -> bool: |
| 62 |
if os.path.__name__ == "ntpath": |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
is_extended1 = str(path1).startswith("\\\\?\\") |
| 68 |
is_extended2 = str(path2).startswith("\\\\?\\") |
| 69 |
if is_extended1 and not is_extended2: |
| 70 |
path2 = pathlib.Path("\\\\?\\" + str(path2)) |
| 71 |
if not is_extended1 and is_extended2: |
| 72 |
path1 = pathlib.Path("\\\\?\\" + str(path1)) |
| 73 |
return path1 == path2 |
| 74 |
|
| 75 |
|
| 76 |
class TestRSync: |
| 77 |
def test_notargets(self, dirs: _dirs) -> None: |
| 78 |
rsync = RSync(dirs.source) |
| 79 |
with pytest.raises(IOError): |
| 80 |
rsync.send() |
| 81 |
assert rsync.send(raises=False) is None |
| 82 |
|
| 83 |
def test_dirsync(self, dirs: _dirs, gw1: Gateway, gw2: Gateway) -> None: |
| 84 |
dest = dirs.dest1 |
| 85 |
dest2 = dirs.dest2 |
| 86 |
source = dirs.source |
| 87 |
|
| 88 |
for s in ("content1", "content2", "content2-a-bit-longer"): |
| 89 |
subdir = source / "subdir" |
| 90 |
subdir.mkdir(exist_ok=True) |
| 91 |
subdir.joinpath("file1").write_text(s) |
| 92 |
rsync = RSync(dirs.source) |
| 93 |
rsync.add_target(gw1, dest) |
| 94 |
rsync.add_target(gw2, dest2) |
| 95 |
rsync.send() |
| 96 |
assert dest.joinpath("subdir").is_dir() |
| 97 |
assert dest.joinpath("subdir", "file1").is_file() |
| 98 |
assert dest.joinpath("subdir", "file1").read_text() == s |
| 99 |
assert dest2.joinpath("subdir").is_dir() |
| 100 |
assert dest2.joinpath("subdir", "file1").is_file() |
| 101 |
assert dest2.joinpath("subdir", "file1").read_text() == s |
| 102 |
for x in dest, dest2: |
| 103 |
fn = x.joinpath("subdir", "file1") |
| 104 |
os.utime(fn, (0, 0)) |
| 105 |
|
| 106 |
source.joinpath("subdir", "file1").unlink() |
| 107 |
rsync = RSync(source) |
| 108 |
rsync.add_target(gw2, dest2) |
| 109 |
rsync.add_target(gw1, dest) |
| 110 |
rsync.send() |
| 111 |
assert dest.joinpath("subdir", "file1").is_file() |
| 112 |
assert dest2.joinpath("subdir", "file1").is_file() |
| 113 |
rsync = RSync(source) |
| 114 |
rsync.add_target(gw1, dest, delete=True) |
| 115 |
rsync.add_target(gw2, dest2) |
| 116 |
rsync.send() |
| 117 |
assert not dest.joinpath("subdir", "file1").exists() |
| 118 |
assert dest2.joinpath("subdir", "file1").exists() |
| 119 |
|
| 120 |
def test_dirsync_twice(self, dirs: _dirs, gw1: Gateway, gw2: Gateway) -> None: |
| 121 |
source = dirs.source |
| 122 |
source.joinpath("hello").touch() |
| 123 |
rsync = RSync(source) |
| 124 |
rsync.add_target(gw1, dirs.dest1) |
| 125 |
rsync.send() |
| 126 |
assert dirs.dest1.joinpath("hello").exists() |
| 127 |
with pytest.raises(IOError): |
| 128 |
rsync.send() |
| 129 |
assert rsync.send(raises=False) is None |
| 130 |
rsync.add_target(gw1, dirs.dest2) |
| 131 |
rsync.send() |
| 132 |
assert dirs.dest2.joinpath("hello").exists() |
| 133 |
with pytest.raises(IOError): |
| 134 |
rsync.send() |
| 135 |
assert rsync.send(raises=False) is None |
| 136 |
|
| 137 |
def test_rsync_default_reporting( |
| 138 |
self, capsys: pytest.CaptureFixture[str], dirs: _dirs, gw1: Gateway |
| 139 |
) -> None: |
| 140 |
source = dirs.source |
| 141 |
source.joinpath("hello").touch() |
| 142 |
rsync = RSync(source) |
| 143 |
rsync.add_target(gw1, dirs.dest1) |
| 144 |
rsync.send() |
| 145 |
out, _err = capsys.readouterr() |
| 146 |
assert out.find("hello") != -1 |
| 147 |
|
| 148 |
def test_rsync_non_verbose( |
| 149 |
self, capsys: pytest.CaptureFixture[str], dirs: _dirs, gw1: Gateway |
| 150 |
) -> None: |
| 151 |
source = dirs.source |
| 152 |
source.joinpath("hello").touch() |
| 153 |
rsync = RSync(source, verbose=False) |
| 154 |
rsync.add_target(gw1, dirs.dest1) |
| 155 |
rsync.send() |
| 156 |
out, err = capsys.readouterr() |
| 157 |
assert not out |
| 158 |
assert not err |
| 159 |
|
| 160 |
@pytest.mark.skipif( |
| 161 |
sys.platform == "win32" or getattr(os, "_name", "") == "nt", |
| 162 |
reason="irrelevant on windows", |
| 163 |
) |
| 164 |
def test_permissions(self, dirs: _dirs, gw1: Gateway, gw2: Gateway) -> None: |
| 165 |
source = dirs.source |
| 166 |
dest = dirs.dest1 |
| 167 |
onedir = dirs.source / "one" |
| 168 |
onedir.mkdir() |
| 169 |
onedir.chmod(448) |
| 170 |
onefile = dirs.source / "file" |
| 171 |
onefile.touch() |
| 172 |
onefile.chmod(504) |
| 173 |
onefile_mtime = onefile.stat().st_mtime |
| 174 |
|
| 175 |
rsync = RSync(source) |
| 176 |
rsync.add_target(gw1, dest) |
| 177 |
rsync.send() |
| 178 |
|
| 179 |
destdir = dirs.dest1 / onedir.name |
| 180 |
destfile = dirs.dest1 / onefile.name |
| 181 |
assert destfile.stat().st_mode & 511 == 504 |
| 182 |
mode = destdir.stat().st_mode |
| 183 |
assert mode & 511 == 448 |
| 184 |
|
| 185 |
|
| 186 |
onedir.chmod(504) |
| 187 |
onefile.chmod(448) |
| 188 |
os.utime(onefile, (onefile_mtime, onefile_mtime)) |
| 189 |
|
| 190 |
rsync = RSync(source) |
| 191 |
rsync.add_target(gw1, dest) |
| 192 |
rsync.send() |
| 193 |
|
| 194 |
mode = destfile.stat().st_mode |
| 195 |
assert mode & 511 == 448, mode |
| 196 |
mode = destdir.stat().st_mode |
| 197 |
assert mode & 511 == 504 |
| 198 |
|
| 199 |
@pytest.mark.skipif( |
| 200 |
sys.platform == "win32" or getattr(os, "_name", "") == "nt", |
| 201 |
reason="irrelevant on windows", |
| 202 |
) |
| 203 |
def test_read_only_directories(self, dirs: _dirs, gw1: Gateway) -> None: |
| 204 |
source = dirs.source |
| 205 |
dest = dirs.dest1 |
| 206 |
sub = source / "sub" |
| 207 |
sub.mkdir() |
| 208 |
subsub = sub / "subsub" |
| 209 |
subsub.mkdir() |
| 210 |
sub.chmod(0o500) |
| 211 |
subsub.chmod(0o500) |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
rsync = RSync(source) |
| 216 |
rsync.add_target(gw1, dest) |
| 217 |
rsync.send() |
| 218 |
|
| 219 |
assert dest.joinpath("sub").stat().st_mode & 0o700 |
| 220 |
assert dest.joinpath("sub", "subsub").stat().st_mode & 0o700 |
| 221 |
|
| 222 |
@needssymlink |
| 223 |
def test_symlink_rsync(self, dirs: _dirs, gw1: Gateway) -> None: |
| 224 |
source = dirs.source |
| 225 |
dest = dirs.dest1 |
| 226 |
subdir = dirs.source / "subdir" |
| 227 |
subdir.mkdir() |
| 228 |
sourcefile = subdir / "existent" |
| 229 |
sourcefile.touch() |
| 230 |
source.joinpath("rellink").symlink_to(sourcefile.relative_to(source)) |
| 231 |
source.joinpath("abslink").symlink_to(sourcefile) |
| 232 |
|
| 233 |
rsync = RSync(source) |
| 234 |
rsync.add_target(gw1, dest) |
| 235 |
rsync.send() |
| 236 |
|
| 237 |
rellink = pathlib.Path(os.readlink(str(dest / "rellink"))) |
| 238 |
assert rellink == pathlib.Path("subdir/existent") |
| 239 |
|
| 240 |
abslink = pathlib.Path(os.readlink(str(dest / "abslink"))) |
| 241 |
expected = dest.joinpath(sourcefile.relative_to(source)) |
| 242 |
assert are_paths_equal(abslink, expected) |
| 243 |
|
| 244 |
@needssymlink |
| 245 |
def test_symlink2_rsync(self, dirs: _dirs, gw1: Gateway) -> None: |
| 246 |
source = dirs.source |
| 247 |
dest = dirs.dest1 |
| 248 |
subdir = dirs.source / "subdir" |
| 249 |
subdir.mkdir() |
| 250 |
sourcefile = subdir / "somefile" |
| 251 |
sourcefile.touch() |
| 252 |
subdir.joinpath("link1").symlink_to( |
| 253 |
subdir.joinpath("link2").relative_to(subdir) |
| 254 |
) |
| 255 |
subdir.joinpath("link2").symlink_to(sourcefile) |
| 256 |
subdir.joinpath("link3").symlink_to(source.parent) |
| 257 |
rsync = RSync(source) |
| 258 |
rsync.add_target(gw1, dest) |
| 259 |
rsync.send() |
| 260 |
expected = dest.joinpath(sourcefile.relative_to(dirs.source)) |
| 261 |
destsub = dest.joinpath("subdir") |
| 262 |
assert destsub.exists() |
| 263 |
link1 = pathlib.Path(os.readlink(str(destsub / "link1"))) |
| 264 |
assert are_paths_equal(link1, pathlib.Path("link2")) |
| 265 |
link2 = pathlib.Path(os.readlink(str(destsub / "link2"))) |
| 266 |
assert are_paths_equal(link2, expected) |
| 267 |
link3 = pathlib.Path(os.readlink(str(destsub / "link3"))) |
| 268 |
assert are_paths_equal(link3, source.parent) |
| 269 |
|
| 270 |
def test_callback(self, dirs: _dirs, gw1: Gateway) -> None: |
| 271 |
dest = dirs.dest1 |
| 272 |
source = dirs.source |
| 273 |
source.joinpath("existent").write_text("a" * 100) |
| 274 |
source.joinpath("existant2").write_text("a" * 10) |
| 275 |
total = {} |
| 276 |
|
| 277 |
def callback(cmd, lgt, channel): |
| 278 |
total[(cmd, lgt)] = True |
| 279 |
|
| 280 |
rsync = RSync(source, callback=callback) |
| 281 |
|
| 282 |
rsync.add_target(gw1, dest) |
| 283 |
rsync.send() |
| 284 |
|
| 285 |
assert total == {("list", 110): True, ("ack", 100): True, ("ack", 10): True} |
| 286 |
|
| 287 |
def test_file_disappearing(self, dirs: _dirs, gw1: Gateway) -> None: |
| 288 |
dest = dirs.dest1 |
| 289 |
source = dirs.source |
| 290 |
source.joinpath("ex").write_text("a" * 100) |
| 291 |
source.joinpath("ex2").write_text("a" * 100) |
| 292 |
|
| 293 |
class DRsync(RSync): |
| 294 |
def filter(self, x: str) -> bool: |
| 295 |
assert x != str(source) |
| 296 |
if x.endswith("ex2"): |
| 297 |
self.x = 1 |
| 298 |
source.joinpath("ex2").unlink() |
| 299 |
return True |
| 300 |
|
| 301 |
rsync = DRsync(source) |
| 302 |
rsync.add_target(gw1, dest) |
| 303 |
rsync.send() |
| 304 |
assert rsync.x == 1 |
| 305 |
assert len(list(dest.iterdir())) == 1 |
| 306 |
assert len(list(source.iterdir())) == 1 |