Skip to content

Commit

Permalink
Remove built_at for more reproducible builds
Browse files Browse the repository at this point in the history
- Now that the timestamp is not being added to the metadata we no longer
need to check to see if the sha is the same from previous builds to
prevent adding a new timestamp on the same bits.
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Mar 24, 2022
1 parent 0a7d45e commit 0236586
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 149 deletions.
19 changes: 0 additions & 19 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ type BuildProcess interface {
Execute(config GoBuildConfiguration) (binaries []string, err error)
}

//go:generate faux --interface ChecksumCalculator --output fakes/checksum_calculator.go
type ChecksumCalculator interface {
Sum(paths ...string) (string, error)
}

//go:generate faux --interface PathManager --output fakes/path_manager.go
type PathManager interface {
Setup(workspace, importPath string) (goPath, path string, err error)
Expand All @@ -40,7 +35,6 @@ type SBOMGenerator interface {
func Build(
parser ConfigurationParser,
buildProcess BuildProcess,
checksumCalculator ChecksumCalculator,
pathManager PathManager,
clock chronos.Clock,
logs scribe.Emitter,
Expand Down Expand Up @@ -97,19 +91,6 @@ func Build(
return packit.BuildResult{}, err
}

sum, err := checksumCalculator.Sum(targetsLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}

cachedSha, _ := targetsLayer.Metadata["cache_sha"].(string)
if cachedSha != sum {
targetsLayer.Metadata = map[string]interface{}{
"cache_sha": sum,
"built_at": clock.Now().Format(time.RFC3339Nano),
}
}

err = pathManager.Teardown(goPath)
if err != nil {
return packit.BuildResult{}, err
Expand Down
88 changes: 6 additions & 82 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package gobuild_test
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

gobuild "github.com/paketo-buildpacks/go-build"
"github.com/paketo-buildpacks/go-build/fakes"
Expand All @@ -28,15 +25,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
workingDir string
cnbDir string
timestamp time.Time
logs *bytes.Buffer

buildProcess *fakes.BuildProcess
pathManager *fakes.PathManager
sourceRemover *fakes.SourceRemover
parser *fakes.ConfigurationParser
checksumCalculator *fakes.ChecksumCalculator
sbomGenerator *fakes.SBOMGenerator
buildProcess *fakes.BuildProcess
pathManager *fakes.PathManager
sourceRemover *fakes.SourceRemover
parser *fakes.ConfigurationParser
sbomGenerator *fakes.SBOMGenerator

build packit.BuildFunc
)
Expand All @@ -59,14 +54,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
pathManager.SetupCall.Returns.GoPath = "some-go-path"
pathManager.SetupCall.Returns.Path = "some-app-path"

timestamp = time.Now()
clock := chronos.NewClock(func() time.Time {
return timestamp
})

checksumCalculator = &fakes.ChecksumCalculator{}
checksumCalculator.SumCall.Returns.String = "some-checksum"

logs = bytes.NewBuffer(nil)

sourceRemover = &fakes.SourceRemover{}
Expand All @@ -84,9 +71,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
build = gobuild.Build(
parser,
buildProcess,
checksumCalculator,
pathManager,
clock,
chronos.DefaultClock,
scribe.NewEmitter(logs),
sourceRemover,
sbomGenerator,
Expand Down Expand Up @@ -118,10 +104,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
targets := result.Layers[0]
Expect(targets.Name).To(Equal("targets"))
Expect(targets.Path).To(Equal(filepath.Join(layersDir, "targets")))
Expect(targets.Metadata).To(Equal(map[string]interface{}{
"cache_sha": "some-checksum",
"built_at": timestamp.Format(time.RFC3339Nano),
}))
Expect(targets.Build).To(BeFalse())
Expect(targets.Cache).To(BeFalse())
Expect(targets.Launch).To(BeTrue())
Expand Down Expand Up @@ -275,44 +257,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
})

context("when the targets were previously built", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(layersDir, "targets.toml"), []byte(fmt.Sprintf(`
launch = true
[metadata]
cache_sha = "some-checksum"
built_at = "%s"
`, timestamp.Add(-10*time.Second).Format(time.RFC3339Nano))), 0600)
Expect(err).NotTo(HaveOccurred())
})

it("uses the cached layer", func() {
result, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(2))
targets := result.Layers[0]
Expect(targets.Name).To(Equal("targets"))
Expect(targets.Path).To(Equal(filepath.Join(layersDir, "targets")))
Expect(targets.Metadata).To(Equal(map[string]interface{}{
"cache_sha": "some-checksum",
"built_at": timestamp.Add(-10 * time.Second).Format(time.RFC3339Nano),
}))
Expect(targets.Build).To(BeFalse())
Expect(targets.Cache).To(BeFalse())
Expect(targets.Launch).To(BeTrue())
})
})

context("failure cases", func() {
context("when the targets layer cannot be retrieved", func() {
it.Before(func() {
Expand Down Expand Up @@ -396,26 +340,6 @@ launch = true
})
})

context("when the checksum cannot be calculated", func() {
it.Before(func() {
checksumCalculator.SumCall.Returns.Error = errors.New("failed to calculate checksum")
})

it("returns an error", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).To(MatchError("failed to calculate checksum"))
})
})

context("when the go path cannot be torn down", func() {
it.Before(func() {
pathManager.TeardownCall.Returns.Error = errors.New("failed to teardown go path")
Expand Down
31 changes: 0 additions & 31 deletions fakes/checksum_calculator.go

This file was deleted.

4 changes: 2 additions & 2 deletions integration/build_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func testBuildFlags(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring(`variable value: "some-value"`),
ContainSubstring("/workspace contents: []"),
),
Expand Down Expand Up @@ -123,7 +123,7 @@ func testBuildFlags(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring(`variable value: "env-value"`),
ContainSubstring("/workspace contents: []"),
),
Expand Down
10 changes: 5 additions & 5 deletions integration/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring("/workspace contents: []"),
),
).OnPort(8080),
Expand Down Expand Up @@ -169,7 +169,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))
})
})

Expand Down Expand Up @@ -220,7 +220,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring("/workspace contents: []"),
),
).OnPort(8080),
Expand Down Expand Up @@ -280,7 +280,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))

Expect(logs).To(ContainLines(
" Assigning launch processes:",
Expand All @@ -296,7 +296,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(noReloadContainer).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(noReloadContainer).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))
})
})
}
2 changes: 1 addition & 1 deletion integration/import_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func testImportPath(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring("/workspace contents: []"),
),
).OnPort(8080),
Expand Down
2 changes: 1 addition & 1 deletion integration/keep_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func testKeepFiles(t *testing.T, context spec.G, it spec.S) {
Eventually(container).Should(
Serve(
SatisfyAll(
ContainSubstring("go1.16"),
ContainSubstring("go1.17"),
ContainSubstring("/workspace contents: [/workspace/assets /workspace/static-file]"),
ContainSubstring("file contents: Hello world!"),
),
Expand Down
2 changes: 1 addition & 1 deletion integration/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testMod(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))
})
})
}
1 change: 0 additions & 1 deletion integration/rebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func testRebuild(t *testing.T, context spec.G, it spec.S) {

Eventually(container).Should(BeAvailable())

Expect(secondImage.Buildpacks[1].Layers["targets"].Metadata["built_at"]).To(Equal(firstImage.Buildpacks[1].Layers["targets"].Metadata["built_at"]))
Expect(secondImage.Buildpacks[1].Layers["targets"].SHA).To(Equal(firstImage.Buildpacks[1].Layers["targets"].SHA))
})
})
Expand Down
4 changes: 2 additions & 2 deletions integration/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func testTargets(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
containerIDs[container.ID] = struct{}{}

Eventually(container).Should(Serve(ContainSubstring("first: go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("first: go1.17")).OnPort(8080))

Expect(logs).To(ContainLines(
" Assigning launch processes:",
Expand Down Expand Up @@ -130,7 +130,7 @@ func testTargets(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
containerIDs[container.ID] = struct{}{}

Eventually(container).Should(Serve(ContainSubstring("second: go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("second: go1.17")).OnPort(8080))
})
})
}
4 changes: 2 additions & 2 deletions integration/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testVendor(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))
})
})

Expand Down Expand Up @@ -120,7 +120,7 @@ func testVendor(t *testing.T, context spec.G, it spec.S) {
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(Serve(ContainSubstring("go1.16")).OnPort(8080))
Eventually(container).Should(Serve(ContainSubstring("go1.17")).OnPort(8080))
})
})
}
2 changes: 0 additions & 2 deletions run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
gobuild "github.com/paketo-buildpacks/go-build"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
Expand All @@ -33,7 +32,6 @@ func main() {
emitter,
chronos.DefaultClock,
),
fs.NewChecksumCalculator(),
gobuild.NewGoPathManager(os.TempDir()),
chronos.DefaultClock,
emitter,
Expand Down

0 comments on commit 0236586

Please sign in to comment.