put_handler_test.go 10.0 KB

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