diff --git a/build.go b/build.go index d52ecb26..c33f9160 100644 --- a/build.go +++ b/build.go @@ -33,16 +33,25 @@ type SBOMGenerator interface { GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error) } -func IsLayerReusable(metadata map[string]interface{}, checksum string, build bool, launch bool) bool { +func IsLayerReusable(nodeLayer packit.Layer, depChecksum string, build bool, launch bool, logger scribe.Emitter) bool { + logger.Debug.Process("Checking if layer %s can be reused", nodeLayer.Path) + + metadata := nodeLayer.Metadata cachedChecksum, _ := metadata[DepKey].(string) + logger.Debug.Subprocess("Checksum of dependency: %s", depChecksum) + logger.Debug.Subprocess("Checksum of layer: %s", cachedChecksum) cachedBuild, found := metadata[BuildKey].(bool) buildOK := found && (build == cachedBuild) + logger.Debug.Subprocess("Build requirements match: %v", buildOK) cachedLaunch, found := metadata[LaunchKey].(bool) launchOK := found && (launch == cachedLaunch) + logger.Debug.Subprocess("Launch requirements match: %v", launchOK) + + logger.Debug.Break() - return cargo.Checksum(checksum).MatchString(cachedChecksum) && buildOK && launchOK + return cargo.Checksum(depChecksum).MatchString(cachedChecksum) && buildOK && launchOK } func Build(entryResolver EntryResolver, dependencyManager DependencyManager, sbomGenerator SBOMGenerator, logger scribe.Emitter, clock chronos.Clock) packit.BuildFunc { @@ -99,7 +108,7 @@ func Build(entryResolver EntryResolver, dependencyManager DependencyManager, sbo launchMetadata = packit.LaunchMetadata{BOM: legacySBOM} } - if IsLayerReusable(nodeLayer.Metadata, dependency.Checksum, build, launch) { + if IsLayerReusable(nodeLayer, dependency.Checksum, build, launch, logger) { logger.Process("Reusing cached layer %s", nodeLayer.Path) logger.Break() diff --git a/build_test.go b/build_test.go index 82b01f77..3e47a0ff 100644 --- a/build_test.go +++ b/build_test.go @@ -567,47 +567,51 @@ func testIsLayerReusable(t *testing.T, context spec.G, it spec.S) { checksum = "sha256:de15b44738578367cfb250b6551b4c97e0e0e8050fa931a4a9a7262d374d6034/sha256" build = true launch = true + layer = packit.Layer{} + logger = scribe.NewEmitter(io.Discard) ) it.Before(func() { metadata = map[string]interface{}{nodeengine.DepKey: checksum, nodeengine.BuildKey: build, nodeengine.LaunchKey: launch} + layer.Path = "test" + layer.Metadata = metadata }) it("returns true if the layer can be reused", func() { - isReusable := nodeengine.IsLayerReusable(metadata, checksum, build, launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, build, launch, logger) Expect(isReusable).To(BeTrue()) }) it("returns false if the checksum differs", func() { - isReusable := nodeengine.IsLayerReusable(metadata, "sha256:aaaab44738578367cfb250b6551b4c97e0e0e8050fa931a4a9a7262d3740000/sha256", build, launch) + isReusable := nodeengine.IsLayerReusable(layer, "sha256:aaaab44738578367cfb250b6551b4c97e0e0e8050fa931a4a9a7262d3740000/sha256", build, launch, logger) Expect(isReusable).To(BeFalse()) }) it("returns false if the build requirement changes", func() { - isReusable := nodeengine.IsLayerReusable(metadata, checksum, !build, launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, !build, launch, logger) Expect(isReusable).To(BeFalse()) }) it("returns false if the launch requirement changes", func() { - isReusable := nodeengine.IsLayerReusable(metadata, checksum, build, !launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, build, !launch, logger) Expect(isReusable).To(BeFalse()) }) it("returns false if the checksum is missing in metadata", func() { delete(metadata, nodeengine.DepKey) - isReusable := nodeengine.IsLayerReusable(metadata, checksum, build, launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, build, launch, logger) Expect(isReusable).To(BeFalse()) }) it("returns false if the build is missing in metadata", func() { delete(metadata, nodeengine.BuildKey) - isReusable := nodeengine.IsLayerReusable(metadata, checksum, build, launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, build, launch, logger) Expect(isReusable).To(BeFalse()) }) it("returns false if the launch is missing in metadata", func() { delete(metadata, nodeengine.LaunchKey) - isReusable := nodeengine.IsLayerReusable(metadata, checksum, build, launch) + isReusable := nodeengine.IsLayerReusable(layer, checksum, build, launch, logger) Expect(isReusable).To(BeFalse()) })