get_handler_test.go 5.0 KB

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