Generate reproducible tarball

Signed-off-by: Tamal Saha <tamal@appscode.com>
pull/9674/head
Tamal Saha 4 years ago
parent 43853ea772
commit 5ab6399ada

@ -23,7 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"
"sort"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
@ -203,6 +203,9 @@ func writeTarContents(out *tar.Writer, c *chart.Chart, prefix string) error {
}
// Save templates
sort.Slice(c.Templates, func(i, j int) bool {
return c.Templates[i].Name < c.Templates[j].Name
})
for _, f := range c.Templates {
n := filepath.Join(base, f.Name)
if err := writeToTar(out, n, f.Data); err != nil {
@ -211,6 +214,9 @@ func writeTarContents(out *tar.Writer, c *chart.Chart, prefix string) error {
}
// Save files
sort.Slice(c.Files, func(i, j int) bool {
return c.Files[i].Name < c.Files[j].Name
})
for _, f := range c.Files {
n := filepath.Join(base, f.Name)
if err := writeToTar(out, n, f.Data); err != nil {
@ -234,7 +240,6 @@ func writeToTar(out *tar.Writer, name string, body []byte) error {
Name: filepath.ToSlash(name),
Mode: 0644,
Size: int64(len(body)),
ModTime: time.Now(),
}
if err := out.WriteHeader(h); err != nil {
return err

@ -123,12 +123,7 @@ func Indent(n int, text string) string {
return startOfLine.ReplaceAllLiteralString(text, indentation)
}
func TestSavePreservesTimestamps(t *testing.T) {
// Test executes so quickly that if we don't subtract a second, the
// check will fail because `initialCreateTime` will be identical to the
// written timestamp for the files.
initialCreateTime := time.Now().Add(-1 * time.Second)
func TestSaveZerosOutTimestamps(t *testing.T) {
tmp, err := ioutil.TempDir("", "helm-")
if err != nil {
t.Fatal(err)
@ -162,8 +157,8 @@ func TestSavePreservesTimestamps(t *testing.T) {
}
for _, header := range allHeaders {
if header.ModTime.Before(initialCreateTime) {
t.Fatalf("File timestamp not preserved: %v", header.ModTime)
if header.ModTime != time.Unix(0, 0) {
t.Fatalf("File %s timestamp is not zero", header.Name)
}
}
}

Loading…
Cancel
Save