2019-01-14 16:00:47 +00:00
|
|
|
package system // import "github.com/docker/docker/integration/system"
|
|
|
|
|
|
|
|
import (
|
2021-02-23 17:21:37 +00:00
|
|
|
"context"
|
2019-01-14 16:00:47 +00:00
|
|
|
"net/http"
|
2022-05-29 11:45:12 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-01-14 16:00:47 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-29 11:45:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2021-02-23 17:21:37 +00:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
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 17:08:49 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2021-02-23 17:21:37 +00:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/skip"
|
2019-01-14 16:00:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPingCacheHeaders(t *testing.T) {
|
2019-04-19 17:38:05 +00:00
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "skip test from new feature")
|
2019-01-14 16:00:47 +00:00
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
res, _, err := request.Get("/_ping")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, res.StatusCode, http.StatusOK)
|
|
|
|
|
|
|
|
assert.Equal(t, hdr(res, "Cache-Control"), "no-cache, no-store, must-revalidate")
|
|
|
|
assert.Equal(t, hdr(res, "Pragma"), "no-cache")
|
|
|
|
}
|
|
|
|
|
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 17:08:49 +00:00
|
|
|
func TestPingGet(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
res, body, err := request.Get("/_ping")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
b, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, string(b), "OK")
|
|
|
|
assert.Equal(t, res.StatusCode, http.StatusOK)
|
|
|
|
assert.Check(t, hdr(res, "API-Version") != "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingHead(t *testing.T) {
|
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "skip test from new feature")
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
res, body, err := request.Head("/_ping")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
b, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 0, len(b))
|
|
|
|
assert.Equal(t, res.StatusCode, http.StatusOK)
|
|
|
|
assert.Check(t, hdr(res, "API-Version") != "")
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:21:37 +00:00
|
|
|
func TestPingSwarmHeader(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.IsRemoteDaemon)
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := daemon.New(t)
|
|
|
|
d.Start(t)
|
|
|
|
defer d.Stop(t)
|
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
t.Run("before swarm init", func(t *testing.T) {
|
2022-05-29 13:17:37 +00:00
|
|
|
p, err := client.Ping(ctx)
|
2021-02-23 17:21:37 +00:00
|
|
|
assert.NilError(t, err)
|
2022-05-29 13:17:37 +00:00
|
|
|
assert.Equal(t, p.SwarmStatus.NodeState, swarm.LocalNodeStateInactive)
|
|
|
|
assert.Equal(t, p.SwarmStatus.ControlAvailable, false)
|
2021-02-23 17:21:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
_, err := client.SwarmInit(ctx, swarm.InitRequest{ListenAddr: "127.0.0.1", AdvertiseAddr: "127.0.0.1:2377"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Run("after swarm init", func(t *testing.T) {
|
2022-05-29 13:17:37 +00:00
|
|
|
p, err := client.Ping(ctx)
|
2021-02-23 17:21:37 +00:00
|
|
|
assert.NilError(t, err)
|
2022-05-29 13:17:37 +00:00
|
|
|
assert.Equal(t, p.SwarmStatus.NodeState, swarm.LocalNodeStateActive)
|
|
|
|
assert.Equal(t, p.SwarmStatus.ControlAvailable, true)
|
2021-02-23 17:21:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err = client.SwarmLeave(ctx, true)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Run("after swarm leave", func(t *testing.T) {
|
2022-05-29 13:17:37 +00:00
|
|
|
p, err := client.Ping(ctx)
|
2021-02-23 17:21:37 +00:00
|
|
|
assert.NilError(t, err)
|
2022-05-29 13:17:37 +00:00
|
|
|
assert.Equal(t, p.SwarmStatus.NodeState, swarm.LocalNodeStateInactive)
|
|
|
|
assert.Equal(t, p.SwarmStatus.ControlAvailable, false)
|
2021-02-23 17:21:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-29 11:45:12 +00:00
|
|
|
func TestPingBuilderHeader(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.IsRemoteDaemon)
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot spin up additional daemons on windows")
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := daemon.New(t)
|
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
t.Run("default config", func(t *testing.T) {
|
|
|
|
d.Start(t)
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
|
|
|
var expected = types.BuilderBuildKit
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = types.BuilderV1
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := client.Ping(ctx)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, p.BuilderVersion, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("buildkit disabled", func(t *testing.T) {
|
|
|
|
cfg := filepath.Join(d.RootDir(), "daemon.json")
|
|
|
|
err := os.WriteFile(cfg, []byte(`{"features": { "buildkit": false }}`), 0644)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
d.Start(t, "--config-file", cfg)
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
|
|
|
var expected = types.BuilderV1
|
|
|
|
p, err := client.Ping(ctx)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, p.BuilderVersion, expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-14 16:00:47 +00:00
|
|
|
func hdr(res *http.Response, name string) string {
|
|
|
|
val, ok := res.Header[http.CanonicalHeaderKey(name)]
|
|
|
|
if !ok || len(val) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.Join(val, ", ")
|
|
|
|
}
|