From 00935c873fcfb2933296d2c58d95ed8b932afc4d Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 11 Jul 2014 11:40:02 -0700 Subject: [PATCH] etcd: refactor tests --- etcd/etcd_test.go | 33 + etcd/v2_http_kv_test.go | 1674 +++++++++++++++++---------------------- etcd/v2_util.go | 51 +- 3 files changed, 774 insertions(+), 984 deletions(-) diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index 6bc376d3f..f8917b58e 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "net/url" "testing" "time" @@ -42,6 +43,38 @@ func TestMultipleTLSNodes(t *testing.T) { afterTest(t) } +func TestV2Redirect(t *testing.T) { + es, hs := buildCluster(3, false) + waitCluster(t, es) + u := hs[1].URL + ru := fmt.Sprintf("%s%s", hs[0].URL, "/v2/keys/foo") + tc := NewTestClient() + + v := url.Values{} + v.Set("value", "XXX") + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) + if resp.StatusCode != http.StatusTemporaryRedirect { + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusTemporaryRedirect) + } + location, err := resp.Location() + if err != nil { + t.Errorf("want err = %, want nil", err) + } + + if location.String() != ru { + t.Errorf("location = %v, want %v", location.String(), ru) + } + + resp.Body.Close() + for i := range es { + es[len(es)-i-1].Stop() + } + for i := range hs { + hs[len(hs)-i-1].Close() + } + afterTest(t) +} + func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) { bootstrapper := 0 es := make([]*Server, number) diff --git a/etcd/v2_http_kv_test.go b/etcd/v2_http_kv_test.go index 8228595c2..82771a0a2 100644 --- a/etcd/v2_http_kv_test.go +++ b/etcd/v2_http_kv_test.go @@ -1,947 +1,668 @@ package etcd -// Ensures that a value can be retrieve for a given key. - import ( "fmt" "net/http" "net/url" + "reflect" "testing" "time" - - "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert" ) -// Ensures that a directory is created -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true -// -func TestV2SetDirectory(t *testing.T) { +func TestV2Set(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a time-to-live is added to a key. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=20 -// -func TestV2SetKeyWithTTL(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - t0 := time.Now() + tc := NewTestClient() v := url.Values{} - v.Set("value", "XXX") - v.Set("ttl", "20") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBodyJSON(resp) - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["ttl"], 20, "") + v.Set("value", "bar") + + tests := []struct { + relativeURL string + value url.Values + wStatus int + w string + }{ + { + "/v2/keys/foo/bar", + v, + http.StatusCreated, + `{"action":"set","node":{"key":"/foo/bar","value":"bar","modifiedIndex":2,"createdIndex":2}}`, + }, + { + "/v2/keys/foodir?dir=true", + url.Values{}, + http.StatusCreated, + `{"action":"set","node":{"key":"/foodir","dir":true,"modifiedIndex":3,"createdIndex":3}}`, + }, + { + "/v2/keys/fooempty", + url.Values(map[string][]string{"value": {""}}), + http.StatusCreated, + `{"action":"set","node":{"key":"/fooempty","value":"","modifiedIndex":4,"createdIndex":4}}`, + }, + } + + for i, tt := range tests { + resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value) + if err != nil { + t.Errorf("#%d: err = %v, want nil", i, err) + } + g := string(tc.ReadBody(resp)) + if g != tt.w { + t.Errorf("#%d: body = %v, want %v", i, g, tt.w) + } + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + } - // Make sure the expiration date is correct. - expiration, _ := time.Parse(time.RFC3339Nano, node["expiration"].(string)) - assert.Equal(t, expiration.Sub(t0)/time.Second, 20, "") es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that an invalid time-to-live is returned as an error. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=bad_ttl -// -func TestV2SetKeyWithBadTTL(t *testing.T) { +func TestV2CreateUpdate(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - v.Set("ttl", "bad_ttl") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusBadRequest) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 202, "") - assert.Equal(t, body["message"], "The given TTL in POST form is not a number", "") - assert.Equal(t, body["cause"], "Update", "") + tc := NewTestClient() + + tests := []struct { + relativeURL string + value url.Values + wStatus int + w map[string]interface{} + }{ + // key with ttl + { + "/v2/keys/ttl/foo", + url.Values(map[string][]string{"value": {"XXX"}, "ttl": {"20"}}), + http.StatusCreated, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "XXX", + "ttl": float64(20), + }, + }, + }, + // key with bad ttl + { + "/v2/keys/ttl/foo", + url.Values(map[string][]string{"value": {"XXX"}, "ttl": {"bad_ttl"}}), + http.StatusBadRequest, + map[string]interface{}{ + "errorCode": float64(202), + "message": "The given TTL in POST form is not a number", + "cause": "Update", + }, + }, + // create key + { + "/v2/keys/create/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"false"}}), + http.StatusCreated, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "XXX", + }, + }, + }, + // created key failed + { + "/v2/keys/create/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"false"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(105), + "message": "Key already exists", + "cause": "/create/foo", + }, + }, + // update the newly created key with ttl + { + "/v2/keys/create/foo", + url.Values(map[string][]string{"value": {"YYY"}, "prevExist": {"true"}, "ttl": {"20"}}), + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "YYY", + "ttl": float64(20), + }, + "action": "update", + }, + }, + // update the ttl to none + { + "/v2/keys/create/foo", + url.Values(map[string][]string{"value": {"ZZZ"}, "prevExist": {"true"}}), + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "ZZZ", + }, + "action": "update", + }, + }, + // update on a non-existing key + { + "/v2/keys/nonexist", + url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"true"}}), + http.StatusNotFound, + map[string]interface{}{ + "errorCode": float64(100), + "message": "Key not found", + "cause": "/nonexist", + }, + }, + } + + for i, tt := range tests { + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } + } + es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that a key is conditionally set if it previously did not exist. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false -// -func TestV2CreateKeySuccess(t *testing.T) { +func TestV2CAS(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - v.Set("prevExist", "false") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBodyJSON(resp) - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["value"], "XXX", "") + tc := NewTestClient() + + tests := []struct { + relativeURL string + value url.Values + wStatus int + w map[string]interface{} + }{ + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}}), + http.StatusCreated, + nil, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"2"}}), + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "YYY", + "modifiedIndex": float64(3), + }, + "action": "compareAndSwap", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"10"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[10 != 3]", + "index": float64(3), + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"bad_index"}}), + http.StatusBadRequest, + map[string]interface{}{ + "errorCode": float64(203), + "message": "The given index in POST form is not a number", + "cause": "CompareAndSwap", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"ZZZ"}, "prevValue": {"YYY"}}), + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "value": "ZZZ", + }, + "action": "compareAndSwap", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[bad_value != ZZZ]", + }, + }, + // prevValue is required + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {""}}), + http.StatusBadRequest, + map[string]interface{}{ + "errorCode": float64(201), + "message": "PrevValue is Required in POST form", + "cause": "CompareAndSwap", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}, "prevIndex": {"100"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[bad_value != ZZZ] [100 != 4]", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"ZZZ"}, "prevIndex": {"100"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[100 != 4]", + }, + }, + { + "/v2/keys/cas/foo", + url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}, "prevIndex": {"4"}}), + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[bad_value != ZZZ]", + }, + }, + } + + for i, tt := range tests { + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } + } + es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that a key is not conditionally set because it previously existed. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false -> fail -// -func TestV2CreateKeyFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - v.Set("prevExist", "false") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 105, "") - assert.Equal(t, body["message"], "Key already exists", "") - assert.Equal(t, body["cause"], "/foo/bar", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is conditionally set only if it previously did exist. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true -// -func TestV2UpdateKeySuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - - v.Set("value", "YYY") - v.Set("prevExist", "true") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "update", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not conditionally set if it previously did not exist. -// -// $ curl -X PUT localhost:4001/v2/keys/foo?dir=true -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true -// -func TestV2UpdateKeyFailOnValue(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), v) - resp.Body.Close() - - assert.Equal(t, resp.StatusCode, http.StatusCreated) - v.Set("value", "YYY") - v.Set("prevExist", "true") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusNotFound) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 100, "") - assert.Equal(t, body["message"], "Key not found", "") - assert.Equal(t, body["cause"], "/foo/bar", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not conditionally set if it previously did not exist. -// -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=YYY -d prevExist=true -> fail -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true -> fail -// -func TestV2UpdateKeyFailOnMissingDirectory(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "YYY") - v.Set("prevExist", "true") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - assert.Equal(t, resp.StatusCode, http.StatusNotFound) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 100, "") - assert.Equal(t, body["message"], "Key not found", "") - assert.Equal(t, body["cause"], "/foo", "") - - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusNotFound) - body = ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 100, "") - assert.Equal(t, body["message"], "Key not found", "") - assert.Equal(t, body["cause"], "/foo", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key could update TTL. -// -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d ttl=1000 -d prevExist=true -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d ttl= -d prevExist=true -// -func TestV2UpdateKeySuccessWithTTL(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - node := (ReadBodyJSON(resp)["node"]).(map[string]interface{}) - createdIndex := node["createdIndex"] - - v.Set("ttl", "1000") - v.Set("prevExist", "true") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - assert.Equal(t, resp.StatusCode, http.StatusOK) - node = (ReadBodyJSON(resp)["node"]).(map[string]interface{}) - assert.Equal(t, node["value"], "XXX", "") - assert.Equal(t, node["ttl"], 1000, "") - assert.NotEqual(t, node["expiration"], "", "") - assert.Equal(t, node["createdIndex"], createdIndex, "") - - v.Del("ttl") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - assert.Equal(t, resp.StatusCode, http.StatusOK) - node = (ReadBodyJSON(resp)["node"]).(map[string]interface{}) - assert.Equal(t, node["value"], "XXX", "") - assert.Equal(t, node["ttl"], nil, "") - assert.Equal(t, node["expiration"], nil, "") - assert.Equal(t, node["createdIndex"], createdIndex, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is set only if the previous index matches. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=1 -// -func TestV2SetKeyCASOnIndexSuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - - v.Set("value", "YYY") - v.Set("prevIndex", "2") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "compareAndSwap", "") - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["value"], "YYY", "") - assert.Equal(t, node["modifiedIndex"], 3, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not set if the previous index does not match. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=10 -// -func TestV2SetKeyCASOnIndexFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevIndex", "10") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101, "") - assert.Equal(t, body["message"], "Compare failed", "") - assert.Equal(t, body["cause"], "[10 != 2]", "") - assert.Equal(t, body["index"], 2, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that an error is thrown if an invalid previous index is provided. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index -// -func TestV2SetKeyCASWithInvalidIndex(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "YYY") - v.Set("prevIndex", "bad_index") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusBadRequest) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 203, "") - assert.Equal(t, body["message"], "The given index in POST form is not a number", "") - assert.Equal(t, body["cause"], "CompareAndSwap", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is set only if the previous value matches. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX -// -func TestV2SetKeyCASOnValueSuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevValue", "XXX") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "compareAndSwap", "") - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["value"], "YYY", "") - assert.Equal(t, node["modifiedIndex"], 3, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not set if the previous value does not match. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA -// -func TestV2SetKeyCASOnValueFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevValue", "AAA") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101, "") - assert.Equal(t, body["message"], "Compare failed", "") - assert.Equal(t, body["cause"], "[AAA != XXX]", "") - assert.Equal(t, body["index"], 2, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that an error is returned if a blank prevValue is set. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue= -// -func TestV2SetKeyCASWithMissingValueFails(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - v.Set("prevValue", "") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusBadRequest) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 201, "") - assert.Equal(t, body["message"], "PrevValue is Required in POST form", "") - assert.Equal(t, body["cause"], "CompareAndSwap", "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not set if both previous value and index do not match. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA -d prevIndex=4 -// -func TestV2SetKeyCASOnValueAndIndexFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevValue", "AAA") - v.Set("prevIndex", "4") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101, "") - assert.Equal(t, body["message"], "Compare failed", "") - assert.Equal(t, body["cause"], "[AAA != XXX] [4 != 2]", "") - assert.Equal(t, body["index"], 2, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not set if previous value match but index does not. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX -d prevIndex=4 -// -func TestV2SetKeyCASOnValueMatchAndIndexFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevValue", "XXX") - v.Set("prevIndex", "4") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101, "") - assert.Equal(t, body["message"], "Compare failed", "") - assert.Equal(t, body["cause"], "[4 != 2]", "") - assert.Equal(t, body["index"], 2, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not set if previous index matches but value does not. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA -d prevIndex=3 -// -func TestV2SetKeyCASOnIndexMatchAndValueFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - ReadBody(resp) - v.Set("value", "YYY") - v.Set("prevValue", "AAA") - v.Set("prevIndex", "2") - resp, _ = PutForm(fullURL, v) - assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101, "") - assert.Equal(t, body["message"], "Compare failed", "") - assert.Equal(t, body["cause"], "[AAA != XXX]", "") - assert.Equal(t, body["index"], 2, "") - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensure that we can set an empty value -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value= -// -func TestV2SetKeyCASWithEmptyValueSuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - v := url.Values{} - v.Set("value", "") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBody(resp) - assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"","modifiedIndex":2,"createdIndex":2}}`) - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -func TestV2SetKey(t *testing.T) { +func TestV2Delete(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL + tc := NewTestClient() v := url.Values{} v.Set("value", "XXX") - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "") - - resp.Body.Close() - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -func TestV2SetKeyRedirect(t *testing.T) { - es, hs := buildCluster(3, false) - waitCluster(t, es) - u := hs[1].URL - ru := fmt.Sprintf("%s%s", hs[0].URL, "/v2/keys/foo/bar") - - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect) - location, err := resp.Location() + resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) if err != nil { - t.Errorf("want err = %, want nil", err) + t.Error(err) + } + resp.Body.Close() + resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/emptydir?dir=true"), v) + if err != nil { + t.Error(err) + } + resp.Body.Close() + resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foodir/bar?dir=true"), v) + if err != nil { + t.Error(err) + } + resp.Body.Close() + + tests := []struct { + relativeURL string + wStatus int + w map[string]interface{} + }{ + { + "/v2/keys/foo", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo", + }, + "prevNode": map[string]interface{}{ + "key": "/foo", + "value": "XXX", + }, + "action": "delete", + }, + }, + { + "/v2/keys/emptydir", + http.StatusForbidden, + map[string]interface{}{ + "errorCode": float64(102), + "message": "Not a file", + "cause": "/emptydir", + }, + }, + { + "/v2/keys/emptydir?dir=true", + http.StatusOK, + nil, + }, + { + "/v2/keys/foodir?dir=true", + http.StatusForbidden, + map[string]interface{}{ + "errorCode": float64(108), + "message": "Directory not empty", + "cause": "/foodir", + }, + }, + { + "/v2/keys/foodir?recursive=true", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foodir", + "dir": true, + }, + "prevNode": map[string]interface{}{ + "key": "/foodir", + "dir": true, + }, + "action": "delete", + }, + }, } - if location.String() != ru { - t.Errorf("location = %v, want %v", location.String(), ru) + for i, tt := range tests { + resp, _ := tc.DeleteForm(fmt.Sprintf("%s%s", u, tt.relativeURL), nil) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } } - resp.Body.Close() - for i := range es { - es[len(es)-i-1].Stop() + es[0].Stop() + hs[0].Close() + afterTest(t) +} + +func TestV2CAD(t *testing.T) { + es, hs := buildCluster(1, false) + u := hs[0].URL + tc := NewTestClient() + + v := url.Values{} + v.Set("value", "XXX") + resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) + if err != nil { + t.Error(err) } - for i := range hs { - hs[len(hs)-i-1].Close() + resp.Body.Close() + + resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foovalue"), v) + if err != nil { + t.Error(err) } - afterTest(t) -} - -// Ensures that a key is deleted. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo/bar -// -func TestV2DeleteKey(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - resp.Body.Close() - ReadBody(resp) - - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "") resp.Body.Close() + tests := []struct { + relativeURL string + wStatus int + w map[string]interface{} + }{ + { + "/v2/keys/foo?prevIndex=100", + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[100 != 2]", + }, + }, + { + "/v2/keys/foo?prevIndex=bad_index", + http.StatusBadRequest, + map[string]interface{}{ + "errorCode": float64(203), + "message": "The given index in POST form is not a number", + "cause": "CompareAndDelete", + }, + }, + { + "/v2/keys/foo?prevIndex=2", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo", + "modifiedIndex": float64(4), + }, + "action": "compareAndDelete", + }, + }, + { + "/v2/keys/foovalue?prevValue=YYY", + http.StatusPreconditionFailed, + map[string]interface{}{ + "errorCode": float64(101), + "message": "Compare failed", + "cause": "[YYY != XXX]", + }, + }, + { + "/v2/keys/foovalue?prevValue=", + http.StatusBadRequest, + map[string]interface{}{ + "errorCode": float64(201), + "message": "PrevValue is Required in POST form", + "cause": "CompareAndDelete", + }, + }, + { + "/v2/keys/foovalue?prevValue=XXX", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foovalue", + "modifiedIndex": float64(5), + }, + "action": "compareAndDelete", + }, + }, + } + + for i, tt := range tests { + resp, _ := tc.DeleteForm(fmt.Sprintf("%s%s", u, tt.relativeURL), nil) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } + } + es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that an empty directory is deleted when dir is set. -// -// $ curl -X PUT localhost:4001/v2/keys/foo?dir=true -// $ curl -X DELETE localhost:4001/v2/keys/foo ->fail -// $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true -// -func TestV2DeleteEmptyDirectory(t *testing.T) { +func TestV2Unique(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL + tc := NewTestClient() - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{}) + tests := []struct { + relativeURL string + value url.Values + wStatus int + w map[string]interface{} + }{ + { + "/v2/keys/foo", + url.Values(map[string][]string{"value": {"XXX"}}), + http.StatusCreated, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo/2", + "value": "XXX", + }, + "action": "create", + }, + }, + { + "/v2/keys/foo", + url.Values(map[string][]string{"value": {"XXX"}}), + http.StatusCreated, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo/3", + "value": "XXX", + }, + "action": "create", + }, + }, + { + "/v2/keys/bar", + url.Values(map[string][]string{"value": {"XXX"}}), + http.StatusCreated, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/bar/4", + "value": "XXX", + }, + "action": "create", + }, + }, + } + + for i, tt := range tests { + resp, _ := tc.PostForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } + } + + es[0].Stop() + hs[0].Close() + afterTest(t) +} + +func TestV2Get(t *testing.T) { + es, hs := buildCluster(1, false) + u := hs[0].URL + tc := NewTestClient() + + v := url.Values{} + v.Set("value", "XXX") + resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar/zar"), v) + if err != nil { + t.Error(err) + } resp.Body.Close() - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusForbidden) - bodyJson := ReadBodyJSON(resp) - assert.Equal(t, bodyJson["errorCode"], 102, "") - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "") + tests := []struct { + relativeURL string + wStatus int + w map[string]interface{} + }{ + { + "/v2/keys/foo/bar/zar", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo/bar/zar", + "value": "XXX", + }, + "action": "get", + }, + }, + { + "/v2/keys/foo", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo", + "dir": true, + "nodes": []interface{}{ + map[string]interface{}{ + "key": "/foo/bar", + "dir": true, + "createdIndex": float64(2), + "modifiedIndex": float64(2), + }, + }, + }, + "action": "get", + }, + }, + { + "/v2/keys/foo?recursive=true", + http.StatusOK, + map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo", + "dir": true, + "nodes": []interface{}{ + map[string]interface{}{ + "key": "/foo/bar", + "dir": true, + "createdIndex": float64(2), + "modifiedIndex": float64(2), + "nodes": []interface{}{ + map[string]interface{}{ + "key": "/foo/bar/zar", + "value": "XXX", + "createdIndex": float64(2), + "modifiedIndex": float64(2), + }, + }, + }, + }, + }, + "action": "get", + }, + }, + } + + for i, tt := range tests { + resp, _ := tc.Get(fmt.Sprintf("%s%s", u, tt.relativeURL)) + if resp.StatusCode != tt.wStatus { + t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus) + } + if resp.Header.Get("Content-Type") != "application/json" { + t.Errorf("#%d: header = %v, want %v", resp.Header.Get("Content-Type"), "application/json") + } + if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil { + t.Errorf("#%d: %v", i, err) + } + } es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that a not-empty directory is deleted when dir is set. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true -// $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true ->fail -// $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true&recursive=true -// -func TestV2DeleteNonEmptyDirectory(t *testing.T) { +func TestV2Watch(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL - - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?dir=true"), url.Values{}) - ReadBody(resp) - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusForbidden) - bodyJson := ReadBodyJSON(resp) - assert.Equal(t, bodyJson["errorCode"], 108, "") - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true&recursive=true"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a directory is deleted when recursive is set. -// -// $ curl -X PUT localhost:4001/v2/keys/foo?dir=true -// $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true -// -func TestV2DeleteDirectoryRecursiveImpliesDir(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{}) - ReadBody(resp) - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?recursive=true"), url.Values{}) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBody(resp) - assert.Nil(t, err, "") - assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is deleted if the previous index matches -// -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo?prevIndex=3 -// -func TestV2DeleteKeyCADOnIndexSuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - ReadBody(resp) - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?prevIndex=2"), url.Values{}) - assert.Nil(t, err, "") - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "compareAndDelete", "") - - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo", "") - assert.Equal(t, node["modifiedIndex"], 3, "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not deleted if the previous index does not match -// -// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo?prevIndex=100 -// -func TestV2DeleteKeyCADOnIndexFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v) - ReadBody(resp) - resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?prevIndex=100"), url.Values{}) - assert.Nil(t, err, "") - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101) - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that an error is thrown if an invalid previous index is provided. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=bad_index -// -func TestV2DeleteKeyCADWithInvalidIndex(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevIndex=bad_index"), v) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 203) - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is deleted only if the previous value matches. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=XXX -// -func TestV2DeleteKeyCADOnValueSuccess(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue=XXX"), v) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "compareAndDelete", "") - - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["modifiedIndex"], 3, "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a key is not deleted if the previous value does not match. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=YYY -// -func TestV2DeleteKeyCADOnValueFail(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue=YYY"), v) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 101) - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that an error is thrown if an invalid previous value is provided. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex= -// -func TestV2DeleteKeyCADWithInvalidValue(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue="), v) - body := ReadBodyJSON(resp) - assert.Equal(t, body["errorCode"], 201) - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures a unique value is added to the key's children. -// -// $ curl -X POST localhost:4001/v2/keys/foo/bar -// $ curl -X POST localhost:4001/v2/keys/foo/bar -// $ curl -X POST localhost:4001/v2/keys/foo/baz -// -func TestV2CreateUnique(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - // POST should add index to list. - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := PostForm(fullURL, nil) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "create", "") - - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/bar/2", "") - assert.Nil(t, node["dir"], "") - assert.Equal(t, node["modifiedIndex"], 2, "") - - // Second POST should add next index to list. - resp, _ = PostForm(fullURL, nil) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body = ReadBodyJSON(resp) - - node = body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/bar/3", "") - - // POST to a different key should add index to that list. - resp, _ = PostForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/baz"), nil) - assert.Equal(t, resp.StatusCode, http.StatusCreated) - body = ReadBodyJSON(resp) - - node = body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/baz/4", "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// -// $ curl localhost:4001/v2/keys/foo/bar -> fail -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl localhost:4001/v2/keys/foo/bar -// -func TestV2GetKey(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := Get(fullURL) - resp.Body.Close() - - resp, _ = PutForm(fullURL, v) - resp.Body.Close() - - resp, _ = Get(fullURL) - assert.Equal(t, resp.Header.Get("Content-Type"), "application/json") - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBodyJSON(resp) - resp.Body.Close() - assert.Equal(t, body["action"], "get", "") - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/bar", "") - assert.Equal(t, node["value"], "XXX", "") - assert.Equal(t, node["modifiedIndex"], 2, "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a directory of values can be recursively retrieved for a given key. -// -// $ curl -X PUT localhost:4001/v2/keys/foo/x -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/y/z -d value=YYY -// $ curl localhost:4001/v2/keys/foo -d recursive=true -// -func TestV2GetKeyRecursively(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - v := url.Values{} - v.Set("value", "XXX") - v.Set("ttl", "10") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/x"), v) - ReadBody(resp) - - v.Set("value", "YYY") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/y/z"), v) - ReadBody(resp) - - resp, _ = Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo?recursive=true")) - assert.Equal(t, resp.StatusCode, http.StatusOK) - body := ReadBodyJSON(resp) - assert.Equal(t, body["action"], "get", "") - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo", "") - assert.Equal(t, node["dir"], true, "") - assert.Equal(t, node["modifiedIndex"], 2, "") - assert.Equal(t, len(node["nodes"].([]interface{})), 2, "") - - // TODO(xiangli): fix the wrong assumption here. - // the order of nodes map cannot be determined. - node0 := node["nodes"].([]interface{})[0].(map[string]interface{}) - assert.Equal(t, node0["key"], "/foo/x", "") - assert.Equal(t, node0["value"], "XXX", "") - assert.Equal(t, node0["ttl"], 10, "") - - node1 := node["nodes"].([]interface{})[1].(map[string]interface{}) - assert.Equal(t, node1["key"], "/foo/y", "") - assert.Equal(t, node1["dir"], true, "") - - node2 := node1["nodes"].([]interface{})[0].(map[string]interface{}) - assert.Equal(t, node2["key"], "/foo/y/z", "") - assert.Equal(t, node2["value"], "YYY", "") - - es[0].Stop() - hs[0].Close() - afterTest(t) -} - -// Ensures that a watcher can wait for a value to be set and return it to the client. -// -// $ curl localhost:4001/v2/keys/foo/bar?wait=true -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// -func TestV2WatchKey(t *testing.T) { - es, hs := buildCluster(1, false) - u := hs[0].URL - - // There exists a little gap between etcd ready to serve and - // it actually serves the first request, which means the response - // delay could be a little bigger. - // This test is time sensitive, so it does one request to ensure - // that the server is working. - resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")) - resp.Body.Close() + tc := NewTestClient() var watchResp *http.Response c := make(chan bool) go func() { - watchResp, _ = Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true")) + watchResp, _ = tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true")) c <- true }() @@ -951,101 +672,96 @@ func TestV2WatchKey(t *testing.T) { // Set a value. v := url.Values{} v.Set("value", "XXX") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - - // A response should follow from the GET above. - time.Sleep(1 * time.Millisecond) + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) + resp.Body.Close() select { case <-c: - default: + case <-time.After(time.Millisecond): t.Fatal("cannot get watch result") } - body := ReadBodyJSON(watchResp) - watchResp.Body.Close() - assert.NotNil(t, body, "") - assert.Equal(t, body["action"], "set", "") + body := tc.ReadBodyJSON(watchResp) + w := map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo/bar", + "value": "XXX", + "modifiedIndex": float64(2), + }, + "action": "set", + } - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/bar", "") - assert.Equal(t, node["value"], "XXX", "") - assert.Equal(t, node["modifiedIndex"], 2, "") + if err := checkBody(body, w); err != nil { + t.Error(err) + } es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that a watcher can wait for a value to be set after a given index. -// -// $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=3 -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -// -func TestV2WatchKeyWithIndex(t *testing.T) { +func TestV2WatchWithIndex(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL + tc := NewTestClient() var body map[string]interface{} - c := make(chan bool) + c := make(chan bool, 1) go func() { - resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=3")) - body = ReadBodyJSON(resp) + resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=3")) + body = tc.ReadBodyJSON(resp) c <- true }() - // Make sure response didn't fire early. - time.Sleep(1 * time.Millisecond) - assert.Nil(t, body, "") + select { + case <-c: + t.Fatal("should not get the watch result") + case <-time.After(time.Millisecond): + } // Set a value (before given index). v := url.Values{} v.Set("value", "XXX") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - - // Make sure response didn't fire early. - time.Sleep(1 * time.Millisecond) - assert.Nil(t, body, "") - - // Set a value (before given index). - v.Set("value", "YYY") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) - ReadBody(resp) - - // A response should follow from the GET above. - time.Sleep(1 * time.Millisecond) + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) + resp.Body.Close() select { case <-c: - default: + t.Fatal("should not get the watch result") + case <-time.After(time.Millisecond): + } + + // Set a value (before given index). + resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v) + resp.Body.Close() + + select { + case <-c: + case <-time.After(time.Millisecond): t.Fatal("cannot get watch result") } - assert.NotNil(t, body, "") - assert.Equal(t, body["action"], "set", "") - - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/foo/bar", "") - assert.Equal(t, node["value"], "YYY", "") - assert.Equal(t, node["modifiedIndex"], 3, "") + w := map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/foo/bar", + "value": "XXX", + "modifiedIndex": float64(3), + }, + "action": "set", + } + if err := checkBody(body, w); err != nil { + t.Error(err) + } es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that a watcher can wait for a value to be set after a given index. -// -// $ curl localhost:4001/v2/keys/keyindir/bar?wait=true -// $ curl -X PUT localhost:4001/v2/keys/keyindir -d dir=true -d ttl=1 -// $ curl -X PUT localhost:4001/v2/keys/keyindir/bar -d value=YYY -// func TestV2WatchKeyInDir(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL + tc := NewTestClient() var body map[string]interface{} c := make(chan bool) @@ -1054,66 +770,104 @@ func TestV2WatchKeyInDir(t *testing.T) { v := url.Values{} v.Set("dir", "true") v.Set("ttl", "1") - resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v) - ReadBody(resp) + resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v) + resp.Body.Close() // Set a value (before given index). v = url.Values{} v.Set("value", "XXX") - resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v) - ReadBody(resp) + resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v) + resp.Body.Close() go func() { - resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true")) - body = ReadBodyJSON(resp) + resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true")) + body = tc.ReadBodyJSON(resp) c <- true }() - // wait for expiration, we do have a up to 500 millisecond delay - time.Sleep(time.Second + time.Millisecond*500) - select { case <-c: - default: + case <-time.After(time.Millisecond * 1500): t.Fatal("cannot get watch result") } - assert.NotNil(t, body, "") - assert.Equal(t, body["action"], "expire", "") - - node := body["node"].(map[string]interface{}) - assert.Equal(t, node["key"], "/keyindir", "") + w := map[string]interface{}{ + "node": map[string]interface{}{ + "key": "/keyindir", + }, + "action": "expire", + } + if err := checkBody(body, w); err != nil { + t.Error(err) + } es[0].Stop() hs[0].Close() afterTest(t) } -// Ensures that HEAD could work. -// -// $ curl -I localhost:4001/v2/keys/foo/bar -> fail -// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -// $ curl -I localhost:4001/v2/keys/foo/bar -// -func TestV2HeadKey(t *testing.T) { +func TestV2Head(t *testing.T) { es, hs := buildCluster(1, false) u := hs[0].URL + tc := NewTestClient() v := url.Values{} v.Set("value", "XXX") fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar") - resp, _ := Head(fullURL) - assert.Equal(t, resp.StatusCode, http.StatusNotFound) - assert.Equal(t, resp.ContentLength, -1) + resp, _ := tc.Head(fullURL) + resp.Body.Close() + if resp.StatusCode != http.StatusNotFound { + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusNotFound) + } + if resp.ContentLength != -1 { + t.Errorf("ContentLength = %d, want -1", resp.ContentLength) + } - resp, _ = PutForm(fullURL, v) - ReadBody(resp) + resp, _ = tc.PutForm(fullURL, v) + resp.Body.Close() - resp, _ = Head(fullURL) - assert.Equal(t, resp.StatusCode, http.StatusOK) - assert.Equal(t, resp.ContentLength, -1) + resp, _ = tc.Head(fullURL) + resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusOK) + } + if resp.ContentLength != -1 { + t.Errorf("ContentLength = %d, want -1", resp.ContentLength) + } es[0].Stop() hs[0].Close() afterTest(t) } + +func checkBody(body map[string]interface{}, w map[string]interface{}) error { + if body["node"] != nil { + if w["node"] != nil { + wn := w["node"].(map[string]interface{}) + n := body["node"].(map[string]interface{}) + for k := range n { + if wn[k] == nil { + delete(n, k) + } + } + body["node"] = n + } + if w["prevNode"] != nil { + wn := w["prevNode"].(map[string]interface{}) + n := body["prevNode"].(map[string]interface{}) + for k := range n { + if wn[k] == nil { + delete(n, k) + } + } + body["prevNode"] = n + } + } + for k, v := range w { + g := body[k] + if !reflect.DeepEqual(g, v) { + return fmt.Errorf("%v = %+v, want %+v", k, g, v) + } + } + return nil +} diff --git a/etcd/v2_util.go b/etcd/v2_util.go index bf2a12fd0..b12e9812b 100644 --- a/etcd/v2_util.go +++ b/etcd/v2_util.go @@ -10,13 +10,17 @@ import ( "strings" ) +type testHttpClient struct { + *http.Client +} + // Creates a new HTTP client with KeepAlive disabled. -func NewHTTPClient() *http.Client { - return &http.Client{Transport: &http.Transport{DisableKeepAlives: true}} +func NewTestClient() *testHttpClient { + return &testHttpClient{&http.Client{Transport: &http.Transport{DisableKeepAlives: true}}} } // Reads the body from the response and closes it. -func ReadBody(resp *http.Response) []byte { +func (t *testHttpClient) ReadBody(resp *http.Response) []byte { if resp == nil { return []byte{} } @@ -26,53 +30,52 @@ func ReadBody(resp *http.Response) []byte { } // Reads the body from the response and parses it as JSON. -func ReadBodyJSON(resp *http.Response) map[string]interface{} { +func (t *testHttpClient) ReadBodyJSON(resp *http.Response) map[string]interface{} { m := make(map[string]interface{}) - b := ReadBody(resp) + b := t.ReadBody(resp) if err := json.Unmarshal(b, &m); err != nil { panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b))) } return m } -func Head(url string) (*http.Response, error) { - return send("HEAD", url, "application/json", nil) +func (t *testHttpClient) Head(url string) (*http.Response, error) { + return t.send("HEAD", url, "application/json", nil) } -func Get(url string) (*http.Response, error) { - return send("GET", url, "application/json", nil) +func (t *testHttpClient) Get(url string) (*http.Response, error) { + return t.send("GET", url, "application/json", nil) } -func Post(url string, bodyType string, body io.Reader) (*http.Response, error) { - return send("POST", url, bodyType, body) +func (t *testHttpClient) Post(url string, bodyType string, body io.Reader) (*http.Response, error) { + return t.send("POST", url, bodyType, body) } -func PostForm(url string, data url.Values) (*http.Response, error) { - return Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) +func (t *testHttpClient) PostForm(url string, data url.Values) (*http.Response, error) { + return t.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) } -func Put(url string, bodyType string, body io.Reader) (*http.Response, error) { - return send("PUT", url, bodyType, body) +func (t *testHttpClient) Put(url string, bodyType string, body io.Reader) (*http.Response, error) { + return t.send("PUT", url, bodyType, body) } -func PutForm(url string, data url.Values) (*http.Response, error) { - return Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) +func (t *testHttpClient) PutForm(url string, data url.Values) (*http.Response, error) { + return t.Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) } -func Delete(url string, bodyType string, body io.Reader) (*http.Response, error) { - return send("DELETE", url, bodyType, body) +func (t *testHttpClient) Delete(url string, bodyType string, body io.Reader) (*http.Response, error) { + return t.send("DELETE", url, bodyType, body) } -func DeleteForm(url string, data url.Values) (*http.Response, error) { - return Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) +func (t *testHttpClient) DeleteForm(url string, data url.Values) (*http.Response, error) { + return t.Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) } -func send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) { - c := NewHTTPClient() +func (t *testHttpClient) send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(method, url, body) if err != nil { return nil, err } req.Header.Set("Content-Type", bodyType) - return c.Do(req) + return t.Do(req) }