-
Notifications
You must be signed in to change notification settings - Fork 136
/
test_verify.py
325 lines (269 loc) · 10.5 KB
/
test_verify.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
import configparser
import os
import sys
import unittest.mock as mock
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from shutil import rmtree
from tempfile import gettempdir
from typing import Any
from unittest.mock import AsyncMock
import pytest
from _pytest.monkeypatch import MonkeyPatch
from aiohttp.client_exceptions import ClientResponseError, ServerTimeoutError
import bandersnatch
from bandersnatch.master import Master
from bandersnatch.utils import convert_url_to_path, find
from bandersnatch_storage_plugins.filesystem import pathlib
from bandersnatch.verify import ( # isort:skip
get_latest_json,
delete_unowned_files,
metadata_verify,
verify_producer,
verify,
)
async def do_nothing(*args: Any, **kwargs: Any) -> None:
pass
async def fake_fetch(_: str, save_path: Path, *__: Any) -> None:
save_path.write_text("fake text")
def some_dirs(*args: Any, **kwargs: Any) -> list[str]:
return ["/data/pypi/web/json/bandersnatch", "/data/pypi/web/json/black"]
def some_paths(*_: Any, **__: Any) -> list[Path]:
return [Path("/data/pypi/web/json/bandersnatch"), Path("/data/pypi/web/json/black")]
class FakeConfig:
def get(self, section: str, item: str) -> str:
if section == "mirror":
if item == "directory":
return "/data/pypi"
if item == "master":
return "https://unittest.org"
if item == "storage-backend":
return "filesystem"
return ""
def getfloat(self, section: str, item: str, fallback: float = 0.5) -> float:
return fallback
def getint(self, section: str, item: str, fallback: int = 5) -> int:
return fallback
def getboolean(self, section: str, item: str, fallback: bool = False) -> bool:
if section == "mirror":
if item == "stop-on-error":
return False
return fallback
# TODO: Support testing sharded simple dirs
class FakeMirror:
def __init__(self, entropy: str = "") -> None:
self.mirror_base = Path(gettempdir()) / f"pypi_unittest_{os.getpid()}{entropy}"
if self.mirror_base.exists():
return
self.web_base = self.mirror_base / "web"
self.web_base.mkdir(parents=True)
self.json_path = self.web_base / "json"
self.package_path = self.web_base / "packages"
self.pypi_path = self.web_base / "pypi"
self.simple_path = self.web_base / "simple"
for web_dir in (
self.json_path,
self.package_path,
self.pypi_path,
self.simple_path,
):
web_dir.mkdir()
self.pypi_packages = {
"bandersnatch": {
"bandersnatch-0.6.9": {
"filename": "bandersnatch-0.6.9.tar.gz",
"contents": "69",
"sha256": "b35e87b5838011a3637be660e4238af9a55e4edc74404c990f7a558e7f416658", # noqa: E501
"url": "https://test.pypi.org/packages/8f/1a/6969/bandersnatch-0.6.9.tar.gz", # noqa: E501
}
},
"black": {
"black-2018.6.9": {
"filename": "black-2018.6.9.tar.gz",
"contents": "69",
"sha256": "b35e87b5838011a3637be660e4238af9a55e4edc74404c990f7a558e7f416658", # noqa: E501
"url": "https://test.pypi.org/packages/8f/1a/6969/black-2018.6.9.tar.gz", # noqa: E501
},
"black-2019.6.9": {
"filename": "black-2019.6.9.tar.gz",
"contents": "1469",
"sha256": "c896470f5975bd5dc7d173871faca19848855b01bacf3171e9424b8a993b528b", # noqa: E501
"url": "https://test.pypi.org/packages/8f/1a/1aa0/black-2019.6.9.tar.gz", # noqa: E501
},
},
}
# Create each subdir of web
self.setup_json()
self.setup_simple()
self.setup_packages()
def clean_up(self) -> None:
if self.mirror_base.exists():
rmtree(self.mirror_base)
def setup_json(self) -> None:
for pkg in self.pypi_packages.keys():
pkg_json = self.json_path / pkg
pkg_json.touch()
pkg_legacy_json = self.pypi_path / pkg / "json"
pkg_legacy_json.parent.mkdir()
pkg_legacy_json.symlink_to(str(pkg_json))
def setup_packages(self) -> None:
for _pkg, dists in self.pypi_packages.items():
for _version, metadata in dists.items():
dist_file = self.web_base / convert_url_to_path(metadata["url"])
dist_file.parent.mkdir(exist_ok=True, parents=True)
with dist_file.open("w") as dfp:
dfp.write(metadata["contents"])
def setup_simple(self) -> None:
for pkg in self.pypi_packages.keys():
pkg_dir = self.simple_path / pkg
pkg_dir.mkdir()
index_path = pkg_dir / "index.html"
index_path.touch()
@pytest.mark.asyncio
async def test_verify_producer(monkeypatch: MonkeyPatch) -> None:
fm = FakeMirror("test_async_verify")
fc = configparser.ConfigParser()
fc["mirror"] = {}
fc["mirror"]["verifiers"] = "2"
master = Master("https://unittest.org")
json_files = ["web/json/bandersnatch", "web/json/black"]
monkeypatch.setattr(bandersnatch.verify, "verify", do_nothing)
await verify_producer(master, fc, [], fm.mirror_base, json_files, mock.Mock(), None)
def test_fake_mirror() -> None:
expected_mirror_layout = """\
web
web{0}json
web{0}json{0}bandersnatch
web{0}json{0}black
web{0}packages
web{0}packages{0}8f
web{0}packages{0}8f{0}1a
web{0}packages{0}8f{0}1a{0}1aa0
web{0}packages{0}8f{0}1a{0}1aa0{0}black-2019.6.9.tar.gz
web{0}packages{0}8f{0}1a{0}6969
web{0}packages{0}8f{0}1a{0}6969{0}bandersnatch-0.6.9.tar.gz
web{0}packages{0}8f{0}1a{0}6969{0}black-2018.6.9.tar.gz
web{0}pypi
web{0}pypi{0}bandersnatch
web{0}pypi{0}bandersnatch{0}json
web{0}pypi{0}black
web{0}pypi{0}black{0}json
web{0}simple
web{0}simple{0}bandersnatch
web{0}simple{0}bandersnatch{0}index.html
web{0}simple{0}black
web{0}simple{0}black{0}index.html""".format(
os.sep
)
fm = FakeMirror("_mirror_base_test")
assert expected_mirror_layout == find(str(fm.mirror_base), True)
fm.clean_up()
@pytest.mark.asyncio
async def test_delete_unowned_files() -> None:
executor = ThreadPoolExecutor(max_workers=2)
fm = FakeMirror("_test_delete_files")
# Leave out black-2018.6.9.tar.gz so it gets deleted
all_pkgs = [
fm.mirror_base / "web/packages/8f/1a/1aa0/black-2019.6.9.tar.gz",
fm.mirror_base / "web/packages/8f/1a/6969/bandersnatch-0.6.9.tar.gz",
]
await delete_unowned_files(fm.mirror_base, executor, all_pkgs, True)
await delete_unowned_files(fm.mirror_base, executor, all_pkgs, False)
deleted_path = fm.mirror_base / "web/packages/8f/1a/6969/black-2018.6.9.tar.gz"
assert not deleted_path.exists()
fm.clean_up()
@pytest.mark.asyncio
async def test_get_latest_json(monkeypatch: MonkeyPatch) -> None:
executor = ThreadPoolExecutor(max_workers=2)
json_path = Path(gettempdir()) / f"unittest_{os.getpid()}.json"
master = Master("https://unittest.org")
monkeypatch.setattr(master, "url_fetch", fake_fetch)
await get_latest_json(master, json_path, executor)
assert json_path.read_text() == "fake text"
@pytest.mark.asyncio
async def test_metadata_verify(monkeypatch: MonkeyPatch) -> None:
class FakeArgs:
delete = True
dry_run = True
workers = 2
fa = FakeArgs()
fc = FakeConfig()
monkeypatch.setattr(bandersnatch.verify, "verify_producer", do_nothing)
monkeypatch.setattr(bandersnatch.verify, "delete_unowned_files", do_nothing)
monkeypatch.setattr(pathlib.Path, "iterdir", some_paths)
await metadata_verify(fc, fa) # type: ignore
@pytest.mark.asyncio
async def test_get_latest_json_timeout(
tmp_path: Path, monkeypatch: MonkeyPatch
) -> None:
class FakeArgs:
delete = True
dry_run = False
json_update = True
workers = 2
fa = FakeArgs()
fc = FakeConfig()
master = Master(fc.get("mirror", "master"))
url_fetch_timeout = AsyncMock(side_effect=ServerTimeoutError)
monkeypatch.setattr(master, "url_fetch", url_fetch_timeout)
jsonpath = tmp_path / "web" / "json"
jsonpath.mkdir(parents=True)
jsonfile = jsonpath / "bandersnatch"
jsonfile.touch()
all_package_files: list[Path] = []
await verify(
master, fc, "bandersnatch", tmp_path, all_package_files, fa # type: ignore
) # noqa: E501
assert jsonfile.exists()
assert not all_package_files
@pytest.mark.asyncio
async def test_get_latest_json_404(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
class FakeArgs:
delete = True
dry_run = False
json_update = True
workers = 2
fa = FakeArgs()
fc = FakeConfig()
master = Master(fc.get("mirror", "master"))
url_fetch_404 = AsyncMock(
side_effect=ClientResponseError(status=404, history=(), request_info=None)
)
monkeypatch.setattr(master, "url_fetch", url_fetch_404)
jsonpath = tmp_path / "web" / "json"
jsonpath.mkdir(parents=True)
jsonfile = jsonpath / "bandersnatch"
jsonfile.touch()
all_package_files: list[Path] = []
await verify(
master, fc, "bandersnatch", tmp_path, all_package_files, fa # type: ignore # noqa: E501
)
assert not jsonfile.exists()
assert not all_package_files
@pytest.mark.asyncio
async def test_verify_url_exception(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
class FakeArgs:
delete = True
dry_run = False
json_update = False
workers = 2
fa = FakeArgs()
fc = FakeConfig()
master = Master(fc.get("mirror", "master"))
url_fetch_404 = AsyncMock(
side_effect=ClientResponseError(status=404, history=(), request_info=None)
)
monkeypatch.setattr(master, "url_fetch", url_fetch_404)
jsonpath = tmp_path / "web" / "json"
jsonpath.mkdir(parents=True, exist_ok=True)
jsonfile = jsonpath / "bandersnatch"
with jsonfile.open("w") as f:
f.write(
'{"releases":{"1.0":["url":"https://unittests.org/packages/a0/a0/a0a0/package-1.0.0.exe"}]}}' # noqa: E501
)
all_package_files: list[Path] = []
await verify(master, fc, "bandersnatch", tmp_path, all_package_files, fa) # type: ignore # noqa: E501
assert jsonfile.exists()
assert not all_package_files
if __name__ == "__main__":
pytest.main(sys.argv)