http_test.go 7.8 KB

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