-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_blocklist.py
More file actions
202 lines (171 loc) · 7.66 KB
/
Copy pathtest_blocklist.py
File metadata and controls
202 lines (171 loc) · 7.66 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
import pytest
from pulpcore.tests.functional.utils import PulpTaskError
from pulp_python.tests.functional.constants import (
PYTHON_EGG_FILENAME,
PYTHON_EGG_URL,
PYTHON_WHEEL_FILENAME,
PYTHON_WHEEL_URL,
)
CONTENT_BODY = {"relative_path": PYTHON_EGG_FILENAME, "file_url": PYTHON_EGG_URL}
BLOCKED_MSG = "Blocklisted packages cannot be added to this repository"
@pytest.mark.parallel
def test_crd_entry(python_bindings, python_repo):
"""
CRD operations on blocklist entries return correct fields and update the entry count.
"""
entries_data = [
({"name": "shelf-reader"}, "shelf-reader", None, None),
({"name": "shelf-reader", "version": "0.1"}, "shelf-reader", "0.1", None),
({"filename": PYTHON_EGG_FILENAME}, None, None, PYTHON_EGG_FILENAME),
]
for body_kwargs, name, version, filename in entries_data:
entry = python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href, python_bindings.PythonPythonBlocklistEntry(**body_kwargs)
)
assert entry.name == name
assert entry.version == version
assert entry.filename == filename
assert entry.added_by == "prn:auth.user:1"
assert entry.pulp_href is not None
assert entry.prn is not None
result = python_bindings.RepositoriesPythonBlocklistEntriesApi.list(python_repo.pulp_href)
assert result.count == 3
entry = result.results[0]
python_bindings.RepositoriesPythonBlocklistEntriesApi.read(entry.pulp_href)
python_bindings.RepositoriesPythonBlocklistEntriesApi.delete(entry.pulp_href)
result = python_bindings.RepositoriesPythonBlocklistEntriesApi.list(python_repo.pulp_href)
assert result.count == 2
@pytest.mark.parallel
@pytest.mark.parametrize(
"body_kwargs, expected_msg",
[
({"name": "shelf-reader"}, "this name and version already exists"),
({"name": "shelf-reader", "version": "0.1"}, "this name and version already exists"),
({"filename": PYTHON_EGG_FILENAME}, "this filename already exists"),
],
)
def test_duplicate_entry_rejected(python_bindings, python_repo, body_kwargs, expected_msg):
"""
Creating a duplicate entry should fail.
"""
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href, python_bindings.PythonPythonBlocklistEntry(**body_kwargs)
)
with pytest.raises(python_bindings.ApiException) as ctx:
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href, python_bindings.PythonPythonBlocklistEntry(**body_kwargs)
)
assert ctx.value.status == 400
assert expected_msg in ctx.value.body
@pytest.mark.parallel
@pytest.mark.parametrize(
"body_kwargs, expected_msg",
[
({"version": "0.1", "filename": PYTHON_EGG_FILENAME}, "version' cannot be used with"),
({"version": "0.1"}, "version' requires 'name'"),
({"name": "shelf-reader", "filename": PYTHON_EGG_FILENAME}, "Exactly one of"),
({}, "Exactly one of"),
({"name": "shelf-reader", "version": "not-a-version"}, "not a valid version"),
],
)
def test_invalid_entry_rejected(python_bindings, python_repo, body_kwargs, expected_msg):
"""
Creating an entry with invalid data should fail.
"""
with pytest.raises(python_bindings.ApiException) as ctx:
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href, python_bindings.PythonPythonBlocklistEntry(**body_kwargs)
)
assert ctx.value.status == 400
assert expected_msg in ctx.value.body
@pytest.mark.parallel
def test_upload_blocked(monitor_task, python_bindings, python_repo):
"""
Uploading a package matching a blocklist entry is rejected.
"""
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href,
python_bindings.PythonPythonBlocklistEntry(name="shelf-reader", version="0.1"),
)
with pytest.raises(PulpTaskError) as exc:
response = python_bindings.ContentPackagesApi.create(
repository=python_repo.pulp_href, **CONTENT_BODY
)
monitor_task(response.task)
assert BLOCKED_MSG in exc.value.task.error["description"]
repo = python_bindings.RepositoriesPythonApi.read(python_repo.pulp_href)
assert repo.latest_version_href.endswith("/0/")
@pytest.mark.parallel
def test_upload_allowed(monitor_task, python_bindings, python_repo):
"""
Uploading a package is allowed when the blocklist entry targets a different version.
"""
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href,
python_bindings.PythonPythonBlocklistEntry(name="shelf-reader", version="9.9"),
)
response = python_bindings.ContentPackagesApi.create(
repository=python_repo.pulp_href, **CONTENT_BODY
)
monitor_task(response.task)
repo = python_bindings.RepositoriesPythonApi.read(python_repo.pulp_href)
assert repo.latest_version_href.endswith("/1/")
@pytest.mark.parallel
def test_modify_blocked(monitor_task, python_bindings, python_repo):
"""
Adding a blocklisted package via repository modify is rejected.
"""
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
python_repo.pulp_href,
python_bindings.PythonPythonBlocklistEntry(name="shelf-reader", version="0.1"),
)
response = python_bindings.ContentPackagesApi.create(**CONTENT_BODY)
task = monitor_task(response.task)
content = python_bindings.ContentPackagesApi.read(task.created_resources[0])
with pytest.raises(python_bindings.ApiException) as exc:
python_bindings.RepositoriesPythonApi.modify(
python_repo.pulp_href, {"add_content_units": [content.pulp_href]}
)
assert exc.value.status == 400
assert BLOCKED_MSG in exc.value.body
repo = python_bindings.RepositoriesPythonApi.read(python_repo.pulp_href)
assert repo.latest_version_href.endswith("/0/")
@pytest.mark.parallel
def test_error_on_reject_false_skips_blocklisted(
monitor_task, python_bindings, python_repo_factory
):
"""
When error_on_reject=False, blocklisted packages in a batch modify are skipped while
non-blocklisted packages are still added.
"""
repo = python_repo_factory(error_on_reject=False)
python_bindings.RepositoriesPythonBlocklistEntriesApi.create(
repo.pulp_href,
python_bindings.PythonPythonBlocklistEntry(filename=PYTHON_EGG_FILENAME),
)
response = python_bindings.ContentPackagesApi.create(**CONTENT_BODY)
blocked = python_bindings.ContentPackagesApi.read(
monitor_task(response.task).created_resources[0]
)
response = python_bindings.ContentPackagesApi.create(
relative_path=PYTHON_WHEEL_FILENAME, file_url=PYTHON_WHEEL_URL
)
allowed = python_bindings.ContentPackagesApi.read(
monitor_task(response.task).created_resources[0]
)
body = {"add_content_units": [blocked.pulp_href, allowed.pulp_href]}
task = monitor_task(python_bindings.RepositoriesPythonApi.modify(repo.pulp_href, body).task)
reports = {report.code: report for report in task.progress_reports}
assert "python.reject.blocklist" in reports
report = reports["python.reject.blocklist"]
assert report.done == 1
assert PYTHON_EGG_FILENAME in report.suffix
assert blocked.prn.split(":")[-1] in report.suffix
repo = python_bindings.RepositoriesPythonApi.read(repo.pulp_href)
content_list = python_bindings.ContentPackagesApi.list(
repository_version=repo.latest_version_href
)
hrefs = {c.pulp_href for c in content_list.results}
assert allowed.pulp_href in hrefs
assert blocked.pulp_href not in hrefs
assert content_list.count == 1