This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_test.go
175 lines (167 loc) · 5.03 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
package couchdb
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"
"gitlab.com/flimzy/testy"
kivik "github.com/go-kivik/kivik/v4"
)
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)
})
}