put_handler_test.go 10 KB

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