get_handler_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package v2
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. "time"
  8. "github.com/coreos/etcd/server"
  9. "github.com/coreos/etcd/tests"
  10. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  11. )
  12. // Ensures that a value can be retrieve for a given key.
  13. //
  14. // $ curl localhost:4001/v2/keys/foo/bar -> fail
  15. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  16. // $ curl localhost:4001/v2/keys/foo/bar
  17. //
  18. func TestV2GetKey(t *testing.T) {
  19. tests.RunServer(func(s *server.Server) {
  20. v := url.Values{}
  21. v.Set("value", "XXX")
  22. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  23. resp, _ := tests.Get(fullURL)
  24. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  25. resp, _ = tests.PutForm(fullURL, v)
  26. tests.ReadBody(resp)
  27. resp, _ = tests.Get(fullURL)
  28. assert.Equal(t, resp.StatusCode, http.StatusOK)
  29. body := tests.ReadBodyJSON(resp)
  30. assert.Equal(t, body["action"], "get", "")
  31. node := body["node"].(map[string]interface{})
  32. assert.Equal(t, node["key"], "/foo/bar", "")
  33. assert.Equal(t, node["value"], "XXX", "")
  34. assert.Equal(t, node["modifiedIndex"], 2, "")
  35. })
  36. }
  37. // Ensures that a directory of values can be recursively retrieved for a given key.
  38. //
  39. // $ curl -X PUT localhost:4001/v2/keys/foo/x -d value=XXX
  40. // $ curl -X PUT localhost:4001/v2/keys/foo/y/z -d value=YYY
  41. // $ curl localhost:4001/v2/keys/foo -d recursive=true
  42. //
  43. func TestV2GetKeyRecursively(t *testing.T) {
  44. tests.RunServer(func(s *server.Server) {
  45. v := url.Values{}
  46. v.Set("value", "XXX")
  47. v.Set("ttl", "10")
  48. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/x"), v)
  49. tests.ReadBody(resp)
  50. v.Set("value", "YYY")
  51. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/y/z"), v)
  52. tests.ReadBody(resp)
  53. resp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?recursive=true"))
  54. assert.Equal(t, resp.StatusCode, http.StatusOK)
  55. body := tests.ReadBodyJSON(resp)
  56. assert.Equal(t, body["action"], "get", "")
  57. node := body["node"].(map[string]interface{})
  58. assert.Equal(t, node["key"], "/foo", "")
  59. assert.Equal(t, node["dir"], true, "")
  60. assert.Equal(t, node["modifiedIndex"], 2, "")
  61. assert.Equal(t, len(node["nodes"].([]interface{})), 2, "")
  62. node0 := node["nodes"].([]interface{})[0].(map[string]interface{})
  63. assert.Equal(t, node0["key"], "/foo/x", "")
  64. assert.Equal(t, node0["value"], "XXX", "")
  65. assert.Equal(t, node0["ttl"], 10, "")
  66. node1 := node["nodes"].([]interface{})[1].(map[string]interface{})
  67. assert.Equal(t, node1["key"], "/foo/y", "")
  68. assert.Equal(t, node1["dir"], true, "")
  69. node2 := node1["nodes"].([]interface{})[0].(map[string]interface{})
  70. assert.Equal(t, node2["key"], "/foo/y/z", "")
  71. assert.Equal(t, node2["value"], "YYY", "")
  72. })
  73. }
  74. // Ensures that a watcher can wait for a value to be set and return it to the client.
  75. //
  76. // $ curl localhost:4001/v2/keys/foo/bar?wait=true
  77. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  78. //
  79. func TestV2WatchKey(t *testing.T) {
  80. tests.RunServer(func(s *server.Server) {
  81. // There exists a little gap between etcd ready to serve and
  82. // it actually serves the first request, which means the response
  83. // delay could be a little bigger.
  84. // This test is time sensitive, so it does one request to ensure
  85. // that the server is working.
  86. tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"))
  87. var watchResp *http.Response
  88. c := make(chan bool)
  89. go func() {
  90. watchResp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true"))
  91. c <- true
  92. }()
  93. // Make sure response didn't fire early.
  94. time.Sleep(1 * time.Millisecond)
  95. // Set a value.
  96. v := url.Values{}
  97. v.Set("value", "XXX")
  98. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  99. tests.ReadBody(resp)
  100. // A response should follow from the GET above.
  101. time.Sleep(1 * time.Millisecond)
  102. select {
  103. case <-c:
  104. default:
  105. t.Fatal("cannot get watch result")
  106. }
  107. body := tests.ReadBodyJSON(watchResp)
  108. assert.NotNil(t, body, "")
  109. assert.Equal(t, body["action"], "set", "")
  110. node := body["node"].(map[string]interface{})
  111. assert.Equal(t, node["key"], "/foo/bar", "")
  112. assert.Equal(t, node["value"], "XXX", "")
  113. assert.Equal(t, node["modifiedIndex"], 2, "")
  114. })
  115. }
  116. // Ensures that a watcher can wait for a value to be set after a given index.
  117. //
  118. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=4
  119. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  120. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  121. //
  122. func TestV2WatchKeyWithIndex(t *testing.T) {
  123. tests.RunServer(func(s *server.Server) {
  124. var body map[string]interface{}
  125. c := make(chan bool)
  126. go func() {
  127. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true&waitIndex=3"))
  128. body = tests.ReadBodyJSON(resp)
  129. c <- true
  130. }()
  131. // Make sure response didn't fire early.
  132. time.Sleep(1 * time.Millisecond)
  133. assert.Nil(t, body, "")
  134. // Set a value (before given index).
  135. v := url.Values{}
  136. v.Set("value", "XXX")
  137. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  138. tests.ReadBody(resp)
  139. // Make sure response didn't fire early.
  140. time.Sleep(1 * time.Millisecond)
  141. assert.Nil(t, body, "")
  142. // Set a value (before given index).
  143. v.Set("value", "YYY")
  144. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  145. tests.ReadBody(resp)
  146. // A response should follow from the GET above.
  147. time.Sleep(1 * time.Millisecond)
  148. select {
  149. case <-c:
  150. default:
  151. t.Fatal("cannot get watch result")
  152. }
  153. assert.NotNil(t, body, "")
  154. assert.Equal(t, body["action"], "set", "")
  155. node := body["node"].(map[string]interface{})
  156. assert.Equal(t, node["key"], "/foo/bar", "")
  157. assert.Equal(t, node["value"], "YYY", "")
  158. assert.Equal(t, node["modifiedIndex"], 3, "")
  159. })
  160. }
  161. // Ensures that a watcher can wait for a value to be set after a given index.
  162. //
  163. // $ curl localhost:4001/v2/keys/keyindir/bar?wait=true
  164. // $ curl -X PUT localhost:4001/v2/keys/keyindir -d dir=true -d ttl=1
  165. // $ curl -X PUT localhost:4001/v2/keys/keyindir/bar -d value=YYY
  166. //
  167. func TestV2WatchKeyInDir(t *testing.T) {
  168. tests.RunServer(func(s *server.Server) {
  169. var body map[string]interface{}
  170. c := make(chan bool)
  171. // Set a value (before given index).
  172. v := url.Values{}
  173. v.Set("dir", "true")
  174. v.Set("ttl", "1")
  175. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir"), v)
  176. tests.ReadBody(resp)
  177. // Set a value (before given index).
  178. v = url.Values{}
  179. v.Set("value", "XXX")
  180. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir/bar"), v)
  181. tests.ReadBody(resp)
  182. go func() {
  183. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir/bar?wait=true"))
  184. body = tests.ReadBodyJSON(resp)
  185. c <- true
  186. }()
  187. // wait for expiration, we do have a up to 500 millisecond delay
  188. time.Sleep(2000 * time.Millisecond)
  189. select {
  190. case <-c:
  191. default:
  192. t.Fatal("cannot get watch result")
  193. }
  194. assert.NotNil(t, body, "")
  195. assert.Equal(t, body["action"], "expire", "")
  196. node := body["node"].(map[string]interface{})
  197. assert.Equal(t, node["key"], "/keyindir", "")
  198. })
  199. }
  200. // Ensures that HEAD could work.
  201. //
  202. // $ curl -I localhost:4001/v2/keys/foo/bar -> fail
  203. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  204. // $ curl -I localhost:4001/v2/keys/foo/bar
  205. //
  206. func TestV2HeadKey(t *testing.T) {
  207. tests.RunServer(func(s *server.Server) {
  208. v := url.Values{}
  209. v.Set("value", "XXX")
  210. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  211. resp, _ := tests.Head(fullURL)
  212. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  213. assert.Equal(t, resp.ContentLength, -1)
  214. resp, _ = tests.PutForm(fullURL, v)
  215. tests.ReadBody(resp)
  216. resp, _ = tests.Head(fullURL)
  217. assert.Equal(t, resp.StatusCode, http.StatusOK)
  218. assert.Equal(t, resp.ContentLength, -1)
  219. })
  220. }