Skip to content

Commit 13f3897

Browse files
authored
Merge pull request #1498 from jobselko/patchback/backports/3.11/def286e8b1513a7d9971a3e6058d9a0541986cab/pr-1459
[3.11] Keep the upload filename when the storage backend renames the file
2 parents c42059f + d040455 commit 13f3897

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage.

‎pulp_python/app/pypi/serializers.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ def validate(self, data):
6363
if action != "file_upload":
6464
raise serializers.ValidationError(_("We do not support the :action {}").format(action))
6565
file = data.get("content")
66+
# The Google Cloud Storage backend renames the file when the artifact is saved.
67+
filename = file.name
6668
for ext, packagetype in DIST_EXTENSIONS.items():
67-
if file.name.endswith(ext):
69+
if filename.endswith(ext):
6870
break
6971
else:
7072
raise serializers.ValidationError(
7173
_(
7274
"Extension on {} is not a valid python extension "
7375
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
74-
).format(file.name)
76+
).format(filename)
7577
)
7678
sha256 = data.get("sha256_digest")
7779
digests = {"sha256": sha256} if sha256 else None
@@ -81,8 +83,8 @@ def validate(self, data):
8183
except IntegrityError:
8284
artifact = Artifact.objects.get(sha256=artifact.sha256)
8385
artifact.touch()
84-
log.info(f"Artifact for {file.name} already existed in database")
85-
data["content"] = (artifact, file.name)
86+
log.info(f"Artifact for {filename} already existed in database")
87+
data["content"] = (artifact, filename)
8688
return data
8789

8890

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import hashlib
2+
from unittest import mock
3+
4+
from django.core.files.uploadedfile import SimpleUploadedFile
5+
6+
from pulpcore.plugin.models import Artifact
7+
8+
from pulp_python.app.pypi.serializers import PackageUploadSerializer
9+
10+
11+
def test_upload_keeps_package_filename_when_saving_the_artifact_renames_the_file():
12+
filename = "shelf_reader-0.1-py2-none-any.whl"
13+
payload = b"not a real wheel"
14+
upload = SimpleUploadedFile(filename, payload)
15+
artifact = mock.Mock()
16+
# The Google Cloud Storage backend renames the file when the artifact is saved.
17+
artifact.save.side_effect = lambda: setattr(upload, "name", "artifact/ab/cdef")
18+
19+
serializer = PackageUploadSerializer(
20+
data={"content": upload, "sha256_digest": hashlib.sha256(payload).hexdigest()}
21+
)
22+
with mock.patch.object(Artifact, "init_and_validate", return_value=artifact):
23+
assert serializer.is_valid(), serializer.errors
24+
25+
artifact.save.assert_called_once()
26+
assert serializer.validated_data["content"] == (artifact, filename)

‎unittest_requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mock
22
pytest<8
3+
pytest-django

0 commit comments

Comments
 (0)