mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: break apart KeysAPI from httpClient
This commit is contained in:
parent
d7f9228133
commit
ce4df96e69
@ -1,60 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 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"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrUnavailable = errors.New("client: no available etcd endpoints")
|
|
||||||
ErrNoLeader = errors.New("client: no leader")
|
|
||||||
ErrKeyNoExist = errors.New("client: key does not exist")
|
|
||||||
ErrKeyExists = errors.New("client: key already exists")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client interface {
|
|
||||||
Create(key, value string, ttl time.Duration) (*Response, error)
|
|
||||||
Get(key string) (*Response, error)
|
|
||||||
Watch(key string, idx uint64) Watcher
|
|
||||||
RecursiveWatch(key string, idx uint64) Watcher
|
|
||||||
}
|
|
||||||
|
|
||||||
type Watcher interface {
|
|
||||||
Next() (*Response, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Response struct {
|
|
||||||
Action string `json:"action"`
|
|
||||||
Node *Node `json:"node"`
|
|
||||||
PrevNode *Node `json:"prevNode"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Nodes []*Node
|
|
||||||
type Node struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
Nodes Nodes `json:"nodes"`
|
|
||||||
ModifiedIndex uint64 `json:"modifiedIndex"`
|
|
||||||
CreatedIndex uint64 `json:"createdIndex"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) String() string {
|
|
||||||
return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
|
|
||||||
}
|
|
||||||
227
client/http.go
227
client/http.go
@ -17,21 +17,15 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultV2KeysPrefix = "/v2/keys"
|
|
||||||
ErrTimeout = context.DeadlineExceeded
|
ErrTimeout = context.DeadlineExceeded
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,14 +37,22 @@ type transport interface {
|
|||||||
CancelRequest(req *http.Request)
|
CancelRequest(req *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type httpAction interface {
|
||||||
|
httpRequest(url.URL) *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
type roundTripResponse struct {
|
||||||
|
resp *http.Response
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
type httpClient struct {
|
type httpClient struct {
|
||||||
transport transport
|
transport transport
|
||||||
endpoint url.URL
|
endpoint url.URL
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
v2KeysPrefix string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPClient(tr *http.Transport, ep string, timeout time.Duration) (*httpClient, error) {
|
func newHTTPClient(tr *http.Transport, ep string, to time.Duration) (*httpClient, error) {
|
||||||
u, err := url.Parse(ep)
|
u, err := url.Parse(ep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -59,68 +61,20 @@ func NewHTTPClient(tr *http.Transport, ep string, timeout time.Duration) (*httpC
|
|||||||
c := &httpClient{
|
c := &httpClient{
|
||||||
transport: tr,
|
transport: tr,
|
||||||
endpoint: *u,
|
endpoint: *u,
|
||||||
timeout: timeout,
|
timeout: to,
|
||||||
v2KeysPrefix: DefaultV2KeysPrefix,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) SetPrefix(p string) {
|
func (c *httpClient) doWithTimeout(act httpAction) (*http.Response, []byte, error) {
|
||||||
c.v2KeysPrefix = p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *httpClient) Endpoint() url.URL {
|
|
||||||
ep := c.endpoint
|
|
||||||
ep.Path = path.Join(ep.Path, c.v2KeysPrefix)
|
|
||||||
return ep
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *httpClient) Create(key, val string, ttl time.Duration) (*Response, error) {
|
|
||||||
create := &createAction{
|
|
||||||
Key: key,
|
|
||||||
Value: val,
|
|
||||||
}
|
|
||||||
if ttl >= 0 {
|
|
||||||
uttl := uint64(ttl.Seconds())
|
|
||||||
create.TTL = &uttl
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
|
||||||
httpresp, body, err := c.do(ctx, create)
|
defer cancel()
|
||||||
cancel()
|
return c.do(ctx, act)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return unmarshalHTTPResponse(httpresp.StatusCode, body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *httpClient) Get(key string) (*Response, error) {
|
|
||||||
get := &getAction{
|
|
||||||
Key: key,
|
|
||||||
Recursive: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
|
|
||||||
httpresp, body, err := c.do(ctx, get)
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return unmarshalHTTPResponse(httpresp.StatusCode, body)
|
|
||||||
}
|
|
||||||
|
|
||||||
type roundTripResponse struct {
|
|
||||||
resp *http.Response
|
|
||||||
err error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
req := act.httpRequest(c.Endpoint())
|
req := act.httpRequest(c.endpoint)
|
||||||
|
|
||||||
rtchan := make(chan roundTripResponse, 1)
|
rtchan := make(chan roundTripResponse, 1)
|
||||||
go func() {
|
go func() {
|
||||||
@ -157,154 +111,3 @@ func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []
|
|||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
return resp, body, err
|
return resp, body, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) Watch(key string, idx uint64) Watcher {
|
|
||||||
return &httpWatcher{
|
|
||||||
httpClient: *c,
|
|
||||||
nextWait: waitAction{
|
|
||||||
Key: key,
|
|
||||||
WaitIndex: idx,
|
|
||||||
Recursive: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *httpClient) RecursiveWatch(key string, idx uint64) Watcher {
|
|
||||||
return &httpWatcher{
|
|
||||||
httpClient: *c,
|
|
||||||
nextWait: waitAction{
|
|
||||||
Key: key,
|
|
||||||
WaitIndex: idx,
|
|
||||||
Recursive: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type httpWatcher struct {
|
|
||||||
httpClient
|
|
||||||
nextWait waitAction
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *httpWatcher) Next() (*Response, error) {
|
|
||||||
httpresp, body, err := hw.httpClient.do(context.Background(), &hw.nextWait)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := unmarshalHTTPResponse(httpresp.StatusCode, body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// v2KeysURL forms a URL representing the location of a key. The provided
|
|
||||||
// endpoint must be the root of the etcd keys API. For example, a valid
|
|
||||||
// endpoint probably has the path "/v2/keys".
|
|
||||||
func v2KeysURL(ep url.URL, key string) *url.URL {
|
|
||||||
ep.Path = path.Join(ep.Path, key)
|
|
||||||
return &ep
|
|
||||||
}
|
|
||||||
|
|
||||||
type httpAction interface {
|
|
||||||
httpRequest(url.URL) *http.Request
|
|
||||||
}
|
|
||||||
|
|
||||||
type getAction struct {
|
|
||||||
Key string
|
|
||||||
Recursive bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *getAction) httpRequest(ep url.URL) *http.Request {
|
|
||||||
u := v2KeysURL(ep, g.Key)
|
|
||||||
|
|
||||||
params := u.Query()
|
|
||||||
params.Set("recursive", strconv.FormatBool(g.Recursive))
|
|
||||||
u.RawQuery = params.Encode()
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
|
|
||||||
type waitAction struct {
|
|
||||||
Key string
|
|
||||||
WaitIndex uint64
|
|
||||||
Recursive bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *waitAction) httpRequest(ep url.URL) *http.Request {
|
|
||||||
u := v2KeysURL(ep, w.Key)
|
|
||||||
|
|
||||||
params := u.Query()
|
|
||||||
params.Set("wait", "true")
|
|
||||||
params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
|
|
||||||
params.Set("recursive", strconv.FormatBool(w.Recursive))
|
|
||||||
u.RawQuery = params.Encode()
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
|
|
||||||
type createAction struct {
|
|
||||||
Key string
|
|
||||||
Value string
|
|
||||||
TTL *uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *createAction) httpRequest(ep url.URL) *http.Request {
|
|
||||||
u := v2KeysURL(ep, c.Key)
|
|
||||||
|
|
||||||
params := u.Query()
|
|
||||||
params.Set("prevExist", "false")
|
|
||||||
u.RawQuery = params.Encode()
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Add("value", c.Value)
|
|
||||||
if c.TTL != nil {
|
|
||||||
form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
|
|
||||||
}
|
|
||||||
body := strings.NewReader(form.Encode())
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", u.String(), body)
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalHTTPResponse(code int, body []byte) (res *Response, err error) {
|
|
||||||
switch code {
|
|
||||||
case http.StatusOK, http.StatusCreated:
|
|
||||||
res, err = unmarshalSuccessfulResponse(body)
|
|
||||||
default:
|
|
||||||
err = unmarshalErrorResponse(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalSuccessfulResponse(body []byte) (*Response, error) {
|
|
||||||
var res Response
|
|
||||||
err := json.Unmarshal(body, &res)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func unmarshalErrorResponse(code int) error {
|
|
||||||
switch code {
|
|
||||||
case http.StatusNotFound:
|
|
||||||
return ErrKeyNoExist
|
|
||||||
case http.StatusPreconditionFailed:
|
|
||||||
return ErrKeyExists
|
|
||||||
case http.StatusInternalServerError:
|
|
||||||
// this isn't necessarily true
|
|
||||||
return ErrNoLeader
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("unrecognized HTTP status code %d", code)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -18,7 +18,6 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -30,334 +29,6 @@ import (
|
|||||||
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestV2URLHelper(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
endpoint url.URL
|
|
||||||
key string
|
|
||||||
want url.URL
|
|
||||||
}{
|
|
||||||
// key is empty, no problem
|
|
||||||
{
|
|
||||||
endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
|
|
||||||
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"},
|
|
||||||
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: ""},
|
|
||||||
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"},
|
|
||||||
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"},
|
|
||||||
key: "",
|
|
||||||
want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tt := range tests {
|
|
||||||
got := v2KeysURL(tt.endpoint, 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"}
|
|
||||||
wantURL := &url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: "example.com",
|
|
||||||
Path: "/v2/keys/foo/bar",
|
|
||||||
}
|
|
||||||
wantHeader := http.Header{}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
recursive bool
|
|
||||||
wantQuery string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
recursive: false,
|
|
||||||
wantQuery: "recursive=false",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
recursive: true,
|
|
||||||
wantQuery: "recursive=true",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tt := range tests {
|
|
||||||
f := getAction{
|
|
||||||
Key: "/foo/bar",
|
|
||||||
Recursive: tt.recursive,
|
|
||||||
}
|
|
||||||
got := *f.httpRequest(ep)
|
|
||||||
|
|
||||||
wantURL := wantURL
|
|
||||||
wantURL.RawQuery = tt.wantQuery
|
|
||||||
|
|
||||||
err := assertResponse(got, 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"}
|
|
||||||
wantURL := &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 := wantURL
|
|
||||||
wantURL.RawQuery = tt.wantQuery
|
|
||||||
|
|
||||||
err := assertResponse(got, wantURL, wantHeader, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("#%d: %v", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateAction(t *testing.T) {
|
|
||||||
ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
|
|
||||||
wantURL := &url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: "example.com",
|
|
||||||
Path: "/v2/keys/foo/bar",
|
|
||||||
RawQuery: "prevExist=false",
|
|
||||||
}
|
|
||||||
wantHeader := http.Header(map[string][]string{
|
|
||||||
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
|
||||||
})
|
|
||||||
|
|
||||||
ttl12 := uint64(12)
|
|
||||||
tests := []struct {
|
|
||||||
value string
|
|
||||||
ttl *uint64
|
|
||||||
wantBody string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
value: "baz",
|
|
||||||
wantBody: "value=baz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: "baz",
|
|
||||||
ttl: &ttl12,
|
|
||||||
wantBody: "ttl=12&value=baz",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tt := range tests {
|
|
||||||
f := createAction{
|
|
||||||
Key: "/foo/bar",
|
|
||||||
Value: tt.value,
|
|
||||||
TTL: tt.ttl,
|
|
||||||
}
|
|
||||||
got := *f.httpRequest(ep)
|
|
||||||
|
|
||||||
err := assertResponse(got, wantURL, wantHeader, []byte(tt.wantBody))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("#%d: %v", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertResponse(got http.Request, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
|
|
||||||
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=%v", 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=%v got.Body=%v", wantBody, gotBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnmarshalSuccessfulResponse(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
body string
|
|
||||||
res *Response
|
|
||||||
expectError bool
|
|
||||||
}{
|
|
||||||
// Neither PrevNode or Node
|
|
||||||
{
|
|
||||||
`{"action":"delete"}`,
|
|
||||||
&Response{Action: "delete"},
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
|
|
||||||
// PrevNode
|
|
||||||
{
|
|
||||||
`{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
||||||
&Response{Action: "delete", PrevNode: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Node
|
|
||||||
{
|
|
||||||
`{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
||||||
&Response{Action: "get", Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
|
|
||||||
// PrevNode and Node
|
|
||||||
{
|
|
||||||
`{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
||||||
&Response{Action: "update", PrevNode: &Node{Key: "/foo", Value: "baz", ModifiedIndex: 10, CreatedIndex: 10}, Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
|
||||||
false,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Garbage in body
|
|
||||||
{
|
|
||||||
`garbage`,
|
|
||||||
nil,
|
|
||||||
true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tt := range tests {
|
|
||||||
res, err := unmarshalSuccessfulResponse([]byte(tt.body))
|
|
||||||
if tt.expectError != (err != nil) {
|
|
||||||
t.Errorf("#%d: expectError=%t, err=%v", i, tt.expectError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == nil) != (tt.res == nil) {
|
|
||||||
t.Errorf("#%d: received res==%v, but expected res==%v", i, res, tt.res)
|
|
||||||
continue
|
|
||||||
} else if tt.res == nil {
|
|
||||||
// expected and succesfully got nil response
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Action != tt.res.Action {
|
|
||||||
t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.res.Action)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(res.Node, tt.res.Node) {
|
|
||||||
t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.res.Node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnmarshalErrorResponse(t *testing.T) {
|
|
||||||
unrecognized := errors.New("test fixture")
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
code int
|
|
||||||
want error
|
|
||||||
}{
|
|
||||||
{http.StatusBadRequest, unrecognized},
|
|
||||||
{http.StatusUnauthorized, unrecognized},
|
|
||||||
{http.StatusPaymentRequired, unrecognized},
|
|
||||||
{http.StatusForbidden, unrecognized},
|
|
||||||
{http.StatusNotFound, ErrKeyNoExist},
|
|
||||||
{http.StatusMethodNotAllowed, unrecognized},
|
|
||||||
{http.StatusNotAcceptable, unrecognized},
|
|
||||||
{http.StatusProxyAuthRequired, unrecognized},
|
|
||||||
{http.StatusRequestTimeout, unrecognized},
|
|
||||||
{http.StatusConflict, unrecognized},
|
|
||||||
{http.StatusGone, unrecognized},
|
|
||||||
{http.StatusLengthRequired, unrecognized},
|
|
||||||
{http.StatusPreconditionFailed, ErrKeyExists},
|
|
||||||
{http.StatusRequestEntityTooLarge, unrecognized},
|
|
||||||
{http.StatusRequestURITooLong, unrecognized},
|
|
||||||
{http.StatusUnsupportedMediaType, unrecognized},
|
|
||||||
{http.StatusRequestedRangeNotSatisfiable, unrecognized},
|
|
||||||
{http.StatusExpectationFailed, unrecognized},
|
|
||||||
{http.StatusTeapot, unrecognized},
|
|
||||||
|
|
||||||
{http.StatusInternalServerError, ErrNoLeader},
|
|
||||||
{http.StatusNotImplemented, unrecognized},
|
|
||||||
{http.StatusBadGateway, unrecognized},
|
|
||||||
{http.StatusServiceUnavailable, unrecognized},
|
|
||||||
{http.StatusGatewayTimeout, unrecognized},
|
|
||||||
{http.StatusHTTPVersionNotSupported, unrecognized},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tt := range tests {
|
|
||||||
want := tt.want
|
|
||||||
if reflect.DeepEqual(unrecognized, want) {
|
|
||||||
want = fmt.Errorf("unrecognized HTTP status code %d", tt.code)
|
|
||||||
}
|
|
||||||
|
|
||||||
got := unmarshalErrorResponse(tt.code)
|
|
||||||
if !reflect.DeepEqual(want, got) {
|
|
||||||
t.Errorf("#%d: want=%v, got=%v", i, want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeTransport struct {
|
type fakeTransport struct {
|
||||||
respchan chan *http.Response
|
respchan chan *http.Response
|
||||||
errchan chan error
|
errchan chan error
|
||||||
|
|||||||
276
client/keys.go
Normal file
276
client/keys.go
Normal file
@ -0,0 +1,276 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 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 (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultV2KeysPrefix = "/v2/keys"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrUnavailable = errors.New("client: no available etcd endpoints")
|
||||||
|
ErrNoLeader = errors.New("client: no leader")
|
||||||
|
ErrKeyNoExist = errors.New("client: key does not exist")
|
||||||
|
ErrKeyExists = errors.New("client: key already exists")
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewKeysAPI(tr *http.Transport, ep string, to time.Duration) (*HTTPKeysAPI, error) {
|
||||||
|
c, err := newHTTPClient(tr, ep, to)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
kAPI := HTTPKeysAPI{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &kAPI, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type KeysAPI interface {
|
||||||
|
Create(key, value string, ttl time.Duration) (*Response, error)
|
||||||
|
Get(key string) (*Response, error)
|
||||||
|
Watch(key string, idx uint64) Watcher
|
||||||
|
RecursiveWatch(key string, idx uint64) Watcher
|
||||||
|
}
|
||||||
|
|
||||||
|
type Watcher interface {
|
||||||
|
Next() (*Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
Node *Node `json:"node"`
|
||||||
|
PrevNode *Node `json:"prevNode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Nodes []*Node
|
||||||
|
type Node struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
Nodes Nodes `json:"nodes"`
|
||||||
|
ModifiedIndex uint64 `json:"modifiedIndex"`
|
||||||
|
CreatedIndex uint64 `json:"createdIndex"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) String() string {
|
||||||
|
return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
type HTTPKeysAPI struct {
|
||||||
|
client *httpClient
|
||||||
|
endpoint url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *HTTPKeysAPI) SetAPIPrefix(p string) {
|
||||||
|
ep := k.endpoint
|
||||||
|
ep.Path = path.Join(ep.Path, p)
|
||||||
|
k.client.endpoint = ep
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *HTTPKeysAPI) Create(key, val string, ttl time.Duration) (*Response, error) {
|
||||||
|
create := &createAction{
|
||||||
|
Key: key,
|
||||||
|
Value: val,
|
||||||
|
}
|
||||||
|
if ttl >= 0 {
|
||||||
|
uttl := uint64(ttl.Seconds())
|
||||||
|
create.TTL = &uttl
|
||||||
|
}
|
||||||
|
|
||||||
|
httpresp, body, err := k.client.doWithTimeout(create)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(httpresp.StatusCode, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *HTTPKeysAPI) Get(key string) (*Response, error) {
|
||||||
|
get := &getAction{
|
||||||
|
Key: key,
|
||||||
|
Recursive: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
httpresp, body, err := k.client.doWithTimeout(get)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return unmarshalHTTPResponse(httpresp.StatusCode, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *HTTPKeysAPI) Watch(key string, idx uint64) Watcher {
|
||||||
|
return &httpWatcher{
|
||||||
|
client: k.client,
|
||||||
|
nextWait: waitAction{
|
||||||
|
Key: key,
|
||||||
|
WaitIndex: idx,
|
||||||
|
Recursive: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *HTTPKeysAPI) RecursiveWatch(key string, idx uint64) Watcher {
|
||||||
|
return &httpWatcher{
|
||||||
|
client: k.client,
|
||||||
|
nextWait: waitAction{
|
||||||
|
Key: key,
|
||||||
|
WaitIndex: idx,
|
||||||
|
Recursive: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpWatcher struct {
|
||||||
|
client *httpClient
|
||||||
|
nextWait waitAction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hw *httpWatcher) Next() (*Response, error) {
|
||||||
|
//TODO(bcwaldon): This needs to be cancellable by the calling user
|
||||||
|
httpresp, body, err := hw.client.do(context.Background(), &hw.nextWait)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := unmarshalHTTPResponse(httpresp.StatusCode, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// v2KeysURL forms a URL representing the location of a key. The provided
|
||||||
|
// endpoint must be the root of the etcd keys API. For example, a valid
|
||||||
|
// endpoint probably has the path "/v2/keys".
|
||||||
|
func v2KeysURL(ep url.URL, key string) *url.URL {
|
||||||
|
ep.Path = path.Join(ep.Path, key)
|
||||||
|
return &ep
|
||||||
|
}
|
||||||
|
|
||||||
|
type getAction struct {
|
||||||
|
Key string
|
||||||
|
Recursive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *getAction) httpRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, g.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
params.Set("recursive", strconv.FormatBool(g.Recursive))
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type waitAction struct {
|
||||||
|
Key string
|
||||||
|
WaitIndex uint64
|
||||||
|
Recursive bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *waitAction) httpRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, w.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
params.Set("wait", "true")
|
||||||
|
params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
|
||||||
|
params.Set("recursive", strconv.FormatBool(w.Recursive))
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
type createAction struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
TTL *uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *createAction) httpRequest(ep url.URL) *http.Request {
|
||||||
|
u := v2KeysURL(ep, c.Key)
|
||||||
|
|
||||||
|
params := u.Query()
|
||||||
|
params.Set("prevExist", "false")
|
||||||
|
u.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Add("value", c.Value)
|
||||||
|
if c.TTL != nil {
|
||||||
|
form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
|
||||||
|
}
|
||||||
|
body := strings.NewReader(form.Encode())
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("PUT", u.String(), body)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalHTTPResponse(code int, body []byte) (res *Response, err error) {
|
||||||
|
switch code {
|
||||||
|
case http.StatusOK, http.StatusCreated:
|
||||||
|
res, err = unmarshalSuccessfulResponse(body)
|
||||||
|
default:
|
||||||
|
err = unmarshalErrorResponse(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalSuccessfulResponse(body []byte) (*Response, error) {
|
||||||
|
var res Response
|
||||||
|
err := json.Unmarshal(body, &res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalErrorResponse(code int) error {
|
||||||
|
switch code {
|
||||||
|
case http.StatusNotFound:
|
||||||
|
return ErrKeyNoExist
|
||||||
|
case http.StatusPreconditionFailed:
|
||||||
|
return ErrKeyExists
|
||||||
|
case http.StatusInternalServerError:
|
||||||
|
// this isn't necessarily true
|
||||||
|
return ErrNoLeader
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("unrecognized HTTP status code %d", code)
|
||||||
|
}
|
||||||
355
client/keys_test.go
Normal file
355
client/keys_test.go
Normal file
@ -0,0 +1,355 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestV2KeysURLHelper(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
endpoint url.URL
|
||||||
|
key string
|
||||||
|
want url.URL
|
||||||
|
}{
|
||||||
|
// key is empty, no problem
|
||||||
|
{
|
||||||
|
endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
|
||||||
|
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"},
|
||||||
|
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: ""},
|
||||||
|
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"},
|
||||||
|
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"},
|
||||||
|
key: "",
|
||||||
|
want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
got := v2KeysURL(tt.endpoint, 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"}
|
||||||
|
wantURL := &url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: "example.com",
|
||||||
|
Path: "/v2/keys/foo/bar",
|
||||||
|
}
|
||||||
|
wantHeader := http.Header{}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
recursive bool
|
||||||
|
wantQuery string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
recursive: false,
|
||||||
|
wantQuery: "recursive=false",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
recursive: true,
|
||||||
|
wantQuery: "recursive=true",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
f := getAction{
|
||||||
|
Key: "/foo/bar",
|
||||||
|
Recursive: tt.recursive,
|
||||||
|
}
|
||||||
|
got := *f.httpRequest(ep)
|
||||||
|
|
||||||
|
wantURL := wantURL
|
||||||
|
wantURL.RawQuery = tt.wantQuery
|
||||||
|
|
||||||
|
err := assertResponse(got, 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"}
|
||||||
|
wantURL := &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 := wantURL
|
||||||
|
wantURL.RawQuery = tt.wantQuery
|
||||||
|
|
||||||
|
err := assertResponse(got, wantURL, wantHeader, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("#%d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateAction(t *testing.T) {
|
||||||
|
ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
|
||||||
|
wantURL := &url.URL{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: "example.com",
|
||||||
|
Path: "/v2/keys/foo/bar",
|
||||||
|
RawQuery: "prevExist=false",
|
||||||
|
}
|
||||||
|
wantHeader := http.Header(map[string][]string{
|
||||||
|
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
||||||
|
})
|
||||||
|
|
||||||
|
ttl12 := uint64(12)
|
||||||
|
tests := []struct {
|
||||||
|
value string
|
||||||
|
ttl *uint64
|
||||||
|
wantBody string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
value: "baz",
|
||||||
|
wantBody: "value=baz",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "baz",
|
||||||
|
ttl: &ttl12,
|
||||||
|
wantBody: "ttl=12&value=baz",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
f := createAction{
|
||||||
|
Key: "/foo/bar",
|
||||||
|
Value: tt.value,
|
||||||
|
TTL: tt.ttl,
|
||||||
|
}
|
||||||
|
got := *f.httpRequest(ep)
|
||||||
|
|
||||||
|
err := assertResponse(got, wantURL, wantHeader, []byte(tt.wantBody))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("#%d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertResponse(got http.Request, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
|
||||||
|
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=%v", 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=%v got.Body=%v", wantBody, gotBytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalSuccessfulResponse(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
body string
|
||||||
|
res *Response
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
// Neither PrevNode or Node
|
||||||
|
{
|
||||||
|
`{"action":"delete"}`,
|
||||||
|
&Response{Action: "delete"},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// PrevNode
|
||||||
|
{
|
||||||
|
`{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
||||||
|
&Response{Action: "delete", PrevNode: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Node
|
||||||
|
{
|
||||||
|
`{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
||||||
|
&Response{Action: "get", Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// PrevNode and Node
|
||||||
|
{
|
||||||
|
`{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
||||||
|
&Response{Action: "update", PrevNode: &Node{Key: "/foo", Value: "baz", ModifiedIndex: 10, CreatedIndex: 10}, Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Garbage in body
|
||||||
|
{
|
||||||
|
`garbage`,
|
||||||
|
nil,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
res, err := unmarshalSuccessfulResponse([]byte(tt.body))
|
||||||
|
if tt.expectError != (err != nil) {
|
||||||
|
t.Errorf("#%d: expectError=%t, err=%v", i, tt.expectError, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res == nil) != (tt.res == nil) {
|
||||||
|
t.Errorf("#%d: received res==%v, but expected res==%v", i, res, tt.res)
|
||||||
|
continue
|
||||||
|
} else if tt.res == nil {
|
||||||
|
// expected and succesfully got nil response
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Action != tt.res.Action {
|
||||||
|
t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.res.Action)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(res.Node, tt.res.Node) {
|
||||||
|
t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.res.Node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalErrorResponse(t *testing.T) {
|
||||||
|
unrecognized := errors.New("test fixture")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
code int
|
||||||
|
want error
|
||||||
|
}{
|
||||||
|
{http.StatusBadRequest, unrecognized},
|
||||||
|
{http.StatusUnauthorized, unrecognized},
|
||||||
|
{http.StatusPaymentRequired, unrecognized},
|
||||||
|
{http.StatusForbidden, unrecognized},
|
||||||
|
{http.StatusNotFound, ErrKeyNoExist},
|
||||||
|
{http.StatusMethodNotAllowed, unrecognized},
|
||||||
|
{http.StatusNotAcceptable, unrecognized},
|
||||||
|
{http.StatusProxyAuthRequired, unrecognized},
|
||||||
|
{http.StatusRequestTimeout, unrecognized},
|
||||||
|
{http.StatusConflict, unrecognized},
|
||||||
|
{http.StatusGone, unrecognized},
|
||||||
|
{http.StatusLengthRequired, unrecognized},
|
||||||
|
{http.StatusPreconditionFailed, ErrKeyExists},
|
||||||
|
{http.StatusRequestEntityTooLarge, unrecognized},
|
||||||
|
{http.StatusRequestURITooLong, unrecognized},
|
||||||
|
{http.StatusUnsupportedMediaType, unrecognized},
|
||||||
|
{http.StatusRequestedRangeNotSatisfiable, unrecognized},
|
||||||
|
{http.StatusExpectationFailed, unrecognized},
|
||||||
|
{http.StatusTeapot, unrecognized},
|
||||||
|
|
||||||
|
{http.StatusInternalServerError, ErrNoLeader},
|
||||||
|
{http.StatusNotImplemented, unrecognized},
|
||||||
|
{http.StatusBadGateway, unrecognized},
|
||||||
|
{http.StatusServiceUnavailable, unrecognized},
|
||||||
|
{http.StatusGatewayTimeout, unrecognized},
|
||||||
|
{http.StatusHTTPVersionNotSupported, unrecognized},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
want := tt.want
|
||||||
|
if reflect.DeepEqual(unrecognized, want) {
|
||||||
|
want = fmt.Errorf("unrecognized HTTP status code %d", tt.code)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := unmarshalErrorResponse(tt.code)
|
||||||
|
if !reflect.DeepEqual(want, got) {
|
||||||
|
t.Errorf("#%d: want=%v, got=%v", i, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -58,7 +58,7 @@ type discovery struct {
|
|||||||
cluster string
|
cluster string
|
||||||
id uint64
|
id uint64
|
||||||
config string
|
config string
|
||||||
c client.Client
|
c client.KeysAPI
|
||||||
retries uint
|
retries uint
|
||||||
url *url.URL
|
url *url.URL
|
||||||
|
|
||||||
@ -105,13 +105,13 @@ func New(durl string, id uint64, config string) (Discoverer, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c, err := client.NewHTTPClient(&http.Transport{Proxy: pf}, u.String(), time.Second*5)
|
c, err := client.NewKeysAPI(&http.Transport{Proxy: pf}, u.String(), time.Second*5)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// discovery service redirects /[key] to /v2/keys/[key]
|
// discovery service redirects /[key] to /v2/keys/[key]
|
||||||
// set the prefix of client to "" to handle this
|
// set the prefix of client to "" to handle this
|
||||||
c.SetPrefix("")
|
c.SetAPIPrefix("")
|
||||||
return &discovery{
|
return &discovery{
|
||||||
cluster: token,
|
cluster: token,
|
||||||
id: id,
|
id: id,
|
||||||
|
|||||||
@ -304,7 +304,7 @@ func TestCreateSelf(t *testing.T) {
|
|||||||
errwc := &clientWithResp{rs, errw}
|
errwc := &clientWithResp{rs, errw}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
c client.Client
|
c client.KeysAPI
|
||||||
werr error
|
werr error
|
||||||
}{
|
}{
|
||||||
// no error
|
// no error
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user