v2_http_endpoint_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Copyright 2014 CoreOS Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "reflect"
  21. "sort"
  22. "strings"
  23. "testing"
  24. "github.com/coreos/etcd/conf"
  25. "github.com/coreos/etcd/store"
  26. )
  27. func TestMachinesEndPoint(t *testing.T) {
  28. cl := &testCluster{Size: 3}
  29. cl.Start()
  30. w := make([]string, cl.Size)
  31. for i := 0; i < cl.Size; i++ {
  32. w[i] = cl.URL(i)
  33. }
  34. for i := 0; i < cl.Size; i++ {
  35. r, err := http.Get(cl.URL(i) + v2machinePrefix)
  36. if err != nil {
  37. t.Errorf("%v", err)
  38. break
  39. }
  40. b, err := ioutil.ReadAll(r.Body)
  41. r.Body.Close()
  42. if err != nil {
  43. t.Errorf("%v", err)
  44. break
  45. }
  46. g := strings.Split(string(b), ",")
  47. sort.Strings(g)
  48. if !reflect.DeepEqual(w, g) {
  49. t.Errorf("machines = %v, want %v", g, w)
  50. }
  51. }
  52. cl.Destroy()
  53. }
  54. func TestLeaderEndPoint(t *testing.T) {
  55. cl := &testCluster{Size: 3}
  56. cl.Start()
  57. us := make([]string, cl.Size)
  58. for i := 0; i < cl.Size; i++ {
  59. us[i] = cl.URL(i)
  60. }
  61. // todo(xiangli) change this to raft port...
  62. w := cl.URL(0) + "/raft"
  63. for i := 0; i < cl.Size; i++ {
  64. r, err := http.Get(cl.URL(i) + v2LeaderPrefix)
  65. if err != nil {
  66. t.Errorf("%v", err)
  67. break
  68. }
  69. b, err := ioutil.ReadAll(r.Body)
  70. r.Body.Close()
  71. if err != nil {
  72. t.Errorf("%v", err)
  73. break
  74. }
  75. if string(b) != w {
  76. t.Errorf("leader = %v, want %v", string(b), w)
  77. }
  78. }
  79. cl.Destroy()
  80. }
  81. func TestStoreStatsEndPoint(t *testing.T) {
  82. cl := &testCluster{Size: 1}
  83. cl.Start()
  84. resp, err := http.Get(cl.URL(0) + v2StoreStatsPrefix)
  85. if err != nil {
  86. t.Errorf("%v", err)
  87. }
  88. stats := new(store.Stats)
  89. d := json.NewDecoder(resp.Body)
  90. err = d.Decode(stats)
  91. resp.Body.Close()
  92. if err != nil {
  93. t.Errorf("%v", err)
  94. }
  95. if stats.SetSuccess != 1 {
  96. t.Errorf("setSuccess = %d, want 1", stats.SetSuccess)
  97. }
  98. cl.Destroy()
  99. }
  100. func TestGetAdminConfigEndPoint(t *testing.T) {
  101. cl := &testCluster{Size: 1}
  102. cl.Start()
  103. r, err := http.Get(cl.URL(0) + v2adminConfigPrefix)
  104. if err != nil {
  105. t.Errorf("%v", err)
  106. }
  107. if g := r.StatusCode; g != 200 {
  108. t.Errorf("status = %d, want %d", g, 200)
  109. }
  110. if g := r.Header.Get("Content-Type"); g != "application/json" {
  111. t.Errorf("ContentType = %s, want application/json", g)
  112. }
  113. cfg := new(conf.ClusterConfig)
  114. err = json.NewDecoder(r.Body).Decode(cfg)
  115. r.Body.Close()
  116. if err != nil {
  117. t.Errorf("%v", err)
  118. }
  119. w := conf.NewClusterConfig()
  120. if !reflect.DeepEqual(cfg, w) {
  121. t.Errorf("config = %+v, want %+v", cfg, w)
  122. }
  123. cl.Destroy()
  124. }
  125. func TestPutAdminConfigEndPoint(t *testing.T) {
  126. tests := []struct {
  127. c, wc string
  128. }{
  129. {
  130. `{"activeSize":1,"removeDelay":1,"syncInterval":1}`,
  131. `{"activeSize":3,"removeDelay":2,"syncInterval":1}`,
  132. },
  133. {
  134. `{"activeSize":5,"removeDelay":20.5,"syncInterval":1.5}`,
  135. `{"activeSize":5,"removeDelay":20.5,"syncInterval":1.5}`,
  136. },
  137. {
  138. `{"activeSize":5 , "removeDelay":20 , "syncInterval": 2 }`,
  139. `{"activeSize":5,"removeDelay":20,"syncInterval":2}`,
  140. },
  141. {
  142. `{"activeSize":3, "removeDelay":60}`,
  143. `{"activeSize":3,"removeDelay":60,"syncInterval":5}`,
  144. },
  145. }
  146. for i, tt := range tests {
  147. cl := &testCluster{Size: 1}
  148. cl.Start()
  149. index := cl.Participant(0).Index()
  150. r, err := NewTestClient().Put(cl.URL(0)+v2adminConfigPrefix, "application/json", bytes.NewBufferString(tt.c))
  151. if err != nil {
  152. t.Fatalf("%v", err)
  153. }
  154. b, err := ioutil.ReadAll(r.Body)
  155. r.Body.Close()
  156. if err != nil {
  157. t.Fatalf("%v", err)
  158. }
  159. if wbody := append([]byte(tt.wc), '\n'); !reflect.DeepEqual(b, wbody) {
  160. t.Errorf("#%d: put result = %s, want %s", i, b, wbody)
  161. }
  162. w, err := cl.Participant(0).Watch(v2configKVPrefix, false, false, index)
  163. if err != nil {
  164. t.Errorf("%v", err)
  165. continue
  166. }
  167. e := <-w.EventChan
  168. if g := *e.Node.Value; g != tt.wc {
  169. t.Errorf("#%d: %s = %s, want %s", i, v2configKVPrefix, g, tt.wc)
  170. }
  171. cl.Destroy()
  172. }
  173. }
  174. func TestGetAdminMachineEndPoint(t *testing.T) {
  175. cl := &testCluster{Size: 3}
  176. cl.Start()
  177. for i := 0; i < cl.Size; i++ {
  178. for j := 0; j < cl.Size; j++ {
  179. name := fmt.Sprint(cl.Id(i))
  180. r, err := http.Get(cl.URL(j) + v2adminMachinesPrefix + name)
  181. if err != nil {
  182. t.Errorf("%v", err)
  183. continue
  184. }
  185. if g := r.StatusCode; g != 200 {
  186. t.Errorf("#%d on %d: status = %d, want %d", i, j, g, 200)
  187. }
  188. if g := r.Header.Get("Content-Type"); g != "application/json" {
  189. t.Errorf("#%d on %d: ContentType = %s, want application/json", i, j, g)
  190. }
  191. m := new(machineMessage)
  192. err = json.NewDecoder(r.Body).Decode(m)
  193. r.Body.Close()
  194. if err != nil {
  195. t.Errorf("%v", err)
  196. continue
  197. }
  198. wm := &machineMessage{
  199. Name: name,
  200. State: stateFollower,
  201. ClientURL: cl.URL(i),
  202. PeerURL: cl.URL(i),
  203. }
  204. if i == 0 {
  205. wm.State = stateLeader
  206. }
  207. if !reflect.DeepEqual(m, wm) {
  208. t.Errorf("#%d on %d: body = %+v, want %+v", i, j, m, wm)
  209. }
  210. }
  211. }
  212. cl.Destroy()
  213. }
  214. func TestGetAdminMachinesEndPoint(t *testing.T) {
  215. cl := &testCluster{Size: 3}
  216. cl.Start()
  217. w := make([]*machineMessage, cl.Size)
  218. for i := 0; i < cl.Size; i++ {
  219. w[i] = &machineMessage{
  220. Name: fmt.Sprint(cl.Id(i)),
  221. State: stateFollower,
  222. ClientURL: cl.URL(i),
  223. PeerURL: cl.URL(i),
  224. }
  225. }
  226. w[0].State = stateLeader
  227. for i := 0; i < cl.Size; i++ {
  228. r, err := http.Get(cl.URL(i) + v2adminMachinesPrefix)
  229. if err != nil {
  230. t.Errorf("%v", err)
  231. continue
  232. }
  233. m := make([]*machineMessage, 0)
  234. err = json.NewDecoder(r.Body).Decode(&m)
  235. r.Body.Close()
  236. if err != nil {
  237. t.Errorf("%v", err)
  238. continue
  239. }
  240. sm := machineSlice(m)
  241. sw := machineSlice(w)
  242. sort.Sort(sm)
  243. sort.Sort(sw)
  244. if !reflect.DeepEqual(sm, sw) {
  245. t.Errorf("on %d: machines = %+v, want %+v", i, sm, sw)
  246. }
  247. }
  248. cl.Destroy()
  249. }
  250. // int64Slice implements sort interface
  251. type machineSlice []*machineMessage
  252. func (s machineSlice) Len() int { return len(s) }
  253. func (s machineSlice) Less(i, j int) bool { return s[i].Name < s[j].Name }
  254. func (s machineSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }