get_handler_test.go 7.9 KB

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