-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest_pypi_apis.py
More file actions
330 lines (294 loc) · 11.4 KB
/
test_pypi_apis.py
File metadata and controls
330 lines (294 loc) · 11.4 KB
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
import pytest
import requests
import subprocess
from urllib.parse import urljoin
from pulp_python.tests.functional.constants import (
PYPI_SERIAL_CONSTANT,
PYTHON_MD_PROJECT_SPECIFIER,
PYTHON_MD_PYPI_SUMMARY,
PYTHON_EGG_FILENAME,
PYTHON_EGG_SHA256,
PYTHON_WHEEL_FILENAME,
PYTHON_WHEEL_SHA256,
SHELF_PYTHON_JSON,
)
from pulp_python.tests.functional.utils import ensure_metadata
PYPI_LAST_SERIAL = "X-PYPI-LAST-SERIAL"
@pytest.mark.parallel
def test_empty_index(python_bindings, python_empty_repo_distro):
"""Checks that summary stats are 0 when index is empty."""
_, distro = python_empty_repo_distro()
summary = python_bindings.PypiApi.read(path=distro.base_path)
assert not any(summary.to_dict().values())
@pytest.mark.parallel
def test_live_index(
python_bindings, python_repo_with_sync, python_remote_factory, python_distribution_factory
):
"""Checks summary stats are correct for indexes pointing to repositories."""
remote = python_remote_factory(includes=PYTHON_MD_PROJECT_SPECIFIER)
repo = python_repo_with_sync(remote)
distro = python_distribution_factory(repository=repo)
summary = python_bindings.PypiApi.read(path=distro.base_path)
assert summary.to_dict() == PYTHON_MD_PYPI_SUMMARY
@pytest.mark.parallel
def test_published_index(
python_bindings,
python_repo_with_sync,
python_remote_factory,
python_publication_factory,
python_distribution_factory,
):
"""Checks summary stats are correct for indexes pointing to publications."""
remote = python_remote_factory(includes=PYTHON_MD_PROJECT_SPECIFIER)
repo = python_repo_with_sync(remote)
pub = python_publication_factory(repository=repo)
distro = python_distribution_factory(publication=pub)
summary = python_bindings.PypiApi.read(path=distro.base_path)
assert summary.to_dict() == PYTHON_MD_PYPI_SUMMARY
@pytest.mark.parallel
def test_package_upload(
python_content_summary, python_empty_repo_distro, python_package_dist_directory, monitor_task
):
"""Tests that packages can be uploaded."""
repo, distro = python_empty_repo_distro()
dist_dir, egg_file, wheel_file = python_package_dist_directory
url = urljoin(distro.base_url, "legacy/")
response = requests.post(
url,
data={"sha256_digest": PYTHON_EGG_SHA256},
files={"content": open(egg_file, "rb")},
auth=("admin", "password"),
)
assert response.status_code == 202
monitor_task(response.json()["task"])
summary = python_content_summary(repository=repo)
assert summary.added["python.python"]["count"] == 1
# Test re-uploading same package gives 400 Bad Request
response = requests.post(
url,
data={"sha256_digest": PYTHON_EGG_SHA256},
files={"content": open(egg_file, "rb")},
auth=("admin", "password"),
)
assert response.status_code == 400
assert response.reason == f"Package {PYTHON_EGG_FILENAME} already exists in index"
@pytest.mark.parallel
def test_package_upload_session(
python_content_summary, python_empty_repo_distro, python_package_dist_directory, monitor_task
):
"""Tests that multiple uploads will be broken up into multiple tasks."""
repo, distro = python_empty_repo_distro()
url = urljoin(distro.base_url, "legacy/")
dist_dir, egg_file, wheel_file = python_package_dist_directory
session = requests.Session()
response = session.post(
url,
data={"sha256_digest": PYTHON_EGG_SHA256},
files={"content": open(egg_file, "rb")},
auth=("admin", "password"),
)
assert response.status_code == 202
task = monitor_task(response.json()["task"])
response2 = session.post(
url,
data={"sha256_digest": PYTHON_WHEEL_SHA256},
files={"content": open(wheel_file, "rb")},
auth=("admin", "password"),
)
assert response2.status_code == 202
task2 = monitor_task(response2.json()["task"])
assert task != task2
summary = python_content_summary(repository=repo)
assert summary.present["python.python"]["count"] == 2
@pytest.mark.parallel
def test_package_upload_simple(
python_content_summary, python_empty_repo_distro, python_package_dist_directory, monitor_task
):
"""Tests that the package upload endpoint exposed at `/simple/` works."""
repo, distro = python_empty_repo_distro()
url = urljoin(distro.base_url, "simple/")
dist_dir, egg_file, wheel_file = python_package_dist_directory
response = requests.post(
url,
data={"sha256_digest": PYTHON_EGG_SHA256},
files={"content": open(egg_file, "rb")},
auth=("admin", "password"),
)
assert response.status_code == 202
monitor_task(response.json()["task"])
summary = python_content_summary(repository=repo)
assert summary.added["python.python"]["count"] == 1
@pytest.mark.parallel
def test_package_upload_with_metadata(
monitor_task,
pulp_content_url,
python_content_summary,
python_empty_repo_distro,
python_package_dist_directory,
):
"""
Test that the upload of a Python wheel package creates a metadata artifact.
"""
repo, distro = python_empty_repo_distro()
url = urljoin(distro.base_url, "simple/")
dist_dir, egg_file, wheel_file = python_package_dist_directory
response = requests.post(
url,
data={"sha256_digest": PYTHON_WHEEL_SHA256},
files={"content": open(wheel_file, "rb")},
auth=("admin", "password"),
)
assert response.status_code == 202
monitor_task(response.json()["task"])
summary = python_content_summary(repository=repo)
assert summary.added["python.python"]["count"] == 1
# Test that metadata is accessible
ensure_metadata(
pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME, "shelf-reader", "0.1"
)
@pytest.mark.parallel
def test_twine_upload(
pulpcore_bindings,
python_content_summary,
python_empty_repo_distro,
python_package_dist_directory,
monitor_task,
):
"""Tests that packages can be properly uploaded through Twine."""
repo, distro = python_empty_repo_distro()
url = urljoin(distro.base_url, "legacy/")
dist_dir, _, _ = python_package_dist_directory
username, password = "admin", "password"
subprocess.run(
(
"twine",
"upload",
"--repository-url",
url,
dist_dir / "*",
"-u",
username,
"-p",
password,
),
capture_output=True,
check=True,
)
tasks = pulpcore_bindings.TasksApi.list(reserved_resources=repo.pulp_href).results
for task in reversed(tasks):
t = monitor_task(task.pulp_href)
repo_ver_href = t.created_resources[-1]
summary = python_content_summary(repository_version=repo_ver_href)
assert summary.present["python.python"]["count"] == 2
# Test re-uploading same packages gives error
with pytest.raises(subprocess.CalledProcessError):
subprocess.run(
(
"twine",
"upload",
"--repository-url",
url,
dist_dir / "*",
"-u",
username,
"-p",
password,
),
capture_output=True,
check=True,
)
@pytest.mark.parallel
def test_simple_redirect_with_publications(
python_remote_factory,
python_repo_with_sync,
python_publication_factory,
python_distribution_factory,
pulp_content_url,
):
"""Checks that requests to `/simple/` get redirected when serving a publication."""
remote = python_remote_factory()
repo = python_repo_with_sync(remote=remote)
pub = python_publication_factory(repository=repo)
distro = python_distribution_factory(publication=pub)
response = requests.get(urljoin(distro.base_url, "simple/"))
assert response.url == str(urljoin(pulp_content_url, f"{distro.base_path}/simple/"))
@pytest.mark.parallel
def test_pypi_json(python_remote_factory, python_repo_with_sync, python_distribution_factory):
"""Checks the data of `pypi/{package_name}/json` endpoint."""
remote = python_remote_factory(policy="immediate")
repo = python_repo_with_sync(remote)
distro = python_distribution_factory(repository=repo)
response = requests.get(urljoin(distro.base_url, "pypi/shelf-reader/json"))
assert_pypi_json(response.json())
# Test serving a repository version
distro = python_distribution_factory(repository=repo, version="1")
assert distro.repository is None
response = requests.get(urljoin(distro.base_url, "pypi/shelf-reader/json"))
assert_pypi_json(response.json())
@pytest.mark.parallel
def test_pypi_json_content_app(
python_remote_factory,
python_repo_with_sync,
python_publication_factory,
python_distribution_factory,
pulp_content_url,
):
"""Checks that the pypi json endpoint of the content app still works. Needs Publication."""
remote = python_remote_factory(policy="immediate")
repo = python_repo_with_sync(remote)
pub = python_publication_factory(repository=repo)
distro = python_distribution_factory(publication=pub)
rel_url = f"{distro.base_path}/pypi/shelf-reader/json/"
response = requests.get(urljoin(pulp_content_url, rel_url))
assert_pypi_json(response.json())
@pytest.mark.parallel
def test_pypi_last_serial(
python_remote_factory,
python_repo_with_sync,
python_publication_factory,
python_distribution_factory,
pulp_content_url,
):
"""
Checks that the endpoint has the header PYPI_LAST_SERIAL and is set
TODO when serial field is added to Repo's, check this header against that
"""
remote = python_remote_factory(policy="immediate")
repo = python_repo_with_sync(remote)
pub = python_publication_factory(repository=repo)
distro = python_distribution_factory(publication=pub)
content_url = urljoin(pulp_content_url, f"{distro.base_path}/pypi/shelf-reader/json")
pypi_url = urljoin(distro.base_url, "pypi/shelf-reader/json/")
for url in [content_url, pypi_url]:
response = requests.get(url)
assert PYPI_LAST_SERIAL in response.headers, url
assert response.headers[PYPI_LAST_SERIAL] == str(PYPI_SERIAL_CONSTANT), url
def assert_pypi_json(package):
"""Asserts that shelf-reader package json is correct."""
assert SHELF_PYTHON_JSON["last_serial"] == package["last_serial"]
assert SHELF_PYTHON_JSON["info"].items() <= package["info"].items()
assert len(SHELF_PYTHON_JSON["urls"]) == len(package["urls"])
assert_download_info(SHELF_PYTHON_JSON["urls"], package["urls"], "Failed to match URLS")
assert SHELF_PYTHON_JSON["releases"].keys() <= package["releases"].keys()
for version in SHELF_PYTHON_JSON["releases"].keys():
assert_download_info(
SHELF_PYTHON_JSON["releases"][version],
package["releases"][version],
"Failed to match version",
)
def assert_download_info(expected, received, message="Failed to match"):
"""
Each version has a list of dists of that version, but the lists might
not be in the same order, so check each dist of the second list
"""
for dist in expected:
dist = dict(dist)
matched = False
dist_items = dist.items()
for dist2 in received:
dist2 = dict(dist2)
dist2["digests"].pop("md5", "")
if dist_items <= dist2.items():
matched = True
break
assert matched is True, message