http_test.go 7.8 KB

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