get_handler_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. var watchResp *http.Response
  82. c := make(chan bool)
  83. go func() {
  84. watchResp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true"))
  85. c <- true
  86. }()
  87. // Make sure response didn't fire early.
  88. time.Sleep(1 * time.Millisecond)
  89. // Set a value.
  90. v := url.Values{}
  91. v.Set("value", "XXX")
  92. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  93. tests.ReadBody(resp)
  94. // A response should follow from the GET above.
  95. time.Sleep(1 * time.Millisecond)
  96. select {
  97. case <-c:
  98. default:
  99. t.Fatal("cannot get watch result")
  100. }
  101. body := tests.ReadBodyJSON(watchResp)
  102. assert.NotNil(t, body, "")
  103. assert.Equal(t, body["action"], "set", "")
  104. node := body["node"].(map[string]interface{})
  105. assert.Equal(t, node["key"], "/foo/bar", "")
  106. assert.Equal(t, node["value"], "XXX", "")
  107. assert.Equal(t, node["modifiedIndex"], 2, "")
  108. })
  109. }
  110. // Ensures that a watcher can wait for a value to be set after a given index.
  111. //
  112. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=4
  113. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  114. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  115. //
  116. func TestV2WatchKeyWithIndex(t *testing.T) {
  117. tests.RunServer(func(s *server.Server) {
  118. var body map[string]interface{}
  119. c := make(chan bool)
  120. go func() {
  121. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true&waitIndex=3"))
  122. body = tests.ReadBodyJSON(resp)
  123. c <- true
  124. }()
  125. // Make sure response didn't fire early.
  126. time.Sleep(1 * time.Millisecond)
  127. assert.Nil(t, body, "")
  128. // Set a value (before given index).
  129. v := url.Values{}
  130. v.Set("value", "XXX")
  131. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  132. tests.ReadBody(resp)
  133. // Make sure response didn't fire early.
  134. time.Sleep(1 * time.Millisecond)
  135. assert.Nil(t, body, "")
  136. // Set a value (before given index).
  137. v.Set("value", "YYY")
  138. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  139. tests.ReadBody(resp)
  140. // A response should follow from the GET above.
  141. time.Sleep(1 * time.Millisecond)
  142. select {
  143. case <-c:
  144. default:
  145. t.Fatal("cannot get watch result")
  146. }
  147. assert.NotNil(t, body, "")
  148. assert.Equal(t, body["action"], "set", "")
  149. node := body["node"].(map[string]interface{})
  150. assert.Equal(t, node["key"], "/foo/bar", "")
  151. assert.Equal(t, node["value"], "YYY", "")
  152. assert.Equal(t, node["modifiedIndex"], 3, "")
  153. })
  154. }
  155. // Ensures that a watcher can wait for a value to be set after a given index.
  156. //
  157. // $ curl localhost:4001/v2/keys/keyindir/bar?wait=true
  158. // $ curl -X PUT localhost:4001/v2/keys/keyindir -d dir=true -d ttl=1
  159. // $ curl -X PUT localhost:4001/v2/keys/keyindir/bar -d value=YYY
  160. //
  161. func TestV2WatchKeyInDir(t *testing.T) {
  162. tests.RunServer(func(s *server.Server) {
  163. var body map[string]interface{}
  164. c := make(chan bool)
  165. // Set a value (before given index).
  166. v := url.Values{}
  167. v.Set("dir", "true")
  168. v.Set("ttl", "1")
  169. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir"), v)
  170. tests.ReadBody(resp)
  171. // Set a value (before given index).
  172. v = url.Values{}
  173. v.Set("value", "XXX")
  174. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir/bar"), v)
  175. tests.ReadBody(resp)
  176. go func() {
  177. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/keyindir/bar?wait=true"))
  178. body = tests.ReadBodyJSON(resp)
  179. c <- true
  180. }()
  181. // wait for expiration, we do have a up to 500 millisecond delay
  182. time.Sleep(2000 * time.Millisecond)
  183. select {
  184. case <-c:
  185. default:
  186. t.Fatal("cannot get watch result")
  187. }
  188. assert.NotNil(t, body, "")
  189. assert.Equal(t, body["action"], "expire", "")
  190. node := body["node"].(map[string]interface{})
  191. assert.Equal(t, node["key"], "/keyindir", "")
  192. })
  193. }
  194. // Ensures that HEAD could work.
  195. //
  196. // $ curl -I localhost:4001/v2/keys/foo/bar -> fail
  197. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  198. // $ curl -I localhost:4001/v2/keys/foo/bar
  199. //
  200. func TestV2HeadKey(t *testing.T) {
  201. tests.RunServer(func(s *server.Server) {
  202. v := url.Values{}
  203. v.Set("value", "XXX")
  204. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  205. resp, _ := tests.Head(fullURL)
  206. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  207. assert.Equal(t, resp.ContentLength, -1)
  208. resp, _ = tests.PutForm(fullURL, v)
  209. tests.ReadBody(resp)
  210. resp, _ = tests.Head(fullURL)
  211. assert.Equal(t, resp.StatusCode, http.StatusOK)
  212. assert.Equal(t, resp.ContentLength, -1)
  213. })
  214. }