get_handler_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/server"
  10. "github.com/coreos/etcd/tests"
  11. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  12. )
  13. // Ensures that a value can be retrieve for a given key.
  14. //
  15. // $ curl localhost:4001/v1/keys/foo/bar -> fail
  16. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  17. // $ curl localhost:4001/v1/keys/foo/bar
  18. //
  19. func TestV1GetKey(t *testing.T) {
  20. tests.RunServer(func(s *server.Server) {
  21. v := url.Values{}
  22. v.Set("value", "XXX")
  23. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  24. resp, _ := tests.Get(fullURL)
  25. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  26. resp, _ = tests.PutForm(fullURL, v)
  27. tests.ReadBody(resp)
  28. resp, _ = tests.Get(fullURL)
  29. assert.Equal(t, resp.StatusCode, http.StatusOK)
  30. body := tests.ReadBodyJSON(resp)
  31. assert.Equal(t, body["action"], "get", "")
  32. assert.Equal(t, body["key"], "/foo/bar", "")
  33. assert.Equal(t, body["value"], "XXX", "")
  34. assert.Equal(t, body["index"], 2, "")
  35. })
  36. }
  37. // Ensures that a directory of values can be retrieved for a given key.
  38. //
  39. // $ curl -X PUT localhost:4001/v1/keys/foo/x -d value=XXX
  40. // $ curl -X PUT localhost:4001/v1/keys/foo/y/z -d value=YYY
  41. // $ curl localhost:4001/v1/keys/foo
  42. //
  43. func TestV1GetKeyDir(t *testing.T) {
  44. tests.RunServer(func(s *server.Server) {
  45. v := url.Values{}
  46. v.Set("value", "XXX")
  47. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/x"), v)
  48. tests.ReadBody(resp)
  49. v.Set("value", "YYY")
  50. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/y/z"), v)
  51. tests.ReadBody(resp)
  52. resp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo"))
  53. assert.Equal(t, resp.StatusCode, http.StatusOK)
  54. body := tests.ReadBody(resp)
  55. nodes := make([]interface{}, 0)
  56. if err := json.Unmarshal(body, &nodes); err != nil {
  57. panic(fmt.Sprintf("HTTP body JSON parse error: %v", err))
  58. }
  59. assert.Equal(t, len(nodes), 2, "")
  60. node0 := nodes[0].(map[string]interface{})
  61. assert.Equal(t, node0["action"], "get", "")
  62. assert.Equal(t, node0["key"], "/foo/x", "")
  63. assert.Equal(t, node0["value"], "XXX", "")
  64. node1 := nodes[1].(map[string]interface{})
  65. assert.Equal(t, node1["action"], "get", "")
  66. assert.Equal(t, node1["key"], "/foo/y", "")
  67. assert.Equal(t, node1["dir"], true, "")
  68. })
  69. }
  70. // Ensures that a watcher can wait for a value to be set and return it to the client.
  71. //
  72. // $ curl localhost:4001/v1/watch/foo/bar
  73. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  74. //
  75. func TestV1WatchKey(t *testing.T) {
  76. tests.RunServer(func(s *server.Server) {
  77. var body map[string]interface{}
  78. c := make(chan bool)
  79. go func() {
  80. resp, _ := tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"))
  81. body = tests.ReadBodyJSON(resp)
  82. c <- true
  83. }()
  84. // Make sure response didn't fire early.
  85. time.Sleep(1 * time.Millisecond)
  86. assert.Nil(t, body, "")
  87. // Set a value.
  88. v := url.Values{}
  89. v.Set("value", "XXX")
  90. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  91. tests.ReadBody(resp)
  92. // A response should follow from the GET above.
  93. time.Sleep(1 * time.Millisecond)
  94. select {
  95. case <-c:
  96. default:
  97. t.Fatal("cannot get watch result")
  98. }
  99. assert.NotNil(t, body, "")
  100. assert.Equal(t, body["action"], "set", "")
  101. assert.Equal(t, body["key"], "/foo/bar", "")
  102. assert.Equal(t, body["value"], "XXX", "")
  103. assert.Equal(t, body["index"], 2, "")
  104. })
  105. }
  106. // Ensures that a watcher can wait for a value to be set after a given index.
  107. //
  108. // $ curl -X POST localhost:4001/v1/watch/foo/bar -d index=4
  109. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  110. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY
  111. //
  112. func TestV1WatchKeyWithIndex(t *testing.T) {
  113. tests.RunServer(func(s *server.Server) {
  114. var body map[string]interface{}
  115. c := make(chan bool)
  116. go func() {
  117. v := url.Values{}
  118. v.Set("index", "3")
  119. resp, _ := tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"), v)
  120. body = tests.ReadBodyJSON(resp)
  121. c <- true
  122. }()
  123. // Make sure response didn't fire early.
  124. time.Sleep(1 * time.Millisecond)
  125. assert.Nil(t, body, "")
  126. // Set a value (before given index).
  127. v := url.Values{}
  128. v.Set("value", "XXX")
  129. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  130. tests.ReadBody(resp)
  131. // Make sure response didn't fire early.
  132. time.Sleep(1 * time.Millisecond)
  133. assert.Nil(t, body, "")
  134. // Set a value (before given index).
  135. v.Set("value", "YYY")
  136. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  137. tests.ReadBody(resp)
  138. // A response should follow from the GET above.
  139. time.Sleep(1 * time.Millisecond)
  140. select {
  141. case <-c:
  142. default:
  143. t.Fatal("cannot get watch result")
  144. }
  145. assert.NotNil(t, body, "")
  146. assert.Equal(t, body["action"], "set", "")
  147. assert.Equal(t, body["key"], "/foo/bar", "")
  148. assert.Equal(t, body["value"], "YYY", "")
  149. assert.Equal(t, body["index"], 3, "")
  150. })
  151. }