Skip to content

Commit

Permalink
need to do more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hyagi committed Oct 31, 2024
1 parent 5fc9c7d commit 7778711
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pulp_container/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ def deferred_files_validation(self, data):
raise serializers.ValidationError(
_("Could not find the Containerfile in the build_context provided")
)

if not data.get("containerfile_name",None):
data["containerfile_name"]="Containerfile"

return data

Expand Down
5 changes: 4 additions & 1 deletion pulp_container/app/tasks/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def build_image(
image and tag.
"""
if not containerfile_tempfile_pk and not containerfile_name:
raise RuntimeError("Neither a name nor temporary file for the Containerfile was specified.")

if containerfile_tempfile_pk:
containerfile_artifact = PulpTemporaryFile.objects.get(pk=containerfile_tempfile_pk)

Expand All @@ -147,7 +150,7 @@ def build_image(
context_path, content_artifact.relative_path, content_artifact.artifact.file
)

containerfile_name = containerfile_name or "Containerfile"
#containerfile_name = containerfile_name or "Containerfile"
_copy_file_from_artifact(working_directory, containerfile_name, containerfile_artifact)
containerfile_path = os.path.join(working_directory, containerfile_name)

Expand Down
2 changes: 1 addition & 1 deletion pulp_container/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def build_image(self, request, pk):
temp_file.save()
containerfile_tempfile_pk = str(temp_file.pk)

containerfile_name = serialized_data.get("containerfile_name", "Containerfile")
containerfile_name = serialized_data.get("containerfile_name", None)
tag = serialized_data["tag"]

result = dispatch(
Expand Down
52 changes: 52 additions & 0 deletions pulp_container/tests/functional/api/test_build_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,55 @@ def containerfile_without_context_files():
local_registry.pull(distribution.base_path)
image = local_registry.inspect(distribution.base_path)
assert image[0]["Config"]["Cmd"] == ["ls", "/"]


def test_with_containerfilename_and_containerfile(
build_image,
containerfile_name,
container_repo,
delete_orphans_pre,
populated_file_repo,
):
"""Test build an OCI image with a containerfile and containerfile_name"""
with pytest.raises(ApiException) as e:
build_image(
repository=container_repo.pulp_href,
containerfile_name="Non_existing_file",
containerfile=containerfile_name,
build_context=f"{populated_file_repo.pulp_href}versions/2/",
)
assert e.value.status == 400
assert "Only one of 'containerfile' or 'containerfile_name' must be specified." in e.value.body



def test_build_image_with_uploaded_containerfile_without_passing_it(
build_image,
check_manifest_fields,
container_distribution_api,
container_repo,
populated_file_repo,
delete_orphans_pre,
gen_object_with_cleanup,
local_registry,
):
"""
Test build an OCI image from a file repository_version without needing to provide the
Containerfile (it should look for a file called Containerfile in the file repository).
"""
build_image(
repository=container_repo.pulp_href,
build_context=f"{populated_file_repo.pulp_href}versions/2/",
)

distribution = gen_object_with_cleanup(
container_distribution_api,
ContainerContainerDistribution(**gen_distribution(repository=container_repo.pulp_href)),
)

local_registry.pull(distribution.base_path)
image = local_registry.inspect(distribution.base_path)
assert image[0]["Config"]["Cmd"] == ["cat", "/tmp/inside-image.txt"]
assert check_manifest_fields(
manifest_filters={"digest": image[0]["Digest"]}, fields={"type": MANIFEST_TYPE.IMAGE}
)

0 comments on commit 7778711

Please sign in to comment.