put_handler_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  98. resp, _ := tests.PutForm(fullURL, v)
  99. tests.ReadBody(resp)
  100. resp, _ = tests.PutForm(fullURL, v)
  101. body := tests.ReadBodyJSON(resp)
  102. assert.Equal(t, body["errorCode"], 105, "")
  103. assert.Equal(t, body["message"], "Key already exists", "")
  104. assert.Equal(t, body["cause"], "/foo/bar", "")
  105. })
  106. }
  107. // Ensures that a key is conditionally set only if it previously did exist.
  108. //
  109. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  110. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true
  111. //
  112. func TestV2UpdateKeySuccess(t *testing.T) {
  113. tests.RunServer(func(s *server.Server) {
  114. v := url.Values{}
  115. v.Set("value", "XXX")
  116. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  117. resp, _ := tests.PutForm(fullURL, v)
  118. tests.ReadBody(resp)
  119. v.Set("value", "YYY")
  120. v.Set("prevExist", "true")
  121. resp, _ = tests.PutForm(fullURL, v)
  122. body := tests.ReadBodyJSON(resp)
  123. assert.Equal(t, body["action"], "update", "")
  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. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  171. resp, _ := tests.PutForm(fullURL, v)
  172. tests.ReadBody(resp)
  173. v.Set("value", "YYY")
  174. v.Set("prevIndex", "2")
  175. resp, _ = tests.PutForm(fullURL, v)
  176. body := tests.ReadBodyJSON(resp)
  177. assert.Equal(t, body["action"], "compareAndSwap", "")
  178. node := body["node"].(map[string]interface{})
  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. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  193. resp, _ := tests.PutForm(fullURL, v)
  194. tests.ReadBody(resp)
  195. v.Set("value", "YYY")
  196. v.Set("prevIndex", "10")
  197. resp, _ = tests.PutForm(fullURL, v)
  198. body := tests.ReadBodyJSON(resp)
  199. assert.Equal(t, body["errorCode"], 101, "")
  200. assert.Equal(t, body["message"], "Compare failed", "")
  201. assert.Equal(t, body["cause"], "[ != XXX] [10 != 2]", "")
  202. assert.Equal(t, body["index"], 2, "")
  203. })
  204. }
  205. // Ensures that an error is thrown if an invalid previous index is provided.
  206. //
  207. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index
  208. //
  209. func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
  210. tests.RunServer(func(s *server.Server) {
  211. v := url.Values{}
  212. v.Set("value", "YYY")
  213. v.Set("prevIndex", "bad_index")
  214. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  215. body := tests.ReadBodyJSON(resp)
  216. assert.Equal(t, body["errorCode"], 203, "")
  217. assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
  218. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  219. })
  220. }
  221. // Ensures that a key is set only if the previous value matches.
  222. //
  223. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  224. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX
  225. //
  226. func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
  227. tests.RunServer(func(s *server.Server) {
  228. v := url.Values{}
  229. v.Set("value", "XXX")
  230. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  231. resp, _ := tests.PutForm(fullURL, v)
  232. tests.ReadBody(resp)
  233. v.Set("value", "YYY")
  234. v.Set("prevValue", "XXX")
  235. resp, _ = tests.PutForm(fullURL, v)
  236. body := tests.ReadBodyJSON(resp)
  237. assert.Equal(t, body["action"], "compareAndSwap", "")
  238. node := body["node"].(map[string]interface{})
  239. assert.Equal(t, node["value"], "YYY", "")
  240. assert.Equal(t, node["modifiedIndex"], 3, "")
  241. })
  242. }
  243. // Ensures that a key is not set if the previous value does not match.
  244. //
  245. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  246. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA
  247. //
  248. func TestV2SetKeyCASOnValueFail(t *testing.T) {
  249. tests.RunServer(func(s *server.Server) {
  250. v := url.Values{}
  251. v.Set("value", "XXX")
  252. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  253. resp, _ := tests.PutForm(fullURL, v)
  254. tests.ReadBody(resp)
  255. v.Set("value", "YYY")
  256. v.Set("prevValue", "AAA")
  257. resp, _ = tests.PutForm(fullURL, v)
  258. body := tests.ReadBodyJSON(resp)
  259. assert.Equal(t, body["errorCode"], 101, "")
  260. assert.Equal(t, body["message"], "Compare failed", "")
  261. assert.Equal(t, body["cause"], "[AAA != XXX] [0 != 2]", "")
  262. assert.Equal(t, body["index"], 2, "")
  263. })
  264. }
  265. // Ensures that an error is returned if a blank prevValue is set.
  266. //
  267. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue=
  268. //
  269. func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
  270. tests.RunServer(func(s *server.Server) {
  271. v := url.Values{}
  272. v.Set("value", "XXX")
  273. v.Set("prevValue", "")
  274. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  275. body := tests.ReadBodyJSON(resp)
  276. assert.Equal(t, body["errorCode"], 201, "")
  277. assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
  278. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  279. })
  280. }