put_handler_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package v1
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. "time"
  8. "github.com/coreos/etcd/server"
  9. "github.com/coreos/etcd/tests"
  10. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  11. )
  12. // Ensures that a key is set to a given value.
  13. //
  14. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  15. //
  16. func TestV1SetKey(t *testing.T) {
  17. tests.RunServer(func(s *server.Server) {
  18. v := url.Values{}
  19. v.Set("value", "XXX")
  20. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  21. assert.Equal(t, resp.StatusCode, http.StatusOK)
  22. body := tests.ReadBody(resp)
  23. assert.Nil(t, err, "")
  24. assert.Equal(t, string(body), `{"action":"set","key":"/foo/bar","value":"XXX","newKey":true,"index":3}`, "")
  25. })
  26. }
  27. // Ensures that a time-to-live is added to a key.
  28. //
  29. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX -d ttl=20
  30. //
  31. func TestV1SetKeyWithTTL(t *testing.T) {
  32. tests.RunServer(func(s *server.Server) {
  33. t0 := time.Now()
  34. v := url.Values{}
  35. v.Set("value", "XXX")
  36. v.Set("ttl", "20")
  37. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  38. assert.Equal(t, resp.StatusCode, http.StatusOK)
  39. body := tests.ReadBodyJSON(resp)
  40. assert.Equal(t, body["ttl"], 20, "")
  41. // Make sure the expiration date is correct.
  42. expiration, _ := time.Parse(time.RFC3339Nano, body["expiration"].(string))
  43. assert.Equal(t, expiration.Sub(t0)/time.Second, 20, "")
  44. })
  45. }
  46. // Ensures that an invalid time-to-live is returned as an error.
  47. //
  48. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX -d ttl=bad_ttl
  49. //
  50. func TestV1SetKeyWithBadTTL(t *testing.T) {
  51. tests.RunServer(func(s *server.Server) {
  52. v := url.Values{}
  53. v.Set("value", "XXX")
  54. v.Set("ttl", "bad_ttl")
  55. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  56. assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
  57. body := tests.ReadBodyJSON(resp)
  58. assert.Equal(t, body["errorCode"], 202, "")
  59. assert.Equal(t, body["message"], "The given TTL in POST form is not a number", "")
  60. assert.Equal(t, body["cause"], "Set", "")
  61. })
  62. }
  63. // Ensures that a key is conditionally set if it previously did not exist.
  64. //
  65. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX -d prevValue=
  66. //
  67. func TestV1CreateKeySuccess(t *testing.T) {
  68. tests.RunServer(func(s *server.Server) {
  69. v := url.Values{}
  70. v.Set("value", "XXX")
  71. v.Set("prevValue", "")
  72. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  73. assert.Equal(t, resp.StatusCode, http.StatusOK)
  74. body := tests.ReadBodyJSON(resp)
  75. assert.Equal(t, body["value"], "XXX", "")
  76. })
  77. }
  78. // Ensures that a key is not conditionally set because it previously existed.
  79. //
  80. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX -d prevValue=
  81. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX -d prevValue= -> fail
  82. //
  83. func TestV1CreateKeyFail(t *testing.T) {
  84. tests.RunServer(func(s *server.Server) {
  85. v := url.Values{}
  86. v.Set("value", "XXX")
  87. v.Set("prevValue", "")
  88. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  89. resp, _ := tests.PutForm(fullURL, v)
  90. assert.Equal(t, resp.StatusCode, http.StatusOK)
  91. tests.ReadBody(resp)
  92. resp, _ = tests.PutForm(fullURL, v)
  93. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  94. body := tests.ReadBodyJSON(resp)
  95. assert.Equal(t, body["errorCode"], 105, "")
  96. assert.Equal(t, body["message"], "Key already exists", "")
  97. assert.Equal(t, body["cause"], "/foo/bar", "")
  98. })
  99. }
  100. // Ensures that a key is set only if the previous value matches.
  101. //
  102. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  103. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY -d prevValue=XXX
  104. //
  105. func TestV1SetKeyCASOnValueSuccess(t *testing.T) {
  106. tests.RunServer(func(s *server.Server) {
  107. v := url.Values{}
  108. v.Set("value", "XXX")
  109. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  110. resp, _ := tests.PutForm(fullURL, v)
  111. assert.Equal(t, resp.StatusCode, http.StatusOK)
  112. tests.ReadBody(resp)
  113. v.Set("value", "YYY")
  114. v.Set("prevValue", "XXX")
  115. resp, _ = tests.PutForm(fullURL, v)
  116. assert.Equal(t, resp.StatusCode, http.StatusOK)
  117. body := tests.ReadBodyJSON(resp)
  118. assert.Equal(t, body["action"], "testAndSet", "")
  119. assert.Equal(t, body["value"], "YYY", "")
  120. assert.Equal(t, body["index"], 4, "")
  121. })
  122. }
  123. // Ensures that a key is not set if the previous value does not match.
  124. //
  125. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  126. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY -d prevValue=AAA
  127. //
  128. func TestV1SetKeyCASOnValueFail(t *testing.T) {
  129. tests.RunServer(func(s *server.Server) {
  130. v := url.Values{}
  131. v.Set("value", "XXX")
  132. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  133. resp, _ := tests.PutForm(fullURL, v)
  134. assert.Equal(t, resp.StatusCode, http.StatusOK)
  135. tests.ReadBody(resp)
  136. v.Set("value", "YYY")
  137. v.Set("prevValue", "AAA")
  138. resp, _ = tests.PutForm(fullURL, v)
  139. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  140. body := tests.ReadBodyJSON(resp)
  141. assert.Equal(t, body["errorCode"], 101, "")
  142. assert.Equal(t, body["message"], "Compare failed", "")
  143. assert.Equal(t, body["cause"], "[AAA != XXX]", "")
  144. assert.Equal(t, body["index"], 3, "")
  145. })
  146. }