forked from adamlaska/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_test.go
More file actions
102 lines (83 loc) · 2.78 KB
/
ping_test.go
File metadata and controls
102 lines (83 loc) · 2.78 KB
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
package system // import "github.com/docker/docker/integration/system"
import (
"context"
"net/http"
"strings"
"testing"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/testutil/daemon"
"github.com/docker/docker/testutil/request"
"gotest.tools/v3/assert"
"gotest.tools/v3/skip"
)
func TestPingCacheHeaders(t *testing.T) {
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "skip test from new feature")
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")
}
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") != "")
}
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) {
res, _, err := request.Get("/_ping")
assert.NilError(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Equal(t, hdr(res, "Swarm"), "inactive")
})
_, 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) {
res, _, err := request.Get("/_ping", request.Host(d.Sock()))
assert.NilError(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Equal(t, hdr(res, "Swarm"), "active/manager")
})
err = client.SwarmLeave(ctx, true)
assert.NilError(t, err)
t.Run("after swarm leave", func(t *testing.T) {
res, _, err := request.Get("/_ping", request.Host(d.Sock()))
assert.NilError(t, err)
assert.Equal(t, res.StatusCode, http.StatusOK)
assert.Equal(t, hdr(res, "Swarm"), "inactive")
})
}
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, ", ")
}