get_handler_test.go 6.7 KB

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