get_handler_test.go 6.4 KB

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