Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit e300c56

Browse files
authored
update libkv & etcd packages (#335)
1 parent c1740e1 commit e300c56

File tree

12 files changed

+113
-117
lines changed

12 files changed

+113
-117
lines changed

Gopkg.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@
2727

2828
[[constraint]]
2929
name = "github.com/coreos/etcd"
30-
version = "3.1.8"
30+
version = "3.2.9"
3131

3232
[[constraint]]
3333
name = "github.com/coreos/pkg"
3434
version = "3.0.0"
3535

36-
[[constraint]]
37-
name = "github.com/docker/libkv"
38-
version = "0.2.1"
39-
4036
[[constraint]]
4137
name = "github.com/golang/mock"
4238

functions/functions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (f *Functions) RegisterFunction(fn *Function) (*Function, error) {
2323
return nil, err
2424
}
2525

26-
_, err := f.DB.Get(string(fn.ID))
26+
_, err := f.DB.Get(string(fn.ID), &store.ReadOptions{Consistent: true})
2727
if err == nil {
2828
return nil, &ErrAlreadyRegistered{fn.ID}
2929
}
@@ -45,7 +45,7 @@ func (f *Functions) RegisterFunction(fn *Function) (*Function, error) {
4545

4646
// UpdateFunction updates function configuration.
4747
func (f *Functions) UpdateFunction(fn *Function) (*Function, error) {
48-
_, err := f.DB.Get(string(fn.ID))
48+
_, err := f.DB.Get(string(fn.ID), &store.ReadOptions{Consistent: true})
4949
if err != nil {
5050
return nil, &ErrNotFound{fn.ID}
5151
}
@@ -71,7 +71,7 @@ func (f *Functions) UpdateFunction(fn *Function) (*Function, error) {
7171

7272
// GetFunction returns function from configuration.
7373
func (f *Functions) GetFunction(id FunctionID) (*Function, error) {
74-
kv, err := f.DB.Get(string(id))
74+
kv, err := f.DB.Get(string(id), &store.ReadOptions{Consistent: true})
7575
if err != nil {
7676
return nil, &ErrNotFound{id}
7777
}
@@ -89,7 +89,7 @@ func (f *Functions) GetFunction(id FunctionID) (*Function, error) {
8989
func (f *Functions) GetAllFunctions() ([]*Function, error) {
9090
fns := []*Function{}
9191

92-
kvs, err := f.DB.List("")
92+
kvs, err := f.DB.List("", &store.ReadOptions{Consistent: true})
9393
if err != nil {
9494
return nil, err
9595
}

functions/functions_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestRegisterFunction(t *testing.T) {
1616
defer ctrl.Finish()
1717

1818
db := mock.NewMockStore(ctrl)
19-
db.EXPECT().Get("testid").Return(nil, errors.New("KV func not found"))
19+
db.EXPECT().Get("testid", &store.ReadOptions{Consistent: true}).Return(nil, errors.New("KV func not found"))
2020
db.EXPECT().Put("testid", []byte(`{"functionId":"testid","provider":{"type":"http","url":"http://example.com"}}`), nil).Return(nil)
2121
service := &Functions{DB: db, Log: zap.NewNop()}
2222

@@ -42,7 +42,7 @@ func TestRegisterFunction_AlreadyExistsError(t *testing.T) {
4242
defer ctrl.Finish()
4343

4444
db := mock.NewMockStore(ctrl)
45-
db.EXPECT().Get("testid").Return(nil, nil)
45+
db.EXPECT().Get("testid", gomock.Any()).Return(nil, nil)
4646
service := &Functions{DB: db, Log: zap.NewNop()}
4747

4848
_, err := service.RegisterFunction(&Function{ID: "testid", Provider: &Provider{Type: HTTPEndpoint, URL: "http://example.com"}})
@@ -55,7 +55,7 @@ func TestRegisterFunction_PutError(t *testing.T) {
5555
defer ctrl.Finish()
5656

5757
db := mock.NewMockStore(ctrl)
58-
db.EXPECT().Get("testid").Return(nil, errors.New("KV func not found"))
58+
db.EXPECT().Get("testid", gomock.Any()).Return(nil, errors.New("KV func not found"))
5959
db.EXPECT().Put("testid", []byte(`{"functionId":"testid","provider":{"type":"http","url":"http://example.com"}}`), nil).Return(errors.New("KV put error"))
6060
service := &Functions{DB: db, Log: zap.NewNop()}
6161

@@ -69,7 +69,7 @@ func TestUpdateFunction(t *testing.T) {
6969
defer ctrl.Finish()
7070

7171
db := mock.NewMockStore(ctrl)
72-
db.EXPECT().Get("testid").Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
72+
db.EXPECT().Get("testid", &store.ReadOptions{Consistent: true}).Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
7373
db.EXPECT().Put("testid", []byte(`{"functionId":"testid","provider":{"type":"http","url":"http://example1.com"}}`), nil).Return(nil)
7474
service := &Functions{DB: db, Log: zap.NewNop()}
7575

@@ -83,7 +83,7 @@ func TestUpdateFunction_ValidationError(t *testing.T) {
8383
defer ctrl.Finish()
8484

8585
db := mock.NewMockStore(ctrl)
86-
db.EXPECT().Get("testid").Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
86+
db.EXPECT().Get("testid", gomock.Any()).Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
8787
service := &Functions{DB: db, Log: zap.NewNop()}
8888

8989
_, err := service.UpdateFunction(&Function{ID: "testid", Provider: &Provider{Type: HTTPEndpoint}})
@@ -96,7 +96,7 @@ func TestUpdateFunction_NotFoundError(t *testing.T) {
9696
defer ctrl.Finish()
9797

9898
db := mock.NewMockStore(ctrl)
99-
db.EXPECT().Get("testid").Return(nil, errors.New("KV not found"))
99+
db.EXPECT().Get("testid", gomock.Any()).Return(nil, errors.New("KV not found"))
100100
service := &Functions{DB: db, Log: zap.NewNop()}
101101

102102
_, err := service.UpdateFunction(&Function{ID: "testid", Provider: &Provider{Type: HTTPEndpoint, URL: "http://example.com"}})
@@ -109,7 +109,7 @@ func TestUpdateFunction_PutError(t *testing.T) {
109109
defer ctrl.Finish()
110110

111111
db := mock.NewMockStore(ctrl)
112-
db.EXPECT().Get("testid").Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
112+
db.EXPECT().Get("testid", gomock.Any()).Return(&store.KVPair{Value: []byte(`{"functionId":"testid", "provider":{"type":"http","url":"http://example.com"}}`)}, nil)
113113
db.EXPECT().Put("testid", []byte(`{"functionId":"testid","provider":{"type":"http","url":"http://example1.com"}}`), nil).Return(errors.New("KV put error"))
114114
service := &Functions{DB: db, Log: zap.NewNop()}
115115

@@ -123,7 +123,7 @@ func TestGetFunction(t *testing.T) {
123123
defer ctrl.Finish()
124124

125125
db := mock.NewMockStore(ctrl)
126-
db.EXPECT().Get("testid").Return(&store.KVPair{Value: []byte(`{"functionId":"testid"}`)}, nil)
126+
db.EXPECT().Get("testid", &store.ReadOptions{Consistent: true}).Return(&store.KVPair{Value: []byte(`{"functionId":"testid"}`)}, nil)
127127
service := &Functions{DB: db, Log: zap.NewNop()}
128128

129129
function, _ := service.GetFunction(FunctionID("testid"))
@@ -136,7 +136,7 @@ func TestGetFunction_NotFound(t *testing.T) {
136136
defer ctrl.Finish()
137137

138138
db := mock.NewMockStore(ctrl)
139-
db.EXPECT().Get("testid").Return(nil, errors.New("KV func not found"))
139+
db.EXPECT().Get("testid", gomock.Any()).Return(nil, errors.New("KV func not found"))
140140
service := &Functions{DB: db, Log: zap.NewNop()}
141141

142142
_, err := service.GetFunction(FunctionID("testid"))
@@ -153,7 +153,7 @@ func TestGetAllFunctions(t *testing.T) {
153153
&store.KVPair{Value: []byte(`{"functionId":"f2"}`)},
154154
}
155155
db := mock.NewMockStore(ctrl)
156-
db.EXPECT().List("").Return(kvs, nil)
156+
db.EXPECT().List("", &store.ReadOptions{Consistent: true}).Return(kvs, nil)
157157
service := &Functions{DB: db, Log: zap.NewNop()}
158158

159159
list, _ := service.GetAllFunctions()
@@ -166,7 +166,7 @@ func TestGetAllFunctions_ListError(t *testing.T) {
166166
defer ctrl.Finish()
167167

168168
db := mock.NewMockStore(ctrl)
169-
db.EXPECT().List("").Return([]*store.KVPair{}, errors.New("KV list err"))
169+
db.EXPECT().List("", gomock.Any()).Return([]*store.KVPair{}, errors.New("KV list err"))
170170
service := &Functions{DB: db, Log: zap.NewNop()}
171171

172172
_, err := service.GetAllFunctions()

internal/kv/mock/store.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,37 @@ func (_mr *_MockStoreRecorder) DeleteTree(arg0 interface{}) *gomock.Call {
8080
return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteTree", arg0)
8181
}
8282

83-
func (_m *MockStore) Exists(_param0 string) (bool, error) {
84-
ret := _m.ctrl.Call(_m, "Exists", _param0)
83+
func (_m *MockStore) Exists(_param0 string, _param1 *store.ReadOptions) (bool, error) {
84+
ret := _m.ctrl.Call(_m, "Exists", _param0, _param1)
8585
ret0, _ := ret[0].(bool)
8686
ret1, _ := ret[1].(error)
8787
return ret0, ret1
8888
}
8989

90-
func (_mr *_MockStoreRecorder) Exists(arg0 interface{}) *gomock.Call {
91-
return _mr.mock.ctrl.RecordCall(_mr.mock, "Exists", arg0)
90+
func (_mr *_MockStoreRecorder) Exists(arg0, arg1 interface{}) *gomock.Call {
91+
return _mr.mock.ctrl.RecordCall(_mr.mock, "Exists", arg0, arg1)
9292
}
9393

94-
func (_m *MockStore) Get(_param0 string) (*store.KVPair, error) {
95-
ret := _m.ctrl.Call(_m, "Get", _param0)
94+
func (_m *MockStore) Get(_param0 string, _param1 *store.ReadOptions) (*store.KVPair, error) {
95+
ret := _m.ctrl.Call(_m, "Get", _param0, _param1)
9696
ret0, _ := ret[0].(*store.KVPair)
9797
ret1, _ := ret[1].(error)
9898
return ret0, ret1
9999
}
100100

101-
func (_mr *_MockStoreRecorder) Get(arg0 interface{}) *gomock.Call {
102-
return _mr.mock.ctrl.RecordCall(_mr.mock, "Get", arg0)
101+
func (_mr *_MockStoreRecorder) Get(arg0, arg1 interface{}) *gomock.Call {
102+
return _mr.mock.ctrl.RecordCall(_mr.mock, "Get", arg0, arg1)
103103
}
104104

105-
func (_m *MockStore) List(_param0 string) ([]*store.KVPair, error) {
106-
ret := _m.ctrl.Call(_m, "List", _param0)
105+
func (_m *MockStore) List(_param0 string, _param1 *store.ReadOptions) ([]*store.KVPair, error) {
106+
ret := _m.ctrl.Call(_m, "List", _param0, _param1)
107107
ret0, _ := ret[0].([]*store.KVPair)
108108
ret1, _ := ret[1].(error)
109109
return ret0, ret1
110110
}
111111

112-
func (_mr *_MockStoreRecorder) List(arg0 interface{}) *gomock.Call {
113-
return _mr.mock.ctrl.RecordCall(_mr.mock, "List", arg0)
112+
func (_mr *_MockStoreRecorder) List(arg0, arg1 interface{}) *gomock.Call {
113+
return _mr.mock.ctrl.RecordCall(_mr.mock, "List", arg0, arg1)
114114
}
115115

116116
func (_m *MockStore) NewLock(_param0 string, _param1 *store.LockOptions) (store.Locker, error) {
@@ -134,24 +134,24 @@ func (_mr *_MockStoreRecorder) Put(arg0, arg1, arg2 interface{}) *gomock.Call {
134134
return _mr.mock.ctrl.RecordCall(_mr.mock, "Put", arg0, arg1, arg2)
135135
}
136136

137-
func (_m *MockStore) Watch(_param0 string, _param1 <-chan struct{}) (<-chan *store.KVPair, error) {
138-
ret := _m.ctrl.Call(_m, "Watch", _param0, _param1)
137+
func (_m *MockStore) Watch(_param0 string, _param1 <-chan struct{}, _param2 *store.ReadOptions) (<-chan *store.KVPair, error) {
138+
ret := _m.ctrl.Call(_m, "Watch", _param0, _param1, _param2)
139139
ret0, _ := ret[0].(<-chan *store.KVPair)
140140
ret1, _ := ret[1].(error)
141141
return ret0, ret1
142142
}
143143

144-
func (_mr *_MockStoreRecorder) Watch(arg0, arg1 interface{}) *gomock.Call {
145-
return _mr.mock.ctrl.RecordCall(_mr.mock, "Watch", arg0, arg1)
144+
func (_mr *_MockStoreRecorder) Watch(arg0, arg1, arg2 interface{}) *gomock.Call {
145+
return _mr.mock.ctrl.RecordCall(_mr.mock, "Watch", arg0, arg1, arg2)
146146
}
147147

148-
func (_m *MockStore) WatchTree(_param0 string, _param1 <-chan struct{}) (<-chan []*store.KVPair, error) {
149-
ret := _m.ctrl.Call(_m, "WatchTree", _param0, _param1)
148+
func (_m *MockStore) WatchTree(_param0 string, _param1 <-chan struct{}, _param2 *store.ReadOptions) (<-chan []*store.KVPair, error) {
149+
ret := _m.ctrl.Call(_m, "WatchTree", _param0, _param1, _param2)
150150
ret0, _ := ret[0].(<-chan []*store.KVPair)
151151
ret1, _ := ret[1].(error)
152152
return ret0, ret1
153153
}
154154

155-
func (_mr *_MockStoreRecorder) WatchTree(arg0, arg1 interface{}) *gomock.Call {
156-
return _mr.mock.ctrl.RecordCall(_mr.mock, "WatchTree", arg0, arg1)
155+
func (_mr *_MockStoreRecorder) WatchTree(arg0, arg1, arg2 interface{}) *gomock.Call {
156+
return _mr.mock.ctrl.RecordCall(_mr.mock, "WatchTree", arg0, arg1, arg2)
157157
}

internal/kv/prefixed.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (ps *PrefixedStore) Put(key string, value []byte, options *store.WriteOptio
2929
}
3030

3131
// Get passes requests to the underlying libkv implementation, appending the root to paths for isolation.
32-
func (ps *PrefixedStore) Get(key string) (*store.KVPair, error) {
33-
return ps.kv.Get(ps.root + key)
32+
func (ps *PrefixedStore) Get(key string, options *store.ReadOptions) (*store.KVPair, error) {
33+
return ps.kv.Get(ps.root+key, options)
3434
}
3535

3636
// Delete passes requests to the underlying libkv implementation, appending the root to paths for isolation.
@@ -39,18 +39,18 @@ func (ps *PrefixedStore) Delete(key string) error {
3939
}
4040

4141
// Exists passes requests to the underlying libkv implementation, appending the root to paths for isolation.
42-
func (ps *PrefixedStore) Exists(key string) (bool, error) {
43-
return ps.kv.Exists(ps.root + key)
42+
func (ps *PrefixedStore) Exists(key string, options *store.ReadOptions) (bool, error) {
43+
return ps.kv.Exists(ps.root+key, options)
4444
}
4545

4646
// Watch passes requests to the underlying libkv implementation, appending the root to paths for isolation.
47-
func (ps *PrefixedStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
48-
return ps.kv.Watch(ps.root+key, stopCh)
47+
func (ps *PrefixedStore) Watch(key string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan *store.KVPair, error) {
48+
return ps.kv.Watch(ps.root+key, stopCh, options)
4949
}
5050

5151
// WatchTree passes requests to the underlying libkv implementation, appending the root to paths for isolation.
52-
func (ps *PrefixedStore) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
53-
return ps.kv.WatchTree(ps.root+directory, stopCh)
52+
func (ps *PrefixedStore) WatchTree(directory string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan []*store.KVPair, error) {
53+
return ps.kv.WatchTree(ps.root+directory, stopCh, options)
5454
}
5555

5656
// NewLock passes requests to the underlying libkv implementation, appending the root to paths for isolation.
@@ -59,8 +59,8 @@ func (ps *PrefixedStore) NewLock(key string, options *store.LockOptions) (store.
5959
}
6060

6161
// List passes requests to the underlying libkv implementation, appending the root to paths for isolation.
62-
func (ps *PrefixedStore) List(directory string) ([]*store.KVPair, error) {
63-
prefixed, err := ps.kv.List(ps.root + directory)
62+
func (ps *PrefixedStore) List(directory string, options *store.ReadOptions) ([]*store.KVPair, error) {
63+
prefixed, err := ps.kv.List(ps.root+directory, options)
6464
if err != nil {
6565
return nil, err
6666
}

internal/kv/prefixed_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"github.com/golang/mock/gomock"
8-
"github.com/serverless/event-gateway/functions/mock"
8+
"github.com/serverless/event-gateway/internal/kv/mock"
99
"github.com/serverless/libkv/store"
1010
"github.com/stretchr/testify/assert"
1111
)
@@ -20,10 +20,10 @@ func TestPrefixedStoreList(t *testing.T) {
2020
&store.KVPair{Key: "testroot/testdir/key2", Value: []byte("value2")},
2121
}
2222
kv := mock.NewMockStore(ctrl)
23-
kv.EXPECT().List("testroot/testdir").Return(kvs, nil)
23+
kv.EXPECT().List("testroot/testdir", &store.ReadOptions{Consistent: true}).Return(kvs, nil)
2424
ps := NewPrefixedStore("testroot", kv)
2525

26-
values, err := ps.List("testdir")
26+
values, err := ps.List("testdir", &store.ReadOptions{Consistent: true})
2727
assert.Nil(t, err)
2828
assert.Equal(t, []*store.KVPair{
2929
&store.KVPair{Key: "testdir/key1", Value: []byte("value1")},
@@ -36,10 +36,10 @@ func TestPrefixedStoreList_Error(t *testing.T) {
3636
defer ctrl.Finish()
3737

3838
kv := mock.NewMockStore(ctrl)
39-
kv.EXPECT().List("testroot/key").Return(nil, errors.New("KV error"))
39+
kv.EXPECT().List("testroot/key", nil).Return(nil, errors.New("KV error"))
4040
ps := NewPrefixedStore("testroot", kv)
4141

42-
values, err := ps.List("key")
42+
values, err := ps.List("key", nil)
4343
assert.Nil(t, values)
4444
assert.EqualError(t, err, "KV error")
4545
}

internal/kv/watcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (w *Watcher) watchRoot(outgoingEvents chan event, shutdown chan struct{}) {
9393
}
9494

9595
// populate directory if it doesn't exist
96-
exists, err := w.kv.Exists(w.path)
96+
exists, err := w.kv.Exists(w.path, nil)
9797
if err != nil {
9898
w.log.Error("Could not access database.",
9999
zap.String("event", "db"),
@@ -114,7 +114,7 @@ func (w *Watcher) watchRoot(outgoingEvents chan event, shutdown chan struct{}) {
114114
}
115115

116116
// create watch chan for this directory
117-
events, err := w.kv.WatchTree(w.path, shutdown)
117+
events, err := w.kv.WatchTree(w.path, shutdown, nil)
118118
if err != nil {
119119
w.log.Error("Could not watch directory.",
120120
zap.String("event", "db"),

subscriptions/mock/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
//go:generate mockgen -package mock -destination ./store.go github.com/docker/libkv/store Store
1+
//go:generate mockgen -package mock -destination ./store.go github.com/serverless /libkv/store Store
22

33
package mock

0 commit comments

Comments
 (0)