1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"strings"
"testing"
"time"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
// TestSetHostHeader should set fake host for local communications, set real host
// for normal communications.
func TestSetHostHeader(t *testing.T) {
const testEndpoint = "/test"
testCases := []struct {
host string
expectedHost string
expectedURLHost string
}{
{
host: "unix:///var/run/docker.sock",
expectedHost: DummyHost,
expectedURLHost: "/var/run/docker.sock",
},
{
host: "npipe:////./pipe/docker_engine",
expectedHost: DummyHost,
expectedURLHost: "//./pipe/docker_engine",
},
{
host: "tcp://0.0.0.0:4243",
expectedHost: "",
expectedURLHost: "0.0.0.0:4243",
},
{
host: "tcp://localhost:4243",
expectedHost: "",
expectedURLHost: "localhost:4243",
},
}
for _, tc := range testCases {
t.Run(tc.host, func(t *testing.T) {
hostURL, err := ParseHostURL(tc.host)
assert.NilError(t, err)
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, testEndpoint) {
return nil, fmt.Errorf("expected URL %q, got %q", testEndpoint, req.URL)
}
if req.Host != tc.expectedHost {
return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host)
}
if req.URL.Host != tc.expectedURLHost {
return nil, fmt.Errorf("expected URL host %q, got %q", tc.expectedURLHost, req.URL.Host)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
proto: hostURL.Scheme,
addr: hostURL.Host,
basePath: hostURL.Path,
}
_, err = client.sendRequest(context.Background(), http.MethodGet, testEndpoint, nil, nil, nil)
assert.NilError(t, err)
})
}
}
// TestPlainTextError tests the server returning an error in plain text for
// backwards compatibility with API versions <1.24. All other tests use
// errors returned as JSON
func TestPlainTextError(t *testing.T) {
client := &Client{
client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerList(context.Background(), container.ListOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
// TestResponseErrors tests handling of error responses returned by the API.
// It includes test-cases for malformed and invalid error-responses, as well
// as plain text errors for backwards compatibility with API versions <1.24.
func TestResponseErrors(t *testing.T) {
errorResponse, err := json.Marshal(&types.ErrorResponse{
Message: "Some error occurred",
})
assert.NilError(t, err)
tests := []struct {
doc string
apiVersion string
contentType string
response string
expected string
}{
{
// Valid (types.ErrorResponse) error, but not using a fixture, to validate current implementation..
doc: "JSON error (non-fixture)",
contentType: "application/json",
response: string(errorResponse),
expected: `Error response from daemon: Some error occurred`,
},
{
// Valid (types.ErrorResponse) error.
doc: "JSON error",
contentType: "application/json",
response: `{"message":"Some error occurred"}`,
expected: `Error response from daemon: Some error occurred`,
},
{
// Valid (types.ErrorResponse) error with additional fields.
doc: "JSON error with extra fields",
contentType: "application/json",
response: `{"message":"Some error occurred", "other_field": "some other field that's not part of types.ErrorResponse"}`,
expected: `Error response from daemon: Some error occurred`,
},
{
// API versions before 1.24 did not support JSON errors. Technically,
// we no longer downgrade to older API versions, but we make an
// exception for errors so that older clients would print a more
// readable error.
doc: "JSON error on old API",
apiVersion: "1.23",
contentType: "text/plain; charset=utf-8",
response: `client version 1.10 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version`,
expected: `Error response from daemon: client version 1.10 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version`,
},
{
doc: "plain-text error",
contentType: "text/plain",
response: `Some error occurred`,
expected: `Error response from daemon: Some error occurred`,
},
{
// TODO(thaJeztah): consider returning (partial) raw response for these
doc: "malformed JSON",
contentType: "application/json",
response: `{"message":"Some error occurred`,
expected: `Error reading JSON: unexpected end of JSON input`,
},
{
// Server response that's valid JSON, but not the expected (types.ErrorResponse) scheme
doc: "incorrect JSON scheme",
contentType: "application/json",
response: `{"error":"Some error occurred"}`,
expected: `Error response from daemon: API returned a 400 (Bad Request) but provided no error-message`,
},
{
// TODO(thaJeztah): improve handling of such errors; we can return the generic "502 Bad Gateway" instead
doc: "html error",
contentType: "text/html",
response: `<!doctype html>
<html lang="en">
<head>
<title>502 Bad Gateway</title>
</head>
<body>
<h1>Bad Gateway</h1>
<p>The server was unable to complete your request. Please try again later.</p>
<p>If this problem persists, please <a href="https://example.com/support">contact support</a>.</p>
</body>
</html>`,
expected: `Error response from daemon: <!doctype html>
<html lang="en">
<head>
<title>502 Bad Gateway</title>
</head>
<body>
<h1>Bad Gateway</h1>
<p>The server was unable to complete your request. Please try again later.</p>
<p>If this problem persists, please <a href="https://example.com/support">contact support</a>.</p>
</body>
</html>`,
},
{
// TODO(thaJeztah): improve handling of these errors (JSON: invalid character '<' looking for beginning of value)
doc: "html error masquerading as JSON",
contentType: "application/json",
response: `<!doctype html>
<html lang="en">
<head>
<title>502 Bad Gateway</title>
</head>
<body>
<h1>Bad Gateway</h1>
<p>The server was unable to complete your request. Please try again later.</p>
<p>If this problem persists, please <a href="https://example.com/support">contact support</a>.</p>
</body>
</html>`,
expected: `Error reading JSON: invalid character '<' looking for beginning of value`,
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client := &Client{
version: tc.apiVersion,
client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{"Content-Type": []string{tc.contentType}},
Body: io.NopCloser(bytes.NewReader([]byte(tc.response))),
}, nil
}),
}
_, err := client.Ping(context.Background())
assert.Check(t, is.Error(err, tc.expected))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
})
}
}
func TestInfiniteError(t *testing.T) {
infinitR := rand.New(rand.NewSource(42))
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Header: http.Header{},
Body: io.NopCloser(infinitR),
}
return resp, nil
}),
}
_, err := client.Ping(context.Background())
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
}
func TestCanceledContext(t *testing.T) {
const testEndpoint = "/test"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.ErrorType(req.Context().Err(), context.Canceled))
return nil, context.Canceled
}),
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorIs(err, context.Canceled))
}
func TestDeadlineExceededContext(t *testing.T) {
const testEndpoint = "/test"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.ErrorType(req.Context().Err(), context.DeadlineExceeded))
return nil, context.DeadlineExceeded
}),
}
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
defer cancel()
<-ctx.Done()
_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorIs(err, context.DeadlineExceeded))
}
|