Skip to content

Commit

Permalink
Fix upload for existing on-demand content
Browse files Browse the repository at this point in the history
fixes: #5501
  • Loading branch information
gerrod3 authored and mdellweg committed Jul 22, 2024
1 parent d2faba8 commit 8f5202b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/5501.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed content upload for existing on-demand content not properly storing the new artifact.
17 changes: 17 additions & 0 deletions pulpcore/app/serializers/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def create(self, validated_data):
artifacts = self.get_artifacts(validated_data)

content = self.retrieve(validated_data)
created = False
if content is not None:
content.touch()
else:
Expand All @@ -74,6 +75,22 @@ def create(self, validated_data):
content = self.retrieve(validated_data)
if content is None:
raise
else:
created = True
# Ensure the content now has the uploaded artifact(s)
if not created:
ca_set = content.contentartifact_set
if ca_set.count() == 0 or ca_set.filter(artifact=None).exists():
cas = [
models.ContentArtifact(artifact=a, content=content, relative_path=rp)
for rp, a in artifacts.items()
]
models.ContentArtifact.objects.bulk_create(
cas,
update_conflicts=True,
update_fields=["artifact"],
unique_fields=["content", "relative_path"],
)

if repository:
repository.cast()
Expand Down
29 changes: 29 additions & 0 deletions pulpcore/tests/functional/api/using_plugin/test_content_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,32 @@ def test_file_remote_on_demand(
distribution = file_distribution_factory(repository=repo.pulp_href)
# attempt to download_file() a file
download_file(f"{distribution.base_url}/1.iso")


@pytest.mark.parallel
def test_upload_file_on_demand_already(
basic_manifest_path,
file_fixtures_root,
file_remote_factory,
file_repo,
file_bindings,
monitor_task,
):
"""Test that on-demand content can be uploaded to Pulp and become immediate content."""
remote = file_remote_factory(basic_manifest_path, policy="on_demand")
body = {"remote": remote.pulp_href}
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
result = file_bindings.ContentFilesApi.list(repository_version=file_repo.latest_version_href)
assert result.count == 3
for content in result.results:
assert content.artifact is None

file_content = file_fixtures_root / "basic" / content.relative_path
body = {"relative_path": content.relative_path, "file": file_content}
task = monitor_task(file_bindings.ContentFilesApi.create(**body).task)
assert len(task.created_resources) == 1
assert task.created_resources[0] == content.pulp_href

content = file_bindings.ContentFilesApi.read(content.pulp_href)
assert content.artifact is not None

0 comments on commit 8f5202b

Please sign in to comment.