2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2020-12-10 18:13:37 -05:00
|
|
|
"errors"
|
2016-09-06 14:46:37 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-10-10 12:37:39 -04:00
|
|
|
"math/rand"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-12-10 18:13:37 -05:00
|
|
|
"time"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-31 12:22:43 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestSetHostHeader should set fake host for local communications, set real host
|
|
|
|
// for normal communications.
|
|
|
|
func TestSetHostHeader(t *testing.T) {
|
|
|
|
testURL := "/test"
|
|
|
|
testCases := []struct {
|
|
|
|
host string
|
|
|
|
expectedHost string
|
|
|
|
expectedURLHost string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"unix:///var/run/docker.sock",
|
|
|
|
"docker",
|
|
|
|
"/var/run/docker.sock",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"npipe:////./pipe/docker_engine",
|
|
|
|
"docker",
|
|
|
|
"//./pipe/docker_engine",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp://0.0.0.0:4243",
|
|
|
|
"",
|
|
|
|
"0.0.0.0:4243",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp://localhost:4243",
|
|
|
|
"",
|
|
|
|
"localhost:4243",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, test := range testCases {
|
2017-09-19 16:12:29 -04:00
|
|
|
hostURL, err := ParseHostURL(test.host)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
if !strings.HasPrefix(req.URL.Path, testURL) {
|
|
|
|
return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
|
|
|
|
}
|
|
|
|
if req.Host != test.expectedHost {
|
|
|
|
return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
|
|
|
|
}
|
|
|
|
if req.URL.Host != test.expectedURLHost {
|
|
|
|
return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2018-05-19 07:38:54 -04:00
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}),
|
2016-09-08 23:44:25 -04:00
|
|
|
|
2017-09-19 16:12:29 -04:00
|
|
|
proto: hostURL.Scheme,
|
|
|
|
addr: hostURL.Host,
|
|
|
|
basePath: hostURL.Path,
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:41:14 -04:00
|
|
|
_, err = client.sendRequest(context.Background(), http.MethodGet, testURL, nil, nil, nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
|
2018-12-31 12:22:43 -05:00
|
|
|
if !errdefs.IsSystem(err) {
|
2019-10-12 18:31:53 -04:00
|
|
|
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
2018-12-31 12:22:43 -05:00
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2018-10-10 12:37:39 -04:00
|
|
|
|
|
|
|
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}
|
|
|
|
resp.Header = http.Header{}
|
|
|
|
resp.Body = ioutil.NopCloser(infinitR)
|
|
|
|
return resp, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.Ping(context.Background())
|
|
|
|
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
|
|
|
|
}
|
2020-12-10 18:13:37 -05:00
|
|
|
|
|
|
|
func TestCanceledContext(t *testing.T) {
|
|
|
|
testURL := "/test"
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Context().Err(), context.Canceled)
|
|
|
|
|
|
|
|
return &http.Response{}, context.Canceled
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
|
|
|
|
assert.Equal(t, true, errdefs.IsCancelled(err))
|
|
|
|
assert.Equal(t, true, errors.Is(err, context.Canceled))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeadlineExceededContext(t *testing.T) {
|
|
|
|
testURL := "/test"
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, req.Context().Err(), context.DeadlineExceeded)
|
|
|
|
|
|
|
|
return &http.Response{}, context.DeadlineExceeded
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
|
|
|
|
assert.Equal(t, true, errdefs.IsDeadline(err))
|
|
|
|
assert.Equal(t, true, errors.Is(err, context.DeadlineExceeded))
|
|
|
|
}
|