From 2fcc6b59e8c97dd1937a8d6b4149cceb49404cb7 Mon Sep 17 00:00:00 2001 From: Husni Faiz Date: Thu, 1 Jun 2023 14:42:56 +0530 Subject: [PATCH] index: fix github action tests failing This fixes the following errors, - use strings.ReplaceAll method instead of strings.Replace - `infromation` is a misspelling of `information` - var-naming: don't use underscores in Go names - unnecessary conversion (unconvert) - ineffectual assignment to err - unnecessary trailing newline - unnecessary leading newline Signed-off-by: Husni Faiz --- local/index.go | 14 +++++++++----- local/index_options.go | 3 ++- local/new_index.go | 11 +++++------ remote/index.go | 6 ++++-- remote/index_options.go | 3 ++- remote/index_test.go | 11 +++-------- remote/new_index.go | 17 ++++++++++------- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/local/index.go b/local/index.go index c7e63b0f..02ef16ce 100644 --- a/local/index.go +++ b/local/index.go @@ -69,6 +69,10 @@ func (i *ImageIndex) Add(repoName string) error { if ref.Context().Name() != indexRef.Context().Name() { imgRefName := indexRef.Context().Name() + "@" + desc.Digest.Algorithm + ":" + desc.Digest.Hex imgRef, err := name.ParseReference(imgRefName) + if err != nil { + return err + } + err = remote.Write(imgRef, img, remote.WithAuthFromKeychain(authn.DefaultKeychain)) if err != nil { return errors.Wrapf(err, "failed to copy image '%s' to index repository", imgRef.Name()) @@ -139,8 +143,8 @@ func (i *ImageIndex) Save(additionalNames ...string) error { // Ex: cnbs/sample-package:hello-multiarch-universe // to cnbs_sample-package-hello-multiarch-universe func makeFileSafeName(ref string) string { - fileName := strings.Replace(ref, ":", "-", -1) - return strings.Replace(fileName, "/", "_", -1) + fileName := strings.ReplaceAll(ref, ":", "-") + return strings.ReplaceAll(fileName, "/", "_") } func (i *ImageIndex) Name() string { @@ -174,8 +178,8 @@ func (i *ImageIndex) AnnotateManifest(manifestName string, opts AnnotateFields) return err } - for i, desc_i := range manifest.Manifests { - if desc_i.Digest.String() == desc.Digest.String() { + for i, iDesc := range manifest.Manifests { + if iDesc.Digest.String() == desc.Digest.String() { if opts.Architecture != "" { manifest.Manifests[i].Platform.Architecture = opts.Architecture } @@ -223,7 +227,7 @@ func GetIndexManifest(repoName string, path string) (v1.IndexManifest, error) { return manifest, errors.Wrapf(err, "Reading local index %q in path %q", repoName, path) } - err = json.Unmarshal([]byte(jsonFile), &manifest) + err = json.Unmarshal(jsonFile, &manifest) if err != nil { return manifest, errors.Wrapf(err, "Decoding local index %q", repoName) } diff --git a/local/index_options.go b/local/index_options.go index cb4adb07..226d080f 100644 --- a/local/index_options.go +++ b/local/index_options.go @@ -1,8 +1,9 @@ package local import ( - "github.com/buildpacks/imgutil" v1 "github.com/google/go-containerregistry/pkg/v1" + + "github.com/buildpacks/imgutil" ) type ImageIndexOption func(*indexOptions) error diff --git a/local/new_index.go b/local/new_index.go index d575c2e3..30081234 100644 --- a/local/new_index.go +++ b/local/new_index.go @@ -33,13 +33,13 @@ func NewIndex(repoName string, path string, ops ...ImageIndexOption) (*ImageInde return nil, err } - for _, manifest_i := range indexOpts.manifest.Manifests { + for _, manifest := range indexOpts.manifest.Manifests { img, _ := emptyImage(imgutil.Platform{ - Architecture: manifest_i.Platform.Architecture, - OS: manifest_i.Platform.OS, - OSVersion: manifest_i.Platform.OSVersion, + Architecture: manifest.Platform.Architecture, + OS: manifest.Platform.OS, + OSVersion: manifest.Platform.OSVersion, }) - index = mutate.AppendManifests(index, mutate.IndexAddendum{Add: img, Descriptor: manifest_i}) + index = mutate.AppendManifests(index, mutate.IndexAddendum{Add: img, Descriptor: manifest}) } idx := &ImageIndex{ @@ -85,7 +85,6 @@ func NewIndex(repoName string, path string, ops ...ImageIndexOption) (*ImageInde } return ridx, nil - } func emptyIndex(mediaType types.MediaType) (v1.ImageIndex, error) { diff --git a/remote/index.go b/remote/index.go index 01e2c53c..5704aee8 100644 --- a/remote/index.go +++ b/remote/index.go @@ -102,7 +102,6 @@ func (i *ImageIndex) SaveAs(name string, additionalNames ...string) error { } return nil - } func (i *ImageIndex) doSave(indexName string) error { @@ -113,10 +112,13 @@ func (i *ImageIndex) doSave(indexName string) error { } iManifest, err := i.index.IndexManifest() + if err != nil { + return err + } // This for loop will check if all the referenced manifests have the plaform information. // This is OPTIONAL if the target is plaform independent. - // Current implementation does not allow to push an index without platform infromation. + // Current implementation does not allow to push an index without platform information. for _, j := range iManifest.Manifests { switch j.MediaType { case types.OCIManifestSchema1, types.DockerManifestSchema2: diff --git a/remote/index_options.go b/remote/index_options.go index d1cfc477..4c13f135 100644 --- a/remote/index_options.go +++ b/remote/index_options.go @@ -1,8 +1,9 @@ package remote import ( - "github.com/buildpacks/imgutil" v1 "github.com/google/go-containerregistry/pkg/v1" + + "github.com/buildpacks/imgutil" ) type ImageIndexOption func(*indexOptions) error diff --git a/remote/index_test.go b/remote/index_test.go index 9f47a209..70b67173 100644 --- a/remote/index_test.go +++ b/remote/index_test.go @@ -29,7 +29,6 @@ func newTestIndexName(providedPrefix ...string) string { } func TestIndex(t *testing.T) { - dockerConfigDir, err := ioutil.TempDir("", "test.docker.config.dir") h.AssertNil(t, err) defer os.RemoveAll(dockerConfigDir) @@ -77,7 +76,6 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { when("when index is found in registry", func() { it("use the index found in registry as base", func() { }) - }) when("#WithIndexMediaTypes", func() { @@ -91,7 +89,6 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { mediatype, err := idxt.MediaType() h.AssertNil(t, err) h.AssertEq(t, mediatype, types.OCIImageIndex) - }) }) }) @@ -106,7 +103,6 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { err = idx.Add(manifestName) h.AssertError(t, err, fmt.Sprintf("error fetching %s from registry", manifestName)) }) - }) when("manifest name is invalid", func() { @@ -118,7 +114,6 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { err = idx.Add(manifestName) h.AssertError(t, err, fmt.Sprintf("could not parse reference: %s", manifestName)) }) - }) when("manifest is in registry", func() { @@ -135,14 +130,13 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { OS: "linux", }), ) + h.AssertNil(t, err) h.AssertNil(t, img.Save()) err = idx.Add(manifestName) h.AssertNil(t, err) }) - }) - }) when("#Save", func() { @@ -161,6 +155,7 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { OS: "linux", }), ) + h.AssertNil(t, err) h.AssertNil(t, img.Save()) h.AssertNil(t, idx.Add(manifestName)) @@ -186,7 +181,7 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { OS: "linux", }), ) - + h.AssertNil(t, err) h.AssertNil(t, img.Save()) h.AssertNil(t, idx.Add(manifestName)) diff --git a/remote/new_index.go b/remote/new_index.go index 648df40c..28815a18 100644 --- a/remote/new_index.go +++ b/remote/new_index.go @@ -33,13 +33,17 @@ func NewIndex(repoName string, keychain authn.Keychain, ops ...ImageIndexOption) return nil, err } - for _, manifest_i := range indexOpts.manifest.Manifests { - img, _ := emptyImage(imgutil.Platform{ - Architecture: manifest_i.Platform.Architecture, - OS: manifest_i.Platform.OS, - OSVersion: manifest_i.Platform.OSVersion, + for _, manifest := range indexOpts.manifest.Manifests { + img, err := emptyImage(imgutil.Platform{ + Architecture: manifest.Platform.Architecture, + OS: manifest.Platform.OS, + OSVersion: manifest.Platform.OSVersion, }) - index = mutate.AppendManifests(index, mutate.IndexAddendum{Add: img, Descriptor: manifest_i}) + if err != nil { + return nil, err + } + + index = mutate.AppendManifests(index, mutate.IndexAddendum{Add: img, Descriptor: manifest}) } idx := &ImageIndex{ @@ -85,7 +89,6 @@ func NewIndex(repoName string, keychain authn.Keychain, ops ...ImageIndexOption) } return ridx, nil - } func emptyIndex(mediaType types.MediaType) (v1.ImageIndex, error) {