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
|
package client // import "github.com/docker/docker/client"
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/docker/docker/api/types/swarm"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
// TestPingFail tests that when a server sends a non-successful response that we
// can still grab API details, when set.
// Some of this is just exercising the code paths to make sure there are no
// panics.
func TestPingFail(t *testing.T) {
var withHeader bool
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusInternalServerError}
if withHeader {
resp.Header = http.Header{}
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "inactive")
}
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
return resp, nil
}),
}
ping, err := client.Ping(context.Background())
assert.Check(t, is.ErrorContains(err, "some error with the server"))
assert.Check(t, is.Equal(false, ping.Experimental))
assert.Check(t, is.Equal("", ping.APIVersion))
var si *swarm.Status
assert.Check(t, is.Equal(si, ping.SwarmStatus))
withHeader = true
ping2, err := client.Ping(context.Background())
assert.Check(t, is.ErrorContains(err, "some error with the server"))
assert.Check(t, is.Equal(true, ping2.Experimental))
assert.Check(t, is.Equal("awesome", ping2.APIVersion))
assert.Check(t, is.Equal(swarm.Status{NodeState: "inactive"}, *ping2.SwarmStatus))
}
// TestPingWithError tests the case where there is a protocol error in the ping.
// This test is mostly just testing that there are no panics in this code path.
func TestPingWithError(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("some connection error")
}),
}
ping, err := client.Ping(context.Background())
assert.Check(t, is.ErrorContains(err, "some connection error"))
assert.Check(t, is.Equal(false, ping.Experimental))
assert.Check(t, is.Equal("", ping.APIVersion))
var si *swarm.Status
assert.Check(t, is.Equal(si, ping.SwarmStatus))
}
// TestPingSuccess tests that we are able to get the expected API headers/ping
// details on success.
func TestPingSuccess(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusOK}
resp.Header = http.Header{}
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "active/manager")
resp.Body = io.NopCloser(strings.NewReader("OK"))
return resp, nil
}),
}
ping, err := client.Ping(context.Background())
assert.NilError(t, err)
assert.Check(t, is.Equal(true, ping.Experimental))
assert.Check(t, is.Equal("awesome", ping.APIVersion))
assert.Check(t, is.Equal(swarm.Status{NodeState: "active", ControlAvailable: true}, *ping.SwarmStatus))
}
// TestPingHeadFallback tests that the client falls back to GET if HEAD fails.
func TestPingHeadFallback(t *testing.T) {
tests := []struct {
status int
expected string
}{
{
status: http.StatusOK,
expected: http.MethodHead,
},
{
status: http.StatusInternalServerError,
expected: http.MethodHead,
},
{
status: http.StatusNotFound,
expected: "HEAD, GET",
},
{
status: http.StatusMethodNotAllowed,
expected: "HEAD, GET",
},
}
for _, tc := range tests {
tc := tc
t.Run(http.StatusText(tc.status), func(t *testing.T) {
var reqs []string
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
reqs = append(reqs, req.Method)
resp := &http.Response{StatusCode: http.StatusOK}
if req.Method == http.MethodHead {
resp.StatusCode = tc.status
}
resp.Header = http.Header{}
resp.Header.Add("Api-Version", strings.Join(reqs, ", "))
return resp, nil
}),
}
ping, _ := client.Ping(context.Background())
assert.Check(t, is.Equal(ping.APIVersion, tc.expected))
})
}
}
|