forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_test.go
240 lines (228 loc) · 7.55 KB
/
cluster_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package couchdb
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
)
const optionEnsureDBsExist = "ensure_dbs_exist"
func TestClusterStatus(t *testing.T) {
type tst struct {
client *client
options map[string]interface{}
expected string
status int
err string
}
tests := testy.NewTable()
tests.Add("network error", tst{
client: newTestClient(nil, errors.New("network error")),
status: http.StatusBadGateway,
err: `Get "?http://example.com/_cluster_setup"?: network error`,
})
tests.Add("finished", tst{
client: newTestClient(&http.Response{
StatusCode: http.StatusOK,
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: ioutil.NopCloser(strings.NewReader(`{"state":"cluster_finished"}`)),
}, nil),
expected: "cluster_finished",
})
tests.Add("invalid option", tst{
client: newCustomClient(func(r *http.Request) (*http.Response, error) {
return nil, nil
}),
options: map[string]interface{}{
optionEnsureDBsExist: 1.0,
},
status: http.StatusBadRequest,
err: "kivik: invalid type float64 for options",
})
tests.Add("invalid param", tst{
client: newCustomClient(func(r *http.Request) (*http.Response, error) {
result := []string{}
err := json.Unmarshal([]byte(r.URL.Query().Get(optionEnsureDBsExist)), &result)
return nil, &kivik.Error{HTTPStatus: http.StatusBadRequest, Err: err}
}),
options: map[string]interface{}{
optionEnsureDBsExist: "foo,bar,baz",
},
status: http.StatusBadRequest,
err: `Get "?http://example.com/_cluster_setup\?ensure_dbs_exist=foo%2Cbar%2Cbaz"?: invalid character 'o' in literal false \(expecting 'a'\)`,
})
tests.Add("ensure dbs", func(t *testing.T) interface{} {
return tst{
client: newCustomClient(func(r *http.Request) (*http.Response, error) {
input := r.URL.Query().Get(optionEnsureDBsExist)
expected := []string{"foo", "bar", "baz"}
result := []string{}
err := json.Unmarshal([]byte(input), &result)
if err != nil {
t.Fatalf("Failed to parse `%s`: %s\n", input, err)
}
if d := testy.DiffInterface(expected, result); d != nil {
t.Errorf("Unexpected db list:\n%s", d)
}
return &http.Response{
StatusCode: http.StatusOK,
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: ioutil.NopCloser(strings.NewReader(`{"state":"cluster_finished"}`)),
}, nil
}),
options: map[string]interface{}{
optionEnsureDBsExist: `["foo","bar","baz"]`,
},
expected: "cluster_finished",
}
})
tests.Run(t, func(t *testing.T, test tst) {
result, err := test.client.ClusterStatus(context.Background(), test.options)
testy.StatusErrorRE(t, test.err, test.status, err)
if result != test.expected {
t.Errorf("Unexpected result:\nExpected: %s\n Actual: %s\n", test.expected, result)
}
})
}
func TestClusterSetup(t *testing.T) {
type tst struct {
client *client
action interface{}
status int
err string
}
tests := testy.NewTable()
tests.Add("network error", tst{
client: newTestClient(nil, errors.New("network error")),
status: http.StatusBadGateway,
err: `Post "?http://example.com/_cluster_setup"?: network error`,
})
tests.Add("invalid action", tst{
client: newTestClient(nil, nil),
action: func() {},
status: http.StatusBadRequest,
err: `Post "?http://example.com/_cluster_setup"?: json: unsupported type: func()`,
})
tests.Add("success", func(t *testing.T) interface{} {
return tst{
client: newCustomClient(func(r *http.Request) (*http.Response, error) {
expected := map[string]interface{}{
"action": "finish_cluster",
}
result := map[string]interface{}{}
if err := json.NewDecoder(r.Body).Decode(&result); err != nil {
t.Fatal(err)
}
if d := testy.DiffInterface(expected, result); d != nil {
t.Errorf("Unexpected request body:\n%s\n", d)
}
return &http.Response{
StatusCode: http.StatusOK,
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: ioutil.NopCloser(strings.NewReader(`{"ok":true}`)),
}, nil
}),
action: map[string]interface{}{
"action": "finish_cluster",
},
}
})
tests.Add("already finished", tst{
client: newTestClient(&http.Response{
StatusCode: http.StatusBadRequest,
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: 63,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: ioutil.NopCloser(strings.NewReader(`{"error":"bad_request","reason":"Cluster is already finished"}`)),
}, nil),
action: map[string]interface{}{
"action": "finish_cluster",
},
status: http.StatusBadRequest,
err: "Bad Request: Cluster is already finished",
})
tests.Run(t, func(t *testing.T, test tst) {
err := test.client.ClusterSetup(context.Background(), test.action)
testy.StatusErrorRE(t, test.err, test.status, err)
})
}
func TestMembership(t *testing.T) {
type tt struct {
client *client
want *driver.ClusterMembership
status int
err string
}
tests := testy.NewTable()
tests.Add("network error", tt{
client: newTestClient(nil, errors.New("network error")),
status: http.StatusBadGateway,
err: `Get "?http://example.com/_membership"?: network error`,
})
tests.Add("success 2.3.1", func(t *testing.T) interface{} {
return tt{
client: newTestClient(&http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Cache-Control": []string{"must-revalidate"},
"Content-Length": []string{"382"},
"Content-Type": []string{"application/json"},
"Date": []string{"Fri, 10 Jul 2020 13:12:10 GMT"},
"Server": []string{"CouchDB/2.3.1 (Erlang OTP/19)"},
},
Body: ioutil.NopCloser(strings.NewReader(`{"all_nodes":["[email protected]","[email protected]","[email protected]"],"cluster_nodes":["[email protected]","[email protected]","[email protected]"]}
`)),
}, nil),
want: &driver.ClusterMembership{
AllNodes: []string{
},
ClusterNodes: []string{
},
},
}
})
tests.Run(t, func(t *testing.T, tt tt) {
got, err := tt.client.Membership(context.Background())
testy.StatusErrorRE(t, tt.err, tt.status, err)
if d := testy.DiffInterface(tt.want, got); d != nil {
t.Error(d)
}
})
}