Skip to content

Commit

Permalink
Merge pull request #1923 from mtrmac/tarball-simplify
Browse files Browse the repository at this point in the history
Simplify the tarball: transport
  • Loading branch information
vrothberg committed Apr 18, 2023
2 parents 7432c03 + 9d0162f commit 9270e46
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 123 deletions.
28 changes: 11 additions & 17 deletions docker/distribution_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ import (
"testing"
)

type nopCloser struct {
io.Reader
}

func (nopCloser) Close() error { return nil }

func TestHandleErrorResponse401ValidBody(t *testing.T) {
json := "{\"errors\":[{\"code\":\"UNAUTHORIZED\",\"message\":\"action requires authentication\"}]}"
json := []byte("{\"errors\":[{\"code\":\"UNAUTHORIZED\",\"message\":\"action requires authentication\"}]}")
response := &http.Response{
Status: "401 Unauthorized",
StatusCode: 401,
Body: nopCloser{bytes.NewBufferString(json)},
Body: io.NopCloser(bytes.NewReader(json)),
}
err := handleErrorResponse(response)

Expand All @@ -45,11 +39,11 @@ func TestHandleErrorResponse401ValidBody(t *testing.T) {
}

func TestHandleErrorResponse401WithInvalidBody(t *testing.T) {
json := "{invalid json}"
json := []byte("{invalid json}")
response := &http.Response{
Status: "401 Unauthorized",
StatusCode: 401,
Body: nopCloser{bytes.NewBufferString(json)},
Body: io.NopCloser(bytes.NewReader(json)),
}
err := handleErrorResponse(response)

Expand All @@ -60,11 +54,11 @@ func TestHandleErrorResponse401WithInvalidBody(t *testing.T) {
}

func TestHandleErrorResponseExpectedStatusCode400ValidBody(t *testing.T) {
json := "{\"errors\":[{\"code\":\"DIGEST_INVALID\",\"message\":\"provided digest does not match\"}]}"
json := []byte("{\"errors\":[{\"code\":\"DIGEST_INVALID\",\"message\":\"provided digest does not match\"}]}")
response := &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: nopCloser{bytes.NewBufferString(json)},
Body: io.NopCloser(bytes.NewReader(json)),
}
err := handleErrorResponse(response)

Expand All @@ -75,11 +69,11 @@ func TestHandleErrorResponseExpectedStatusCode400ValidBody(t *testing.T) {
}

func TestHandleErrorResponseExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
json := `{"randomkey": "randomvalue"}`
json := []byte(`{"randomkey": "randomvalue"}`)
response := &http.Response{
Status: "404 Not Found",
StatusCode: 404,
Body: nopCloser{bytes.NewBufferString(json)},
Body: io.NopCloser(bytes.NewReader(json)),
}
err := handleErrorResponse(response)

Expand All @@ -90,11 +84,11 @@ func TestHandleErrorResponseExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
}

func TestHandleErrorResponseExpectedStatusCode404InvalidBody(t *testing.T) {
json := "{invalid json}"
json := []byte("{invalid json}")
response := &http.Response{
Status: "404 Not Found",
StatusCode: 404,
Body: nopCloser{bytes.NewBufferString(json)},
Body: io.NopCloser(bytes.NewReader(json)),
}
err := handleErrorResponse(response)

Expand All @@ -108,7 +102,7 @@ func TestHandleErrorResponseUnexpectedStatusCode501(t *testing.T) {
response := &http.Response{
Status: "501 Not Implemented",
StatusCode: 501,
Body: nopCloser{bytes.NewBufferString("{\"Error Encountered\" : \"Function not implemented.\"}")},
Body: io.NopCloser(bytes.NewReader([]byte("{\"Error Encountered\" : \"Function not implemented.\"}"))),
}
err := handleErrorResponse(response)

Expand Down
2 changes: 1 addition & 1 deletion sif/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (s *sifImageSource) Close() error {
func (s *sifImageSource) GetBlob(ctx context.Context, info types.BlobInfo, cache types.BlobInfoCache) (io.ReadCloser, int64, error) {
switch info.Digest {
case s.configDigest:
return io.NopCloser(bytes.NewBuffer(s.config)), int64(len(s.config)), nil
return io.NopCloser(bytes.NewReader(s.config)), int64(len(s.config)), nil
case s.layerDigest:
reader, err := os.Open(s.layerFile)
if err != nil {
Expand Down
26 changes: 13 additions & 13 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestWriteRead(t *testing.T) {
compression = archive.Gzip
}
digest, decompressedSize, size, blob := makeLayer(t, compression)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -557,7 +557,7 @@ func TestDuplicateName(t *testing.T) {
t.Fatalf("NewImageDestination(%q, first pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob := makeLayer(t, archive.Uncompressed)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -598,7 +598,7 @@ func TestDuplicateName(t *testing.T) {
t.Fatalf("NewImageDestination(%q, second pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob = makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestDuplicateID(t *testing.T) {
t.Fatalf("NewImageDestination(%q, first pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -697,7 +697,7 @@ func TestDuplicateID(t *testing.T) {
t.Fatalf("NewImageDestination(%q, second pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob = makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -758,7 +758,7 @@ func TestDuplicateNameID(t *testing.T) {
t.Fatalf("NewImageDestination(%q, first pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -799,7 +799,7 @@ func TestDuplicateNameID(t *testing.T) {
t.Fatalf("NewImageDestination(%q, second pass) returned no destination", ref.StringWithinTransport())
}
digest, _, size, blob = makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size,
Digest: digest,
}, cache, false); err != nil {
Expand Down Expand Up @@ -909,14 +909,14 @@ func TestSize(t *testing.T) {
t.Fatalf("Error saving config to destination: %v", err)
}
digest1, usize1, size1, blob := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size1,
Digest: digest1,
}, cache, false); err != nil {
t.Fatalf("Error saving randomly-generated layer 1 to destination: %v", err)
}
digest2, usize2, size2, blob := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob), types.BlobInfo{
Size: size2,
Digest: digest2,
}, cache, false); err != nil {
Expand Down Expand Up @@ -1004,26 +1004,26 @@ func TestDuplicateBlob(t *testing.T) {
t.Fatalf("NewImageDestination(%q) returned no destination", ref.StringWithinTransport())
}
digest1, _, size1, blob1 := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob1), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob1), types.BlobInfo{
Size: size1,
Digest: digest1,
}, cache, false); err != nil {
t.Fatalf("Error saving randomly-generated layer 1 to destination (first copy): %v", err)
}
digest2, _, size2, blob2 := makeLayer(t, archive.Gzip)
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob2), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob2), types.BlobInfo{
Size: size2,
Digest: digest2,
}, cache, false); err != nil {
t.Fatalf("Error saving randomly-generated layer 2 to destination (first copy): %v", err)
}
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob1), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob1), types.BlobInfo{
Size: size1,
Digest: digest1,
}, cache, false); err != nil {
t.Fatalf("Error saving randomly-generated layer 1 to destination (second copy): %v", err)
}
if _, err := dest.PutBlob(context.Background(), bytes.NewBuffer(blob2), types.BlobInfo{
if _, err := dest.PutBlob(context.Background(), bytes.NewReader(blob2), types.BlobInfo{
Size: size2,
Digest: digest2,
}, cache, false); err != nil {
Expand Down
Loading

0 comments on commit 9270e46

Please sign in to comment.