get_handler_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  21. tests.ReadBody(resp)
  22. resp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"))
  23. body := tests.ReadBodyJSON(resp)
  24. assert.Equal(t, body["action"], "get", "")
  25. node := body["node"].(map[string]interface{})
  26. assert.Equal(t, node["key"], "/foo/bar", "")
  27. assert.Equal(t, node["value"], "XXX", "")
  28. assert.Equal(t, node["modifiedIndex"], 2, "")
  29. })
  30. }
  31. // Ensures that a directory of values can be recursively retrieved for a given key.
  32. //
  33. // $ curl -X PUT localhost:4001/v2/keys/foo/x -d value=XXX
  34. // $ curl -X PUT localhost:4001/v2/keys/foo/y/z -d value=YYY
  35. // $ curl localhost:4001/v2/keys/foo -d recursive=true
  36. //
  37. func TestV2GetKeyRecursively(t *testing.T) {
  38. tests.RunServer(func(s *server.Server) {
  39. v := url.Values{}
  40. v.Set("value", "XXX")
  41. v.Set("ttl", "10")
  42. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/x"), v)
  43. tests.ReadBody(resp)
  44. v.Set("value", "YYY")
  45. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/y/z"), v)
  46. tests.ReadBody(resp)
  47. resp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?recursive=true"))
  48. body := tests.ReadBodyJSON(resp)
  49. assert.Equal(t, body["action"], "get", "")
  50. node := body["node"].(map[string]interface{})
  51. assert.Equal(t, node["key"], "/foo", "")
  52. assert.Equal(t, node["dir"], true, "")
  53. assert.Equal(t, node["modifiedIndex"], 2, "")
  54. assert.Equal(t, len(node["nodes"].([]interface{})), 2, "")
  55. node0 := node["nodes"].([]interface{})[0].(map[string]interface{})
  56. assert.Equal(t, node0["key"], "/foo/x", "")
  57. assert.Equal(t, node0["value"], "XXX", "")
  58. assert.Equal(t, node0["ttl"], 10, "")
  59. node1 := node["nodes"].([]interface{})[1].(map[string]interface{})
  60. assert.Equal(t, node1["key"], "/foo/y", "")
  61. assert.Equal(t, node1["dir"], true, "")
  62. node2 := node1["nodes"].([]interface{})[0].(map[string]interface{})
  63. assert.Equal(t, node2["key"], "/foo/y/z", "")
  64. assert.Equal(t, node2["value"], "YYY", "")
  65. })
  66. }
  67. // Ensures that a watcher can wait for a value to be set and return it to the client.
  68. //
  69. // $ curl localhost:4001/v2/keys/foo/bar?wait=true
  70. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  71. //
  72. func TestV2WatchKey(t *testing.T) {
  73. tests.RunServer(func(s *server.Server) {
  74. var body map[string]interface{}
  75. c := make(chan bool)
  76. go func() {
  77. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true"))
  78. body = tests.ReadBodyJSON(resp)
  79. c <- true
  80. }()
  81. // Make sure response didn't fire early.
  82. time.Sleep(1 * time.Millisecond)
  83. assert.Nil(t, body, "")
  84. // Set a value.
  85. v := url.Values{}
  86. v.Set("value", "XXX")
  87. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  88. tests.ReadBody(resp)
  89. // A response should follow from the GET above.
  90. time.Sleep(1 * time.Millisecond)
  91. select {
  92. case <-c:
  93. default:
  94. t.Fatal("cannot get watch result")
  95. }
  96. assert.NotNil(t, body, "")
  97. assert.Equal(t, body["action"], "set", "")
  98. node := body["node"].(map[string]interface{})
  99. assert.Equal(t, node["key"], "/foo/bar", "")
  100. assert.Equal(t, node["value"], "XXX", "")
  101. assert.Equal(t, node["modifiedIndex"], 2, "")
  102. })
  103. }
  104. // Ensures that a watcher can wait for a value to be set after a given index.
  105. //
  106. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=4
  107. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  108. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  109. //
  110. func TestV2WatchKeyWithIndex(t *testing.T) {
  111. tests.RunServer(func(s *server.Server) {
  112. var body map[string]interface{}
  113. c := make(chan bool)
  114. go func() {
  115. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?wait=true&waitIndex=3"))
  116. body = tests.ReadBodyJSON(resp)
  117. c <- true
  118. }()
  119. // Make sure response didn't fire early.
  120. time.Sleep(1 * time.Millisecond)
  121. assert.Nil(t, body, "")
  122. // Set a value (before given index).
  123. v := url.Values{}
  124. v.Set("value", "XXX")
  125. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  126. tests.ReadBody(resp)
  127. // Make sure response didn't fire early.
  128. time.Sleep(1 * time.Millisecond)
  129. assert.Nil(t, body, "")
  130. // Set a value (before given index).
  131. v.Set("value", "YYY")
  132. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  133. tests.ReadBody(resp)
  134. // A response should follow from the GET above.
  135. time.Sleep(1 * time.Millisecond)
  136. select {
  137. case <-c:
  138. default:
  139. t.Fatal("cannot get watch result")
  140. }
  141. assert.NotNil(t, body, "")
  142. assert.Equal(t, body["action"], "set", "")
  143. node := body["node"].(map[string]interface{})
  144. assert.Equal(t, node["key"], "/foo/bar", "")
  145. assert.Equal(t, node["value"], "YYY", "")
  146. assert.Equal(t, node["modifiedIndex"], 3, "")
  147. })
  148. }