client: TestImageLoad: rewrite to use table-tests, use asserts

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-11-18 14:00:32 +01:00
parent 1608746b24
commit 613538469b

View File

@@ -3,10 +3,9 @@ package client // import "github.com/docker/docker/client"
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"net/url"
"testing"
"github.com/docker/docker/api/types/image"
@@ -25,73 +24,63 @@ func TestImageLoadError(t *testing.T) {
}
func TestImageLoad(t *testing.T) {
expectedURL := "/images/load"
expectedInput := "inputBody"
expectedOutput := "outputBody"
loadCases := []struct {
const (
expectedURL = "/images/load"
expectedContentType = "application/x-tar"
expectedInput = "inputBody"
expectedOutput = "outputBody"
)
tests := []struct {
doc string
quiet bool
responseContentType string
expectedResponseJSON bool
expectedQueryParams map[string]string
expectedQueryParams url.Values
}{
{
doc: "plain-text",
quiet: false,
responseContentType: "text/plain",
expectedResponseJSON: false,
expectedQueryParams: map[string]string{
"quiet": "0",
expectedQueryParams: url.Values{
"quiet": {"0"},
},
},
{
doc: "json quiet",
quiet: true,
responseContentType: "application/json",
expectedResponseJSON: true,
expectedQueryParams: map[string]string{
"quiet": "1",
expectedQueryParams: url.Values{
"quiet": {"1"},
},
},
}
for _, loadCase := range loadCases {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
contentType := req.Header.Get("Content-Type")
if contentType != "application/x-tar" {
return nil, fmt.Errorf("content-type not set in URL headers properly. Expected 'application/x-tar', got %s", contentType)
}
query := req.URL.Query()
for key, expected := range loadCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
headers := http.Header{}
headers.Add("Content-Type", loadCase.responseContentType)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
Header: headers,
}, nil
}),
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, is.Equal(req.Header.Get("Content-Type"), expectedContentType))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
Header: http.Header{"Content-Type": []string{tc.responseContentType}},
}, nil
}),
}
input := bytes.NewReader([]byte(expectedInput))
imageLoadResponse, err := client.ImageLoad(context.Background(), input, image.LoadOptions{Quiet: loadCase.quiet})
if err != nil {
t.Fatal(err)
}
if imageLoadResponse.JSON != loadCase.expectedResponseJSON {
t.Fatalf("expected a JSON response, was not.")
}
body, err := io.ReadAll(imageLoadResponse.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != expectedOutput {
t.Fatalf("expected %s, got %s", expectedOutput, string(body))
}
input := bytes.NewReader([]byte(expectedInput))
imageLoadResponse, err := client.ImageLoad(context.Background(), input, image.LoadOptions{
Quiet: tc.quiet,
})
assert.NilError(t, err)
assert.Check(t, is.Equal(imageLoadResponse.JSON, tc.expectedResponseJSON))
body, err := io.ReadAll(imageLoadResponse.Body)
assert.NilError(t, err)
assert.Check(t, is.Equal(string(body), expectedOutput))
})
}
}