Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BuildpackForKey helper method. #165

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ func NewImageFromInspectOutput(output []byte) (Image, error) {
Labels: inspect[0].Config.Labels,
}, nil
}

func (i Image) BuildpackForKey(key string) (ImageBuildpackMetadata, error) {
for _, buildpack := range i.Buildpacks {
if buildpack.Key == key {
return buildpack, nil
}
}

return ImageBuildpackMetadata{}, fmt.Errorf("no buildpack found for key: %s", key)
}
68 changes: 68 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package occam_test

import (
"testing"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testImage(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect

context("BuildpackForKey", func() {
it("returns the Buildpack with the key", func() {
image := occam.Image{
Buildpacks: []occam.ImageBuildpackMetadata{
{
Key: "first",
Layers: map[string]occam.ImageBuildpackMetadataLayer{
"first-layer-1": {
SHA: "first-layer-1-sha",
},
"first-layer-2": {
SHA: "first-layer-2-sha",
},
},
},
{
Key: "second",
Layers: map[string]occam.ImageBuildpackMetadataLayer{
"second-layer-1": {
SHA: "second-layer-1-sha",
},
"second-layer-2": {
SHA: "second-layer-2-sha",
},
},
},
},
}

firstBuildpack, err := image.BuildpackForKey("first")
Expect(err).NotTo(HaveOccurred())

Expect(firstBuildpack.Key).To(Equal("first"))
Expect(firstBuildpack.Layers["first-layer-2"].SHA).To(Equal("first-layer-2-sha"))

secondBuildpack, err := image.BuildpackForKey("second")
Expect(err).NotTo(HaveOccurred())

Expect(secondBuildpack.Key).To(Equal("second"))
Expect(secondBuildpack.Layers["second-layer-2"].SHA).To(Equal("second-layer-2-sha"))
})

context("failure cases", func() {
context("when no buildpack exists with the provided key", func() {
it("returns an error", func() {
image := occam.Image{}

_, err := image.BuildpackForKey("some-non-existent-key")
Expect(err).To(HaveOccurred())
})
})
})
})
}
1 change: 1 addition & 0 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestUnitOccam(t *testing.T) {
suite("CacheVolumeNames", testCacheVolumeNames)
suite("Container", testContainer)
suite("Docker", testDocker)
suite("Image", testImage)
suite("Pack", testPack)
suite("RandomName", testRandomName)
suite("Source", testSource)
Expand Down