delete_handler_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 key is deleted only if the previous index matches.
  28. //
  29. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  30. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=1
  31. //
  32. func TestV2DeleteKeyCADOnIndexSuccess(t *testing.T) {
  33. tests.RunServer(func(s *server.Server) {
  34. v := url.Values{}
  35. v.Set("value", "XXX")
  36. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  37. tests.ReadBody(resp)
  38. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=1"), v)
  39. body := tests.ReadBodyJSON(resp)
  40. assert.Equal(t, body["action"], "compareAndDelete", "")
  41. assert.Equal(t, body["prevValue"], "XXX", "")
  42. assert.Equal(t, body["modifiedIndex"], 2, "")
  43. })
  44. }
  45. // Ensures that a key is not deleted if the previous index does not matche.
  46. //
  47. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  48. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=2
  49. //
  50. func TestV2DeleteKeyCADOnIndexFail(t *testing.T) {
  51. tests.RunServer(func(s *server.Server) {
  52. v := url.Values{}
  53. v.Set("value", "XXX")
  54. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  55. tests.ReadBody(resp)
  56. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=100"), v)
  57. body := tests.ReadBodyJSON(resp)
  58. fmt.Println(body)
  59. assert.Equal(t, body["errorCode"], 101)
  60. })
  61. }
  62. // Ensures that an error is thrown if an invalid previous index is provided.
  63. //
  64. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  65. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=bad_index
  66. //
  67. func TestV2DeleteKeyCADWithInvalidIndex(t *testing.T) {
  68. tests.RunServer(func(s *server.Server) {
  69. v := url.Values{}
  70. v.Set("value", "XXX")
  71. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  72. tests.ReadBody(resp)
  73. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevIndex=bad_index"), v)
  74. body := tests.ReadBodyJSON(resp)
  75. assert.Equal(t, body["errorCode"], 203)
  76. })
  77. }
  78. // Ensures that a key is deleted only if the previous value matches.
  79. //
  80. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  81. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=XXX
  82. //
  83. func TestV2DeleteKeyCADOnValueSuccess(t *testing.T) {
  84. tests.RunServer(func(s *server.Server) {
  85. v := url.Values{}
  86. v.Set("value", "XXX")
  87. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  88. tests.ReadBody(resp)
  89. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue=XXX"), v)
  90. body := tests.ReadBodyJSON(resp)
  91. assert.Equal(t, body["action"], "compareAndDelete", "")
  92. assert.Equal(t, body["prevValue"], "XXX", "")
  93. assert.Equal(t, body["modifiedIndex"], 2, "")
  94. })
  95. }
  96. // Ensures that a key is not deleted if the previous value does not matche.
  97. //
  98. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  99. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=YYY
  100. //
  101. func TestV2DeleteKeyCADOnValueFail(t *testing.T) {
  102. tests.RunServer(func(s *server.Server) {
  103. v := url.Values{}
  104. v.Set("value", "XXX")
  105. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  106. tests.ReadBody(resp)
  107. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue=YYY"), v)
  108. body := tests.ReadBodyJSON(resp)
  109. assert.Equal(t, body["errorCode"], 101)
  110. })
  111. }
  112. // Ensures that an error is thrown if an invalid previous value is provided.
  113. //
  114. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  115. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=
  116. //
  117. func TestV2DeleteKeyCADWithInvalidValue(t *testing.T) {
  118. tests.RunServer(func(s *server.Server) {
  119. v := url.Values{}
  120. v.Set("value", "XXX")
  121. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  122. tests.ReadBody(resp)
  123. resp, _ = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?prevValue="), v)
  124. body := tests.ReadBodyJSON(resp)
  125. assert.Equal(t, body["errorCode"], 201)
  126. })
  127. }