put_handler_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. node := body["node"].(map[string]interface{})
  111. assert.Equal(t, node["prevValue"], "XXX", "")
  112. })
  113. }
  114. // Ensures that a key is not conditionally set if it previously did not exist.
  115. //
  116. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  117. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true
  118. //
  119. func TestV2UpdateKeyFailOnValue(t *testing.T) {
  120. tests.RunServer(func(s *server.Server) {
  121. v := url.Values{}
  122. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), v)
  123. v.Set("value", "YYY")
  124. v.Set("prevExist", "true")
  125. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  126. body := tests.ReadBodyJSON(resp)
  127. assert.Equal(t, body["errorCode"], 100, "")
  128. assert.Equal(t, body["message"], "Key Not Found", "")
  129. assert.Equal(t, body["cause"], "/foo/bar", "")
  130. })
  131. }
  132. // Ensures that a key is not conditionally set if it previously did not exist.
  133. //
  134. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d prevExist=true
  135. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true
  136. //
  137. func TestV2UpdateKeyFailOnMissingDirectory(t *testing.T) {
  138. tests.RunServer(func(s *server.Server) {
  139. v := url.Values{}
  140. v.Set("value", "YYY")
  141. v.Set("prevExist", "true")
  142. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  143. body := tests.ReadBodyJSON(resp)
  144. assert.Equal(t, body["errorCode"], 100, "")
  145. assert.Equal(t, body["message"], "Key Not Found", "")
  146. assert.Equal(t, body["cause"], "/foo", "")
  147. })
  148. }
  149. // Ensures that a key is set only if the previous index matches.
  150. //
  151. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  152. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=1
  153. //
  154. func TestV2SetKeyCASOnIndexSuccess(t *testing.T) {
  155. tests.RunServer(func(s *server.Server) {
  156. v := url.Values{}
  157. v.Set("value", "XXX")
  158. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  159. tests.ReadBody(resp)
  160. v.Set("value", "YYY")
  161. v.Set("prevIndex", "2")
  162. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  163. body := tests.ReadBodyJSON(resp)
  164. assert.Equal(t, body["action"], "compareAndSwap", "")
  165. node := body["node"].(map[string]interface{})
  166. assert.Equal(t, node["prevValue"], "XXX", "")
  167. assert.Equal(t, node["value"], "YYY", "")
  168. assert.Equal(t, node["modifiedIndex"], 3, "")
  169. })
  170. }
  171. // Ensures that a key is not set if the previous index does not match.
  172. //
  173. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  174. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=10
  175. //
  176. func TestV2SetKeyCASOnIndexFail(t *testing.T) {
  177. tests.RunServer(func(s *server.Server) {
  178. v := url.Values{}
  179. v.Set("value", "XXX")
  180. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  181. tests.ReadBody(resp)
  182. v.Set("value", "YYY")
  183. v.Set("prevIndex", "10")
  184. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  185. body := tests.ReadBodyJSON(resp)
  186. assert.Equal(t, body["errorCode"], 101, "")
  187. assert.Equal(t, body["message"], "Test Failed", "")
  188. assert.Equal(t, body["cause"], "[ != XXX] [10 != 2]", "")
  189. assert.Equal(t, body["index"], 2, "")
  190. })
  191. }
  192. // Ensures that an error is thrown if an invalid previous index is provided.
  193. //
  194. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index
  195. //
  196. func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
  197. tests.RunServer(func(s *server.Server) {
  198. v := url.Values{}
  199. v.Set("value", "YYY")
  200. v.Set("prevIndex", "bad_index")
  201. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  202. body := tests.ReadBodyJSON(resp)
  203. assert.Equal(t, body["errorCode"], 203, "")
  204. assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
  205. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  206. })
  207. }
  208. // Ensures that a key is set only if the previous value matches.
  209. //
  210. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  211. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX
  212. //
  213. func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
  214. tests.RunServer(func(s *server.Server) {
  215. v := url.Values{}
  216. v.Set("value", "XXX")
  217. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  218. tests.ReadBody(resp)
  219. v.Set("value", "YYY")
  220. v.Set("prevValue", "XXX")
  221. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  222. body := tests.ReadBodyJSON(resp)
  223. assert.Equal(t, body["action"], "compareAndSwap", "")
  224. node := body["node"].(map[string]interface{})
  225. assert.Equal(t, node["prevValue"], "XXX", "")
  226. assert.Equal(t, node["value"], "YYY", "")
  227. assert.Equal(t, node["modifiedIndex"], 3, "")
  228. })
  229. }
  230. // Ensures that a key is not set if the previous value does not match.
  231. //
  232. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  233. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA
  234. //
  235. func TestV2SetKeyCASOnValueFail(t *testing.T) {
  236. tests.RunServer(func(s *server.Server) {
  237. v := url.Values{}
  238. v.Set("value", "XXX")
  239. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  240. tests.ReadBody(resp)
  241. v.Set("value", "YYY")
  242. v.Set("prevValue", "AAA")
  243. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  244. body := tests.ReadBodyJSON(resp)
  245. assert.Equal(t, body["errorCode"], 101, "")
  246. assert.Equal(t, body["message"], "Test Failed", "")
  247. assert.Equal(t, body["cause"], "[AAA != XXX] [0 != 2]", "")
  248. assert.Equal(t, body["index"], 2, "")
  249. })
  250. }
  251. // Ensures that an error is returned if a blank prevValue is set.
  252. //
  253. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue=
  254. //
  255. func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
  256. tests.RunServer(func(s *server.Server) {
  257. v := url.Values{}
  258. v.Set("value", "XXX")
  259. v.Set("prevValue", "")
  260. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  261. body := tests.ReadBodyJSON(resp)
  262. assert.Equal(t, body["errorCode"], 201, "")
  263. assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
  264. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  265. })
  266. }