put_handler_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package v2
  2. import (
  3. "fmt"
  4. "net/url"
  5. "testing"
  6. "time"
  7. "github.com/coreos/etcd/server"
  8. "github.com/coreos/etcd/tests"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // Ensures that a key is set to a given value.
  12. //
  13. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  14. //
  15. func TestV2SetKey(t *testing.T) {
  16. tests.RunServer(func(s *server.Server) {
  17. v := url.Values{}
  18. v.Set("value", "XXX")
  19. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  20. body := tests.ReadBody(resp)
  21. assert.Nil(t, err, "")
  22. assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
  23. })
  24. }
  25. // Ensures that a time-to-live is added to a key.
  26. //
  27. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=20
  28. //
  29. func TestV2SetKeyWithTTL(t *testing.T) {
  30. tests.RunServer(func(s *server.Server) {
  31. t0 := time.Now()
  32. v := url.Values{}
  33. v.Set("value", "XXX")
  34. v.Set("ttl", "20")
  35. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  36. body := tests.ReadBodyJSON(resp)
  37. node := body["node"].(map[string]interface{})
  38. assert.Equal(t, node["ttl"], 20, "")
  39. // Make sure the expiration date is correct.
  40. expiration, _ := time.Parse(time.RFC3339Nano, node["expiration"].(string))
  41. assert.Equal(t, expiration.Sub(t0)/time.Second, 20, "")
  42. })
  43. }
  44. // Ensures that an invalid time-to-live is returned as an error.
  45. //
  46. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=bad_ttl
  47. //
  48. func TestV2SetKeyWithBadTTL(t *testing.T) {
  49. tests.RunServer(func(s *server.Server) {
  50. v := url.Values{}
  51. v.Set("value", "XXX")
  52. v.Set("ttl", "bad_ttl")
  53. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  54. body := tests.ReadBodyJSON(resp)
  55. assert.Equal(t, body["errorCode"], 202, "")
  56. assert.Equal(t, body["message"], "The given TTL in POST form is not a number", "")
  57. assert.Equal(t, body["cause"], "Update", "")
  58. })
  59. }
  60. // Ensures that a key is conditionally set only if it previously did not exist.
  61. //
  62. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false
  63. //
  64. func TestV2CreateKeySuccess(t *testing.T) {
  65. tests.RunServer(func(s *server.Server) {
  66. v := url.Values{}
  67. v.Set("value", "XXX")
  68. v.Set("prevExist", "false")
  69. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  70. body := tests.ReadBodyJSON(resp)
  71. node := body["node"].(map[string]interface{})
  72. assert.Equal(t, node["value"], "XXX", "")
  73. })
  74. }
  75. // Ensures that a key is not conditionally because it previously existed.
  76. //
  77. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  78. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false
  79. //
  80. func TestV2CreateKeyFail(t *testing.T) {
  81. tests.RunServer(func(s *server.Server) {
  82. v := url.Values{}
  83. v.Set("value", "XXX")
  84. v.Set("prevExist", "false")
  85. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  86. tests.ReadBody(resp)
  87. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  88. body := tests.ReadBodyJSON(resp)
  89. assert.Equal(t, body["errorCode"], 105, "")
  90. assert.Equal(t, body["message"], "Already exists", "")
  91. assert.Equal(t, body["cause"], "/foo/bar", "")
  92. })
  93. }
  94. // Ensures that a key is conditionally set only if it previously did exist.
  95. //
  96. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  97. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true
  98. //
  99. func TestV2UpdateKeySuccess(t *testing.T) {
  100. tests.RunServer(func(s *server.Server) {
  101. v := url.Values{}
  102. v.Set("value", "XXX")
  103. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  104. tests.ReadBody(resp)
  105. v.Set("value", "YYY")
  106. v.Set("prevExist", "true")
  107. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  108. body := tests.ReadBodyJSON(resp)
  109. assert.Equal(t, body["action"], "update", "")
  110. })
  111. }
  112. // Ensures that a key is not conditionally set if it previously did not exist.
  113. //
  114. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true
  115. //
  116. func TestV2UpdateKeyFailOnValue(t *testing.T) {
  117. tests.RunServer(func(s *server.Server) {
  118. v := url.Values{}
  119. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo"), v)
  120. v.Set("value", "YYY")
  121. v.Set("prevExist", "true")
  122. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  123. body := tests.ReadBodyJSON(resp)
  124. assert.Equal(t, body["errorCode"], 100, "")
  125. assert.Equal(t, body["message"], "Key Not Found", "")
  126. assert.Equal(t, body["cause"], "/foo/bar", "")
  127. })
  128. }
  129. // Ensures that a key is not conditionally set if it previously did not exist.
  130. //
  131. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d prevExist=true
  132. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true
  133. //
  134. func TestV2UpdateKeyFailOnMissingDirectory(t *testing.T) {
  135. tests.RunServer(func(s *server.Server) {
  136. v := url.Values{}
  137. v.Set("value", "YYY")
  138. v.Set("prevExist", "true")
  139. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  140. body := tests.ReadBodyJSON(resp)
  141. assert.Equal(t, body["errorCode"], 100, "")
  142. assert.Equal(t, body["message"], "Key Not Found", "")
  143. assert.Equal(t, body["cause"], "/foo", "")
  144. })
  145. }
  146. // Ensures that a key is set only if the previous index matches.
  147. //
  148. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  149. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=1
  150. //
  151. func TestV2SetKeyCASOnIndexSuccess(t *testing.T) {
  152. tests.RunServer(func(s *server.Server) {
  153. v := url.Values{}
  154. v.Set("value", "XXX")
  155. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  156. tests.ReadBody(resp)
  157. v.Set("value", "YYY")
  158. v.Set("prevIndex", "2")
  159. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  160. body := tests.ReadBodyJSON(resp)
  161. assert.Equal(t, body["action"], "compareAndSwap", "")
  162. node := body["node"].(map[string]interface{})
  163. assert.Equal(t, node["value"], "YYY", "")
  164. assert.Equal(t, node["modifiedIndex"], 3, "")
  165. })
  166. }
  167. // Ensures that a key is not set if the previous index does not match.
  168. //
  169. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  170. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=10
  171. //
  172. func TestV2SetKeyCASOnIndexFail(t *testing.T) {
  173. tests.RunServer(func(s *server.Server) {
  174. v := url.Values{}
  175. v.Set("value", "XXX")
  176. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  177. tests.ReadBody(resp)
  178. v.Set("value", "YYY")
  179. v.Set("prevIndex", "10")
  180. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  181. body := tests.ReadBodyJSON(resp)
  182. assert.Equal(t, body["errorCode"], 101, "")
  183. assert.Equal(t, body["message"], "Test Failed", "")
  184. assert.Equal(t, body["cause"], "[ != XXX] [10 != 2]", "")
  185. assert.Equal(t, body["index"], 2, "")
  186. })
  187. }
  188. // Ensures that an error is thrown if an invalid previous index is provided.
  189. //
  190. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index
  191. //
  192. func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
  193. tests.RunServer(func(s *server.Server) {
  194. v := url.Values{}
  195. v.Set("value", "YYY")
  196. v.Set("prevIndex", "bad_index")
  197. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  198. body := tests.ReadBodyJSON(resp)
  199. assert.Equal(t, body["errorCode"], 203, "")
  200. assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
  201. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  202. })
  203. }
  204. // Ensures that a key is set only if the previous value matches.
  205. //
  206. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  207. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX
  208. //
  209. func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
  210. tests.RunServer(func(s *server.Server) {
  211. v := url.Values{}
  212. v.Set("value", "XXX")
  213. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  214. tests.ReadBody(resp)
  215. v.Set("value", "YYY")
  216. v.Set("prevValue", "XXX")
  217. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  218. body := tests.ReadBodyJSON(resp)
  219. assert.Equal(t, body["action"], "compareAndSwap", "")
  220. node := body["node"].(map[string]interface{})
  221. assert.Equal(t, node["value"], "YYY", "")
  222. assert.Equal(t, node["modifiedIndex"], 3, "")
  223. })
  224. }
  225. // Ensures that a key is not set if the previous value does not match.
  226. //
  227. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  228. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA
  229. //
  230. func TestV2SetKeyCASOnValueFail(t *testing.T) {
  231. tests.RunServer(func(s *server.Server) {
  232. v := url.Values{}
  233. v.Set("value", "XXX")
  234. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  235. tests.ReadBody(resp)
  236. v.Set("value", "YYY")
  237. v.Set("prevValue", "AAA")
  238. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  239. body := tests.ReadBodyJSON(resp)
  240. assert.Equal(t, body["errorCode"], 101, "")
  241. assert.Equal(t, body["message"], "Test Failed", "")
  242. assert.Equal(t, body["cause"], "[AAA != XXX] [0 != 2]", "")
  243. assert.Equal(t, body["index"], 2, "")
  244. })
  245. }
  246. // Ensures that an error is returned if a blank prevValue is set.
  247. //
  248. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue=
  249. //
  250. func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
  251. tests.RunServer(func(s *server.Server) {
  252. v := url.Values{}
  253. v.Set("value", "XXX")
  254. v.Set("prevValue", "")
  255. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  256. body := tests.ReadBodyJSON(resp)
  257. assert.Equal(t, body["errorCode"], 201, "")
  258. assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
  259. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  260. })
  261. }