-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
97 lines (79 loc) · 2.38 KB
/
store_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
package main
import (
"errors"
"testing"
. "github.com/aandryashin/matchers"
"github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
type apiGetError struct {
client.KeysAPI
}
func (api apiGetError) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
return nil, errors.New("store is broken")
}
func TestGetPanic(t *testing.T) {
api := apiGetError{}
store := &EtcdStore{"/", client.KeysAPI(api)}
defer func() {
e := recover()
AssertThat(t, e.(error).Error(), EqualTo{"store is broken"})
}()
store.Get(0)
}
type apiGetErrorKeyNotFound struct {
client.KeysAPI
}
func (api apiGetErrorKeyNotFound) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
return nil, client.Error{Code: client.ErrorCodeKeyNotFound}
}
func TestGetKeyNotFound(t *testing.T) {
api := apiGetErrorKeyNotFound{}
store := &EtcdStore{"/", client.KeysAPI(api)}
_, ok := store.Get(0)
AssertThat(t, ok, Is{false})
}
type apiGetValue struct {
client.KeysAPI
}
func (api apiGetValue) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
return &client.Response{Node: &client.Node{Value: "value"}}, nil
}
func TestGet(t *testing.T) {
api := apiGetValue{}
store := &EtcdStore{"/", client.KeysAPI(api)}
v, ok := store.Get(0)
AssertThat(t, ok, Is{true})
AssertThat(t, v, EqualTo{"value"})
}
type apiCreateInOrderError struct {
client.KeysAPI
}
func (api apiCreateInOrderError) CreateInOrder(ctx context.Context, dir, value string, opts *client.CreateInOrderOptions) (*client.Response, error) {
return nil, errors.New("store is broken")
}
func TestPutError(t *testing.T) {
api := apiCreateInOrderError{}
store := &EtcdStore{"/", client.KeysAPI(api)}
defer func() {
e := recover()
AssertThat(t, e.(error).Error(), EqualTo{"store is broken"})
}()
store.Put("")
}
type apiCreateInOrderValue struct {
client.KeysAPI
}
func (api apiCreateInOrderValue) CreateInOrder(ctx context.Context, dir, value string, opts *client.CreateInOrderOptions) (*client.Response, error) {
return &client.Response{Index: 12345}, nil
}
func TestPutValue(t *testing.T) {
api := apiCreateInOrderValue{}
store := &EtcdStore{"/", client.KeysAPI(api)}
k := store.Put("")
AssertThat(t, k, EqualTo{uint64(12345)})
}
func TestNewEtcdStore(t *testing.T) {
store := NewEtcdStore("/", nil)
AssertThat(t, store.dir, EqualTo{"/"})
}