-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd_test.go
123 lines (99 loc) · 2.99 KB
/
etcd_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
package etcd
import (
"testing"
"github.com/Masterminds/cookoo"
"github.com/coreos/go-etcd/etcd"
)
func TestInterfaces(t *testing.T) {
// Throughout the codebase, we assume that our interfaces match what the
// etcd client provides. This is a canary to verify.
cli := etcd.NewClient([]string{"http://localhost:4001"})
var _ Getter = cli
var _ = cli
var _ GetterSetter = cli
}
func TestCreateClient(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
reg.Route("test", "Test route").
Does(CreateClient, "res").Using("url").WithDefault("localhost:4100")
if err := router.HandleRequest("test", cxt, true); err != nil {
t.Error(err)
}
// All we really want to know is whether we got a valid client back.
_ = cxt.Get("res", nil).(*etcd.Client)
}
func TestGet(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
reg.Route("test", "Test route").
Does(Get, "res").
Using("client").WithDefault(&stubClient{}).
Using("path").WithDefault("/")
err := router.HandleRequest("test", cxt, true)
if err != nil {
t.Error(err)
}
if res := cxt.Get("res", nil); res == nil {
t.Error("Expected an *etcd.Response, not nil.")
} else if tt, ok := res.(*etcd.Response); !ok {
t.Errorf("Expected instance of *etcd.Response. Got %T", tt)
}
}
func TestSet(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
reg.Route("test", "Test route").
Does(Set, "res").
Using("client").WithDefault(&stubClient{}).
Using("key").WithDefault("Hello").
Using("value").WithDefault("World")
err := router.HandleRequest("test", cxt, true)
if err != nil {
t.Error(err)
}
if res := cxt.Get("res", nil); res == nil {
t.Error("Expected an *etcd.Response, not nil.")
} else if tt, ok := res.(*etcd.Response); !ok {
t.Errorf("Expected instance of *etcd.Response. Got %T", tt)
}
}
func TestMakeDir(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
reg.Route("test", "Test route").
Does(MakeDir, "res").
Using("client").WithDefault(&stubClient{}).
Using("path").WithDefault("/deis/users/foo")
err := router.HandleRequest("test", cxt, true)
if err != nil {
t.Error(err)
}
if res := cxt.Get("res", nil); res == nil {
t.Error("Expected an *etcd.Response, not nil.")
} else if tt, ok := res.(*etcd.Response); !ok {
t.Errorf("Expected instance of *etcd.Response. Got %T", tt)
}
}
// stubClient implements EtcdGetter and EtcdDirCreator
type stubClient struct {
}
func (s *stubClient) Get(key string, sort, recurse bool) (*etcd.Response, error) {
return s.response("get"), nil
}
func (s *stubClient) CreateDir(key string, ttl uint64) (*etcd.Response, error) {
return s.response("createdir"), nil
}
func (s *stubClient) Set(key string, value string, ttl uint64) (*etcd.Response, error) {
return s.response("set"), nil
}
func (s *stubClient) response(a string) *etcd.Response {
// This is totally fake data. It may or may not reflect what etcd really
// returns.
return &etcd.Response{
Action: a,
EtcdIndex: 1,
RaftIndex: 1,
RaftTerm: 1,
Node: &etcd.Node{
Dir: true,
Key: "/foo",
},
}
}