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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
package etcd
import (
"context"
"errors"
"reflect"
"testing"
"time"
etcd "go.etcd.io/etcd/client/v2"
)
func TestNewClient(t *testing.T) {
client, err := NewClient(
context.Background(),
[]string{"http://irrelevant:12345"},
ClientOptions{
DialTimeout: 2 * time.Second,
DialKeepAlive: 2 * time.Second,
HeaderTimeoutPerRequest: 2 * time.Second,
},
)
if err != nil {
t.Fatalf("unexpected error creating client: %v", err)
}
if client == nil {
t.Fatal("expected new Client, got nil")
}
}
// NewClient should fail when providing invalid or missing endpoints.
func TestOptions(t *testing.T) {
a, err := NewClient(
context.Background(),
[]string{},
ClientOptions{
Cert: "",
Key: "",
CACert: "",
DialTimeout: 2 * time.Second,
DialKeepAlive: 2 * time.Second,
HeaderTimeoutPerRequest: 2 * time.Second,
},
)
if err == nil {
t.Errorf("expected error: %v", err)
}
if a != nil {
t.Fatalf("expected client to be nil on failure")
}
_, err = NewClient(
context.Background(),
[]string{"http://irrelevant:12345"},
ClientOptions{
Cert: "blank.crt",
Key: "blank.key",
CACert: "blank.CACert",
DialTimeout: 2 * time.Second,
DialKeepAlive: 2 * time.Second,
HeaderTimeoutPerRequest: 2 * time.Second,
},
)
if err == nil {
t.Errorf("expected error: %v", err)
}
}
// Mocks of the underlying etcd.KeysAPI interface that is called by the methods we want to test
// fakeKeysAPI implements etcd.KeysAPI, event and err are channels used to emulate
// an etcd event or error, getres will be returned when etcd.KeysAPI.Get is called.
type fakeKeysAPI struct {
event chan bool
err chan bool
getres *getResult
}
type getResult struct {
resp *etcd.Response
err error
}
// Get return the content of getres or nil, nil
func (fka *fakeKeysAPI) Get(ctx context.Context, key string, opts *etcd.GetOptions) (*etcd.Response, error) {
if fka.getres == nil {
return nil, nil
}
return fka.getres.resp, fka.getres.err
}
// Set is not used in the tests
func (fka *fakeKeysAPI) Set(ctx context.Context, key, value string, opts *etcd.SetOptions) (*etcd.Response, error) {
return nil, nil
}
// Delete is not used in the tests
func (fka *fakeKeysAPI) Delete(ctx context.Context, key string, opts *etcd.DeleteOptions) (*etcd.Response, error) {
return nil, nil
}
// Create is not used in the tests
func (fka *fakeKeysAPI) Create(ctx context.Context, key, value string) (*etcd.Response, error) {
return nil, nil
}
// CreateInOrder is not used in the tests
func (fka *fakeKeysAPI) CreateInOrder(ctx context.Context, dir, value string, opts *etcd.CreateInOrderOptions) (*etcd.Response, error) {
return nil, nil
}
// Update is not used in the tests
func (fka *fakeKeysAPI) Update(ctx context.Context, key, value string) (*etcd.Response, error) {
return nil, nil
}
// Watcher return a fakeWatcher that will forward event and error received on the channels
func (fka *fakeKeysAPI) Watcher(key string, opts *etcd.WatcherOptions) etcd.Watcher {
return &fakeWatcher{fka.event, fka.err}
}
// fakeWatcher implements etcd.Watcher
type fakeWatcher struct {
event chan bool
err chan bool
}
// Next blocks until an etcd event or error is emulated.
// When an event occurs it just return nil response and error.
// When an error occur it return a non nil error.
func (fw *fakeWatcher) Next(context.Context) (*etcd.Response, error) {
select {
case <-fw.event:
return nil, nil
case <-fw.err:
return nil, errors.New("error from underlying etcd watcher")
}
}
// newFakeClient return a new etcd.Client built on top of the mocked interfaces
func newFakeClient(event, err chan bool, getres *getResult) Client {
return &client{
keysAPI: &fakeKeysAPI{event, err, getres},
ctx: context.Background(),
}
}
// Register should fail when the provided service has an empty key or value
func TestRegisterClient(t *testing.T) {
client := newFakeClient(nil, nil, nil)
err := client.Register(Service{Key: "", Value: "value", DeleteOptions: nil})
if want, have := ErrNoKey, err; want != have {
t.Fatalf("want %v, have %v", want, have)
}
err = client.Register(Service{Key: "key", Value: "", DeleteOptions: nil})
if want, have := ErrNoValue, err; want != have {
t.Fatalf("want %v, have %v", want, have)
}
err = client.Register(Service{Key: "key", Value: "value", DeleteOptions: nil})
if err != nil {
t.Fatal(err)
}
}
// Deregister should fail if the input service has an empty key
func TestDeregisterClient(t *testing.T) {
client := newFakeClient(nil, nil, nil)
err := client.Deregister(Service{Key: "", Value: "value", DeleteOptions: nil})
if want, have := ErrNoKey, err; want != have {
t.Fatalf("want %v, have %v", want, have)
}
err = client.Deregister(Service{Key: "key", Value: "", DeleteOptions: nil})
if err != nil {
t.Fatal(err)
}
}
// WatchPrefix notify the caller by writing on the channel if an etcd event occurs
// or return in case of an underlying error
func TestWatchPrefix(t *testing.T) {
err := make(chan bool)
event := make(chan bool)
watchPrefixReturned := make(chan bool, 1)
client := newFakeClient(event, err, nil)
ch := make(chan struct{})
go func() {
client.WatchPrefix("prefix", ch) // block until an etcd event or error occurs
watchPrefixReturned <- true
}()
// WatchPrefix force the caller to read once from the channel before actually
// sending notification, emulate that first read.
<-ch
// Emulate an etcd event
event <- true
if want, have := struct{}{}, <-ch; want != have {
t.Fatalf("want %v, have %v", want, have)
}
// Emulate an error, WatchPrefix should return
err <- true
select {
case <-watchPrefixReturned:
break
case <-time.After(1 * time.Second):
t.Fatal("WatchPrefix not returning on errors")
}
}
var errKeyAPI = errors.New("emulate error returned by KeysAPI.Get")
// table of test cases for method GetEntries
var getEntriesTestTable = []struct {
input getResult // value returned by the underlying etcd.KeysAPI.Get
resp []string // response expected in output of GetEntries
err error //error expected in output of GetEntries
}{
// test case: an error is returned by etcd.KeysAPI.Get
{getResult{nil, errKeyAPI}, nil, errKeyAPI},
// test case: return a single leaf node, with an empty value
{getResult{&etcd.Response{
Action: "get",
Node: &etcd.Node{
Key: "nodekey",
Dir: false,
Value: "",
Nodes: nil,
CreatedIndex: 0,
ModifiedIndex: 0,
Expiration: nil,
TTL: 0,
},
PrevNode: nil,
Index: 0,
}, nil}, []string{}, nil},
// test case: return a single leaf node, with a value
{getResult{&etcd.Response{
Action: "get",
Node: &etcd.Node{
Key: "nodekey",
Dir: false,
Value: "nodevalue",
Nodes: nil,
CreatedIndex: 0,
ModifiedIndex: 0,
Expiration: nil,
TTL: 0,
},
PrevNode: nil,
Index: 0,
}, nil}, []string{"nodevalue"}, nil},
// test case: return a node with two childs
{getResult{&etcd.Response{
Action: "get",
Node: &etcd.Node{
Key: "nodekey",
Dir: true,
Value: "nodevalue",
Nodes: []*etcd.Node{
{
Key: "childnode1",
Dir: false,
Value: "childvalue1",
Nodes: nil,
CreatedIndex: 0,
ModifiedIndex: 0,
Expiration: nil,
TTL: 0,
},
{
Key: "childnode2",
Dir: false,
Value: "childvalue2",
Nodes: nil,
CreatedIndex: 0,
ModifiedIndex: 0,
Expiration: nil,
TTL: 0,
},
},
CreatedIndex: 0,
ModifiedIndex: 0,
Expiration: nil,
TTL: 0,
},
PrevNode: nil,
Index: 0,
}, nil}, []string{"childvalue1", "childvalue2"}, nil},
}
func TestGetEntries(t *testing.T) {
for _, et := range getEntriesTestTable {
client := newFakeClient(nil, nil, &et.input)
resp, err := client.GetEntries("prefix")
if want, have := et.resp, resp; !reflect.DeepEqual(want, have) {
t.Fatalf("want %v, have %v", want, have)
}
if want, have := et.err, err; want != have {
t.Fatalf("want %v, have %v", want, have)
}
}
}
|