-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ftpsync.py
368 lines (314 loc) · 10.9 KB
/
test_ftpsync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""Unit tests."""
import ftplib
import json
import netrc
import os
from pathlib import Path
from typing import Any
from unittest.mock import ANY, call, patch
import pytest
from ftpsync import deleted_files, file_hash, folder_hashes, main, new_files
# ruff: noqa: S101 (use of assert detected), expected in test cases.
# Using the license file for this test, as it rarely changes.
# If it changes, recalculate using `shasum -a 256 LICENSE`.
LICENSE_HASH = "2f3a98ffc7e14d7476db1fcc4f2ed041c8d9050f1ced14355f0d653cd5d5d24c"
TEST_DATA_FOLDER = "test-data"
TEST_DATA_HASHES = {
"a": "a5b2de337a986e7b9c1d178b7ed171b6cf912d05ab69c4aeca4e7e7665bfacb2",
"b": "c5e1064872056c435c0aa2239a3c11bdbb32536615cef6a5a412fdb7e08c5f76",
"folder/c": "92b735e707afcfa9ac4ba9017a2a633f72d3f8901f309adf1052eeed375bb1b5",
"folder/sub/d": "6b5c8a22ec39c76fb504f5b377d43ed67008ef3407df29b111b5dcbb15a8c986",
}
def test_filehash() -> None:
"""Test that filehash returns the correct SHA256 hash."""
testfile = Path(__file__).parent / "LICENSE"
assert file_hash(testfile) == LICENSE_HASH
def test_filehash_raises() -> None:
"""Test that filehash with a nonexisting file raises FileNotFoundError."""
with pytest.raises(FileNotFoundError):
file_hash(Path("This file doesn't exist"))
def test_folder_hashes() -> None:
"""Test that folder_hashes returns the correct result."""
os.chdir(Path(__file__).parent / TEST_DATA_FOLDER)
hashes = folder_hashes()
assert hashes == TEST_DATA_HASHES
FAKE_DIR = {
"a": "1234",
"b": "4567",
"folder/c": "8901",
"folder/subfolder/d": "2345",
"folder/subfolder/e": "XYZ",
}
def test_new_files_unchanged_is_empty() -> None:
"""Test that new_files returns an empty list if new and old are the same."""
assert len(new_files(FAKE_DIR, FAKE_DIR)) == 0
def test_new_files_ignores_deletions() -> None:
"""Test that new_files is unaffected by deletions."""
assert len(new_files(new={}, old=FAKE_DIR)) == 0
def test_new_files_finds_added_files() -> None:
"""Test that new_files returns added files."""
added_items = {
"f": "ABCD",
"folder/g": "EFGH",
"folder/subfolder/h": "IJKL",
}
new_dir = FAKE_DIR | added_items
assert set(new_files(new=new_dir, old=FAKE_DIR)) == set(added_items.keys())
def test_new_files_finds_changed_files() -> None:
"""Test that new_files returns changed files."""
changed_items = {
"a": "ABCD",
"folder/c": "EFGH",
"folder/subfolder/e": "IJKL",
}
new_dir = FAKE_DIR | changed_items
assert set(new_files(new=new_dir, old=FAKE_DIR)) == set(changed_items.keys())
def test_deleted_files_unchanged_is_empty() -> None:
"""Test that deleted_files returns an empty list if new and old are the same."""
assert len(deleted_files(FAKE_DIR, FAKE_DIR)) == 0
def test_deleted_files_finds_all() -> None:
"""Test that deleted_files works correctly if all files are deleted."""
assert set(deleted_files(new={}, old=FAKE_DIR)) == set(FAKE_DIR.keys())
def test_deleted_files_finds_deleted() -> None:
"""Test that deleted_files returns deleted files."""
deleted = {
"a",
"folder/subfolder/e",
}
new_dir = FAKE_DIR.copy()
for item in deleted:
del new_dir[item]
assert set(deleted_files(new=new_dir, old=FAKE_DIR)) == deleted
def test_deleted_files_ignores_changed_files() -> None:
"""Test that deleted_files ignores changed files."""
changed_items = {
"a": "ABCD",
"folder/c": "EFGH",
"folder/subfolder/e": "IJKL",
}
new_dir = FAKE_DIR | changed_items
assert len(deleted_files(new=new_dir, old=FAKE_DIR)) == 0
FAKE_SERVER = "ftp.example.com"
FAKE_USER = "USER"
FAKE_PASSWORD = "SUPER SECRET" # noqa: S105 possible secret
FULL_UPLOAD_OPERATIONS = [
call.storbinary("STOR a", ANY),
call.storbinary("STOR b", ANY),
call.mkd("folder"),
call.storbinary("STOR folder/c", ANY),
call.mkd("folder/sub"),
call.storbinary("STOR folder/sub/d", ANY),
]
@pytest.mark.parametrize(
("hashes", "operations"),
[
pytest.param(TEST_DATA_HASHES, [], id="unchanged"),
pytest.param({}, FULL_UPLOAD_OPERATIONS, id="blank"),
pytest.param(
TEST_DATA_HASHES | {"other_folder/x": "0000"},
[
call.delete("other_folder/x"),
],
id="delete",
),
pytest.param(
TEST_DATA_HASHES | {"folder/c": "CCCC"},
[
call.storbinary("STOR folder/c", ANY),
],
id="modified",
),
pytest.param(
{k: v for k, v in TEST_DATA_HASHES.items() if k != "folder/c"},
[
call.storbinary("STOR folder/c", ANY),
],
id="added",
),
pytest.param(
{f"./{k}": v for k, v in TEST_DATA_HASHES.items()},
[],
id="old-format",
),
],
)
def test_incremental_mocked(hashes: dict[str, str], operations: list[Any]) -> None:
"""Test incremental upload (hash file exists and is valid).
Uses a mocked FTP class.
"""
args = [
"--user",
FAKE_USER,
"--password",
FAKE_PASSWORD,
"--source",
str(Path(__file__).parent / TEST_DATA_FOLDER),
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("io.StringIO") as mock_string_io_class,
):
ftp = mock_ftp_class.return_value
ftp.__enter__.return_value = ftp
io = mock_string_io_class.return_value
io.read.return_value = json.dumps(hashes)
main(args)
assert ftp.mock_calls == [
call.__enter__(),
call.connect(FAKE_SERVER),
call.login(FAKE_USER, FAKE_PASSWORD),
call.set_debuglevel(0),
call.prot_p(),
call.cwd("html"),
call.retrlines("RETR .hashes.json", io.write),
call.delete(".hashes.json"),
*operations,
call.storlines("STOR .hashes.json", ANY),
call.__exit__(None, None, None),
]
def test_full_upload_mocked() -> None:
"""Test full upload (hash file absent).
Uses a mocked FTP class.
"""
args = [
"--user",
FAKE_USER,
"--password",
FAKE_PASSWORD,
"--source",
str(Path(__file__).parent / TEST_DATA_FOLDER),
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("io.StringIO") as mock_string_io_class,
):
ftp = mock_ftp_class.return_value
ftp.__enter__.return_value = ftp
io = mock_string_io_class.return_value
ftp.retrlines.side_effect = ftplib.error_perm
ftp.mlsd.side_effect = [
[
("file1", {"type": "file"}),
("file2", {"type": "file"}),
("folder", {"type": "dir"}),
],
[
("child", {"type": "file"}),
],
]
main(args)
assert ftp.mock_calls == [
call.__enter__(),
call.connect(FAKE_SERVER),
call.login(FAKE_USER, FAKE_PASSWORD),
call.set_debuglevel(0),
call.prot_p(),
call.cwd("html"),
call.retrlines("RETR .hashes.json", io.write),
call.mlsd("", facts=["type"]),
call.delete("file1"),
call.delete("file2"),
call.mlsd("folder", facts=["type"]),
call.delete("folder/child"),
call.rmd("folder"),
*FULL_UPLOAD_OPERATIONS,
call.storlines("STOR .hashes.json", ANY),
call.__exit__(None, None, None),
]
def test_netrc() -> None:
"""Test loading credentials from .netrc."""
args = [
"--netrc",
"--source",
str(Path(__file__).parent / TEST_DATA_FOLDER),
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("io.StringIO") as mock_string_io_class,
patch("netrc.netrc") as mock_netrc_class,
):
ftp = mock_ftp_class.return_value
ftp.__enter__.return_value = ftp
io = mock_string_io_class.return_value
io.read.return_value = json.dumps(TEST_DATA_HASHES)
mock_netrc_class.return_value.authenticators.return_value = (
FAKE_USER,
"",
FAKE_PASSWORD,
)
main(args)
assert ftp.mock_calls == [
call.__enter__(),
call.connect(FAKE_SERVER),
call.login(FAKE_USER, FAKE_PASSWORD),
call.set_debuglevel(0),
call.prot_p(),
call.cwd("html"),
call.retrlines("RETR .hashes.json", io.write),
call.delete(".hashes.json"),
call.storlines("STOR .hashes.json", ANY),
call.__exit__(None, None, None),
]
def test_netrc_no_credentials(capsys: pytest.CaptureFixture) -> None:
"""Test that missing credentials in .netrc report an appropriate error."""
args = [
"--netrc",
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("netrc.netrc") as mock_netrc_class,
):
mock_netrc_class.return_value.authenticators.return_value = None
with pytest.raises(SystemExit):
main(args)
assert mock_ftp_class.mock_calls == []
captured = capsys.readouterr()
assert "Traceback" not in captured.err
assert "no credentials" in captured.err
def test_netrc_user_mismatch(capsys: pytest.CaptureFixture) -> None:
"""Test user name mismatch between command line and .netrc.
This should report an appropriate error.
"""
args = [
"--user",
FAKE_USER,
"--netrc",
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("netrc.netrc") as mock_netrc_class,
):
mock_netrc_class.return_value.authenticators.return_value = (
"someone else",
"",
"super secret",
)
with pytest.raises(SystemExit):
main(args)
assert mock_ftp_class.mock_calls == []
captured = capsys.readouterr()
assert "Traceback" not in captured.err
assert "do not match" in captured.err
@pytest.mark.parametrize("error", [FileNotFoundError, netrc.NetrcParseError("test")])
def test_netrc_no_file(capsys: pytest.CaptureFixture, error: Exception) -> None:
"""Test that common .netrc errors are reported neatly, without traceback."""
args = [
"--netrc",
FAKE_SERVER,
]
with (
patch("ftplib.FTP_TLS") as mock_ftp_class,
patch("netrc.netrc") as mock_netrc_class,
):
mock_netrc_class.side_effect = error
with pytest.raises(SystemExit):
main(args)
assert mock_ftp_class.mock_calls == []
captured = capsys.readouterr()
assert "Traceback" not in captured.err
assert ".netrc" in captured.err