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"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2017-06-26 12:06:34 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/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")
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
|
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.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")
|
2021-08-24 06:10:50 -04:00
|
|
|
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
|
2017-06-26 12:06:34 -04:00
|
|
|
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")
|
2021-08-24 06:10:50 -04:00
|
|
|
resp.Body = io.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
|
|
|
}
|
Add HEAD support for /_ping endpoint
Monitoring systems and load balancers are usually configured to use HEAD
requests for health monitoring. The /_ping endpoint currently does not
support this type of request, which means that those systems have fallback
to GET requests.
This patch adds support for HEAD requests on the /_ping endpoint.
Although optional, this patch also returns `Content-Type` and `Content-Length`
headers in case of a HEAD request; Refering to RFC 7231, section 4.3.2:
The HEAD method is identical to GET except that the server MUST NOT
send a message body in the response (i.e., the response terminates at
the end of the header section). The server SHOULD send the same
header fields in response to a HEAD request as it would have sent if
the request had been a GET, except that the payload header fields
(Section 3.3) MAY be omitted. This method can be used for obtaining
metadata about the selected representation without transferring the
representation data and is often used for testing hypertext links for
validity, accessibility, and recent modification.
A payload within a HEAD request message has no defined semantics;
sending a payload body on a HEAD request might cause some existing
implementations to reject the request.
The response to a HEAD request is cacheable; a cache MAY use it to
satisfy subsequent HEAD requests unless otherwise indicated by the
Cache-Control header field (Section 5.2 of [RFC7234]). A HEAD
response might also have an effect on previously cached responses to
GET; see Section 4.3.5 of [RFC7234].
With this patch applied, either `GET` or `HEAD` requests work; the only
difference is that the body is empty in case of a `HEAD` request;
curl -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:35:16 GMT
Content-Length: 2
Content-Type: text/plain; charset=utf-8
OK
curl --head -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:34:15 GMT
The client is also updated to use `HEAD` by default, but fallback to `GET`
if the daemon does not support this method.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-01-14 12:08:49 -05:00
|
|
|
|
|
|
|
// 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,
|
2019-10-12 14:41:14 -04:00
|
|
|
expected: http.MethodHead,
|
Add HEAD support for /_ping endpoint
Monitoring systems and load balancers are usually configured to use HEAD
requests for health monitoring. The /_ping endpoint currently does not
support this type of request, which means that those systems have fallback
to GET requests.
This patch adds support for HEAD requests on the /_ping endpoint.
Although optional, this patch also returns `Content-Type` and `Content-Length`
headers in case of a HEAD request; Refering to RFC 7231, section 4.3.2:
The HEAD method is identical to GET except that the server MUST NOT
send a message body in the response (i.e., the response terminates at
the end of the header section). The server SHOULD send the same
header fields in response to a HEAD request as it would have sent if
the request had been a GET, except that the payload header fields
(Section 3.3) MAY be omitted. This method can be used for obtaining
metadata about the selected representation without transferring the
representation data and is often used for testing hypertext links for
validity, accessibility, and recent modification.
A payload within a HEAD request message has no defined semantics;
sending a payload body on a HEAD request might cause some existing
implementations to reject the request.
The response to a HEAD request is cacheable; a cache MAY use it to
satisfy subsequent HEAD requests unless otherwise indicated by the
Cache-Control header field (Section 5.2 of [RFC7234]). A HEAD
response might also have an effect on previously cached responses to
GET; see Section 4.3.5 of [RFC7234].
With this patch applied, either `GET` or `HEAD` requests work; the only
difference is that the body is empty in case of a `HEAD` request;
curl -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:35:16 GMT
Content-Length: 2
Content-Type: text/plain; charset=utf-8
OK
curl --head -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:34:15 GMT
The client is also updated to use `HEAD` by default, but fallback to `GET`
if the daemon does not support this method.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-01-14 12:08:49 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
status: http.StatusInternalServerError,
|
2019-10-12 14:41:14 -04:00
|
|
|
expected: http.MethodHead,
|
Add HEAD support for /_ping endpoint
Monitoring systems and load balancers are usually configured to use HEAD
requests for health monitoring. The /_ping endpoint currently does not
support this type of request, which means that those systems have fallback
to GET requests.
This patch adds support for HEAD requests on the /_ping endpoint.
Although optional, this patch also returns `Content-Type` and `Content-Length`
headers in case of a HEAD request; Refering to RFC 7231, section 4.3.2:
The HEAD method is identical to GET except that the server MUST NOT
send a message body in the response (i.e., the response terminates at
the end of the header section). The server SHOULD send the same
header fields in response to a HEAD request as it would have sent if
the request had been a GET, except that the payload header fields
(Section 3.3) MAY be omitted. This method can be used for obtaining
metadata about the selected representation without transferring the
representation data and is often used for testing hypertext links for
validity, accessibility, and recent modification.
A payload within a HEAD request message has no defined semantics;
sending a payload body on a HEAD request might cause some existing
implementations to reject the request.
The response to a HEAD request is cacheable; a cache MAY use it to
satisfy subsequent HEAD requests unless otherwise indicated by the
Cache-Control header field (Section 5.2 of [RFC7234]). A HEAD
response might also have an effect on previously cached responses to
GET; see Section 4.3.5 of [RFC7234].
With this patch applied, either `GET` or `HEAD` requests work; the only
difference is that the body is empty in case of a `HEAD` request;
curl -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:35:16 GMT
Content-Length: 2
Content-Type: text/plain; charset=utf-8
OK
curl --head -i --unix-socket /var/run/docker.sock http://localhost/_ping
HTTP/1.1 200 OK
Api-Version: 1.40
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/dev (linux)
Date: Mon, 14 Jan 2019 12:34:15 GMT
The client is also updated to use `HEAD` by default, but fallback to `GET`
if the daemon does not support this method.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-01-14 12:08:49 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|