12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379 |
- // Copyright 2015 CoreOS, Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package client
- import (
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "reflect"
- "testing"
- "time"
- "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
- )
- func TestV2KeysURLHelper(t *testing.T) {
- tests := []struct {
- endpoint url.URL
- prefix string
- key string
- want url.URL
- }{
- // key is empty, no problem
- {
- endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
- prefix: "",
- key: "",
- want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
- },
- // key is joined to path
- {
- endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
- prefix: "",
- key: "/foo/bar",
- want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys/foo/bar"},
- },
- // key is joined to path when path is empty
- {
- endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
- prefix: "",
- key: "/foo/bar",
- want: url.URL{Scheme: "http", Host: "example.com", Path: "/foo/bar"},
- },
- // Host field carries through with port
- {
- endpoint: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
- prefix: "",
- key: "",
- want: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
- },
- // Scheme carries through
- {
- endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
- prefix: "",
- key: "",
- want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
- },
- // Prefix is applied
- {
- endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/foo"},
- prefix: "/bar",
- key: "/baz",
- want: url.URL{Scheme: "https", Host: "example.com", Path: "/foo/bar/baz"},
- },
- }
- for i, tt := range tests {
- got := v2KeysURL(tt.endpoint, tt.prefix, tt.key)
- if tt.want != *got {
- t.Errorf("#%d: want=%#v, got=%#v", i, tt.want, *got)
- }
- }
- }
- func TestGetAction(t *testing.T) {
- ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
- baseWantURL := &url.URL{
- Scheme: "http",
- Host: "example.com",
- Path: "/v2/keys/foo/bar",
- }
- wantHeader := http.Header{}
- tests := []struct {
- recursive bool
- sorted bool
- wantQuery string
- }{
- {
- recursive: false,
- sorted: false,
- wantQuery: "recursive=false&sorted=false",
- },
- {
- recursive: true,
- sorted: false,
- wantQuery: "recursive=true&sorted=false",
- },
- {
- recursive: false,
- sorted: true,
- wantQuery: "recursive=false&sorted=true",
- },
- {
- recursive: true,
- sorted: true,
- wantQuery: "recursive=true&sorted=true",
- },
- }
- for i, tt := range tests {
- f := getAction{
- Key: "/foo/bar",
- Recursive: tt.recursive,
- Sorted: tt.sorted,
- }
- got := *f.HTTPRequest(ep)
- wantURL := baseWantURL
- wantURL.RawQuery = tt.wantQuery
- err := assertRequest(got, "GET", wantURL, wantHeader, nil)
- if err != nil {
- t.Errorf("#%d: %v", i, err)
- }
- }
- }
- func TestWaitAction(t *testing.T) {
- ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
- baseWantURL := &url.URL{
- Scheme: "http",
- Host: "example.com",
- Path: "/v2/keys/foo/bar",
- }
- wantHeader := http.Header{}
- tests := []struct {
- waitIndex uint64
- recursive bool
- wantQuery string
- }{
- {
- recursive: false,
- waitIndex: uint64(0),
- wantQuery: "recursive=false&wait=true&waitIndex=0",
- },
- {
- recursive: false,
- waitIndex: uint64(12),
- wantQuery: "recursive=false&wait=true&waitIndex=12",
- },
- {
- recursive: true,
- waitIndex: uint64(12),
- wantQuery: "recursive=true&wait=true&waitIndex=12",
- },
- }
- for i, tt := range tests {
- f := waitAction{
- Key: "/foo/bar",
- WaitIndex: tt.waitIndex,
- Recursive: tt.recursive,
- }
- got := *f.HTTPRequest(ep)
- wantURL := baseWantURL
- wantURL.RawQuery = tt.wantQuery
- err := assertRequest(got, "GET", wantURL, wantHeader, nil)
- if err != nil {
- t.Errorf("#%d: unexpected error: %#v", i, err)
- }
- }
- }
- func TestSetAction(t *testing.T) {
- wantHeader := http.Header(map[string][]string{
- "Content-Type": []string{"application/x-www-form-urlencoded"},
- })
- tests := []struct {
- act setAction
- wantURL string
- wantBody string
- }{
- // default prefix
- {
- act: setAction{
- Prefix: defaultV2KeysPrefix,
- Key: "foo",
- },
- wantURL: "http://example.com/v2/keys/foo",
- wantBody: "value=",
- },
- // non-default prefix
- {
- act: setAction{
- Prefix: "/pfx",
- Key: "foo",
- },
- wantURL: "http://example.com/pfx/foo",
- wantBody: "value=",
- },
- // no prefix
- {
- act: setAction{
- Key: "foo",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=",
- },
- // Key with path separators
- {
- act: setAction{
- Prefix: defaultV2KeysPrefix,
- Key: "foo/bar/baz",
- },
- wantURL: "http://example.com/v2/keys/foo/bar/baz",
- wantBody: "value=",
- },
- // Key with leading slash, Prefix with trailing slash
- {
- act: setAction{
- Prefix: "/foo/",
- Key: "/bar",
- },
- wantURL: "http://example.com/foo/bar",
- wantBody: "value=",
- },
- // Key with trailing slash
- {
- act: setAction{
- Key: "/foo/",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=",
- },
- // Value is set
- {
- act: setAction{
- Key: "foo",
- Value: "baz",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=baz",
- },
- // PrevExist set, but still ignored
- {
- act: setAction{
- Key: "foo",
- PrevExist: PrevIgnore,
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=",
- },
- // PrevExist set to true
- {
- act: setAction{
- Key: "foo",
- PrevExist: PrevExist,
- },
- wantURL: "http://example.com/foo?prevExist=true",
- wantBody: "value=",
- },
- // PrevExist set to false
- {
- act: setAction{
- Key: "foo",
- PrevExist: PrevNoExist,
- },
- wantURL: "http://example.com/foo?prevExist=false",
- wantBody: "value=",
- },
- // PrevValue is urlencoded
- {
- act: setAction{
- Key: "foo",
- PrevValue: "bar baz",
- },
- wantURL: "http://example.com/foo?prevValue=bar+baz",
- wantBody: "value=",
- },
- // PrevIndex is set
- {
- act: setAction{
- Key: "foo",
- PrevIndex: uint64(12),
- },
- wantURL: "http://example.com/foo?prevIndex=12",
- wantBody: "value=",
- },
- // TTL is set
- {
- act: setAction{
- Key: "foo",
- TTL: 3 * time.Minute,
- },
- wantURL: "http://example.com/foo",
- wantBody: "ttl=180&value=",
- },
- // Dir is set
- {
- act: setAction{
- Key: "foo",
- Dir: true,
- },
- wantURL: "http://example.com/foo?dir=true",
- wantBody: "",
- },
- // Dir is set with a value
- {
- act: setAction{
- Key: "foo",
- Value: "bar",
- Dir: true,
- },
- wantURL: "http://example.com/foo?dir=true",
- wantBody: "",
- },
- // Dir is set with PrevExist set to true
- {
- act: setAction{
- Key: "foo",
- PrevExist: PrevExist,
- Dir: true,
- },
- wantURL: "http://example.com/foo?dir=true&prevExist=true",
- wantBody: "",
- },
- // Dir is set with PrevValue
- {
- act: setAction{
- Key: "foo",
- PrevValue: "bar",
- Dir: true,
- },
- wantURL: "http://example.com/foo?dir=true",
- wantBody: "",
- },
- }
- for i, tt := range tests {
- u, err := url.Parse(tt.wantURL)
- if err != nil {
- t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
- }
- got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
- if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
- t.Errorf("#%d: %v", i, err)
- }
- }
- }
- func TestCreateInOrderAction(t *testing.T) {
- wantHeader := http.Header(map[string][]string{
- "Content-Type": []string{"application/x-www-form-urlencoded"},
- })
- tests := []struct {
- act createInOrderAction
- wantURL string
- wantBody string
- }{
- // default prefix
- {
- act: createInOrderAction{
- Prefix: defaultV2KeysPrefix,
- Dir: "foo",
- },
- wantURL: "http://example.com/v2/keys/foo",
- wantBody: "value=",
- },
- // non-default prefix
- {
- act: createInOrderAction{
- Prefix: "/pfx",
- Dir: "foo",
- },
- wantURL: "http://example.com/pfx/foo",
- wantBody: "value=",
- },
- // no prefix
- {
- act: createInOrderAction{
- Dir: "foo",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=",
- },
- // Key with path separators
- {
- act: createInOrderAction{
- Prefix: defaultV2KeysPrefix,
- Dir: "foo/bar/baz",
- },
- wantURL: "http://example.com/v2/keys/foo/bar/baz",
- wantBody: "value=",
- },
- // Key with leading slash, Prefix with trailing slash
- {
- act: createInOrderAction{
- Prefix: "/foo/",
- Dir: "/bar",
- },
- wantURL: "http://example.com/foo/bar",
- wantBody: "value=",
- },
- // Key with trailing slash
- {
- act: createInOrderAction{
- Dir: "/foo/",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=",
- },
- // Value is set
- {
- act: createInOrderAction{
- Dir: "foo",
- Value: "baz",
- },
- wantURL: "http://example.com/foo",
- wantBody: "value=baz",
- },
- // TTL is set
- {
- act: createInOrderAction{
- Dir: "foo",
- TTL: 3 * time.Minute,
- },
- wantURL: "http://example.com/foo",
- wantBody: "ttl=180&value=",
- },
- }
- for i, tt := range tests {
- u, err := url.Parse(tt.wantURL)
- if err != nil {
- t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
- }
- got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
- if err := assertRequest(*got, "POST", u, wantHeader, []byte(tt.wantBody)); err != nil {
- t.Errorf("#%d: %v", i, err)
- }
- }
- }
- func TestDeleteAction(t *testing.T) {
- wantHeader := http.Header(map[string][]string{
- "Content-Type": []string{"application/x-www-form-urlencoded"},
- })
- tests := []struct {
- act deleteAction
- wantURL string
- }{
- // default prefix
- {
- act: deleteAction{
- Prefix: defaultV2KeysPrefix,
- Key: "foo",
- },
- wantURL: "http://example.com/v2/keys/foo",
- },
- // non-default prefix
- {
- act: deleteAction{
- Prefix: "/pfx",
- Key: "foo",
- },
- wantURL: "http://example.com/pfx/foo",
- },
- // no prefix
- {
- act: deleteAction{
- Key: "foo",
- },
- wantURL: "http://example.com/foo",
- },
- // Key with path separators
- {
- act: deleteAction{
- Prefix: defaultV2KeysPrefix,
- Key: "foo/bar/baz",
- },
- wantURL: "http://example.com/v2/keys/foo/bar/baz",
- },
- // Key with leading slash, Prefix with trailing slash
- {
- act: deleteAction{
- Prefix: "/foo/",
- Key: "/bar",
- },
- wantURL: "http://example.com/foo/bar",
- },
- // Key with trailing slash
- {
- act: deleteAction{
- Key: "/foo/",
- },
- wantURL: "http://example.com/foo",
- },
- // Recursive set to true
- {
- act: deleteAction{
- Key: "foo",
- Recursive: true,
- },
- wantURL: "http://example.com/foo?recursive=true",
- },
- // PrevValue is urlencoded
- {
- act: deleteAction{
- Key: "foo",
- PrevValue: "bar baz",
- },
- wantURL: "http://example.com/foo?prevValue=bar+baz",
- },
- // PrevIndex is set
- {
- act: deleteAction{
- Key: "foo",
- PrevIndex: uint64(12),
- },
- wantURL: "http://example.com/foo?prevIndex=12",
- },
- }
- for i, tt := range tests {
- u, err := url.Parse(tt.wantURL)
- if err != nil {
- t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
- }
- got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
- if err := assertRequest(*got, "DELETE", u, wantHeader, nil); err != nil {
- t.Errorf("#%d: %v", i, err)
- }
- }
- }
- func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
- if wantMethod != got.Method {
- return fmt.Errorf("want.Method=%#v got.Method=%#v", wantMethod, got.Method)
- }
- if !reflect.DeepEqual(wantURL, got.URL) {
- return fmt.Errorf("want.URL=%#v got.URL=%#v", wantURL, got.URL)
- }
- if !reflect.DeepEqual(wantHeader, got.Header) {
- return fmt.Errorf("want.Header=%#v got.Header=%#v", wantHeader, got.Header)
- }
- if got.Body == nil {
- if wantBody != nil {
- return fmt.Errorf("want.Body=%v got.Body=%v", wantBody, got.Body)
- }
- } else {
- if wantBody == nil {
- return fmt.Errorf("want.Body=%v got.Body=%s", wantBody, got.Body)
- } else {
- gotBytes, err := ioutil.ReadAll(got.Body)
- if err != nil {
- return err
- }
- if !reflect.DeepEqual(wantBody, gotBytes) {
- return fmt.Errorf("want.Body=%s got.Body=%s", wantBody, gotBytes)
- }
- }
- }
- return nil
- }
- func TestUnmarshalSuccessfulResponse(t *testing.T) {
- var expiration time.Time
- expiration.UnmarshalText([]byte("2015-04-07T04:40:23.044979686Z"))
- tests := []struct {
- hdr string
- body string
- wantRes *Response
- wantErr bool
- }{
- // Neither PrevNode or Node
- {
- hdr: "1",
- body: `{"action":"delete"}`,
- wantRes: &Response{Action: "delete", Index: 1},
- wantErr: false,
- },
- // PrevNode
- {
- hdr: "15",
- body: `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
- wantRes: &Response{
- Action: "delete",
- Index: 15,
- Node: nil,
- PrevNode: &Node{
- Key: "/foo",
- Value: "bar",
- ModifiedIndex: 12,
- CreatedIndex: 10,
- },
- },
- wantErr: false,
- },
- // Node
- {
- hdr: "15",
- body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10, "ttl": 10, "expiration": "2015-04-07T04:40:23.044979686Z"}}`,
- wantRes: &Response{
- Action: "get",
- Index: 15,
- Node: &Node{
- Key: "/foo",
- Value: "bar",
- ModifiedIndex: 12,
- CreatedIndex: 10,
- TTL: 10,
- Expiration: &expiration,
- },
- PrevNode: nil,
- },
- wantErr: false,
- },
- // Node Dir
- {
- hdr: "15",
- body: `{"action":"get", "node": {"key": "/foo", "dir": true, "modifiedIndex": 12, "createdIndex": 10}}`,
- wantRes: &Response{
- Action: "get",
- Index: 15,
- Node: &Node{
- Key: "/foo",
- Dir: true,
- ModifiedIndex: 12,
- CreatedIndex: 10,
- },
- PrevNode: nil,
- },
- wantErr: false,
- },
- // PrevNode and Node
- {
- hdr: "15",
- body: `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
- wantRes: &Response{
- Action: "update",
- Index: 15,
- PrevNode: &Node{
- Key: "/foo",
- Value: "baz",
- ModifiedIndex: 10,
- CreatedIndex: 10,
- },
- Node: &Node{
- Key: "/foo",
- Value: "bar",
- ModifiedIndex: 12,
- CreatedIndex: 10,
- },
- },
- wantErr: false,
- },
- // Garbage in body
- {
- hdr: "",
- body: `garbage`,
- wantRes: nil,
- wantErr: true,
- },
- // non-integer index
- {
- hdr: "poo",
- body: `{}`,
- wantRes: nil,
- wantErr: true,
- },
- }
- for i, tt := range tests {
- h := make(http.Header)
- h.Add("X-Etcd-Index", tt.hdr)
- res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
- if tt.wantErr != (err != nil) {
- t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
- }
- if (res == nil) != (tt.wantRes == nil) {
- t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
- continue
- } else if tt.wantRes == nil {
- // expected and successfully got nil response
- continue
- }
- if res.Action != tt.wantRes.Action {
- t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
- }
- if res.Index != tt.wantRes.Index {
- t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
- }
- if !reflect.DeepEqual(res.Node, tt.wantRes.Node) {
- t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.wantRes.Node)
- }
- }
- }
- func TestUnmarshalFailedKeysResponse(t *testing.T) {
- body := []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`)
- wantErr := Error{
- Code: 100,
- Message: "Key not found",
- Cause: "/foo",
- Index: uint64(18),
- }
- gotErr := unmarshalFailedKeysResponse(body)
- if !reflect.DeepEqual(wantErr, gotErr) {
- t.Errorf("unexpected error: want=%#v got=%#v", wantErr, gotErr)
- }
- }
- func TestUnmarshalFailedKeysResponseBadJSON(t *testing.T) {
- err := unmarshalFailedKeysResponse([]byte(`{"er`))
- if err == nil {
- t.Errorf("got nil error")
- } else if _, ok := err.(Error); ok {
- t.Errorf("error is of incorrect type *Error: %#v", err)
- }
- }
- func TestHTTPWatcherNextWaitAction(t *testing.T) {
- initAction := waitAction{
- Prefix: "/pants",
- Key: "/foo/bar",
- Recursive: true,
- WaitIndex: 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,
- WaitIndex: 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,
- WaitIndex: 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)
- }
- }
- }
- func TestHTTPKeysAPIWatcherAction(t *testing.T) {
- tests := []struct {
- key string
- opts *WatcherOptions
- want waitAction
- }{
- {
- key: "/foo",
- opts: nil,
- want: waitAction{
- Key: "/foo",
- Recursive: false,
- WaitIndex: 0,
- },
- },
- {
- key: "/foo",
- opts: &WatcherOptions{
- Recursive: false,
- AfterIndex: 0,
- },
- want: waitAction{
- Key: "/foo",
- Recursive: false,
- WaitIndex: 0,
- },
- },
- {
- key: "/foo",
- opts: &WatcherOptions{
- Recursive: true,
- AfterIndex: 0,
- },
- want: waitAction{
- Key: "/foo",
- Recursive: true,
- WaitIndex: 0,
- },
- },
- {
- key: "/foo",
- opts: &WatcherOptions{
- Recursive: false,
- AfterIndex: 19,
- },
- want: waitAction{
- Key: "/foo",
- Recursive: false,
- WaitIndex: 20,
- },
- },
- }
- for i, tt := range tests {
- kAPI := &httpKeysAPI{
- client: &staticHTTPClient{err: errors.New("fail!")},
- }
- want := &httpWatcher{
- client: &staticHTTPClient{err: errors.New("fail!")},
- nextWait: tt.want,
- }
- got := kAPI.Watcher(tt.key, tt.opts)
- if !reflect.DeepEqual(want, got) {
- t.Errorf("#%d: incorrect watcher: want=%#v got=%#v", i, want, got)
- }
- }
- }
- func TestHTTPKeysAPISetAction(t *testing.T) {
- tests := []struct {
- key string
- value string
- opts *SetOptions
- wantAction httpAction
- }{
- // nil SetOptions
- {
- key: "/foo",
- value: "bar",
- opts: nil,
- wantAction: &setAction{
- Key: "/foo",
- Value: "bar",
- PrevValue: "",
- PrevIndex: 0,
- PrevExist: PrevIgnore,
- TTL: 0,
- },
- },
- // empty SetOptions
- {
- key: "/foo",
- value: "bar",
- opts: &SetOptions{},
- wantAction: &setAction{
- Key: "/foo",
- Value: "bar",
- PrevValue: "",
- PrevIndex: 0,
- PrevExist: PrevIgnore,
- TTL: 0,
- },
- },
- // populated SetOptions
- {
- key: "/foo",
- value: "bar",
- opts: &SetOptions{
- PrevValue: "baz",
- PrevIndex: 13,
- PrevExist: PrevExist,
- TTL: time.Minute,
- Dir: true,
- },
- wantAction: &setAction{
- Key: "/foo",
- Value: "bar",
- PrevValue: "baz",
- PrevIndex: 13,
- PrevExist: PrevExist,
- TTL: time.Minute,
- Dir: true,
- },
- },
- }
- for i, tt := range tests {
- client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
- kAPI := httpKeysAPI{client: client}
- kAPI.Set(context.Background(), tt.key, tt.value, tt.opts)
- }
- }
- func TestHTTPKeysAPISetError(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.StatusInternalServerError,
- },
- body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
- },
- }
- for i, tt := range tests {
- kAPI := httpKeysAPI{client: tt}
- resp, err := kAPI.Set(context.Background(), "/foo", "bar", nil)
- if err == nil {
- t.Errorf("#%d: received nil error", i)
- }
- if resp != nil {
- t.Errorf("#%d: received non-nil Response: %#v", i, resp)
- }
- }
- }
- func TestHTTPKeysAPISetResponse(t *testing.T) {
- client := &staticHTTPClient{
- resp: http.Response{
- StatusCode: http.StatusOK,
- Header: http.Header{"X-Etcd-Index": []string{"21"}},
- },
- body: []byte(`{"action":"set","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":21,"createdIndex":21},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
- }
- wantResponse := &Response{
- Action: "set",
- Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(21), ModifiedIndex: uint64(21)},
- PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
- Index: uint64(21),
- }
- kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
- resp, err := kAPI.Set(context.Background(), "/foo/bar/baz", "snarf", nil)
- if err != nil {
- t.Errorf("non-nil error: %#v", err)
- }
- if !reflect.DeepEqual(wantResponse, resp) {
- t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
- }
- }
- func TestHTTPKeysAPIGetAction(t *testing.T) {
- tests := []struct {
- key string
- opts *GetOptions
- wantAction httpAction
- }{
- // nil GetOptions
- {
- key: "/foo",
- opts: nil,
- wantAction: &getAction{
- Key: "/foo",
- Sorted: false,
- Recursive: false,
- },
- },
- // empty GetOptions
- {
- key: "/foo",
- opts: &GetOptions{},
- wantAction: &getAction{
- Key: "/foo",
- Sorted: false,
- Recursive: false,
- },
- },
- // populated GetOptions
- {
- key: "/foo",
- opts: &GetOptions{
- Sort: true,
- Recursive: true,
- },
- wantAction: &getAction{
- Key: "/foo",
- Sorted: true,
- Recursive: true,
- },
- },
- }
- for i, tt := range tests {
- client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
- kAPI := httpKeysAPI{client: client}
- kAPI.Get(context.Background(), tt.key, tt.opts)
- }
- }
- func TestHTTPKeysAPIGetError(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.StatusInternalServerError,
- },
- body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
- },
- }
- for i, tt := range tests {
- kAPI := httpKeysAPI{client: tt}
- resp, err := kAPI.Get(context.Background(), "/foo", nil)
- if err == nil {
- t.Errorf("#%d: received nil error", i)
- }
- if resp != nil {
- t.Errorf("#%d: received non-nil Response: %#v", i, resp)
- }
- }
- }
- func TestHTTPKeysAPIGetResponse(t *testing.T) {
- client := &staticHTTPClient{
- resp: http.Response{
- StatusCode: http.StatusOK,
- Header: http.Header{"X-Etcd-Index": []string{"42"}},
- },
- body: []byte(`{"action":"get","node":{"key":"/pants/foo/bar","modifiedIndex":25,"createdIndex":19,"nodes":[{"key":"/pants/foo/bar/baz","value":"snarf","createdIndex":21,"modifiedIndex":25}]}}`),
- }
- wantResponse := &Response{
- Action: "get",
- Node: &Node{
- Key: "/pants/foo/bar",
- Nodes: []*Node{
- &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: 21, ModifiedIndex: 25},
- },
- CreatedIndex: uint64(19),
- ModifiedIndex: uint64(25),
- },
- Index: uint64(42),
- }
- kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
- resp, err := kAPI.Get(context.Background(), "/foo/bar", &GetOptions{Recursive: true})
- if err != nil {
- t.Errorf("non-nil error: %#v", err)
- }
- if !reflect.DeepEqual(wantResponse, resp) {
- t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
- }
- }
- func TestHTTPKeysAPIDeleteAction(t *testing.T) {
- tests := []struct {
- key string
- value string
- opts *DeleteOptions
- wantAction httpAction
- }{
- // nil DeleteOptions
- {
- key: "/foo",
- opts: nil,
- wantAction: &deleteAction{
- Key: "/foo",
- PrevValue: "",
- PrevIndex: 0,
- Recursive: false,
- },
- },
- // empty DeleteOptions
- {
- key: "/foo",
- opts: &DeleteOptions{},
- wantAction: &deleteAction{
- Key: "/foo",
- PrevValue: "",
- PrevIndex: 0,
- Recursive: false,
- },
- },
- // populated DeleteOptions
- {
- key: "/foo",
- opts: &DeleteOptions{
- PrevValue: "baz",
- PrevIndex: 13,
- Recursive: true,
- },
- wantAction: &deleteAction{
- Key: "/foo",
- PrevValue: "baz",
- PrevIndex: 13,
- Recursive: true,
- },
- },
- }
- for i, tt := range tests {
- client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
- kAPI := httpKeysAPI{client: client}
- kAPI.Delete(context.Background(), tt.key, tt.opts)
- }
- }
- func TestHTTPKeysAPIDeleteError(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.StatusInternalServerError,
- },
- body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
- },
- }
- for i, tt := range tests {
- kAPI := httpKeysAPI{client: tt}
- resp, err := kAPI.Delete(context.Background(), "/foo", nil)
- if err == nil {
- t.Errorf("#%d: received nil error", i)
- }
- if resp != nil {
- t.Errorf("#%d: received non-nil Response: %#v", i, resp)
- }
- }
- }
- func TestHTTPKeysAPIDeleteResponse(t *testing.T) {
- client := &staticHTTPClient{
- resp: http.Response{
- StatusCode: http.StatusOK,
- Header: http.Header{"X-Etcd-Index": []string{"22"}},
- },
- body: []byte(`{"action":"delete","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":22,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
- }
- wantResponse := &Response{
- Action: "delete",
- Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(22)},
- PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
- Index: uint64(22),
- }
- kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
- resp, err := kAPI.Delete(context.Background(), "/foo/bar/baz", nil)
- if err != nil {
- t.Errorf("non-nil error: %#v", err)
- }
- if !reflect.DeepEqual(wantResponse, resp) {
- t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
- }
- }
- func TestHTTPKeysAPICreateAction(t *testing.T) {
- act := &setAction{
- Key: "/foo",
- Value: "bar",
- PrevExist: PrevNoExist,
- PrevIndex: 0,
- PrevValue: "",
- TTL: 0,
- }
- kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
- kAPI.Create(context.Background(), "/foo", "bar")
- }
- func TestHTTPKeysAPICreateInOrderAction(t *testing.T) {
- act := &createInOrderAction{
- Dir: "/foo",
- Value: "bar",
- TTL: 0,
- }
- kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
- kAPI.CreateInOrder(context.Background(), "/foo", "bar", nil)
- }
- func TestHTTPKeysAPIUpdateAction(t *testing.T) {
- act := &setAction{
- Key: "/foo",
- Value: "bar",
- PrevExist: PrevExist,
- PrevIndex: 0,
- PrevValue: "",
- TTL: 0,
- }
- kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
- kAPI.Update(context.Background(), "/foo", "bar")
- }
- func TestNodeTTLDuration(t *testing.T) {
- tests := []struct {
- node *Node
- want time.Duration
- }{
- {
- node: &Node{TTL: 0},
- want: 0,
- },
- {
- node: &Node{TTL: 97},
- want: 97 * time.Second,
- },
- }
- for i, tt := range tests {
- got := tt.node.TTLDuration()
- if tt.want != got {
- t.Errorf("#%d: incorrect duration: want=%v got=%v", i, tt.want, got)
- }
- }
- }
|