2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2017-06-26 12:06:34 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-06-26 12:06:34 -04:00
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-06-26 12:06:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestPingFail tests that when a server sends a non-successful response that we
|
|
|
|
// can still grab API details, when set.
|
2018-05-15 21:15:43 -04:00
|
|
|
// Some of this is just exercising the code paths to make sure there are no
|
2017-06-26 12:06:34 -04:00
|
|
|
// 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.Body = ioutil.NopCloser(strings.NewReader("some error with the server"))
|
|
|
|
return resp, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
ping, err := client.Ping(context.Background())
|
2019-01-14 15:47:30 -05:00
|
|
|
assert.ErrorContains(t, err, "some error with the server")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(false, ping.Experimental))
|
|
|
|
assert.Check(t, is.Equal("", ping.APIVersion))
|
2017-06-26 12:06:34 -04:00
|
|
|
|
|
|
|
withHeader = true
|
|
|
|
ping2, err := client.Ping(context.Background())
|
2019-01-14 15:47:30 -05:00
|
|
|
assert.ErrorContains(t, err, "some error with the server")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(true, ping2.Experimental))
|
|
|
|
assert.Check(t, is.Equal("awesome", ping2.APIVersion))
|
2017-06-26 12:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
resp := &http.Response{StatusCode: http.StatusInternalServerError}
|
|
|
|
resp.Header = http.Header{}
|
|
|
|
resp.Header.Set("API-Version", "awesome")
|
|
|
|
resp.Header.Set("Docker-Experimental", "true")
|
|
|
|
resp.Body = ioutil.NopCloser(strings.NewReader("some error with the server"))
|
|
|
|
return resp, errors.New("some error")
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
ping, err := client.Ping(context.Background())
|
2019-01-14 15:47:30 -05:00
|
|
|
assert.ErrorContains(t, err, "some error")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(false, ping.Experimental))
|
|
|
|
assert.Check(t, is.Equal("", ping.APIVersion))
|
2017-06-26 12:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-01-14 15:47:30 -05:00
|
|
|
resp := &http.Response{StatusCode: http.StatusOK}
|
2017-06-26 12:06:34 -04:00
|
|
|
resp.Header = http.Header{}
|
|
|
|
resp.Header.Set("API-Version", "awesome")
|
|
|
|
resp.Header.Set("Docker-Experimental", "true")
|
2019-01-14 15:47:30 -05:00
|
|
|
resp.Body = ioutil.NopCloser(strings.NewReader("OK"))
|
2017-06-26 12:06:34 -04:00
|
|
|
return resp, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
ping, err := client.Ping(context.Background())
|
2019-01-14 15:47:30 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(true, ping.Experimental))
|
|
|
|
assert.Check(t, is.Equal("awesome", ping.APIVersion))
|
2017-06-26 12:06:34 -04:00
|
|
|
}
|