Skip to content

Commit 4bd5505

Browse files
committed
Added support for Distributor when creating repository
Previously it wasn't possible to create repository with distributor because of inconsintent naming of various fields which resulted in Bad requests. This is now fixed and distributors can be created along repository. Only mininal configuration is currently supported.
1 parent 7cf6636 commit 4bd5505

File tree

4 files changed

+151
-27
lines changed

4 files changed

+151
-27
lines changed

src/pubtools/pulplib/_impl/client/client.py

+61-17
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,13 @@ def create_repository(self, repo):
10831083
body["importer_type_id"] = importer[0]["importer_type_id"] if importer else None
10841084
body["importer_config"] = importer[0]["config"] if importer else None
10851085

1086+
# re-key distributor dict keys due to pulp API inconsistency
1087+
for item in body["distributors"]:
1088+
item["distributor_id"] = item["id"]
1089+
item["distributor_config"] = item["config"]
1090+
del item["config"]
1091+
del item["id"]
1092+
10861093
def log_existing_repo(exception):
10871094
if (
10881095
getattr(exception, "response", None) is not None
@@ -1099,25 +1106,42 @@ def check_repo(repo_on_server):
10991106
repo_on_server == repo
11001107
), "Repo exists on server with unexpected values"
11011108
except AssertionError:
1109+
fatal = True
1110+
# non-fatal cases, some fields don't have to be set before repository creation
1111+
# but they're typically set automatically after creation call which results in
1112+
# inequality between `repo_on_server`` and `repo`
1113+
if repo.relative_url is None:
1114+
fatal = False
1115+
1116+
if any(dist.repo_id is None for dist in repo.distributors):
1117+
fatal = False
1118+
1119+
# fatal cases
11021120
if importer:
1103-
for attr in ["type_id", "config"]:
1104-
expected = getattr(repo.importer, attr)
1105-
current = getattr(repo_on_server.importer, attr)
1106-
if expected != current:
1107-
LOG.error(
1108-
"Repository %s contains wrong importer %s\n"
1109-
"\t expected: %s\n"
1110-
"\t current: %s\n",
1111-
repo_id,
1112-
attr,
1113-
expected,
1114-
current,
1121+
fatal = self._check_unit(
1122+
"importer",
1123+
["type_id", "config"],
1124+
repo.importer,
1125+
repo_on_server.importer,
1126+
repo.id,
1127+
)
1128+
for expected_item in repo.distributors:
1129+
for current_item in repo_on_server.distributors:
1130+
if expected_item.type_id == current_item.type_id:
1131+
fatal = self._check_unit(
1132+
"distributor",
1133+
["id", "type_id", "last_publish"],
1134+
expected_item,
1135+
current_item,
1136+
repo.id,
11151137
)
1116-
LOG.exception(
1117-
"Repository %s exists on server and contains unexpected values",
1118-
repo_id,
1119-
)
1120-
raise
1138+
1139+
if fatal:
1140+
LOG.exception(
1141+
"Repository %s exists on server and contains unexpected values",
1142+
repo_id,
1143+
)
1144+
raise
11211145

11221146
return f_return(repo_on_server)
11231147

@@ -1131,3 +1155,23 @@ def check_repo(repo_on_server):
11311155
out = f_flat_map(out, check_repo)
11321156

11331157
return f_proxy(out)
1158+
1159+
@staticmethod
1160+
def _check_unit(unit_type, fields, expected_unit, current_unit, repo_id):
1161+
fatal = False
1162+
for attr in fields:
1163+
expected = getattr(expected_unit, attr)
1164+
current = getattr(current_unit, attr)
1165+
if expected != current:
1166+
fatal = True
1167+
LOG.error(
1168+
"Repository %s contains wrong %s %s\n"
1169+
"\t expected: %s\n"
1170+
"\t current: %s\n",
1171+
repo_id,
1172+
unit_type,
1173+
attr,
1174+
expected,
1175+
current,
1176+
)
1177+
return fatal

src/pubtools/pulplib/_impl/client/retry.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
LOG = logging.getLogger("pubtools.pulplib")
1010

11-
ATTEMPTS = int(os.environ.get("PUBTOOLS_PULPLIB_RETRY_ATTEMPTS", "10"))
11+
ATTEMPTS = int(os.environ.get("PUBTOOLS_PULPLIB_RETRY_ATTEMPTS", "1"))
1212
SLEEP = float(os.environ.get("PUBTOOLS_PULPLIB_RETRY_SLEEP", "1.0"))
1313
MAX_SLEEP = float(os.environ.get("PUBTOOLS_PULPLIB_RETRY_MAX_SLEEP", "120.0"))
1414

tests/fake/test_fake_create_repo.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
from pubtools.pulplib import FakeController, Repository
1+
from pubtools.pulplib import FakeController, Repository, Distributor
22

33

44
def test_create_repository():
55
"""Client.create_repository() with fake client adds new repositories to controller."""
66
controller = FakeController()
77

88
client = controller.client
9-
repo_1 = client.create_repository(Repository(id="repo1"))
10-
repo_2 = client.create_repository(Repository(id="repo2"))
9+
repo_1 = client.create_repository(
10+
Repository(
11+
id="repo1",
12+
distributors=[Distributor(id="dist1", type_id="yum_distributor")],
13+
)
14+
)
15+
repo_2 = client.create_repository(
16+
Repository(
17+
id="repo2",
18+
distributors=[Distributor(id="dist2", type_id="yum_distributor")],
19+
)
20+
)
1121

1222
# adding already existing repository has no effect
13-
_ = client.create_repository(Repository(id="repo1"))
23+
_ = client.create_repository(
24+
Repository(
25+
id="repo1",
26+
distributors=[Distributor(id="dist1", type_id="yum_distributor")],
27+
)
28+
)
1429
# The change should be reflected in the controller,
1530
# with two repositories present
1631
assert controller.repositories == [repo_1.result(), repo_2.result()]

tests/repository/test_yum_repository.py

+70-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
import logging
33
import requests
44

5-
from pubtools.pulplib import Repository, YumRepository, DetachedException, YumImporter
5+
import pubtools.pulplib._impl.compat_attr as attr
6+
from pubtools.pulplib import (
7+
Repository,
8+
YumRepository,
9+
DetachedException,
10+
YumImporter,
11+
Distributor,
12+
)
613

714

815
def test_from_data_gives_yum_repository():
@@ -216,7 +223,16 @@ def test_related_repositories_detached_client():
216223

217224

218225
def test_create_repository(client, requests_mocker):
219-
repo = YumRepository(id="yum_repo_new")
226+
repo = YumRepository(
227+
id="yum_repo_new",
228+
distributors=[
229+
Distributor(
230+
id="fake_id",
231+
type_id="yum_distributor",
232+
relative_url="yum_repo_new/foo/bar",
233+
)
234+
],
235+
)
220236

221237
# create request
222238
requests_mocker.post(
@@ -237,13 +253,44 @@ def test_create_repository(client, requests_mocker):
237253
"config": {},
238254
}
239255
],
256+
"distributors": [
257+
{
258+
"id": "fake_id",
259+
"distributor_type_id": "yum_distributor",
260+
"config": {"relative_url": "yum_repo_new/foo/bar"},
261+
"repo_id": "yum_repo_new",
262+
}
263+
],
240264
}
241265
],
242266
)
243267

244-
out = client.create_repository(repo)
268+
out = client.create_repository(repo).result()
245269
# check return value of create_repository() call
246-
assert out.result() == repo
270+
# relative_url wasn't set before request for `repo`
271+
assert repo.relative_url is None
272+
# but it's set after creation
273+
assert out.relative_url == "yum_repo_new/foo/bar"
274+
275+
# repo_id of distributor isn't set before creation call
276+
assert repo.distributors[0].repo_id is None
277+
# but it's set after creation
278+
assert out.distributors[0].repo_id == "yum_repo_new"
279+
# evolve original repo object with new values
280+
repo = attr.evolve(
281+
repo,
282+
relative_url="yum_repo_new/foo/bar",
283+
distributors=[
284+
Distributor(
285+
id="fake_id",
286+
type_id="yum_distributor",
287+
relative_url="yum_repo_new/foo/bar",
288+
repo_id="yum_repo_new",
289+
)
290+
],
291+
)
292+
# and do full assert
293+
assert out == repo
247294

248295
hist = requests_mocker.request_history
249296
# there should be exactly 2 requests sent - create and search
@@ -256,6 +303,14 @@ def test_create_repository(client, requests_mocker):
256303
# are automatically added for yum repository
257304
assert create_query["importer_type_id"] == "yum_importer"
258305
assert create_query["importer_config"] == {}
306+
# check distributor data
307+
assert len(create_query["distributors"]) == 1
308+
assert create_query["distributors"][0]["distributor_id"] == "fake_id"
309+
assert create_query["distributors"][0]["distributor_type_id"] == "yum_distributor"
310+
assert (
311+
create_query["distributors"][0]["distributor_config"]["relative_url"]
312+
== "yum_repo_new/foo/bar"
313+
)
259314

260315
# check the search request for created repo
261316
search_query = hist[1].json()
@@ -331,7 +386,9 @@ def test_create_repository_already_exists(client, requests_mocker, caplog):
331386

332387
def test_create_repository_wrong_data(client, requests_mocker, caplog):
333388
repo = YumRepository(
334-
id="yum_repo_existing", importer=YumImporter(config={"new": "value"})
389+
id="yum_repo_existing",
390+
importer=YumImporter(config={"new": "value"}),
391+
distributors=[Distributor(id="test_dist", type_id="yum_distributor")],
335392
)
336393

337394
requests_mocker.post(
@@ -353,6 +410,13 @@ def test_create_repository_wrong_data(client, requests_mocker, caplog):
353410
"config": {"current": "value"},
354411
}
355412
],
413+
"distributors": [
414+
{
415+
"id": "fake_id",
416+
"distributor_type_id": "yum_distributor",
417+
"config": {},
418+
}
419+
],
356420
}
357421
],
358422
)
@@ -363,6 +427,7 @@ def test_create_repository_wrong_data(client, requests_mocker, caplog):
363427
for text in (
364428
"Repository yum_repo_existing already exists",
365429
"Repository yum_repo_existing contains wrong importer config",
430+
"Repository yum_repo_existing contains wrong distributor id",
366431
"Repository yum_repo_existing exists on server and contains unexpected values",
367432
):
368433
assert text in caplog.text

0 commit comments

Comments
 (0)