From 09017af35e08b01f4b738e298a7fbb8e8fa80cf4 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 29 Jan 2015 14:18:13 -0800 Subject: [PATCH] client: test httpWatcher --- client/keys_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/client/keys_test.go b/client/keys_test.go index 97b424a6f..a5f32a601 100644 --- a/client/keys_test.go +++ b/client/keys_test.go @@ -15,6 +15,7 @@ package client import ( + "errors" "fmt" "io/ioutil" "net/http" @@ -22,6 +23,8 @@ import ( "reflect" "testing" "time" + + "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" ) func TestV2KeysURLHelper(t *testing.T) { @@ -622,3 +625,103 @@ func TestUnmarshalFailedKeysResponseBadJSON(t *testing.T) { t.Errorf("error is of incorrect type *Error: %#v", err) } } + +func TestHTTPWatcherNextWaitAction(t *testing.T) { + initAction := waitAction{ + Prefix: "/pants", + Key: "/foo/bar", + Recursive: true, + AfterIndex: 19, + } + + client := &actionAssertingHTTPClient{ + t: t, + act: &initAction, + resp: http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"X-Etcd-Index": []string{"42"}}, + }, + body: []byte(`{"action":"update","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":21,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`), + } + + wantResponse := &Response{ + Action: "update", + Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(21)}, + PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)}, + Index: uint64(42), + } + + wantNextWait := waitAction{ + Prefix: "/pants", + Key: "/foo/bar", + Recursive: true, + AfterIndex: 22, + } + + watcher := &httpWatcher{ + client: client, + nextWait: initAction, + } + + resp, err := watcher.Next(context.Background()) + if err != nil { + t.Errorf("non-nil error: %#v", err) + } + + if !reflect.DeepEqual(wantResponse, resp) { + t.Errorf("received incorrect Response: want=%#v got=%#v", wantResponse, resp) + } + + if !reflect.DeepEqual(wantNextWait, watcher.nextWait) { + t.Errorf("nextWait incorrect: want=%#v got=%#v", wantNextWait, watcher.nextWait) + } +} + +func TestHTTPWatcherNextFail(t *testing.T) { + tests := []httpClient{ + // generic HTTP client failure + &staticHTTPClient{ + err: errors.New("fail!"), + }, + + // unusable status code + &staticHTTPClient{ + resp: http.Response{ + StatusCode: http.StatusTeapot, + }, + }, + + // etcd Error response + &staticHTTPClient{ + resp: http.Response{ + StatusCode: http.StatusNotFound, + }, + body: []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`), + }, + } + + for i, tt := range tests { + act := waitAction{ + Prefix: "/pants", + Key: "/foo/bar", + Recursive: true, + AfterIndex: 19, + } + + watcher := &httpWatcher{ + client: tt, + nextWait: act, + } + + resp, err := watcher.Next(context.Background()) + if err == nil { + t.Errorf("#%d: expected non-nil error", i) + } + if resp != nil { + t.Errorf("#%d: expected nil Response, got %#v", i, resp) + } + if !reflect.DeepEqual(act, watcher.nextWait) { + t.Errorf("#%d: nextWait changed: want=%#v got=%#v", i, act, watcher.nextWait) + } + } +}