-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_test.go
239 lines (215 loc) · 5.42 KB
/
client_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
package main
import (
"github.com/coreos/go-etcd/etcd"
"gopkg.in/BlueDragonX/go-settings.v1"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
"time"
)
const (
validURI = "http://127.0.0.1:4001"
invalidURI = "http://127.0.0.1:9999"
)
type MockClient struct {
WaitFor time.Duration
GetValue map[string]interface{}
GetError error
Changes chan string
}
func (mc *MockClient) Wait(stop chan bool) bool {
select {
case <-time.After(mc.WaitFor):
case <-stop:
return false
}
return true
}
func (mc *MockClient) Get(keys []string) (map[string]interface{}, error) {
return mc.GetValue, mc.GetError
}
func (mc *MockClient) Watch(prefixes []string, changes chan string, stop chan bool) {
mc.Changes = changes
<-stop
}
func getEtcdClient(t *testing.T, uri string) *EtcdClient {
config := settings.Settings{}
if uri != "" {
config.Set("uris", []string{uri})
}
config.Set("prefix", "test")
client, err := NewEtcdClient(&config)
if err != nil {
t.Fatal(err)
}
return client
}
func etcdClientSetUp(t *testing.T, rawClient *etcd.Client) map[string]interface{} {
var err error
if _, err = rawClient.SetDir("test", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/index", "1", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.SetDir("test/values", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/values/a", "aye", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/values/b", "bee", 0); err != nil {
t.Fatal(err)
}
want := make(map[string]interface{})
wantValues := make(map[string]interface{})
wantValues["a"] = "aye"
wantValues["b"] = "bee"
want["values"] = wantValues
want["index"] = "1"
return want
}
func etcdClientTearDown(t *testing.T, rawClient *etcd.Client) {
if _, err := rawClient.Delete("test", true); err != nil {
t.Fatal(err)
}
}
// Ensure the etcd server does something when we knock.
func TestEtcdClientRaw(t *testing.T) {
if resp, err := http.Get(validURI + "/v2/keys/"); err == nil {
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err == nil {
t.Log(string(body))
} else {
t.Error(err)
}
} else {
t.Error(err)
}
if _, err := http.Get(invalidURI + "/v2/keys/"); err == nil {
t.Errorf("no error raised on invalid URI")
} else {
t.Logf("%v: %v", reflect.TypeOf(err), err)
}
}
// Ensure Get returns the correct values.
func TestEtcdClientGet(t *testing.T) {
client := getEtcdClient(t, validURI)
rawClient := client.client
// set some keys to play with
data := etcdClientSetUp(t, rawClient)
defer etcdClientTearDown(t, rawClient)
type getCheck struct {
keys []string
want interface{}
}
getChecks := []getCheck{
{
[]string{"test/values"},
map[string]interface{}{
"test": map[string]interface{}{
"values": data["values"],
},
},
},
{
[]string{"test/values", "test/index"},
map[string]interface{}{
"test": map[string]interface{}{
"values": data["values"],
"index": data["index"],
},
},
},
{
[]string{"test/values", "test/index", "test/missing"},
map[string]interface{}{
"test": map[string]interface{}{
"values": data["values"],
"index": data["index"],
},
},
},
{
[]string{"/test/values"},
map[string]interface{}{
"test": map[string]interface{}{
"values": data["values"],
},
},
},
{
[]string{"/test/values/"},
map[string]interface{}{
"test": map[string]interface{}{
"values": data["values"],
},
},
},
}
for _, check := range getChecks {
if have, err := client.Get(check.keys); err == nil {
if !reflect.DeepEqual(check.want, have) {
t.Errorf("keys %v are invalid: %v != %v", check.keys, check.want, have)
}
} else {
t.Errorf("keys %v not retrieved: %s\n", check.keys, err)
}
}
}
// Ensure the client returns properly when get fails.
func TestEtcdClientGetFailure(t *testing.T) {
// missing key should return an empty map
client := getEtcdClient(t, validURI)
if value, err := client.Get([]string{"test/values"}); err == nil {
t.Logf("received a value from etcd: %v", value)
} else {
t.Errorf("received %v: %v", reflect.TypeOf(err), err)
}
}
// Ensure the client watches properly.
func TestEtcdClientWatch(t *testing.T) {
client := getEtcdClient(t, validURI)
rawClient := client.client
etcdClientSetUp(t, rawClient)
defer etcdClientTearDown(t, rawClient)
// server down should return an error
join := make(chan bool)
changes := make(chan string)
stop := make(chan bool)
go func() {
client.Watch([]string{"test/index"}, changes, stop)
close(join)
}()
time.Sleep(50 * time.Millisecond)
go func() {
rawClient.Set("/test/index", "2", 0)
}()
if key := <-changes; key != "test/index" {
t.Errorf("changed key is '%s' not 'test/index'", key)
} else {
t.Logf("changed key is '%s'\n", key)
}
stop <- true
<-join
}
// Ensure URIs with and without trailing slashes work.
func TestEtcdClientTrailingSlash(t *testing.T) {
client := getEtcdClient(t, validURI)
etcdClientSetUp(t, client.client)
defer etcdClientTearDown(t, client.client)
// test without a trailing slash
uri := strings.TrimRight(validURI, "/")
client = getEtcdClient(t, uri)
if _, err := client.Get([]string{"test/values"}); err != nil {
t.Errorf("URI without trailing slash failed: %s", err)
}
// test with a trailing slash
uri = uri + "/"
client = getEtcdClient(t, uri)
if _, err := client.Get([]string{"test/values"}); err != nil {
t.Errorf("URI with trailing slash failed: %s", err)
}
}