forked from fsouza/go-dockerclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm_test.go
210 lines (197 loc) · 6.95 KB
/
swarm_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2016 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"net/http"
"net/url"
"reflect"
"testing"
"github.com/docker/docker/api/types/swarm"
)
func TestInitSwarm(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: `"body"`, status: http.StatusOK}
client := newTestClient(fakeRT)
response, err := client.InitSwarm(InitSwarmOptions{})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "POST"
if req.Method != expectedMethod {
t.Errorf("InitSwarm: Wrong HTTP method. Want %s. Got %s.", expectedMethod, req.Method)
}
u, _ := url.Parse(client.getURL("/swarm/init"))
if req.URL.Path != u.Path {
t.Errorf("InitSwarm: Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
expected := "body"
if response != expected {
t.Errorf("InitSwarm: Wrong response. Want %q. Got %q.", expected, response)
}
}
func TestInitSwarmAlreadyInSwarm(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "", status: http.StatusNotAcceptable})
_, err := client.InitSwarm(InitSwarmOptions{})
if err != ErrNodeAlreadyInSwarm {
t.Errorf("InitSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeAlreadyInSwarm, err)
}
client = newTestClient(&FakeRoundTripper{message: "", status: http.StatusServiceUnavailable})
_, err = client.InitSwarm(InitSwarmOptions{})
if err != ErrNodeAlreadyInSwarm {
t.Errorf("InitSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeAlreadyInSwarm, err)
}
}
func TestJoinSwarm(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
err := client.JoinSwarm(JoinSwarmOptions{})
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "POST"
if req.Method != expectedMethod {
t.Errorf("JoinSwarm: Wrong HTTP method. Want %s. Got %s.", expectedMethod, req.Method)
}
u, _ := url.Parse(client.getURL("/swarm/join"))
if req.URL.Path != u.Path {
t.Errorf("JoinSwarm: Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
}
func TestJoinSwarmAlreadyInSwarm(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "", status: http.StatusNotAcceptable})
err := client.JoinSwarm(JoinSwarmOptions{})
if err != ErrNodeAlreadyInSwarm {
t.Errorf("JoinSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeAlreadyInSwarm, err)
}
client = newTestClient(&FakeRoundTripper{message: "", status: http.StatusServiceUnavailable})
err = client.JoinSwarm(JoinSwarmOptions{})
if err != ErrNodeAlreadyInSwarm {
t.Errorf("JoinSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeAlreadyInSwarm, err)
}
}
func TestLeaveSwarm(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var testData = []struct {
force bool
expectedURI string
}{
{false, "/swarm/leave?force=false"},
{true, "/swarm/leave?force=true"},
}
for i, tt := range testData {
err := client.LeaveSwarm(LeaveSwarmOptions{Force: tt.force})
if err != nil {
t.Fatal(err)
}
expectedMethod := "POST"
req := fakeRT.requests[i]
if req.Method != expectedMethod {
t.Errorf("LeaveSwarm: Wrong HTTP method. Want %s. Got %s.", expectedMethod, req.Method)
}
expected, _ := url.Parse(client.getURL(tt.expectedURI))
if req.URL.String() != expected.String() {
t.Errorf("LeaveSwarm: Wrong request string. Want %q. Got %q.", expected.String(), req.URL.String())
}
}
}
func TestLeaveSwarmNotInSwarm(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "", status: http.StatusNotAcceptable})
err := client.LeaveSwarm(LeaveSwarmOptions{})
if err != ErrNodeNotInSwarm {
t.Errorf("LeaveSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
client = newTestClient(&FakeRoundTripper{message: "", status: http.StatusServiceUnavailable})
err = client.LeaveSwarm(LeaveSwarmOptions{})
if err != ErrNodeNotInSwarm {
t.Errorf("LeaveSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
}
func TestUpdateSwarm(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
opts := UpdateSwarmOptions{
Version: 10,
RotateManagerToken: true,
RotateWorkerToken: false,
}
err := client.UpdateSwarm(opts)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "POST"
if req.Method != expectedMethod {
t.Errorf("UpdateSwarm: Wrong HTTP method. Want %s. Got %s.", expectedMethod, req.Method)
}
expectedPath := "/swarm/update"
if req.URL.Path != expectedPath {
t.Errorf("UpdateSwarm: Wrong request path. Want %q. Got %q.", expectedPath, req.URL.Path)
}
expected := map[string][]string{
"version": {"10"},
"rotateManagerToken": {"true"},
"rotateWorkerToken": {"false"},
}
got := map[string][]string(req.URL.Query())
if !reflect.DeepEqual(got, expected) {
t.Errorf("UpdateSwarm: Wrong request query. Want %v. Got %v", expected, got)
}
}
func TestUpdateSwarmNotInSwarm(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "", status: http.StatusNotAcceptable})
err := client.UpdateSwarm(UpdateSwarmOptions{})
if err != ErrNodeNotInSwarm {
t.Errorf("UpdateSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
client = newTestClient(&FakeRoundTripper{message: "", status: http.StatusServiceUnavailable})
err = client.UpdateSwarm(UpdateSwarmOptions{})
if err != ErrNodeNotInSwarm {
t.Errorf("UpdateSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
}
func TestInspectSwarm(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: `{"ID": "123"}`, status: http.StatusOK}
client := newTestClient(fakeRT)
response, err := client.InspectSwarm(nil)
if err != nil {
t.Fatal(err)
}
req := fakeRT.requests[0]
expectedMethod := "GET"
if req.Method != expectedMethod {
t.Errorf("InspectSwarm: Wrong HTTP method. Want %s. Got %s.", expectedMethod, req.Method)
}
u, _ := url.Parse(client.getURL("/swarm"))
if req.URL.Path != u.Path {
t.Errorf("InspectSwarm: Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
expected := swarm.Swarm{ClusterInfo: swarm.ClusterInfo{ID: "123"}}
if !reflect.DeepEqual(expected, response) {
t.Errorf("InspectSwarm: Wrong response. Want %#v. Got %#v.", expected, response)
}
}
func TestInspectSwarmNotInSwarm(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "", status: http.StatusNotAcceptable})
_, err := client.InspectSwarm(nil)
if err != ErrNodeNotInSwarm {
t.Errorf("InspectSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
client = newTestClient(&FakeRoundTripper{message: "", status: http.StatusServiceUnavailable})
_, err = client.InspectSwarm(nil)
if err != ErrNodeNotInSwarm {
t.Errorf("InspectSwarm: Wrong error type. Want %#v. Got %#v", ErrNodeNotInSwarm, err)
}
}