peer_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 etcdhttp
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "errors"
  18. "io"
  19. "net/http"
  20. "net/http/httptest"
  21. "path"
  22. "strings"
  23. "testing"
  24. "github.com/coreos/etcd/etcdserver"
  25. "github.com/coreos/etcd/raft/raftpb"
  26. )
  27. func mustMarshalMsg(t *testing.T, m raftpb.Message) []byte {
  28. json, err := m.Marshal()
  29. if err != nil {
  30. t.Fatalf("error marshalling raft Message: %#v", err)
  31. }
  32. return json
  33. }
  34. // errReader implements io.Reader to facilitate a broken request.
  35. type errReader struct{}
  36. func (er *errReader) Read(_ []byte) (int, error) { return 0, errors.New("some error") }
  37. func TestServeRaft(t *testing.T) {
  38. testCases := []struct {
  39. method string
  40. body io.Reader
  41. serverErr error
  42. clusterID string
  43. wcode int
  44. }{
  45. {
  46. // bad method
  47. "GET",
  48. bytes.NewReader(
  49. mustMarshalMsg(
  50. t,
  51. raftpb.Message{},
  52. ),
  53. ),
  54. nil,
  55. "0",
  56. http.StatusMethodNotAllowed,
  57. },
  58. {
  59. // bad method
  60. "PUT",
  61. bytes.NewReader(
  62. mustMarshalMsg(
  63. t,
  64. raftpb.Message{},
  65. ),
  66. ),
  67. nil,
  68. "0",
  69. http.StatusMethodNotAllowed,
  70. },
  71. {
  72. // bad method
  73. "DELETE",
  74. bytes.NewReader(
  75. mustMarshalMsg(
  76. t,
  77. raftpb.Message{},
  78. ),
  79. ),
  80. nil,
  81. "0",
  82. http.StatusMethodNotAllowed,
  83. },
  84. {
  85. // bad request body
  86. "POST",
  87. &errReader{},
  88. nil,
  89. "0",
  90. http.StatusBadRequest,
  91. },
  92. {
  93. // bad request protobuf
  94. "POST",
  95. strings.NewReader("malformed garbage"),
  96. nil,
  97. "0",
  98. http.StatusBadRequest,
  99. },
  100. {
  101. // good request, etcdserver.Server internal error
  102. "POST",
  103. bytes.NewReader(
  104. mustMarshalMsg(
  105. t,
  106. raftpb.Message{},
  107. ),
  108. ),
  109. errors.New("some error"),
  110. "0",
  111. http.StatusInternalServerError,
  112. },
  113. {
  114. // good request from removed member
  115. "POST",
  116. bytes.NewReader(
  117. mustMarshalMsg(
  118. t,
  119. raftpb.Message{},
  120. ),
  121. ),
  122. etcdserver.ErrRemoved,
  123. "0",
  124. http.StatusForbidden,
  125. },
  126. {
  127. // good request
  128. "POST",
  129. bytes.NewReader(
  130. mustMarshalMsg(
  131. t,
  132. raftpb.Message{},
  133. ),
  134. ),
  135. nil,
  136. "1",
  137. http.StatusPreconditionFailed,
  138. },
  139. {
  140. // good request
  141. "POST",
  142. bytes.NewReader(
  143. mustMarshalMsg(
  144. t,
  145. raftpb.Message{},
  146. ),
  147. ),
  148. nil,
  149. "0",
  150. http.StatusNoContent,
  151. },
  152. }
  153. for i, tt := range testCases {
  154. req, err := http.NewRequest(tt.method, "foo", tt.body)
  155. if err != nil {
  156. t.Fatalf("#%d: could not create request: %#v", i, err)
  157. }
  158. req.Header.Set("X-Etcd-Cluster-ID", tt.clusterID)
  159. rw := httptest.NewRecorder()
  160. h := &raftHandler{stats: nil, server: &errServer{tt.serverErr}, clusterInfo: &fakeCluster{id: 0}}
  161. h.ServeHTTP(rw, req)
  162. if rw.Code != tt.wcode {
  163. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  164. }
  165. }
  166. }
  167. func TestServeMembersFails(t *testing.T) {
  168. tests := []struct {
  169. method string
  170. wcode int
  171. }{
  172. {
  173. "POST",
  174. http.StatusMethodNotAllowed,
  175. },
  176. {
  177. "DELETE",
  178. http.StatusMethodNotAllowed,
  179. },
  180. {
  181. "BAD",
  182. http.StatusMethodNotAllowed,
  183. },
  184. }
  185. for i, tt := range tests {
  186. rw := httptest.NewRecorder()
  187. h := &peerMembersHandler{clusterInfo: nil}
  188. h.ServeHTTP(rw, &http.Request{Method: tt.method})
  189. if rw.Code != tt.wcode {
  190. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  191. }
  192. }
  193. }
  194. func TestServeMembersGet(t *testing.T) {
  195. memb1 := etcdserver.Member{ID: 1, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8080"}}}
  196. memb2 := etcdserver.Member{ID: 2, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8081"}}}
  197. cluster := &fakeCluster{
  198. id: 1,
  199. members: map[uint64]*etcdserver.Member{1: &memb1, 2: &memb2},
  200. }
  201. h := &peerMembersHandler{clusterInfo: cluster}
  202. msb, err := json.Marshal([]etcdserver.Member{memb1, memb2})
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. wms := string(msb) + "\n"
  207. tests := []struct {
  208. path string
  209. wcode int
  210. wct string
  211. wbody string
  212. }{
  213. {peerMembersPrefix, http.StatusOK, "application/json", wms},
  214. {path.Join(peerMembersPrefix, "bad"), http.StatusBadRequest, "text/plain; charset=utf-8", "bad path\n"},
  215. }
  216. for i, tt := range tests {
  217. req, err := http.NewRequest("GET", mustNewURL(t, tt.path).String(), nil)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. rw := httptest.NewRecorder()
  222. h.ServeHTTP(rw, req)
  223. if rw.Code != tt.wcode {
  224. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  225. }
  226. if gct := rw.Header().Get("Content-Type"); gct != tt.wct {
  227. t.Errorf("#%d: content-type = %s, want %s", i, gct, tt.wct)
  228. }
  229. if rw.Body.String() != tt.wbody {
  230. t.Errorf("#%d: body = %s, want %s", i, rw.Body.String(), tt.wbody)
  231. }
  232. gcid := rw.Header().Get("X-Etcd-Cluster-ID")
  233. wcid := cluster.ID().String()
  234. if gcid != wcid {
  235. t.Errorf("#%d: cid = %s, want %s", i, gcid, wcid)
  236. }
  237. }
  238. }