get_handler_test.go 5.0 KB

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