delete_handler_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package v2
  2. import (
  3. "fmt"
  4. "net/url"
  5. "testing"
  6. "github.com/coreos/etcd/server"
  7. "github.com/coreos/etcd/tests"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // Ensures that a key is deleted.
  11. //
  12. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  13. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar
  14. //
  15. func TestV2DeleteKey(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. tests.ReadBody(resp)
  21. resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), url.Values{})
  22. body := tests.ReadBody(resp)
  23. assert.Nil(t, err, "")
  24. assert.Equal(t, string(body), `{"action":"delete","key":"/foo/bar","prevValue":"XXX","modifiedIndex":2}`, "")
  25. })
  26. }
  27. // Ensures that a directory is deleted.
  28. //
  29. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  30. // $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true
  31. //
  32. func TestV2DeleteDirectory(t *testing.T) {
  33. tests.RunServer(func(s *server.Server) {
  34. v := url.Values{}
  35. v.Set("value", "XXX")
  36. resp, err := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  37. tests.ReadBody(resp)
  38. resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo?recursive=true"), url.Values{})
  39. assert.Nil(t, err, "")
  40. body := tests.ReadBodyJSON(resp)
  41. assert.Equal(t, body["action"], "delete", "")
  42. assert.Equal(t, body["dir"], true, "")
  43. assert.Equal(t, body["modifiedIndex"], 2, "")
  44. })
  45. }
  46. // Ensures that a directory is deleted if the previous index matches
  47. //
  48. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  49. // $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true&prevIndex=1
  50. //
  51. func TestV2DeleteDirectoryCADOnIndexSuccess(t *testing.T) {
  52. tests.RunServer(func(s *server.Server) {
  53. v := url.Values{}
  54. v.Set("value", "XXX")
  55. resp, err := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  56. tests.ReadBody(resp)
  57. resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo?recursive=true&prevIndex=1"), url.Values{})
  58. assert.Nil(t, err, "")
  59. body := tests.ReadBodyJSON(resp)
  60. assert.Equal(t, body["action"], "compareAndDelete", "")
  61. assert.Equal(t, body["dir"], true, "")
  62. assert.Equal(t, body["modifiedIndex"], 2, "")
  63. })
  64. }
  65. // Ensures that a directory is not deleted if the previous index does not match
  66. //
  67. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  68. // $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true&prevIndex=100
  69. //
  70. func TestV2DeleteDirectoryCADOnIndexFail(t *testing.T) {
  71. tests.RunServer(func(s *server.Server) {
  72. v := url.Values{}
  73. v.Set("value", "XXX")
  74. resp, err := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  75. tests.ReadBody(resp)
  76. resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo?recursive=true&prevIndex=100"), url.Values{})
  77. assert.Nil(t, err, "")
  78. body := tests.ReadBodyJSON(resp)
  79. assert.Equal(t, body["errorCode"], 101)
  80. })
  81. }
  82. // Ensures that a key is deleted only if the previous index matches.
  83. //
  84. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  85. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=1
  86. //
  87. func TestV2DeleteKeyCADOnIndexSuccess(t *testing.T) {
  88. tests.RunServer(func(s *server.Server) {
  89. v := url.Values{}
  90. v.Set("value", "XXX")
  91. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  92. tests.ReadBody(resp)
  93. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=1"), v)
  94. body := tests.ReadBodyJSON(resp)
  95. assert.Equal(t, body["action"], "compareAndDelete", "")
  96. assert.Equal(t, body["prevValue"], "XXX", "")
  97. assert.Equal(t, body["modifiedIndex"], 2, "")
  98. })
  99. }
  100. // Ensures that a key is not deleted if the previous index does not matche.
  101. //
  102. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  103. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=2
  104. //
  105. func TestV2DeleteKeyCADOnIndexFail(t *testing.T) {
  106. tests.RunServer(func(s *server.Server) {
  107. v := url.Values{}
  108. v.Set("value", "XXX")
  109. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  110. tests.ReadBody(resp)
  111. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=100"), v)
  112. body := tests.ReadBodyJSON(resp)
  113. assert.Equal(t, body["errorCode"], 101)
  114. })
  115. }
  116. // Ensures that an error is thrown if an invalid previous index is provided.
  117. //
  118. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  119. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=bad_index
  120. //
  121. func TestV2DeleteKeyCADWithInvalidIndex(t *testing.T) {
  122. tests.RunServer(func(s *server.Server) {
  123. v := url.Values{}
  124. v.Set("value", "XXX")
  125. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  126. tests.ReadBody(resp)
  127. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=bad_index"), v)
  128. body := tests.ReadBodyJSON(resp)
  129. assert.Equal(t, body["errorCode"], 203)
  130. })
  131. }
  132. // Ensures that a key is deleted only if the previous value matches.
  133. //
  134. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  135. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=XXX
  136. //
  137. func TestV2DeleteKeyCADOnValueSuccess(t *testing.T) {
  138. tests.RunServer(func(s *server.Server) {
  139. v := url.Values{}
  140. v.Set("value", "XXX")
  141. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  142. tests.ReadBody(resp)
  143. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue=XXX"), v)
  144. body := tests.ReadBodyJSON(resp)
  145. assert.Equal(t, body["action"], "compareAndDelete", "")
  146. assert.Equal(t, body["prevValue"], "XXX", "")
  147. assert.Equal(t, body["modifiedIndex"], 2, "")
  148. })
  149. }
  150. // Ensures that a key is not deleted if the previous value does not matche.
  151. //
  152. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  153. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=YYY
  154. //
  155. func TestV2DeleteKeyCADOnValueFail(t *testing.T) {
  156. tests.RunServer(func(s *server.Server) {
  157. v := url.Values{}
  158. v.Set("value", "XXX")
  159. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  160. tests.ReadBody(resp)
  161. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue=YYY"), v)
  162. body := tests.ReadBodyJSON(resp)
  163. assert.Equal(t, body["errorCode"], 101)
  164. })
  165. }
  166. // Ensures that an error is thrown if an invalid previous value is provided.
  167. //
  168. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  169. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=
  170. //
  171. func TestV2DeleteKeyCADWithInvalidValue(t *testing.T) {
  172. tests.RunServer(func(s *server.Server) {
  173. v := url.Values{}
  174. v.Set("value", "XXX")
  175. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  176. tests.ReadBody(resp)
  177. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue="), v)
  178. body := tests.ReadBodyJSON(resp)
  179. assert.Equal(t, body["errorCode"], 201)
  180. })
  181. }