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

Persist expected layer types even when layer is cached. #60

Merged
merged 1 commit into from
Jun 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func NewLayerContributor(name string, expectedMetadata interface{}, expectedType
// LayerFunc is a callback function that is invoked when a layer needs to be contributed.
type LayerFunc func() (libcnb.Layer, error)


// Contribute is the function to call when implementing your libcnb.LayerContributor.
func (l *LayerContributor) Contribute(layer libcnb.Layer, f LayerFunc) (libcnb.Layer, error) {
raw, err := toml.Marshal(l.ExpectedMetadata)
Expand All @@ -80,6 +79,7 @@ func (l *LayerContributor) Contribute(layer libcnb.Layer, f LayerFunc) (libcnb.L
// TODO: compare entire layer not just metadata (in case build, launch, or cache have changed)
if reflect.DeepEqual(expected, layer.Metadata) {
l.Logger.Headerf("%s: %s cached layer", color.BlueString(l.Name), color.GreenString("Reusing"))
layer.LayerTypes = l.ExpectedTypes
return layer, nil
}

Expand Down
86 changes: 86 additions & 0 deletions layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ func testLayer(t *testing.T, context spec.G, it spec.S) {

Expect(layer.LayerTypes.Launch).To(BeTrue())
})

it("sets layer flags regardless of caching behavior (required for 0.6 API)", func() {
lc.ExpectedTypes.Launch = true
lc.ExpectedTypes.Cache = true
lc.ExpectedTypes.Build = true

layer.Metadata = map[string]interface{}{
"alpha": "test-alpha",
"bravo": map[string]interface{}{
"bravo-1": "test-bravo-1",
"bravo-2": "test-bravo-2",
},
}

var called bool

layer, err := lc.Contribute(layer, func() (libcnb.Layer, error) {
called = true
return layer, nil
})
Expect(err).NotTo(HaveOccurred())
Expect(called).To(BeFalse())

Expect(layer.LayerTypes.Launch).To(BeTrue())
Expect(layer.LayerTypes.Cache).To(BeTrue())
Expect(layer.LayerTypes.Build).To(BeTrue())
})
})

context("NewDependencyLayer", func() {
Expand Down Expand Up @@ -417,6 +444,42 @@ func testLayer(t *testing.T, context spec.G, it spec.S) {
},
}))
})

it("sets layer flags regardless of caching behavior (required for 0.6 API)", func() {
layer.Metadata = map[string]interface{}{
"id": dependency.ID,
"name": dependency.Name,
"version": dependency.Version,
"uri": dependency.URI,
"sha256": dependency.SHA256,
"stacks": []interface{}{dependency.Stacks[0]},
"licenses": []map[string]interface{}{
{
"type": dependency.Licenses[0].Type,
"uri": dependency.Licenses[0].URI,
},
},
}
dlc.ExpectedTypes.Launch = true
dlc.ExpectedTypes.Cache = true
dlc.ExpectedTypes.Build = true

var called bool

layer, err := dlc.Contribute(layer, func(artifact *os.File) (libcnb.Layer, error) {
defer artifact.Close()

called = true
return layer, nil
})
Expect(err).NotTo(HaveOccurred())

Expect(called).To(BeFalse())

Expect(layer.LayerTypes.Launch).To(BeTrue())
Expect(layer.LayerTypes.Cache).To(BeTrue())
Expect(layer.LayerTypes.Build).To(BeTrue())
})
})

context("NewHelperLayer", func() {
Expand Down Expand Up @@ -532,5 +595,28 @@ func testLayer(t *testing.T, context spec.G, it spec.S) {
"keywords": []interface{}{},
}))
})

it("sets layer flags regardless of caching behavior (required for 0.6 API)", func() {
layer.Metadata = map[string]interface{}{
"id": buildpack.Info.ID,
"name": buildpack.Info.Name,
"version": buildpack.Info.Version,
"homepage": buildpack.Info.Homepage,
"clear-env": buildpack.Info.ClearEnvironment,
"description": "",
"keywords": []interface{}{},
}
// Launch is the only one set & always true

layer, err := hlc.Contribute(layer)
Expect(err).NotTo(HaveOccurred())

Expect(filepath.Join(layer.Exec.FilePath("test-name-1"))).NotTo(BeAnExistingFile())
Expect(filepath.Join(layer.Exec.FilePath("test-name-2"))).NotTo(BeAnExistingFile())

Expect(layer.LayerTypes.Launch).To(BeTrue())
Expect(layer.LayerTypes.Cache).To(BeFalse())
Expect(layer.LayerTypes.Build).To(BeFalse())
})
})
}