From 6e7f0f29885acbd43441a8cbf8f8b7ec5ccb292b Mon Sep 17 00:00:00 2001 From: Juan Bustamante Date: Mon, 6 May 2024 15:56:56 -0500 Subject: [PATCH] Fixing an error when format is not specified when creating an image index Signed-off-by: Juan Bustamante --- pkg/client/manifest_create.go | 3 ++ pkg/client/manifest_create_test.go | 46 +++++++++++++++++++++--------- pkg/index/index_factory_test.go | 4 +-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/pkg/client/manifest_create.go b/pkg/client/manifest_create.go index bfa9ee6ef..81ffe3c03 100644 --- a/pkg/client/manifest_create.go +++ b/pkg/client/manifest_create.go @@ -73,6 +73,9 @@ func parseOptsToIndexOptions(opts CreateManifestOptions) (idxOpts []imgutil.Inde imgutil.WithInsecure(), } } + if opts.Format == "" { + opts.Format = types.OCIImageIndex + } return []imgutil.IndexOption{ imgutil.WithMediaType(opts.Format), } diff --git a/pkg/client/manifest_create_test.go b/pkg/client/manifest_create_test.go index d61530a0d..d1f65bff4 100644 --- a/pkg/client/manifest_create_test.go +++ b/pkg/client/manifest_create_test.go @@ -92,19 +92,38 @@ func testCreateManifest(t *testing.T, when spec.G, it spec.S) { indexLocalPath = filepath.Join(tmpDir, imgutil.MakeFileSafeName(indexRepoName)) }) - it("creates the index adding the manifest", func() { - err = subject.CreateManifest( - context.TODO(), - CreateManifestOptions{ - IndexRepoName: indexRepoName, - RepoNames: []string{"busybox:1.36-musl"}, - Format: types.OCIImageIndex, - }, - ) - h.AssertNil(t, err) - index := h.ReadIndexManifest(t, indexLocalPath) - h.AssertEq(t, len(index.Manifests), 1) - h.AssertEq(t, index.MediaType, types.OCIImageIndex) + when("no media type is provided", func() { + it("creates the index adding the manifest", func() { + err = subject.CreateManifest( + context.TODO(), + CreateManifestOptions{ + IndexRepoName: indexRepoName, + RepoNames: []string{"busybox:1.36-musl"}, + }, + ) + h.AssertNil(t, err) + index := h.ReadIndexManifest(t, indexLocalPath) + h.AssertEq(t, len(index.Manifests), 1) + // By default uses OCI media-types + h.AssertEq(t, index.MediaType, types.OCIImageIndex) + }) + }) + + when("media type is provided", func() { + it("creates the index adding the manifest", func() { + err = subject.CreateManifest( + context.TODO(), + CreateManifestOptions{ + IndexRepoName: indexRepoName, + RepoNames: []string{"busybox:1.36-musl"}, + Format: types.DockerManifestList, + }, + ) + h.AssertNil(t, err) + index := h.ReadIndexManifest(t, indexLocalPath) + h.AssertEq(t, len(index.Manifests), 1) + h.AssertEq(t, index.MediaType, types.DockerManifestList) + }) }) }) }) @@ -131,7 +150,6 @@ func testCreateManifest(t *testing.T, when spec.G, it spec.S) { CreateManifestOptions{ IndexRepoName: indexRepoName, RepoNames: []string{"busybox:1.36-musl"}, - Format: types.OCIImageIndex, Publish: true, }, ) diff --git a/pkg/index/index_factory_test.go b/pkg/index/index_factory_test.go index d4bfe7191..da3c791e4 100644 --- a/pkg/index/index_factory_test.go +++ b/pkg/index/index_factory_test.go @@ -109,7 +109,7 @@ func testIndexFactory(t *testing.T, when spec.G, it spec.S) { indexRepoName = h.NewRandomIndexRepoName() }) - it("errors a message", func() { + it("errors with a message", func() { _, err = indexFactory.LoadIndex(indexRepoName) h.AssertError(t, err, fmt.Sprintf("Image: '%s' not found", indexRepoName)) }) @@ -137,7 +137,7 @@ func testIndexFactory(t *testing.T, when spec.G, it spec.S) { indexRepoName = h.NewRandomIndexRepoName() }) - it("errors a message", func() { + it("errors with a message", func() { _, err = indexFactory.FetchIndex(indexRepoName, imgutil.FromBaseIndex(indexRepoName)) h.AssertNotNil(t, err) })