Skip to content

Commit

Permalink
add tests for internal/kv package
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Winnicki committed Sep 26, 2017
1 parent 311b2bf commit 3f6808f
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ script:
- gometalinter --install --force
- gometalinter --vendor --fast --disable=gotype --disable=vetshadow --skip=mock ./...
- go get github.com/mattn/goveralls
- goveralls -race -service=travis-ci -ignore=**/mock/*
- goveralls -race -service=travis-ci -ignore=*/mock/*,*/*/mock/*
after_success:
- test -n "$TRAVIS_TAG" && curl -sL https://git.io/goreleaser | bash
notifications:
Expand Down
4 changes: 2 additions & 2 deletions cmd/event-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"go.uber.org/zap/zapcore"

"github.com/serverless/event-gateway/api"
"github.com/serverless/event-gateway/internal/embedded"
"github.com/serverless/event-gateway/internal/httpapi"
"github.com/serverless/event-gateway/internal/kv"
"github.com/serverless/event-gateway/internal/metrics"
"github.com/serverless/event-gateway/internal/sync"
)
Expand Down Expand Up @@ -61,7 +61,7 @@ func main() {
shutdownGuard := sync.NewShutdownGuard()

if *developmentMode {
kv.EmbedEtcd(*embedDataDir, *embedPeerAddr, *embedCliAddr, shutdownGuard)
embedded.EmbedEtcd(*embedDataDir, *embedPeerAddr, *embedCliAddr, shutdownGuard)
}

dbHostStrings := strings.Split(*dbHosts, ",")
Expand Down
2 changes: 1 addition & 1 deletion internal/kv/embed.go → internal/embedded/embedded.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kv
package embedded

import (
"net/url"
Expand Down
3 changes: 3 additions & 0 deletions internal/kv/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate mockgen -package mock -destination ./store.go github.com/serverless/libkv/store Store

package mock
157 changes: 157 additions & 0 deletions internal/kv/mock/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Automatically generated by MockGen. DO NOT EDIT!
// Source: github.com/serverless/libkv/store (interfaces: Store)

package mock

import (
gomock "github.com/golang/mock/gomock"
store "github.com/serverless/libkv/store"
)

// Mock of Store interface
type MockStore struct {
ctrl *gomock.Controller
recorder *_MockStoreRecorder
}

// Recorder for MockStore (not exported)
type _MockStoreRecorder struct {
mock *MockStore
}

func NewMockStore(ctrl *gomock.Controller) *MockStore {
mock := &MockStore{ctrl: ctrl}
mock.recorder = &_MockStoreRecorder{mock}
return mock
}

func (_m *MockStore) EXPECT() *_MockStoreRecorder {
return _m.recorder
}

func (_m *MockStore) AtomicDelete(_param0 string, _param1 *store.KVPair) (bool, error) {
ret := _m.ctrl.Call(_m, "AtomicDelete", _param0, _param1)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) AtomicDelete(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "AtomicDelete", arg0, arg1)
}

func (_m *MockStore) AtomicPut(_param0 string, _param1 []byte, _param2 *store.KVPair, _param3 *store.WriteOptions) (bool, *store.KVPair, error) {
ret := _m.ctrl.Call(_m, "AtomicPut", _param0, _param1, _param2, _param3)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(*store.KVPair)
ret2, _ := ret[2].(error)
return ret0, ret1, ret2
}

func (_mr *_MockStoreRecorder) AtomicPut(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "AtomicPut", arg0, arg1, arg2, arg3)
}

func (_m *MockStore) Close() {
_m.ctrl.Call(_m, "Close")
}

func (_mr *_MockStoreRecorder) Close() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Close")
}

func (_m *MockStore) Delete(_param0 string) error {
ret := _m.ctrl.Call(_m, "Delete", _param0)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockStoreRecorder) Delete(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Delete", arg0)
}

func (_m *MockStore) DeleteTree(_param0 string) error {
ret := _m.ctrl.Call(_m, "DeleteTree", _param0)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockStoreRecorder) DeleteTree(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteTree", arg0)
}

func (_m *MockStore) Exists(_param0 string) (bool, error) {
ret := _m.ctrl.Call(_m, "Exists", _param0)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) Exists(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Exists", arg0)
}

func (_m *MockStore) Get(_param0 string) (*store.KVPair, error) {
ret := _m.ctrl.Call(_m, "Get", _param0)
ret0, _ := ret[0].(*store.KVPair)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) Get(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Get", arg0)
}

func (_m *MockStore) List(_param0 string) ([]*store.KVPair, error) {
ret := _m.ctrl.Call(_m, "List", _param0)
ret0, _ := ret[0].([]*store.KVPair)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) List(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "List", arg0)
}

func (_m *MockStore) NewLock(_param0 string, _param1 *store.LockOptions) (store.Locker, error) {
ret := _m.ctrl.Call(_m, "NewLock", _param0, _param1)
ret0, _ := ret[0].(store.Locker)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) NewLock(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "NewLock", arg0, arg1)
}

func (_m *MockStore) Put(_param0 string, _param1 []byte, _param2 *store.WriteOptions) error {
ret := _m.ctrl.Call(_m, "Put", _param0, _param1, _param2)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockStoreRecorder) Put(arg0, arg1, arg2 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Put", arg0, arg1, arg2)
}

func (_m *MockStore) Watch(_param0 string, _param1 <-chan struct{}) (<-chan *store.KVPair, error) {
ret := _m.ctrl.Call(_m, "Watch", _param0, _param1)
ret0, _ := ret[0].(<-chan *store.KVPair)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) Watch(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Watch", arg0, arg1)
}

func (_m *MockStore) WatchTree(_param0 string, _param1 <-chan struct{}) (<-chan []*store.KVPair, error) {
ret := _m.ctrl.Call(_m, "WatchTree", _param0, _param1)
ret0, _ := ret[0].(<-chan []*store.KVPair)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockStoreRecorder) WatchTree(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "WatchTree", arg0, arg1)
}
52 changes: 26 additions & 26 deletions internal/kv/prefixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,56 @@ func NewPrefixedStore(root string, kv store.Store) *PrefixedStore {
}

// Put passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) Put(key string, value []byte, options *store.WriteOptions) error {
return rfs.kv.Put(rfs.root+key, value, options)
func (ps *PrefixedStore) Put(key string, value []byte, options *store.WriteOptions) error {
return ps.kv.Put(ps.root+key, value, options)
}

// Get passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) Get(key string) (*store.KVPair, error) {
return rfs.kv.Get(rfs.root + key)
func (ps *PrefixedStore) Get(key string) (*store.KVPair, error) {
return ps.kv.Get(ps.root + key)
}

// Delete passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) Delete(key string) error {
return rfs.kv.Delete(rfs.root + key)
func (ps *PrefixedStore) Delete(key string) error {
return ps.kv.Delete(ps.root + key)
}

// Exists passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) Exists(key string) (bool, error) {
return rfs.kv.Exists(rfs.root + key)
func (ps *PrefixedStore) Exists(key string) (bool, error) {
return ps.kv.Exists(ps.root + key)
}

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

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

// NewLock passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
return rfs.kv.NewLock(rfs.root+key, options)
func (ps *PrefixedStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
return ps.kv.NewLock(ps.root+key, options)
}

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

unprefixed := []*store.KVPair{}
for _, kv := range prefixed {
// Is directory
if kv.Key == rfs.root {
if kv.Key == ps.root {
continue
}

unprefixed = append(unprefixed, &store.KVPair{
Key: strings.TrimPrefix(kv.Key, rfs.root),
Key: strings.TrimPrefix(kv.Key, ps.root),
Value: kv.Value,
LastIndex: kv.LastIndex,
})
Expand All @@ -83,21 +83,21 @@ func (rfs *PrefixedStore) List(directory string) ([]*store.KVPair, error) {
}

// DeleteTree passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) DeleteTree(directory string) error {
return rfs.kv.DeleteTree(rfs.root + directory)
func (ps *PrefixedStore) DeleteTree(directory string) error {
return ps.kv.DeleteTree(ps.root + directory)
}

// AtomicPut passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
return rfs.kv.AtomicPut(rfs.root+key, value, previous, options)
func (ps *PrefixedStore) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
return ps.kv.AtomicPut(ps.root+key, value, previous, options)
}

// AtomicDelete passes requests to the underlying libkv implementation, appending the root to paths for isolation.
func (rfs *PrefixedStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
return rfs.kv.AtomicDelete(rfs.root+key, previous)
func (ps *PrefixedStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
return ps.kv.AtomicDelete(ps.root+key, previous)
}

// Close closes the underlying libkv client.
func (rfs *PrefixedStore) Close() {
rfs.kv.Close()
func (ps *PrefixedStore) Close() {
ps.kv.Close()
}
45 changes: 45 additions & 0 deletions internal/kv/prefixed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package kv

import (
"errors"
"testing"

"github.com/golang/mock/gomock"
"github.com/serverless/event-gateway/functions/mock"
"github.com/serverless/libkv/store"
"github.com/stretchr/testify/assert"
)

func TestPrefixedStoreList(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

kvs := []*store.KVPair{
&store.KVPair{Key: "testroot/", Value: []byte(nil)},
&store.KVPair{Key: "testroot/testdir/key1", Value: []byte("value1")},
&store.KVPair{Key: "testroot/testdir/key2", Value: []byte("value2")},
}
kv := mock.NewMockStore(ctrl)
kv.EXPECT().List("testroot/testdir").Return(kvs, nil)
ps := NewPrefixedStore("testroot", kv)

values, err := ps.List("testdir")
assert.Nil(t, err)
assert.Equal(t, []*store.KVPair{
&store.KVPair{Key: "testdir/key1", Value: []byte("value1")},
&store.KVPair{Key: "testdir/key2", Value: []byte("value2")},
}, values)
}

func TestPrefixedStoreList_Error(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

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

values, err := ps.List("key")
assert.Nil(t, values)
assert.EqualError(t, err, "KV error")
}
Loading

0 comments on commit 3f6808f

Please sign in to comment.