mockEtcdClient

package module
v0.0.0-...-95c2820 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2017 License: MIT Imports: 5 Imported by: 0

README

mockEtcdClient

Mock library to simulate interactions with etcd as if by the github.com/coreos/etcd/client library.

Starting with a faked KeysAPI, tests look like this (from mock_test.go):
package mockEtcdClient

import (
	"errors"
	"fmt"
	"testing"

	"github.com/coreos/etcd/client"
	. "github.com/onsi/gomega"
	"golang.org/x/net/context"
)

func TestExpectGet(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock
	mock.ExpectGet("/test/key").WillReturnValue("test value")

	resp, err := kapi.Get(context.Background(), "/test/key", nil)

	Expect(err).NotTo(HaveOccurred())
	Expect(resp).NotTo(BeNil())
	Expect(resp.Node).NotTo(BeNil())
	Expect(resp.Node.Key).To(Equal("/test/key"))
	Expect(resp.Node.Value).To(Equal("test value"))
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectErr(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock
	key := "/test/key"
	mock.ExpectGet(key).WillReturnError(errors.New(fmt.Sprintf("100: Key not found (%v) [39881395]", key)))

	resp, err := kapi.Get(context.Background(), "/test/key", nil)
	Expect(err).To(HaveOccurred())
	Expect(err.Error()).To(Equal(fmt.Sprintf("100: Key not found (%v) [39881395]", key)))
	Expect(resp).To(BeNil())
}

func TestExpectSet(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock

	mock.ExpectSet("/some/key", "some value")
	resp, err := kapi.Set(context.Background(), "/some/key", "some value", nil)
	Expect(err).NotTo(HaveOccurred())
	Expect(resp.Node).NotTo(BeNil())
	Expect(resp.Node.Key).To(Equal("/some/key"))
	Expect(resp.Node.Value).To(Equal("some value"))
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectSetError(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock

	mock.ExpectSet("/some/key", "some value").WillReturnError(errors.New("this is a testing error"))
	resp, err := kapi.Set(context.Background(), "/some/other/key", "some value", nil)
	Expect(err).To(HaveOccurred())
	Expect(resp).To(BeNil())
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectSetNotRecvd(t *testing.T) {
	RegisterTestingT(t)
	mock := FakeKeysAPI{}

	mock.ExpectSet("/some/key", "some value")
	err := mock.ExpectationsFulfilled()
	Expect(err).To(HaveOccurred())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectedResponse

type ExpectedResponse struct {
	Err      error
	Response *client.Response
}

func (*ExpectedResponse) WillReturnError

func (e *ExpectedResponse) WillReturnError(err error) *ExpectedResponse

func (*ExpectedResponse) WillReturnValue

func (e *ExpectedResponse) WillReturnValue(value string) *ExpectedResponse

type FakeKeysAPI

type FakeKeysAPI struct {
	ExpectedResponses []*ExpectedResponse
	ExpectedSets      []*ExpectedResponse
	ReceivedSets      []*ExpectedResponse
	AllowUnordered    bool
}

func (*FakeKeysAPI) Create

func (f *FakeKeysAPI) Create(ctx context.Context, key, value string) (*client.Response, error)

func (*FakeKeysAPI) CreateInOrder

func (f *FakeKeysAPI) CreateInOrder(ctx context.Context, dir, value string, opts *client.CreateInOrderOptions) (*client.Response, error)

func (*FakeKeysAPI) Delete

func (f *FakeKeysAPI) Delete(ctx context.Context, key string, opts *client.DeleteOptions) (*client.Response, error)

func (*FakeKeysAPI) ExpectGet

func (f *FakeKeysAPI) ExpectGet(key string) *ExpectedResponse

func (*FakeKeysAPI) ExpectSet

func (f *FakeKeysAPI) ExpectSet(key, value string) *ExpectedResponse

func (*FakeKeysAPI) ExpectationsFulfilled

func (f *FakeKeysAPI) ExpectationsFulfilled() error

func (*FakeKeysAPI) Get

func (f *FakeKeysAPI) Get(ctx context.Context, key string, opts *client.GetOptions) (resp *client.Response, err error)

func (*FakeKeysAPI) Set

func (f *FakeKeysAPI) Set(ctx context.Context, key, val string, opts *client.SetOptions) (*client.Response, error)

func (*FakeKeysAPI) Update

func (f *FakeKeysAPI) Update(ctx context.Context, key, value string) (*client.Response, error)

func (*FakeKeysAPI) Watcher

func (f *FakeKeysAPI) Watcher(key string, opts *client.WatcherOptions) client.Watcher

type KeysAPIWrapper

type KeysAPIWrapper interface {
	Watcher(key string, opts *client.WatcherOptions) client.Watcher
	Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error)
	Set(ctx context.Context, key, val string, opts *client.SetOptions) (*client.Response, error)
}

type MockWatcher

type MockWatcher struct {
	Nodes []*client.Node
}

func (*MockWatcher) ExpectResponse

func (f *MockWatcher) ExpectResponse(node *client.Node)

func (*MockWatcher) ExpectationsWereFulfilled

func (f *MockWatcher) ExpectationsWereFulfilled() error

func (*MockWatcher) Next

func (f *MockWatcher) Next(ctx context.Context) (*client.Response, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL