put_handler_test.go 10 KB

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