/
utils.py
317 lines (258 loc) · 10.2 KB
/
utils.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
# coding=utf-8
"""Utilities for tests for the deb plugin."""
import os
from functools import partial
from unittest import SkipTest
from time import sleep
from tempfile import NamedTemporaryFile
from pulp_smash import api, selectors, utils
from pulp_smash.pulp3.utils import (
gen_remote,
gen_repo,
get_content,
require_pulp_3,
require_pulp_plugins,
sync,
)
from pulp_deb.tests.functional.constants import (
DEB_FIXTURE_RELEASE,
DEB_FIXTURE_URL,
DEB_GENERIC_CONTENT_NAME,
DEB_GENERIC_CONTENT_PATH,
DEB_GENERIC_CONTENT_RELPATH,
# DEB_PACKAGE_INDEX_NAME,
DEB_PACKAGE_NAME,
DEB_PACKAGE_RELEASE_COMPONENT_NAME,
DEB_PUBLICATION_PATH,
DEB_RELEASE_COMPONENT_NAME,
DEB_RELEASE_FILE_NAME,
DEB_REMOTE_PATH,
DEB_REPO_PATH,
VERBATIM_PUBLICATION_PATH,
)
from pulpcore.client.pulpcore import (
ApiClient as CoreApiClient,
ArtifactsApi,
Configuration,
TasksApi,
)
from pulpcore.client.pulp_deb import (
ApiClient as DebApiClient,
ContentGenericContentsApi,
ContentPackagesApi,
DistributionsAptApi,
PublicationsAptApi,
PublicationsVerbatimApi,
RemotesAptApi,
RepositoriesAptApi,
)
skip_if = partial(selectors.skip_if, exc=SkipTest) # pylint:disable=invalid-name
"""The ``@skip_if`` decorator, customized for unittest.
:func:`pulp_smash.selectors.skip_if` is test runner agnostic. This function is
identical, except that ``exc`` has been set to ``unittest.SkipTest``.
"""
configuration = Configuration()
configuration.username = "admin"
configuration.password = "password"
configuration.safe_chars_for_path_param = "/"
core_client = CoreApiClient(configuration)
artifact_api = ArtifactsApi(core_client)
task_api = TasksApi(core_client)
deb_client = DebApiClient(configuration)
deb_generic_content_api = ContentGenericContentsApi(deb_client)
deb_package_api = ContentPackagesApi(deb_client)
deb_remote_api = RemotesAptApi(deb_client)
deb_repository_api = RepositoriesAptApi(deb_client)
deb_apt_publication_api = PublicationsAptApi(deb_client)
deb_verbatim_publication_api = PublicationsVerbatimApi(deb_client)
deb_distribution_api = DistributionsAptApi(deb_client)
def set_up_module():
"""Skip tests Pulp 3 isn't under test or if pulp_deb isn't installed."""
require_pulp_3(SkipTest)
require_pulp_plugins({"pulp_deb"}, SkipTest)
def gen_deb_client():
"""Return an OBJECT for deb client."""
return deb_client
def gen_deb_remote(
url=DEB_FIXTURE_URL, distributions=DEB_FIXTURE_RELEASE, sync_udebs=False, gpgkey=None, **kwargs
):
"""Return a semi-random dict for use in creating a deb Remote.
:param url: The URL of an external content source.
"""
if gpgkey:
kwargs["gpgkey"] = gpgkey
return gen_remote(url, distributions=distributions, sync_udebs=sync_udebs, **kwargs)
def get_deb_content_unit_paths(repo, version_href=None):
"""Return the relative path of content units present in a deb repository.
:param repo: A dict of information about the repository.
:param version_href: The repository version to read.
:returns: A dict of list with the paths of units present in a given repository
for different content types. Paths are given as pairs with the remote and the
local version.
"""
def _rel_path(package, component=""):
sourcename = package["source"] or package["package"]
if sourcename.startswith("lib"):
prefix = sourcename[0:4]
else:
prefix = sourcename[0]
return os.path.join(
"pool",
component,
prefix,
sourcename,
"{}_{}_{}.deb".format(package["package"], package["version"], package["architecture"]),
)
content = get_content(repo.to_dict(), version_href)
result = {
DEB_PACKAGE_NAME: [
(content_unit["relative_path"], _rel_path(content_unit, "all"))
for content_unit in content[DEB_PACKAGE_NAME]
]
}
for prc in content[DEB_PACKAGE_RELEASE_COMPONENT_NAME]:
package = next(
package
for package in content[DEB_PACKAGE_NAME]
if package["pulp_href"] == prc["package"]
)
release_component = next(
rc
for rc in content[DEB_RELEASE_COMPONENT_NAME]
if rc["pulp_href"] == prc["release_component"]
)
result[DEB_PACKAGE_NAME].append(
(package["relative_path"], _rel_path(package, release_component["component"]))
)
return result
def get_deb_verbatim_content_unit_paths(repo, version_href=None):
"""Return the relative path of content units present in a deb repository.
:param repo: A dict of information about the repository.
:param version_href: The repository version to read.
:returns: A dict of list with the paths of units present in a given repository
for different content types. Paths are given as pairs with the remote and the
local version.
"""
return {
DEB_RELEASE_FILE_NAME: [
(content_unit["relative_path"], content_unit["relative_path"])
for content_unit in get_content(repo.to_dict(), version_href)[DEB_RELEASE_FILE_NAME]
],
DEB_PACKAGE_NAME: [
(content_unit["relative_path"], content_unit["relative_path"])
for content_unit in get_content(repo.to_dict(), version_href)[DEB_PACKAGE_NAME]
],
DEB_GENERIC_CONTENT_NAME: [
(content_unit["relative_path"], content_unit["relative_path"])
for content_unit in get_content(repo.to_dict(), version_href)[DEB_GENERIC_CONTENT_NAME]
],
}
def gen_deb_content_attrs(artifact):
"""Generate a dict with generic content unit attributes.
:param: artifact: A dict of info about the artifact.
:returns: A semi-random dict for use in creating a content unit.
"""
return {"artifact": artifact["pulp_href"], "relative_path": DEB_GENERIC_CONTENT_RELPATH}
def gen_deb_content_upload_attrs():
"""Generate a dict with generic content unit attributes for upload.
:returns: A semi-random dict for use in creating a content unit.
"""
return {"relative_path": DEB_GENERIC_CONTENT_RELPATH}
def gen_deb_package_attrs(artifact):
"""Generate a dict with package unit attributes.
:param: artifact: A dict of info about the artifact.
:returns: A semi-random dict for use in creating a content unit.
"""
return {"artifact": artifact["pulp_href"]}
def gen_deb_package_upload_attrs():
"""Generate a dict with package unit attributes for upload.
:returns: A semi-random dict for use in creating a content unit.
"""
return {}
def populate_pulp(cfg, url=DEB_FIXTURE_URL):
"""Add deb contents to Pulp.
:param pulp_smash.config.PulpSmashConfig: Information about a Pulp application.
:param url: The deb repository URL. Defaults to
:data:`pulp_smash.constants.DEB_FIXTURE_URL`
:returns: A list of dicts, where each dict describes one file content in Pulp.
"""
client = api.Client(cfg, api.json_handler)
remote = {}
repo = {}
try:
remote.update(client.post(DEB_REMOTE_PATH, gen_deb_remote(url)))
repo.update(client.post(DEB_REPO_PATH, gen_repo()))
sync(cfg, remote, repo)
finally:
if remote:
client.delete(remote["pulp_href"])
if repo:
client.delete(repo["pulp_href"])
return client.get(DEB_GENERIC_CONTENT_PATH)["results"]
def create_deb_publication(cfg, repo, version_href=None):
"""Create a deb publication.
:param pulp_smash.config.PulpSmashConfig cfg: Information about the Pulp
host.
:param repo: A dict of information about the repository.
:param version_href: A href for the repo version to be published.
:returns: A publication. A dict of information about the just created
publication.
"""
if version_href:
body = {"repository_version": version_href}
else:
body = {"repository": repo["pulp_href"]}
body["simple"] = True
body["structured"] = True
client = api.Client(cfg, api.json_handler)
call_report = client.post(DEB_PUBLICATION_PATH, body)
tasks = tuple(api.poll_spawned_tasks(cfg, call_report))
return client.get(tasks[-1]["created_resources"][0])
def create_verbatim_publication(cfg, repo, version_href=None):
"""Create a verbatim publication.
:param pulp_smash.config.PulpSmashConfig cfg: Information about the Pulp
host.
:param repo: A dict of information about the repository.
:param version_href: A href for the repo version to be published.
:returns: A publication. A dict of information about the just created
publication.
"""
if version_href:
body = {"repository_version": version_href}
else:
body = {"repository": repo["pulp_href"]}
client = api.Client(cfg, api.json_handler)
call_report = client.post(VERBATIM_PUBLICATION_PATH, body)
tasks = tuple(api.poll_spawned_tasks(cfg, call_report))
return client.get(tasks[-1]["created_resources"][0])
def gen_artifact(url):
"""Creates an artifact."""
response = utils.http_get(url)
with NamedTemporaryFile() as temp_file:
temp_file.write(response)
temp_file.flush()
artifact = ArtifactsApi(core_client).create(file=temp_file.name)
return artifact.to_dict()
class PulpTaskError(Exception):
"""Exception to describe task errors."""
def __init__(self, task):
"""Provide task info to exception."""
description = task.error["description"]
super().__init__(self, f"Pulp task failed ({description})")
self.task = task
def monitor_task(task_href):
"""Polls the Task API until the task is in a completed state.
Prints the task details and a success or failure message. Exits on failure.
Args:
task_href(str): The href of the task to monitor
Returns:
list[str]: List of hrefs that identify resource created by the task
"""
completed = ["completed", "failed", "canceled"]
task = task_api.read(task_href)
while task.state not in completed:
sleep(2)
task = task_api.read(task_href)
if task.state == "completed":
return task.created_resources
raise PulpTaskError(task=task)