get_handler_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. 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["index"], 4, "")
  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. kv1 := body["kvs"].([]interface{})[1].(map[string]interface{})
  57. assert.Equal(t, kv1["key"], "/foo/y", "")
  58. assert.Equal(t, kv1["dir"], true, "")
  59. kvs2 := kv1["kvs"].([]interface{})[0].(map[string]interface{})
  60. assert.Equal(t, kvs2["key"], "/foo/y/z", "")
  61. assert.Equal(t, kvs2["value"], "YYY", "")
  62. })
  63. }
  64. // Ensures that a watcher can wait for a value to be set and return it to the client.
  65. //
  66. // $ curl localhost:4001/v2/keys/foo/bar?wait=true
  67. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  68. //
  69. func TestV2WatchKey(t *testing.T) {
  70. tests.RunServer(func(s *server.Server) {
  71. var body map[string]interface{}
  72. go func() {
  73. resp, _ := tests.Get(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?wait=true"))
  74. body = tests.ReadBodyJSON(resp)
  75. }()
  76. // Make sure response didn't fire early.
  77. time.Sleep(1 * time.Millisecond)
  78. assert.Nil(t, body, "")
  79. // Set a value.
  80. v := url.Values{}
  81. v.Set("value", "XXX")
  82. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  83. tests.ReadBody(resp)
  84. // A response should follow from the GET above.
  85. time.Sleep(1 * time.Millisecond)
  86. assert.NotNil(t, body, "")
  87. assert.Equal(t, body["action"], "set", "")
  88. assert.Equal(t, body["key"], "/foo/bar", "")
  89. assert.Equal(t, body["value"], "XXX", "")
  90. assert.Equal(t, body["index"], 3, "")
  91. assert.Equal(t, body["term"], 0, "")
  92. })
  93. }
  94. // Ensures that a watcher can wait for a value to be set after a given index.
  95. //
  96. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=4
  97. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  98. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  99. //
  100. func TestV2WatchKeyWithIndex(t *testing.T) {
  101. tests.RunServer(func(s *server.Server) {
  102. var body map[string]interface{}
  103. go func() {
  104. resp, _ := tests.Get(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar?wait=true&waitIndex=5"))
  105. body = tests.ReadBodyJSON(resp)
  106. }()
  107. // Make sure response didn't fire early.
  108. time.Sleep(1 * time.Millisecond)
  109. assert.Nil(t, body, "")
  110. // Set a value (before given index).
  111. v := url.Values{}
  112. v.Set("value", "XXX")
  113. resp, _ := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  114. tests.ReadBody(resp)
  115. // Make sure response didn't fire early.
  116. time.Sleep(1 * time.Millisecond)
  117. assert.Nil(t, body, "")
  118. // Set a value (before given index).
  119. v.Set("value", "YYY")
  120. resp, _ = tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  121. tests.ReadBody(resp)
  122. // A response should follow from the GET above.
  123. time.Sleep(1 * time.Millisecond)
  124. assert.NotNil(t, body, "")
  125. assert.Equal(t, body["action"], "set", "")
  126. assert.Equal(t, body["key"], "/foo/bar", "")
  127. assert.Equal(t, body["value"], "YYY", "")
  128. assert.Equal(t, body["index"], 4, "")
  129. assert.Equal(t, body["term"], 0, "")
  130. })
  131. }