Skip to content

Commit 3c7c7ce

Browse files
mdellwegdralley
authored andcommitted
Make export version check more resilient
[noissue]
1 parent 7d0dbf7 commit 3c7c7ce

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

pulpcore/app/importexport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def export_versions(export, version_info):
9191
9292
Args:
9393
export (django.db.models.PulpExport): export instance that's doing the export
94-
version_info (set): set of (distribution-label,version) tuples for repos in this export
94+
version_info (set): iterable of (distribution-label,version) tuples for repos in this export
9595
"""
9696
# build the version-list from the distributions for each component
9797
versions = [{"component": label, "version": version} for (label, version) in version_info]

pulpcore/app/tasks/export.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from gettext import gettext as _
1010
from glob import glob
1111
from pathlib import Path
12-
from pkg_resources import get_distribution
1312

1413
from django.conf import settings
1514

15+
from pulpcore.app.apps import get_plugin_config
1616
from pulpcore.app.models import (
1717
CreatedResource,
1818
ExportedResource,
@@ -21,13 +21,14 @@
2121
Publication,
2222
PulpExport,
2323
PulpExporter,
24+
Repository,
2425
RepositoryVersion,
2526
Task,
2627
)
2728
from pulpcore.app.models.content import Artifact, ContentArtifact
2829
from pulpcore.app.serializers import PulpExportSerializer
2930

30-
from pulpcore.app.util import compute_file_hash, get_version_from_model, Crc32Hasher
31+
from pulpcore.app.util import compute_file_hash, Crc32Hasher
3132
from pulpcore.app.importexport import (
3233
export_versions,
3334
export_artifacts,
@@ -318,17 +319,12 @@ def _get_versions_info(the_exporter):
318319
"""
319320
Return plugin-version-info based on plugins are responsible for exporter-repositories.
320321
"""
321-
repositories = the_exporter.repositories.all()
322-
323322
# extract plugin-version-info based on the repositories we're exporting from
324-
vers_info = set()
325-
# We always need to know what version of pulpcore was in place
326-
vers_info.add(("pulpcore", get_distribution("pulpcore").version))
327-
# for each repository being exported, get the version-info for the plugin that
328-
# owns/controls that kind-of repository
329-
for r in repositories:
330-
vers_info.add(get_version_from_model(r.cast()))
331-
323+
repositories = the_exporter.repositories.all()
324+
repo_types = {r.pulp_type for r in repositories}
325+
app_labels = {Repository.get_model_for_pulp_type(rt)._meta.app_label for rt in repo_types}
326+
app_labels.add("core")
327+
vers_info = [(app_label, get_plugin_config(app_label).version) for app_label in app_labels]
332328
return vers_info
333329

334330

pulpcore/app/tasks/importer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from django.core.files.storage import default_storage
1313
from django.db.models import F
1414
from io import StringIO
15-
from pkg_resources import DistributionNotFound, get_distribution
1615
from rest_framework.serializers import ValidationError
1716
from tablib import Dataset
1817

18+
from pulpcore.exceptions.plugin import MissingPlugin
1919
from pulpcore.app.apps import get_plugin_config
2020
from pulpcore.app.models import (
2121
Artifact,
@@ -282,8 +282,8 @@ def _check_versions(version_json):
282282
error_messages = []
283283
for component in version_json:
284284
try:
285-
version = get_distribution(component["component"]).version
286-
except DistributionNotFound:
285+
version = get_plugin_config(component["component"]).version
286+
except MissingPlugin:
287287
error_messages.append(
288288
_("Export uses {} which is not installed.").format(component["component"])
289289
)
@@ -363,8 +363,8 @@ def import_repository_version(
363363
mapping = json.load(mapping_file)
364364

365365
# Content
366-
plugin_name = src_repo_type.split(".")[0]
367-
cfg = get_plugin_config(plugin_name)
366+
app_label = src_repo_type.split(".")[0]
367+
cfg = get_plugin_config(app_label)
368368

369369
resulting_content_ids = []
370370
for res_class in cfg.exportable_classes:

pulpcore/app/util.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import gnupg
1414

1515
from django.conf import settings
16-
from django.apps import apps
1716
from django.urls import Resolver404, resolve, reverse
18-
from django.contrib.contenttypes.models import ContentType
19-
from pkg_resources import get_distribution
2017
from rest_framework.serializers import ValidationError
2118

2219
from pulpcore.app.loggers import deprecation_logger
@@ -199,23 +196,6 @@ def batch_qs(qs, batch_size=1000):
199196
yield qs[start:end]
200197

201198

202-
def get_version_from_model(in_model):
203-
"""
204-
Return a tuple (dist-label, version) for the distribution that 'owns' the model
205-
206-
Args:
207-
in_model (models.Model): model whose owning-plugin-version we need
208-
209-
Returns:
210-
(str, str): tuple containing owning-plugin's (distribution, version)
211-
"""
212-
app_label = ContentType.objects.get_for_model(in_model, for_concrete_model=False).app_label
213-
app_config_module = apps.get_app_config(app_label).name
214-
maybe_the_distribution_name = app_config_module.split(".")[0]
215-
version = get_distribution(maybe_the_distribution_name).version # hope for the best!
216-
return maybe_the_distribution_name, version
217-
218-
219199
def get_view_urlpattern(view):
220200
"""
221201
Get a full urlpattern for a view which includes a parent urlpattern if it exists.

pulpcore/tests/unit/test_import_checks.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,52 @@
22

33
from rest_framework.serializers import ValidationError
44

5-
import pulpcore.app.tasks.importer
5+
import pulpcore.app.apps
66
from pulpcore.app.tasks.importer import _check_versions
77

88

9-
class TestObject:
10-
version = "1.2.3" # Every component is vers 1.2.3
9+
def _pulp_plugin_configs():
10+
for _label, _version in [("123", "1.2.3"), ("215", "2.1.5")]:
11+
12+
class _AppConfigMock:
13+
label = _label
14+
version = _version # Every component is vers 1.2.3
15+
16+
yield _AppConfigMock
1117

1218

1319
def test_vers_check(monkeypatch):
14-
monkeypatch.setattr(pulpcore.app.tasks.importer, "get_distribution", lambda _: TestObject())
15-
export_json = [{"component": "xyz", "version": "1.2.3"}]
20+
monkeypatch.setattr(pulpcore.app.apps, "pulp_plugin_configs", _pulp_plugin_configs)
21+
export_json = [{"component": "123", "version": "1.2.3"}]
22+
_check_versions(export_json)
23+
24+
export_json = [{"component": "123", "version": "1.2"}]
25+
_check_versions(export_json)
26+
27+
export_json = [{"component": "123", "version": "1.2.7"}]
1628
_check_versions(export_json)
1729

18-
export_json = [{"component": "xy", "version": "1.2"}]
30+
export_json = [
31+
{"component": "123", "version": "1.2.0"},
32+
{"component": "215", "version": "2.1.9"},
33+
]
1934
_check_versions(export_json)
2035

21-
export_json = [{"component": "x_noty_z", "version": "1.4.3"}]
36+
export_json = [{"component": "123", "version": "1.4.3"}]
37+
with pytest.raises(ValidationError):
38+
_check_versions(export_json)
39+
40+
export_json = [{"component": "123", "version": "2.2.3"}]
41+
with pytest.raises(ValidationError):
42+
_check_versions(export_json)
43+
44+
export_json = [{"component": "non_existent", "version": "1.2.3"}]
2245
with pytest.raises(ValidationError):
2346
_check_versions(export_json)
2447

25-
export_json = [{"component": "notx_y_z", "version": "2.2.3"}]
48+
export_json = [
49+
{"component": "123", "version": "1.2.3"},
50+
{"component": "non_existent", "version": "1.2.3"},
51+
]
2652
with pytest.raises(ValidationError):
2753
_check_versions(export_json)

0 commit comments

Comments
 (0)