Skip to content

Commit

Permalink
adding #Inspect and #Remove test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Apr 13, 2024
1 parent fd63c31 commit 530a05d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
24 changes: 13 additions & 11 deletions cnb_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,15 +1171,15 @@ func (h *CNBIndex) Add(name string, ops ...func(*IndexAddOptions) error) error {
return path.AppendDescriptor(desc)

Check warning on line 1171 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L1171

Added line #L1171 was not covered by tests
}

// Fetch Descriptor of the given reference.
//
// This call is returns a v1.Descriptor with `Size`, `MediaType`, `Digest` fields only!!
// This is a lightweight call used for checking MediaType of given Reference
ref, auth, err := referenceForRepoName(h.KeyChain, name, h.Insecure)
if err != nil {
return err

Check warning on line 1176 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L1174-L1176

Added lines #L1174 - L1176 were not covered by tests
}

// Fetch Descriptor of the given reference.
//
// This call is returns a v1.Descriptor with `Size`, `MediaType`, `Digest` fields only!!
// This is a lightweight call used for checking MediaType of given Reference
desc, err := remote.Head(
ref,
remote.WithAuth(auth),
Expand Down Expand Up @@ -1702,8 +1702,13 @@ func (h *CNBIndex) Inspect() (string, error) {
// Remove Image/Index from ImageIndex.
//
// Accepts both Tags and Digests.
func (h *CNBIndex) Remove(ref name.Reference) (err error) {
hash, err := parseReferenceToHash(ref, h.KeyChain, h.Insecure)
func (h *CNBIndex) Remove(repoName string) (err error) {
ref, auth, err := referenceForRepoName(h.KeyChain, repoName, h.Insecure)
if err != nil {
return err

Check warning on line 1708 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L1705-L1708

Added lines #L1705 - L1708 were not covered by tests
}

hash, err := parseReferenceToHash(ref, auth)
if err != nil {
return err

Check warning on line 1713 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L1711-L1713

Added lines #L1711 - L1713 were not covered by tests
}
Expand Down Expand Up @@ -1912,15 +1917,12 @@ func appendAnnotatedManifests(desc v1.Descriptor, imgDesc v1.Descriptor, path la
}
}

func parseReferenceToHash(ref name.Reference, keychain authn.Keychain, insecure bool) (hash v1.Hash, err error) {
func parseReferenceToHash(ref name.Reference, auth authn.Authenticator) (hash v1.Hash, err error) {
switch v := ref.(type) {
case name.Tag:
desc, err := remote.Head(
v,
remote.WithAuthFromKeychain(keychain),
remote.WithTransport(
GetTransport(insecure),
),
remote.WithAuth(auth),
)
if err != nil {
return hash, err

Check warning on line 1928 in cnb_index.go

View check run for this annotation

Codecov / codecov/patch

cnb_index.go#L1920-L1928

Added lines #L1920 - L1928 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ type ImageIndex interface {
Delete() error
Inspect() (string, error)
Push(ops ...func(options *IndexPushOptions) error) error
Remove(ref name.Reference) error
Remove(repoName string) error
Save() error
}
65 changes: 60 additions & 5 deletions layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
Expand Down Expand Up @@ -1346,11 +1347,58 @@ func testImageIndex(t *testing.T, when spec.G, it spec.S) {
})
})
})
/*
Push(ops ...func(options *IndexPushOptions) error) error
Inspect() (string, error)
Remove(ref name.Reference) error
*/

when("#Remove", func() {
var digest name.Digest
when("index exists on disk", func() {
when("#FromBaseImageIndex", func() {
it.Before(func() {
idx = setUpImageIndex(t, "busybox-multi-platform", tmpDir, imgutil.FromBaseImageIndex(baseIndexPath), imgutil.WithKeychain(authn.DefaultKeychain))
localPath = filepath.Join(tmpDir, "busybox-multi-platform")
digest, err = name.NewDigest("busybox@sha256:4be429a5fbb2e71ae7958bfa558bc637cf3a61baf40a708cb8fff532b39e52d0")
h.AssertNil(t, err)
})

it("given manifest is removed", func() {
err = idx.Remove(digest.String())
h.AssertNil(t, err)

// After removing any operation to get something about the digest must fail
_, err = idx.OS(digest)
h.AssertNotNil(t, err)
h.AssertError(t, err, "no image or image index found for digest")

// After saving, the index on disk must reflect the change
err = idx.Save()
h.AssertNil(t, err)

index := h.ReadIndexManifest(t, localPath)
h.AssertEq(t, len(index.Manifests), 1)
h.AssertEq(t, index.Manifests[0].Digest.String(), "sha256:8a4415fb43600953cbdac6ec03c2d96d900bb21f8d78964837dad7f73b9afcdc")
})
})
})
})

when("#Inspect", func() {
var indexString string
when("index exists on disk", func() {
when("#FromBaseImageIndex", func() {
it.Before(func() {
idx = setUpImageIndex(t, "busybox-multi-platform", tmpDir, imgutil.FromBaseImageIndex(baseIndexPath))
localPath = filepath.Join(tmpDir, "busybox-multi-platform")
})

it("returns an image index string representation", func() {
indexString, err = idx.Inspect()
h.AssertNil(t, err)

idxFromString := parseImageIndex(t, indexString)
h.AssertEq(t, len(idxFromString.Manifests), 2)
})
})
})
})
}

func setUpImageIndex(t *testing.T, repoName string, tmpDir string, ops ...imgutil.Option) imgutil.ImageIndex {
Expand All @@ -1370,3 +1418,10 @@ func newRepoName() string {
func newTestImageIndexName(name string) string {
return dockerRegistry.RepoName(name + "-" + h.RandString(10))
}

func parseImageIndex(t *testing.T, index string) *v1.IndexManifest {
r := strings.NewReader(index)
idx, err := v1.ParseIndexManifest(r)
h.AssertNil(t, err)
return idx
}

0 comments on commit 530a05d

Please sign in to comment.