| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- package v2
- import (
- "fmt"
- "net/http"
- "net/url"
- "testing"
- "time"
- "github.com/coreos/etcd/server"
- "github.com/coreos/etcd/tests"
- "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
- )
- // Ensures that a key is set to a given value.
- //
- // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
- //
- func TestV2SetKey(t *testing.T) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- body := tests.ReadBody(resp)
- assert.Nil(t, err, "")
- assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
- })
- }
- // Ensures that a directory is created
- //
- // $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true
- //
- func TestV2SetDirectory(t *testing.T) {
- tests.RunServer(func(s *server.Server) {
- resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- body := tests.ReadBody(resp)
- assert.Nil(t, err, "")
- assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- t0 := time.Now()
- v := url.Values{}
- v.Set("value", "XXX")
- v.Set("ttl", "20")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- body := tests.ReadBodyJSON(resp)
- node := body["node"].(map[string]interface{})
- assert.Equal(t, node["ttl"], 20, "")
- // Make sure the expiration date is correct.
- expiration, _ := time.Parse(time.RFC3339Nano, node["expiration"].(string))
- assert.Equal(t, expiration.Sub(t0)/time.Second, 20, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- v.Set("ttl", "bad_ttl")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
- body := tests.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", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- v.Set("prevExist", "false")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- body := tests.ReadBodyJSON(resp)
- node := body["node"].(map[string]interface{})
- assert.Equal(t, node["value"], "XXX", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- v.Set("prevExist", "false")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 105, "")
- assert.Equal(t, body["message"], "Key already exists", "")
- assert.Equal(t, body["cause"], "/foo/bar", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- v.Set("value", "YYY")
- v.Set("prevExist", "true")
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusOK)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["action"], "update", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- v.Set("value", "YYY")
- v.Set("prevExist", "true")
- resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusNotFound)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 100, "")
- assert.Equal(t, body["message"], "Key not found", "")
- assert.Equal(t, body["cause"], "/foo/bar", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "YYY")
- v.Set("prevExist", "true")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo"), v)
- assert.Equal(t, resp.StatusCode, http.StatusNotFound)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 100, "")
- assert.Equal(t, body["message"], "Key not found", "")
- assert.Equal(t, body["cause"], "/foo", "")
- resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusNotFound)
- body = tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 100, "")
- assert.Equal(t, body["message"], "Key not found", "")
- assert.Equal(t, body["cause"], "/foo", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- v.Set("value", "YYY")
- v.Set("prevIndex", "2")
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusOK)
- body := tests.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, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- v.Set("value", "YYY")
- v.Set("prevIndex", "10")
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 101, "")
- assert.Equal(t, body["message"], "Compare failed", "")
- assert.Equal(t, body["cause"], "[ != XXX] [10 != 2]", "")
- assert.Equal(t, body["index"], 2, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "YYY")
- v.Set("prevIndex", "bad_index")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
- body := tests.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", "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- v.Set("value", "YYY")
- v.Set("prevValue", "XXX")
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusOK)
- body := tests.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, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
- resp, _ := tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- tests.ReadBody(resp)
- v.Set("value", "YYY")
- v.Set("prevValue", "AAA")
- resp, _ = tests.PutForm(fullURL, v)
- assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
- body := tests.ReadBodyJSON(resp)
- assert.Equal(t, body["errorCode"], 101, "")
- assert.Equal(t, body["message"], "Compare failed", "")
- assert.Equal(t, body["cause"], "[AAA != XXX] [0 != 2]", "")
- assert.Equal(t, body["index"], 2, "")
- })
- }
- // 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) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "XXX")
- v.Set("prevValue", "")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
- body := tests.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", "")
- })
- }
- // Ensure that we can set an empty value
- //
- // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=
- //
- func TestV2SetKeyCASWithEmptyValueSuccess(t *testing.T) {
- tests.RunServer(func(s *server.Server) {
- v := url.Values{}
- v.Set("value", "")
- resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
- assert.Equal(t, resp.StatusCode, http.StatusCreated)
- body := tests.ReadBody(resp)
- assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"","modifiedIndex":2,"createdIndex":2}}`)
- })
- }
|