Skip to content

Commit

Permalink
Fix small file upload
Browse files Browse the repository at this point in the history
fixes pulp#535
  • Loading branch information
mdellweg committed Jul 19, 2022
1 parent 2e900c7 commit 21a92f5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGES/535.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed uploading content for files smaller than the chunk size.
2 changes: 1 addition & 1 deletion pulpcore/cli/common/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
verify: Optional[Union[bool, str]] = (
os.environ.get("PULP_CA_BUNDLE") if validate_certs is not False else False
)
session_settings = self._session.merge_environment_settings( # type:ignore
session_settings = self._session.merge_environment_settings(
base_url, {}, None, verify, None
)
self._session.verify = session_settings["verify"]
Expand Down
11 changes: 6 additions & 5 deletions pulpcore/cli/file/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ def upload(
) -> None:
"""Create a file content unit by uploading a file"""
size = os.path.getsize(file.name)
content: Dict[str, Any] = {"relative_path": relative_path}
body: Dict[str, Any] = {"relative_path": relative_path}
uploads: Dict[str, IO[bytes]] = {}
if chunk_size > size:
content["file"] = file
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
content["upload"] = upload_href
body["upload"] = upload_href
else:
artifact_href = PulpArtifactContext(pulp_ctx).upload(file, chunk_size)
content["artifact"] = artifact_href
result = entity_ctx.create(body=content)
body["artifact"] = artifact_href
result = entity_ctx.create(body=body, uploads=uploads)
pulp_ctx.output_result(result)
21 changes: 16 additions & 5 deletions pulpcore/cli/python/content.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import IO, Optional, Union
import os
from typing import IO, Any, Dict, Optional, Union

import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpEntityContext,
pass_entity_context,
Expand All @@ -17,7 +19,7 @@
show_command,
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpArtifactContext
from pulpcore.cli.core.context import PulpArtifactContext, PulpUploadContext
from pulpcore.cli.python.context import PulpPythonContentContext

translation = get_translation(__name__)
Expand Down Expand Up @@ -87,7 +89,16 @@ def upload(
chunk_size: int,
) -> None:
"""Create a Python package content unit through uploading a file"""
artifact_href = PulpArtifactContext(pulp_ctx).upload(file, chunk_size)
content = {"relative_path": relative_path, "artifact": artifact_href}
result = entity_ctx.create(body=content)
size = os.path.getsize(file.name)
body: Dict[str, Any] = {"relative_path": relative_path}
uploads: Dict[str, IO[bytes]] = {}
if chunk_size > size:
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
artifact_href = PulpArtifactContext(pulp_ctx).upload(file, chunk_size)
body["artifact"] = artifact_href
result = entity_ctx.create(body=body, uploads=uploads)
pulp_ctx.output_result(result)
29 changes: 19 additions & 10 deletions pulpcore/cli/rpm/content.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any, Optional, Union
import os
from typing import IO, Any, Dict, Optional, Union

import click

from pulpcore.cli.common.context import (
PluginRequirement,
PulpContext,
PulpEntityContext,
pass_entity_context,
Expand All @@ -19,7 +21,7 @@
type_option,
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpArtifactContext
from pulpcore.cli.core.context import PulpArtifactContext, PulpUploadContext
from pulpcore.cli.rpm.context import (
PulpRpmAdvisoryContext,
PulpRpmDistributionTreeContext,
Expand Down Expand Up @@ -241,20 +243,27 @@ def upload(
PulpRpmPackageContext,
PulpRpmAdvisoryContext,
],
file: IO[bytes],
chunk_size: int,
**kwargs: Any,
) -> None:
"""Create a content unit by uploading a file"""
file = kwargs.pop("file")
body = {}
size = os.path.getsize(file.name)
body: Dict[str, Any] = {}
uploads: Dict[str, IO[bytes]] = {}
if isinstance(entity_ctx, PulpRpmPackageContext):
chunk_size = kwargs.pop("chunk_size")
artifact_ctx = PulpArtifactContext(pulp_ctx)
artifact_href = artifact_ctx.upload(file, chunk_size)
body["artifact"] = artifact_href
if chunk_size > size:
uploads["file"] = file
elif pulp_ctx.has_plugin(PluginRequirement("core", min="3.20")):
upload_href = PulpUploadContext(pulp_ctx).upload_file(file, chunk_size)
body["upload"] = upload_href
else:
artifact_href = PulpArtifactContext(pulp_ctx).upload(file, chunk_size)
body["artifact"] = artifact_href
body.update(kwargs)
result = entity_ctx.create(body=body)
elif isinstance(entity_ctx, PulpRpmAdvisoryContext):
result = entity_ctx.create(body=body, uploads={"file": file.read()})
uploads["file"] = file
else:
raise NotImplementedError()
result = entity_ctx.create(body=body, uploads=uploads)
pulp_ctx.output_result(result)
8 changes: 8 additions & 0 deletions tests/scripts/pulp_file/test_content.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ test "$(echo "$OUTPUT" | jq -r length)" -eq "1"
content_href="$(echo "$OUTPUT" | jq -r .[0].pulp_href)"
expect_succ pulp file content show --href "$content_href"

# Test small file upload
dd if=/dev/urandom of=test.txt bs=64 count=1
sha256=$(sha256sum test.txt | cut -d' ' -f1)

expect_succ pulp file content upload --file test.txt --relative-path upload_test/test.txt
expect_succ pulp file content list --sha256 "$sha256"
test "$(echo "$OUTPUT" | jq -r length)" -eq "1"

# Test creation from artifact
dd if=/dev/urandom of=test.txt bs=2MiB count=1
sha256=$(sha256sum test.txt | cut -d' ' -f1)
Expand Down

0 comments on commit 21a92f5

Please sign in to comment.