http_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "net/http/httptest"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/coreos/etcd/pkg/pbutil"
  26. "github.com/coreos/etcd/pkg/types"
  27. "github.com/coreos/etcd/raft/raftpb"
  28. )
  29. func TestServeRaftPrefix(t *testing.T) {
  30. testCases := []struct {
  31. method string
  32. body io.Reader
  33. p Raft
  34. clusterID string
  35. wcode int
  36. }{
  37. {
  38. // bad method
  39. "GET",
  40. bytes.NewReader(
  41. pbutil.MustMarshal(&raftpb.Message{}),
  42. ),
  43. &fakeRaft{},
  44. "0",
  45. http.StatusMethodNotAllowed,
  46. },
  47. {
  48. // bad method
  49. "PUT",
  50. bytes.NewReader(
  51. pbutil.MustMarshal(&raftpb.Message{}),
  52. ),
  53. &fakeRaft{},
  54. "0",
  55. http.StatusMethodNotAllowed,
  56. },
  57. {
  58. // bad method
  59. "DELETE",
  60. bytes.NewReader(
  61. pbutil.MustMarshal(&raftpb.Message{}),
  62. ),
  63. &fakeRaft{},
  64. "0",
  65. http.StatusMethodNotAllowed,
  66. },
  67. {
  68. // bad request body
  69. "POST",
  70. &errReader{},
  71. &fakeRaft{},
  72. "0",
  73. http.StatusBadRequest,
  74. },
  75. {
  76. // bad request protobuf
  77. "POST",
  78. strings.NewReader("malformed garbage"),
  79. &fakeRaft{},
  80. "0",
  81. http.StatusBadRequest,
  82. },
  83. {
  84. // good request, wrong cluster ID
  85. "POST",
  86. bytes.NewReader(
  87. pbutil.MustMarshal(&raftpb.Message{}),
  88. ),
  89. &fakeRaft{},
  90. "1",
  91. http.StatusPreconditionFailed,
  92. },
  93. {
  94. // good request, Processor failure
  95. "POST",
  96. bytes.NewReader(
  97. pbutil.MustMarshal(&raftpb.Message{}),
  98. ),
  99. &fakeRaft{
  100. err: &resWriterToError{code: http.StatusForbidden},
  101. },
  102. "0",
  103. http.StatusForbidden,
  104. },
  105. {
  106. // good request, Processor failure
  107. "POST",
  108. bytes.NewReader(
  109. pbutil.MustMarshal(&raftpb.Message{}),
  110. ),
  111. &fakeRaft{
  112. err: &resWriterToError{code: http.StatusInternalServerError},
  113. },
  114. "0",
  115. http.StatusInternalServerError,
  116. },
  117. {
  118. // good request, Processor failure
  119. "POST",
  120. bytes.NewReader(
  121. pbutil.MustMarshal(&raftpb.Message{}),
  122. ),
  123. &fakeRaft{err: errors.New("blah")},
  124. "0",
  125. http.StatusInternalServerError,
  126. },
  127. {
  128. // good request
  129. "POST",
  130. bytes.NewReader(
  131. pbutil.MustMarshal(&raftpb.Message{}),
  132. ),
  133. &fakeRaft{},
  134. "0",
  135. http.StatusNoContent,
  136. },
  137. }
  138. for i, tt := range testCases {
  139. req, err := http.NewRequest(tt.method, "foo", tt.body)
  140. if err != nil {
  141. t.Fatalf("#%d: could not create request: %#v", i, err)
  142. }
  143. req.Header.Set("X-Etcd-Cluster-ID", tt.clusterID)
  144. rw := httptest.NewRecorder()
  145. h := NewHandler(tt.p, types.ID(0))
  146. h.ServeHTTP(rw, req)
  147. if rw.Code != tt.wcode {
  148. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  149. }
  150. }
  151. }
  152. func TestServeRaftStreamPrefix(t *testing.T) {
  153. tests := []struct {
  154. path string
  155. wtype streamType
  156. }{
  157. {
  158. RaftStreamPrefix + "/message/1",
  159. streamTypeMessage,
  160. },
  161. {
  162. RaftStreamPrefix + "/msgapp/1",
  163. streamTypeMsgAppV2,
  164. },
  165. // backward compatibility
  166. {
  167. RaftStreamPrefix + "/1",
  168. streamTypeMsgApp,
  169. },
  170. }
  171. for i, tt := range tests {
  172. req, err := http.NewRequest("GET", "http://localhost:2380"+tt.path, nil)
  173. if err != nil {
  174. t.Fatalf("#%d: could not create request: %#v", i, err)
  175. }
  176. req.Header.Set("X-Etcd-Cluster-ID", "1")
  177. req.Header.Set("X-Raft-To", "2")
  178. wterm := "1"
  179. req.Header.Set("X-Raft-Term", wterm)
  180. peer := newFakePeer()
  181. peerGetter := &fakePeerGetter{peers: map[types.ID]Peer{types.ID(1): peer}}
  182. h := newStreamHandler(peerGetter, &fakeRaft{}, types.ID(2), types.ID(1))
  183. rw := httptest.NewRecorder()
  184. go h.ServeHTTP(rw, req)
  185. var conn *outgoingConn
  186. select {
  187. case conn = <-peer.connc:
  188. case <-time.After(time.Second):
  189. t.Fatalf("#%d: failed to attach outgoingConn", i)
  190. }
  191. if conn.t != tt.wtype {
  192. t.Errorf("$%d: type = %s, want %s", i, conn.t, tt.wtype)
  193. }
  194. if conn.termStr != wterm {
  195. t.Errorf("$%d: term = %s, want %s", i, conn.termStr, wterm)
  196. }
  197. conn.Close()
  198. }
  199. }
  200. func TestServeRaftStreamPrefixBad(t *testing.T) {
  201. removedID := uint64(5)
  202. tests := []struct {
  203. method string
  204. path string
  205. clusterID string
  206. remote string
  207. wcode int
  208. }{
  209. // bad method
  210. {
  211. "PUT",
  212. RaftStreamPrefix + "/message/1",
  213. "1",
  214. "1",
  215. http.StatusMethodNotAllowed,
  216. },
  217. // bad method
  218. {
  219. "POST",
  220. RaftStreamPrefix + "/message/1",
  221. "1",
  222. "1",
  223. http.StatusMethodNotAllowed,
  224. },
  225. // bad method
  226. {
  227. "DELETE",
  228. RaftStreamPrefix + "/message/1",
  229. "1",
  230. "1",
  231. http.StatusMethodNotAllowed,
  232. },
  233. // bad path
  234. {
  235. "GET",
  236. RaftStreamPrefix + "/strange/1",
  237. "1",
  238. "1",
  239. http.StatusNotFound,
  240. },
  241. // bad path
  242. {
  243. "GET",
  244. RaftStreamPrefix + "/strange",
  245. "1",
  246. "1",
  247. http.StatusNotFound,
  248. },
  249. // non-existant peer
  250. {
  251. "GET",
  252. RaftStreamPrefix + "/message/2",
  253. "1",
  254. "1",
  255. http.StatusNotFound,
  256. },
  257. // removed peer
  258. {
  259. "GET",
  260. RaftStreamPrefix + "/message/" + fmt.Sprint(removedID),
  261. "1",
  262. "1",
  263. http.StatusGone,
  264. },
  265. // wrong cluster ID
  266. {
  267. "GET",
  268. RaftStreamPrefix + "/message/1",
  269. "2",
  270. "1",
  271. http.StatusPreconditionFailed,
  272. },
  273. // wrong remote id
  274. {
  275. "GET",
  276. RaftStreamPrefix + "/message/1",
  277. "1",
  278. "2",
  279. http.StatusPreconditionFailed,
  280. },
  281. }
  282. for i, tt := range tests {
  283. req, err := http.NewRequest(tt.method, "http://localhost:2380"+tt.path, nil)
  284. if err != nil {
  285. t.Fatalf("#%d: could not create request: %#v", i, err)
  286. }
  287. req.Header.Set("X-Etcd-Cluster-ID", tt.clusterID)
  288. req.Header.Set("X-Raft-To", tt.remote)
  289. rw := httptest.NewRecorder()
  290. peerGetter := &fakePeerGetter{peers: map[types.ID]Peer{types.ID(1): newFakePeer()}}
  291. r := &fakeRaft{removedID: removedID}
  292. h := newStreamHandler(peerGetter, r, types.ID(1), types.ID(1))
  293. h.ServeHTTP(rw, req)
  294. if rw.Code != tt.wcode {
  295. t.Errorf("#%d: code = %d, want %d", i, rw.Code, tt.wcode)
  296. }
  297. }
  298. }
  299. func TestCloseNotifier(t *testing.T) {
  300. c := newCloseNotifier()
  301. select {
  302. case <-c.closeNotify():
  303. t.Fatalf("received unexpected close notification")
  304. default:
  305. }
  306. c.Close()
  307. select {
  308. case <-c.closeNotify():
  309. default:
  310. t.Fatalf("failed to get close notification")
  311. }
  312. }
  313. // errReader implements io.Reader to facilitate a broken request.
  314. type errReader struct{}
  315. func (er *errReader) Read(_ []byte) (int, error) { return 0, errors.New("some error") }
  316. type resWriterToError struct {
  317. code int
  318. }
  319. func (e *resWriterToError) Error() string { return "" }
  320. func (e *resWriterToError) WriteTo(w http.ResponseWriter) { w.WriteHeader(e.code) }
  321. type fakePeerGetter struct {
  322. peers map[types.ID]Peer
  323. }
  324. func (pg *fakePeerGetter) Get(id types.ID) Peer { return pg.peers[id] }
  325. type fakePeer struct {
  326. msgs []raftpb.Message
  327. urls types.URLs
  328. connc chan *outgoingConn
  329. }
  330. func newFakePeer() *fakePeer {
  331. return &fakePeer{
  332. connc: make(chan *outgoingConn, 1),
  333. }
  334. }
  335. func (pr *fakePeer) Send(m raftpb.Message) { pr.msgs = append(pr.msgs, m) }
  336. func (pr *fakePeer) Update(urls types.URLs) { pr.urls = urls }
  337. func (pr *fakePeer) attachOutgoingConn(conn *outgoingConn) { pr.connc <- conn }
  338. func (pr *fakePeer) Stop() {}