Browse code

Merge pull request #9248 from vbatts/vbatts-fix_gh9241

pkg/tarsum: actually init the TarSum struct

Michael Crosby authored on 2014/11/21 10:15:15
Showing 2 changed files
... ...
@@ -27,11 +27,7 @@ const (
27 27
 // including the byte payload of the image's json metadata as well, and for
28 28
 // calculating the checksums for buildcache.
29 29
 func NewTarSum(r io.Reader, dc bool, v Version) (TarSum, error) {
30
-	headerSelector, err := getTarHeaderSelector(v)
31
-	if err != nil {
32
-		return nil, err
33
-	}
34
-	return &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector}, nil
30
+	return NewTarSumHash(r, dc, v, DefaultTHash)
35 31
 }
36 32
 
37 33
 // 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)
40 40
 	if err != nil {
41 41
 		return nil, err
42 42
 	}
43
-	return &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector, tHash: tHash}, nil
43
+	ts := &tarSum{Reader: r, DisableCompression: dc, tarSumVersion: v, headerSelector: headerSelector, tHash: tHash}
44
+	err = ts.initTarSum()
45
+	return ts, err
44 46
 }
45 47
 
46 48
 // TarSum is the generic interface for calculating fixed time
... ...
@@ -134,12 +132,6 @@ func (ts *tarSum) initTarSum() error {
134 134
 }
135 135
 
136 136
 func (ts *tarSum) Read(buf []byte) (int, error) {
137
-	if ts.writer == nil {
138
-		if err := ts.initTarSum(); err != nil {
139
-			return 0, err
140
-		}
141
-	}
142
-
143 137
 	if ts.finished {
144 138
 		return ts.bufWriter.Read(buf)
145 139
 	}
... ...
@@ -230,6 +230,17 @@ func TestEmptyTar(t *testing.T) {
230 230
 	if resultSum != expectedSum {
231 231
 		t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum)
232 232
 	}
233
+
234
+	// Test without ever actually writing anything.
235
+	if ts, err = NewTarSum(bytes.NewReader([]byte{}), true, Version0); err != nil {
236
+		t.Fatal(err)
237
+	}
238
+
239
+	resultSum = ts.Sum(nil)
240
+
241
+	if resultSum != expectedSum {
242
+		t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum)
243
+	}
233 244
 }
234 245
 
235 246
 var (