etcd/client/keys_test.go

625 lines
13 KiB
Go
Raw Normal View History

// 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 (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"testing"
2015-01-23 22:39:45 +03:00
"time"
)
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
2015-01-28 23:45:05 +03:00
sorted bool
wantQuery string
}{
{
recursive: false,
2015-01-28 23:45:05 +03:00
sorted: false,
wantQuery: "recursive=false&sorted=false",
},
{
recursive: true,
2015-01-28 23:45:05 +03:00
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,
2015-01-28 23:45:05 +03:00
Sorted: tt.sorted,
}
got := *f.HTTPRequest(ep)
wantURL := baseWantURL
wantURL.RawQuery = tt.wantQuery
2015-01-23 01:30:58 +03:00
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 {
2015-01-29 01:29:03 +03:00
afterIndex uint64
recursive bool
wantQuery string
}{
{
2015-01-29 01:29:03 +03:00
recursive: false,
afterIndex: uint64(0),
wantQuery: "recursive=false&wait=true&waitIndex=0",
},
{
2015-01-29 01:29:03 +03:00
recursive: false,
afterIndex: uint64(12),
wantQuery: "recursive=false&wait=true&waitIndex=12",
},
{
2015-01-29 01:29:03 +03:00
recursive: true,
afterIndex: uint64(12),
wantQuery: "recursive=true&wait=true&waitIndex=12",
},
}
for i, tt := range tests {
f := waitAction{
2015-01-29 01:29:03 +03:00
Key: "/foo/bar",
AfterIndex: tt.afterIndex,
Recursive: tt.recursive,
}
got := *f.HTTPRequest(ep)
wantURL := baseWantURL
wantURL.RawQuery = tt.wantQuery
2015-01-23 01:30:58 +03:00
err := assertRequest(got, "GET", wantURL, wantHeader, nil)
if err != nil {
t.Errorf("#%d: %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{
2015-01-27 22:25:52 +03:00
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{
2015-01-27 22:25:52 +03:00
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{
2015-01-23 04:04:41 +03:00
Key: "foo",
PrevExist: PrevIgnore,
},
wantURL: "http://example.com/foo",
wantBody: "value=",
},
// PrevExist set to true
{
act: setAction{
2015-01-23 04:04:41 +03:00
Key: "foo",
PrevExist: PrevExist,
},
wantURL: "http://example.com/foo?prevExist=true",
wantBody: "value=",
},
// PrevExist set to false
{
act: setAction{
2015-01-23 04:04:41 +03:00
Key: "foo",
PrevExist: PrevNoExist,
},
wantURL: "http://example.com/foo?prevExist=false",
wantBody: "value=",
},
// PrevValue is urlencoded
{
act: setAction{
2015-01-23 04:04:41 +03:00
Key: "foo",
PrevValue: "bar baz",
},
wantURL: "http://example.com/foo?prevValue=bar+baz",
wantBody: "value=",
},
// PrevIndex is set
{
act: setAction{
2015-01-23 04:04:41 +03:00
Key: "foo",
PrevIndex: uint64(12),
},
wantURL: "http://example.com/foo?prevIndex=12",
wantBody: "value=",
},
2015-01-23 22:39:45 +03:00
// TTL is set
{
act: setAction{
Key: "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"})
2015-01-23 01:30:58 +03:00
if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
t.Errorf("#%d: %v", i, err)
}
}
}
2015-01-23 01:35:18 +03:00
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{
2015-01-27 22:25:52 +03:00
Prefix: defaultV2KeysPrefix,
2015-01-23 01:35:18 +03:00
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{
2015-01-27 22:25:52 +03:00
Prefix: defaultV2KeysPrefix,
2015-01-23 01:35:18 +03:00
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,
2015-01-23 01:35:18 +03:00
},
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",
},
2015-01-23 01:35:18 +03:00
}
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)
}
}
}
2015-01-23 01:30:58 +03:00
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) {
tests := []struct {
2015-01-29 20:47:57 +03:00
hdr string
body string
wantRes *Response
wantErr bool
}{
// Neither PrevNode or Node
{
2015-01-29 20:47:57 +03:00
hdr: "1",
body: `{"action":"delete"}`,
wantRes: &Response{Action: "delete", Index: 1},
wantErr: false,
},
// PrevNode
{
2015-01-29 20:47:57 +03:00
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
{
2015-01-29 20:47:57 +03:00
hdr: "15",
body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
wantRes: &Response{
Action: "get",
Index: 15,
Node: &Node{
Key: "/foo",
Value: "bar",
ModifiedIndex: 12,
CreatedIndex: 10,
},
PrevNode: nil,
},
wantErr: false,
},
// PrevNode and Node
{
2015-01-29 20:47:57 +03:00
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
{
2015-01-29 20:47:57 +03:00
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)
2015-01-29 20:47:57 +03:00
h.Add("X-Etcd-Index", tt.hdr)
2015-01-29 03:46:58 +03:00
res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
2015-01-29 20:47:57 +03:00
if tt.wantErr != (err != nil) {
t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
}
2015-01-29 20:47:57 +03:00
if (res == nil) != (tt.wantRes == nil) {
t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
continue
2015-01-29 20:47:57 +03:00
} else if tt.wantRes == nil {
// expected and successfully got nil response
continue
}
2015-01-29 20:47:57 +03:00
if res.Action != tt.wantRes.Action {
t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
}
2015-01-29 20:47:57 +03:00
if res.Index != tt.wantRes.Index {
t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
}
2015-01-29 20:47:57 +03:00
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)
}
}