Skip to content

Commit

Permalink
index: fix github action tests failing
Browse files Browse the repository at this point in the history
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 <ahamedhusni73@gmail.com>
  • Loading branch information
husni-faiz committed Sep 13, 2023
1 parent d4eb44a commit 2fcc6b5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 30 deletions.
14 changes: 9 additions & 5 deletions local/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion local/index_options.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 5 additions & 6 deletions local/new_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -85,7 +85,6 @@ func NewIndex(repoName string, path string, ops ...ImageIndexOption) (*ImageInde
}

return ridx, nil

}

func emptyIndex(mediaType types.MediaType) (v1.ImageIndex, error) {
Expand Down
6 changes: 4 additions & 2 deletions remote/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (i *ImageIndex) SaveAs(name string, additionalNames ...string) error {
}

return nil

}

func (i *ImageIndex) doSave(indexName string) error {
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion remote/index_options.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 3 additions & 8 deletions remote/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand All @@ -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)

})
})
})
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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))
Expand All @@ -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))
Expand Down
17 changes: 10 additions & 7 deletions remote/new_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -85,7 +89,6 @@ func NewIndex(repoName string, keychain authn.Keychain, ops ...ImageIndexOption)
}

return ridx, nil

}

func emptyIndex(mediaType types.MediaType) (v1.ImageIndex, error) {
Expand Down

0 comments on commit 2fcc6b5

Please sign in to comment.