From 64b1cbc28945f193a604ca1f52825f43b8f52041 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 19 Nov 2014 15:46:03 -0500 Subject: [PATCH 1/2] pkg/tarsum: actually init the TarSum struct closes #9241 Signed-off-by: Vincent Batts --- tarsum/tarsum.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tarsum/tarsum.go b/tarsum/tarsum.go index 34386ff..ba09d4a 100644 --- a/tarsum/tarsum.go +++ b/tarsum/tarsum.go @@ -27,11 +27,7 @@ const ( // including the byte payload of the image's json metadata as well, and for // calculating the checksums for buildcache. func NewTarSum(r io.Reader, dc bool, v Version) (TarSum, error) { - headerSelector, err := getTarHeaderSelector(v) - if err != nil { - return nil, err - } - return &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector}, nil + return NewTarSumHash(r, dc, v, DefaultTHash) } // Create a new TarSum, providing a THash to use rather than the DefaultTHash @@ -40,7 +36,9 @@ func NewTarSumHash(r io.Reader, dc bool, v Version, tHash THash) (TarSum, error) if err != nil { return nil, err } - return &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector, tHash: tHash}, nil + ts := &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector, tHash: tHash} + err = ts.initTarSum() + return ts, err } // TarSum is the generic interface for calculating fixed time @@ -134,12 +132,6 @@ func (ts *tarSum) initTarSum() error { } func (ts *tarSum) Read(buf []byte) (int, error) { - if ts.writer == nil { - if err := ts.initTarSum(); err != nil { - return 0, err - } - } - if ts.finished { return ts.bufWriter.Read(buf) } From c91f8d09a5d5f7bfc47a402e66c926bb546c57b6 Mon Sep 17 00:00:00 2001 From: Josh Hawn Date: Wed, 19 Nov 2014 12:15:20 -0800 Subject: [PATCH 2/2] Add unit test for tarSum.Sum() with no data Docker-DCO-1.1-Signed-off-by: Josh Hawn (github: jlhawn) --- tarsum/tarsum_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tarsum/tarsum_test.go b/tarsum/tarsum_test.go index 1e06cda..c5dca6a 100644 --- a/tarsum/tarsum_test.go +++ b/tarsum/tarsum_test.go @@ -230,6 +230,17 @@ func TestEmptyTar(t *testing.T) { if resultSum != expectedSum { t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) } + + // Test without ever actually writing anything. + if ts, err = NewTarSum(bytes.NewReader([]byte{}), true, Version0); err != nil { + t.Fatal(err) + } + + resultSum = ts.Sum(nil) + + if resultSum != expectedSum { + t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) + } } var (