tools

package
v0.0.0-...-6eef5cf Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2014 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package tools implements general tools which depend on the api package.

Index

Constants

View Source
const (
	EtcdErrorCodeNotFound      = 100
	EtcdErrorCodeValueRequired = 200
)

Variables

View Source
var (
	EtcdErrorNotFound      = &etcd.EtcdError{ErrorCode: EtcdErrorCodeNotFound}
	EtcdErrorValueRequired = &etcd.EtcdError{ErrorCode: EtcdErrorCodeValueRequired}
)

Functions

func Everything

func Everything(interface{}) bool

Everything is a FilterFunc which accepts all objects.

func IsEtcdConflict

func IsEtcdConflict(err error) bool

Returns true iff err is an etcd write conflict.

func IsEtcdNotFound

func IsEtcdNotFound(err error) bool

Returns true iff err is an etcd not found error.

func IsEtcdWatchStoppedByUser

func IsEtcdWatchStoppedByUser(err error) bool

IsEtcdWatchStoppedByUser returns true iff err is a client triggered stop.

Types

type APIEventDecoder

type APIEventDecoder struct {
	// contains filtered or unexported fields
}

APIEventDecoder implements the watch.Decoder interface for io.ReadClosers that have contents which consist of a series of api.WatchEvent objects encoded via JSON.

func NewAPIEventDecoder

func NewAPIEventDecoder(stream io.ReadCloser) *APIEventDecoder

NewAPIEventDecoder makes an APIEventDecoder for the given stream.

func (*APIEventDecoder) Close

func (d *APIEventDecoder) Close()

Close closes the underlying stream.

func (*APIEventDecoder) Decode

func (d *APIEventDecoder) Decode() (action watch.EventType, object interface{}, err error)

Decode blocks until it can return the next object in the stream. Returns an error if the stream is closed or an object can't be decoded.

type EtcdClient

type EtcdClient interface {
	AddChild(key, data string, ttl uint64) (*etcd.Response, error)
	Get(key string, sort, recursive bool) (*etcd.Response, error)
	Set(key, value string, ttl uint64) (*etcd.Response, error)
	Create(key, value string, ttl uint64) (*etcd.Response, error)
	CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)
	Delete(key string, recursive bool) (*etcd.Response, error)
	// I'd like to use directional channels here (e.g. <-chan) but this interface mimics
	// the etcd client interface which doesn't, and it doesn't seem worth it to wrap the api.
	Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}

EtcdClient is an injectable interface for testing.

type EtcdGetSet

type EtcdGetSet interface {
	Get(key string, sort, recursive bool) (*etcd.Response, error)
	Set(key, value string, ttl uint64) (*etcd.Response, error)
	CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)
	Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}

Interface exposing only the etcd operations needed by EtcdHelper.

type EtcdHelper

type EtcdHelper struct {
	Client EtcdGetSet
}

EtcdHelper offers common object marshalling/unmarshalling operations on an etcd client.

func (*EtcdHelper) AtomicUpdate

func (h *EtcdHelper) AtomicUpdate(key string, ptrToType interface{}, tryUpdate EtcdUpdateFunc) error

AtomicUpdate generalizes the pattern that allows for making atomic updates to etcd objects. Note, tryUpdate may be called more than once.

Example:

h := &util.EtcdHelper{client}

err := h.AtomicUpdate("myKey", &MyType{}, func(input interface{}) (interface{}, error) {
	// Before this function is called, currentObj has been reset to etcd's current
	// contents for "myKey".

	cur := input.(*MyType) // Gauranteed to work.

	// Make a *modification*.
	cur.Counter++

	// Return the modified object. Return an error to stop iterating.
	return cur, nil
})

func (*EtcdHelper) ExtractList

func (h *EtcdHelper) ExtractList(key string, slicePtr interface{}) error

Extract a go object per etcd node into a slice.

func (*EtcdHelper) ExtractObj

func (h *EtcdHelper) ExtractObj(key string, objPtr interface{}, ignoreNotFound bool) error

Unmarshals json found at key into objPtr. On a not found error, will either return a zero object of the requested type, or an error, depending on ignoreNotFound. Treats empty responses and nil response nodes exactly like a not found error.

func (*EtcdHelper) SetObj

func (h *EtcdHelper) SetObj(key string, obj interface{}) error

SetObj marshals obj via json, and stores under key. Will do an atomic update if obj's ResourceVersion field is set.

func (*EtcdHelper) Watch

func (h *EtcdHelper) Watch(key string) (watch.Interface, error)

Watch begins watching the specified key. Events are decoded into API objects and sent down the returned watch.Interface.

func (*EtcdHelper) WatchList

func (h *EtcdHelper) WatchList(key string, filter FilterFunc) (watch.Interface, error)

WatchList begins watching the specified key's items. Items are decoded into API objects, and any items passing 'filter' are sent down the returned watch.Interface.

type EtcdResponseWithError

type EtcdResponseWithError struct {
	R *etcd.Response
	E error
}

type EtcdUpdateFunc

type EtcdUpdateFunc func(input interface{}) (output interface{}, err error)

Pass an EtcdUpdateFunc to EtcdHelper.AtomicUpdate to make an atomic etcd update. See the comment for AtomicUpdate for more detail.

type FakeEtcdClient

type FakeEtcdClient struct {
	Data        map[string]EtcdResponseWithError
	DeletedKeys []string
	Err         error

	Ix int

	// Will become valid after Watch is called; tester may write to it. Tester may
	// also read from it to verify that it's closed after injecting an error.
	WatchResponse chan *etcd.Response
	// Write to this to prematurely stop a Watch that is running in a goroutine.
	WatchInjectError chan<- error
	WatchStop        chan<- bool
	// contains filtered or unexported fields
}

func MakeFakeEtcdClient

func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient

func (*FakeEtcdClient) AddChild

func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) CompareAndSwap

func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)

func (*FakeEtcdClient) Create

func (f *FakeEtcdClient) Create(key, value string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) Delete

func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error)

func (*FakeEtcdClient) Get

func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error)

func (*FakeEtcdClient) Set

func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) WaitForWatchCompletion

func (f *FakeEtcdClient) WaitForWatchCompletion()

func (*FakeEtcdClient) Watch

func (f *FakeEtcdClient) Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)

type FilterFunc

type FilterFunc func(obj interface{}) bool

FilterFunc is a predicate which takes an API object and returns true iff the object should remain in the set.

type TestLogger

type TestLogger interface {
	Errorf(format string, args ...interface{})
	Logf(format string, args ...interface{})
}

TestLogger is a type passed to Test functions to support formatted test logs.

Jump to

Keyboard shortcuts

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