put_handler_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 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 set because it previously existed.
  88. //
  89. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false
  90. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false -> fail
  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=YYY -d prevExist=true -> fail
  147. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true -> fail
  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"), 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. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  160. body = tests.ReadBodyJSON(resp)
  161. assert.Equal(t, body["errorCode"], 100, "")
  162. assert.Equal(t, body["message"], "Key not found", "")
  163. assert.Equal(t, body["cause"], "/foo", "")
  164. })
  165. }
  166. // Ensures that a key is set only if the previous index matches.
  167. //
  168. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  169. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=1
  170. //
  171. func TestV2SetKeyCASOnIndexSuccess(t *testing.T) {
  172. tests.RunServer(func(s *server.Server) {
  173. v := url.Values{}
  174. v.Set("value", "XXX")
  175. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  176. resp, _ := tests.PutForm(fullURL, v)
  177. tests.ReadBody(resp)
  178. v.Set("value", "YYY")
  179. v.Set("prevIndex", "2")
  180. resp, _ = tests.PutForm(fullURL, v)
  181. body := tests.ReadBodyJSON(resp)
  182. assert.Equal(t, body["action"], "compareAndSwap", "")
  183. node := body["node"].(map[string]interface{})
  184. assert.Equal(t, node["value"], "YYY", "")
  185. assert.Equal(t, node["modifiedIndex"], 3, "")
  186. })
  187. }
  188. // Ensures that a key is not set if the previous index does not match.
  189. //
  190. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  191. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=10
  192. //
  193. func TestV2SetKeyCASOnIndexFail(t *testing.T) {
  194. tests.RunServer(func(s *server.Server) {
  195. v := url.Values{}
  196. v.Set("value", "XXX")
  197. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  198. resp, _ := tests.PutForm(fullURL, v)
  199. tests.ReadBody(resp)
  200. v.Set("value", "YYY")
  201. v.Set("prevIndex", "10")
  202. resp, _ = tests.PutForm(fullURL, v)
  203. body := tests.ReadBodyJSON(resp)
  204. assert.Equal(t, body["errorCode"], 101, "")
  205. assert.Equal(t, body["message"], "Compare failed", "")
  206. assert.Equal(t, body["cause"], "[ != XXX] [10 != 2]", "")
  207. assert.Equal(t, body["index"], 2, "")
  208. })
  209. }
  210. // Ensures that an error is thrown if an invalid previous index is provided.
  211. //
  212. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index
  213. //
  214. func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
  215. tests.RunServer(func(s *server.Server) {
  216. v := url.Values{}
  217. v.Set("value", "YYY")
  218. v.Set("prevIndex", "bad_index")
  219. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  220. body := tests.ReadBodyJSON(resp)
  221. assert.Equal(t, body["errorCode"], 203, "")
  222. assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
  223. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  224. })
  225. }
  226. // Ensures that a key is set only if the previous value matches.
  227. //
  228. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  229. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX
  230. //
  231. func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
  232. tests.RunServer(func(s *server.Server) {
  233. v := url.Values{}
  234. v.Set("value", "XXX")
  235. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  236. resp, _ := tests.PutForm(fullURL, v)
  237. tests.ReadBody(resp)
  238. v.Set("value", "YYY")
  239. v.Set("prevValue", "XXX")
  240. resp, _ = tests.PutForm(fullURL, v)
  241. body := tests.ReadBodyJSON(resp)
  242. assert.Equal(t, body["action"], "compareAndSwap", "")
  243. node := body["node"].(map[string]interface{})
  244. assert.Equal(t, node["value"], "YYY", "")
  245. assert.Equal(t, node["modifiedIndex"], 3, "")
  246. })
  247. }
  248. // Ensures that a key is not set if the previous value does not match.
  249. //
  250. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  251. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA
  252. //
  253. func TestV2SetKeyCASOnValueFail(t *testing.T) {
  254. tests.RunServer(func(s *server.Server) {
  255. v := url.Values{}
  256. v.Set("value", "XXX")
  257. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  258. resp, _ := tests.PutForm(fullURL, v)
  259. tests.ReadBody(resp)
  260. v.Set("value", "YYY")
  261. v.Set("prevValue", "AAA")
  262. resp, _ = tests.PutForm(fullURL, v)
  263. body := tests.ReadBodyJSON(resp)
  264. assert.Equal(t, body["errorCode"], 101, "")
  265. assert.Equal(t, body["message"], "Compare failed", "")
  266. assert.Equal(t, body["cause"], "[AAA != XXX] [0 != 2]", "")
  267. assert.Equal(t, body["index"], 2, "")
  268. })
  269. }
  270. // Ensures that an error is returned if a blank prevValue is set.
  271. //
  272. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue=
  273. //
  274. func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
  275. tests.RunServer(func(s *server.Server) {
  276. v := url.Values{}
  277. v.Set("value", "XXX")
  278. v.Set("prevValue", "")
  279. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  280. body := tests.ReadBodyJSON(resp)
  281. assert.Equal(t, body["errorCode"], 201, "")
  282. assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
  283. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  284. })
  285. }