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