http_test.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. package etcdhttp
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "net/url"
  10. "path"
  11. "reflect"
  12. "strings"
  13. "testing"
  14. "time"
  15. etcdErr "github.com/coreos/etcd/error"
  16. "github.com/coreos/etcd/etcdserver"
  17. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/raft/raftpb"
  19. "github.com/coreos/etcd/store"
  20. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  21. )
  22. func boolp(b bool) *bool { return &b }
  23. func mustNewURL(t *testing.T, s string) *url.URL {
  24. u, err := url.Parse(s)
  25. if err != nil {
  26. t.Fatalf("error creating URL from %q: %v", s, err)
  27. }
  28. return u
  29. }
  30. // mustNewRequest takes a path, appends it to the standard keysPrefix, and constructs
  31. // a GET *http.Request referencing the resulting URL
  32. func mustNewRequest(t *testing.T, p string) *http.Request {
  33. return mustNewMethodRequest(t, "GET", p)
  34. }
  35. func mustNewMethodRequest(t *testing.T, m, p string) *http.Request {
  36. return &http.Request{
  37. Method: m,
  38. URL: mustNewURL(t, path.Join(keysPrefix, p)),
  39. }
  40. }
  41. // mustNewForm takes a set of Values and constructs a PUT *http.Request,
  42. // with a URL constructed from appending the given path to the standard keysPrefix
  43. func mustNewForm(t *testing.T, p string, vals url.Values) *http.Request {
  44. u := mustNewURL(t, path.Join(keysPrefix, p))
  45. req, err := http.NewRequest("PUT", u.String(), strings.NewReader(vals.Encode()))
  46. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  47. if err != nil {
  48. t.Fatalf("error creating new request: %v", err)
  49. }
  50. return req
  51. }
  52. func TestBadParseRequest(t *testing.T) {
  53. tests := []struct {
  54. in *http.Request
  55. wcode int
  56. }{
  57. {
  58. // parseForm failure
  59. &http.Request{
  60. Body: nil,
  61. Method: "PUT",
  62. },
  63. etcdErr.EcodeInvalidForm,
  64. },
  65. {
  66. // bad key prefix
  67. &http.Request{
  68. URL: mustNewURL(t, "/badprefix/"),
  69. },
  70. etcdErr.EcodeInvalidForm,
  71. },
  72. // bad values for prevIndex, waitIndex, ttl
  73. {
  74. mustNewForm(t, "foo", url.Values{"prevIndex": []string{"garbage"}}),
  75. etcdErr.EcodeIndexNaN,
  76. },
  77. {
  78. mustNewForm(t, "foo", url.Values{"prevIndex": []string{"1.5"}}),
  79. etcdErr.EcodeIndexNaN,
  80. },
  81. {
  82. mustNewForm(t, "foo", url.Values{"prevIndex": []string{"-1"}}),
  83. etcdErr.EcodeIndexNaN,
  84. },
  85. {
  86. mustNewForm(t, "foo", url.Values{"waitIndex": []string{"garbage"}}),
  87. etcdErr.EcodeIndexNaN,
  88. },
  89. {
  90. mustNewForm(t, "foo", url.Values{"waitIndex": []string{"??"}}),
  91. etcdErr.EcodeIndexNaN,
  92. },
  93. {
  94. mustNewForm(t, "foo", url.Values{"ttl": []string{"-1"}}),
  95. etcdErr.EcodeTTLNaN,
  96. },
  97. // bad values for recursive, sorted, wait, prevExist, stream
  98. {
  99. mustNewForm(t, "foo", url.Values{"recursive": []string{"hahaha"}}),
  100. etcdErr.EcodeInvalidField,
  101. },
  102. {
  103. mustNewForm(t, "foo", url.Values{"recursive": []string{"1234"}}),
  104. etcdErr.EcodeInvalidField,
  105. },
  106. {
  107. mustNewForm(t, "foo", url.Values{"recursive": []string{"?"}}),
  108. etcdErr.EcodeInvalidField,
  109. },
  110. {
  111. mustNewForm(t, "foo", url.Values{"sorted": []string{"?"}}),
  112. etcdErr.EcodeInvalidField,
  113. },
  114. {
  115. mustNewForm(t, "foo", url.Values{"sorted": []string{"x"}}),
  116. etcdErr.EcodeInvalidField,
  117. },
  118. {
  119. mustNewForm(t, "foo", url.Values{"wait": []string{"?!"}}),
  120. etcdErr.EcodeInvalidField,
  121. },
  122. {
  123. mustNewForm(t, "foo", url.Values{"wait": []string{"yes"}}),
  124. etcdErr.EcodeInvalidField,
  125. },
  126. {
  127. mustNewForm(t, "foo", url.Values{"prevExist": []string{"yes"}}),
  128. etcdErr.EcodeInvalidField,
  129. },
  130. {
  131. mustNewForm(t, "foo", url.Values{"prevExist": []string{"#2"}}),
  132. etcdErr.EcodeInvalidField,
  133. },
  134. {
  135. mustNewForm(t, "foo", url.Values{"stream": []string{"zzz"}}),
  136. etcdErr.EcodeInvalidField,
  137. },
  138. {
  139. mustNewForm(t, "foo", url.Values{"stream": []string{"something"}}),
  140. etcdErr.EcodeInvalidField,
  141. },
  142. // prevValue cannot be empty
  143. {
  144. mustNewForm(t, "foo", url.Values{"prevValue": []string{""}}),
  145. etcdErr.EcodeInvalidField,
  146. },
  147. // wait is only valid with GET requests
  148. {
  149. mustNewMethodRequest(t, "HEAD", "foo?wait=true"),
  150. etcdErr.EcodeInvalidField,
  151. },
  152. // query values are considered
  153. {
  154. mustNewRequest(t, "foo?prevExist=wrong"),
  155. etcdErr.EcodeInvalidField,
  156. },
  157. {
  158. mustNewRequest(t, "foo?ttl=wrong"),
  159. etcdErr.EcodeTTLNaN,
  160. },
  161. // but body takes precedence if both are specified
  162. {
  163. mustNewForm(
  164. t,
  165. "foo?ttl=12",
  166. url.Values{"ttl": []string{"garbage"}},
  167. ),
  168. etcdErr.EcodeTTLNaN,
  169. },
  170. {
  171. mustNewForm(
  172. t,
  173. "foo?prevExist=false",
  174. url.Values{"prevExist": []string{"yes"}},
  175. ),
  176. etcdErr.EcodeInvalidField,
  177. },
  178. }
  179. for i, tt := range tests {
  180. got, err := parseRequest(tt.in, 1234)
  181. if err == nil {
  182. t.Errorf("#%d: unexpected nil error!", i)
  183. continue
  184. }
  185. ee, ok := err.(*etcdErr.Error)
  186. if !ok {
  187. t.Errorf("#%d: err is not etcd.Error!", i)
  188. continue
  189. }
  190. if ee.ErrorCode != tt.wcode {
  191. t.Errorf("#%d: code=%d, want %v", i, ee.ErrorCode, tt.wcode)
  192. t.Logf("cause: %#v", ee.Cause)
  193. }
  194. if !reflect.DeepEqual(got, etcdserverpb.Request{}) {
  195. t.Errorf("#%d: unexpected non-empty Request: %#v", i, got)
  196. }
  197. }
  198. }
  199. func TestGoodParseRequest(t *testing.T) {
  200. tests := []struct {
  201. in *http.Request
  202. w etcdserverpb.Request
  203. }{
  204. {
  205. // good prefix, all other values default
  206. mustNewRequest(t, "foo"),
  207. etcdserverpb.Request{
  208. Id: 1234,
  209. Method: "GET",
  210. Path: "/foo",
  211. },
  212. },
  213. {
  214. // value specified
  215. mustNewForm(
  216. t,
  217. "foo",
  218. url.Values{"value": []string{"some_value"}},
  219. ),
  220. etcdserverpb.Request{
  221. Id: 1234,
  222. Method: "PUT",
  223. Val: "some_value",
  224. Path: "/foo",
  225. },
  226. },
  227. {
  228. // prevIndex specified
  229. mustNewForm(
  230. t,
  231. "foo",
  232. url.Values{"prevIndex": []string{"98765"}},
  233. ),
  234. etcdserverpb.Request{
  235. Id: 1234,
  236. Method: "PUT",
  237. PrevIndex: 98765,
  238. Path: "/foo",
  239. },
  240. },
  241. {
  242. // recursive specified
  243. mustNewForm(
  244. t,
  245. "foo",
  246. url.Values{"recursive": []string{"true"}},
  247. ),
  248. etcdserverpb.Request{
  249. Id: 1234,
  250. Method: "PUT",
  251. Recursive: true,
  252. Path: "/foo",
  253. },
  254. },
  255. {
  256. // sorted specified
  257. mustNewForm(
  258. t,
  259. "foo",
  260. url.Values{"sorted": []string{"true"}},
  261. ),
  262. etcdserverpb.Request{
  263. Id: 1234,
  264. Method: "PUT",
  265. Sorted: true,
  266. Path: "/foo",
  267. },
  268. },
  269. {
  270. // wait specified
  271. mustNewRequest(t, "foo?wait=true"),
  272. etcdserverpb.Request{
  273. Id: 1234,
  274. Method: "GET",
  275. Wait: true,
  276. Path: "/foo",
  277. },
  278. },
  279. {
  280. // prevExist should be non-null if specified
  281. mustNewForm(
  282. t,
  283. "foo",
  284. url.Values{"prevExist": []string{"true"}},
  285. ),
  286. etcdserverpb.Request{
  287. Id: 1234,
  288. Method: "PUT",
  289. PrevExist: boolp(true),
  290. Path: "/foo",
  291. },
  292. },
  293. {
  294. // prevExist should be non-null if specified
  295. mustNewForm(
  296. t,
  297. "foo",
  298. url.Values{"prevExist": []string{"false"}},
  299. ),
  300. etcdserverpb.Request{
  301. Id: 1234,
  302. Method: "PUT",
  303. PrevExist: boolp(false),
  304. Path: "/foo",
  305. },
  306. },
  307. // mix various fields
  308. {
  309. mustNewForm(
  310. t,
  311. "foo",
  312. url.Values{
  313. "value": []string{"some value"},
  314. "prevExist": []string{"true"},
  315. "prevValue": []string{"previous value"},
  316. },
  317. ),
  318. etcdserverpb.Request{
  319. Id: 1234,
  320. Method: "PUT",
  321. PrevExist: boolp(true),
  322. PrevValue: "previous value",
  323. Val: "some value",
  324. Path: "/foo",
  325. },
  326. },
  327. // query parameters should be used if given
  328. {
  329. mustNewForm(
  330. t,
  331. "foo?prevValue=woof",
  332. url.Values{},
  333. ),
  334. etcdserverpb.Request{
  335. Id: 1234,
  336. Method: "PUT",
  337. PrevValue: "woof",
  338. Path: "/foo",
  339. },
  340. },
  341. // but form values should take precedence over query parameters
  342. {
  343. mustNewForm(
  344. t,
  345. "foo?prevValue=woof",
  346. url.Values{
  347. "prevValue": []string{"miaow"},
  348. },
  349. ),
  350. etcdserverpb.Request{
  351. Id: 1234,
  352. Method: "PUT",
  353. PrevValue: "miaow",
  354. Path: "/foo",
  355. },
  356. },
  357. }
  358. for i, tt := range tests {
  359. got, err := parseRequest(tt.in, 1234)
  360. if err != nil {
  361. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  362. }
  363. if !reflect.DeepEqual(got, tt.w) {
  364. t.Errorf("#%d: request=%#v, want %#v", i, got, tt.w)
  365. }
  366. }
  367. }
  368. // eventingWatcher immediately returns a simple event of the given action on its channel
  369. type eventingWatcher struct {
  370. action string
  371. }
  372. func (w *eventingWatcher) EventChan() chan *store.Event {
  373. ch := make(chan *store.Event)
  374. go func() {
  375. ch <- &store.Event{
  376. Action: w.action,
  377. Node: &store.NodeExtern{},
  378. }
  379. }()
  380. return ch
  381. }
  382. func (w *eventingWatcher) Remove() {}
  383. func TestWriteError(t *testing.T) {
  384. // nil error should not panic
  385. rw := httptest.NewRecorder()
  386. writeError(rw, nil)
  387. h := rw.Header()
  388. if len(h) > 0 {
  389. t.Fatalf("unexpected non-empty headers: %#v", h)
  390. }
  391. b := rw.Body.String()
  392. if len(b) > 0 {
  393. t.Fatalf("unexpected non-empty body: %q", b)
  394. }
  395. tests := []struct {
  396. err error
  397. wcode int
  398. wi string
  399. }{
  400. {
  401. etcdErr.NewError(etcdErr.EcodeKeyNotFound, "/foo/bar", 123),
  402. http.StatusNotFound,
  403. "123",
  404. },
  405. {
  406. etcdErr.NewError(etcdErr.EcodeTestFailed, "/foo/bar", 456),
  407. http.StatusPreconditionFailed,
  408. "456",
  409. },
  410. {
  411. err: errors.New("something went wrong"),
  412. wcode: http.StatusInternalServerError,
  413. },
  414. }
  415. for i, tt := range tests {
  416. rw := httptest.NewRecorder()
  417. writeError(rw, tt.err)
  418. if code := rw.Code; code != tt.wcode {
  419. t.Errorf("#%d: code=%d, want %d", i, code, tt.wcode)
  420. }
  421. if idx := rw.Header().Get("X-Etcd-Index"); idx != tt.wi {
  422. t.Errorf("#%d: X-Etcd-Index=%q, want %q", i, idx, tt.wi)
  423. }
  424. }
  425. }
  426. func TestWriteEvent(t *testing.T) {
  427. // nil event should not panic
  428. rw := httptest.NewRecorder()
  429. writeEvent(rw, nil)
  430. h := rw.Header()
  431. if len(h) > 0 {
  432. t.Fatalf("unexpected non-empty headers: %#v", h)
  433. }
  434. b := rw.Body.String()
  435. if len(b) > 0 {
  436. t.Fatalf("unexpected non-empty body: %q", b)
  437. }
  438. tests := []struct {
  439. ev *store.Event
  440. idx string
  441. // TODO(jonboulle): check body as well as just status code
  442. code int
  443. err error
  444. }{
  445. // standard case, standard 200 response
  446. {
  447. &store.Event{
  448. Action: store.Get,
  449. Node: &store.NodeExtern{},
  450. PrevNode: &store.NodeExtern{},
  451. },
  452. "0",
  453. http.StatusOK,
  454. nil,
  455. },
  456. // check new nodes return StatusCreated
  457. {
  458. &store.Event{
  459. Action: store.Create,
  460. Node: &store.NodeExtern{},
  461. PrevNode: &store.NodeExtern{},
  462. },
  463. "0",
  464. http.StatusCreated,
  465. nil,
  466. },
  467. }
  468. for i, tt := range tests {
  469. rw := httptest.NewRecorder()
  470. writeEvent(rw, tt.ev)
  471. if gct := rw.Header().Get("Content-Type"); gct != "application/json" {
  472. t.Errorf("case %d: bad Content-Type: got %q, want application/json", i, gct)
  473. }
  474. if gei := rw.Header().Get("X-Etcd-Index"); gei != tt.idx {
  475. t.Errorf("case %d: bad X-Etcd-Index header: got %s, want %s", i, gei, tt.idx)
  476. }
  477. if rw.Code != tt.code {
  478. t.Errorf("case %d: bad response code: got %d, want %v", i, rw.Code, tt.code)
  479. }
  480. }
  481. }
  482. type dummyWatcher struct {
  483. echan chan *store.Event
  484. }
  485. func (w *dummyWatcher) EventChan() chan *store.Event {
  486. return w.echan
  487. }
  488. func (w *dummyWatcher) Remove() {}
  489. func TestV2MachinesEndpoint(t *testing.T) {
  490. tests := []struct {
  491. method string
  492. wcode int
  493. }{
  494. {"GET", http.StatusOK},
  495. {"HEAD", http.StatusOK},
  496. {"POST", http.StatusMethodNotAllowed},
  497. }
  498. m := NewClientHandler(nil, Peers{}, time.Hour)
  499. s := httptest.NewServer(m)
  500. defer s.Close()
  501. for _, tt := range tests {
  502. req, err := http.NewRequest(tt.method, s.URL+machinesPrefix, nil)
  503. if err != nil {
  504. t.Fatal(err)
  505. }
  506. resp, err := http.DefaultClient.Do(req)
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. if resp.StatusCode != tt.wcode {
  511. t.Errorf("StatusCode = %d, expected %d", resp.StatusCode, tt.wcode)
  512. }
  513. }
  514. }
  515. func TestServeMachines(t *testing.T) {
  516. peers := Peers{}
  517. peers.Set("0xBEEF0=localhost:8080&0xBEEF1=localhost:8081&0xBEEF2=localhost:8082")
  518. writer := httptest.NewRecorder()
  519. req, err := http.NewRequest("GET", "", nil)
  520. if err != nil {
  521. t.Fatal(err)
  522. }
  523. h := &serverHandler{peers: peers}
  524. h.serveMachines(writer, req)
  525. w := "http://localhost:8080, http://localhost:8081, http://localhost:8082"
  526. if g := writer.Body.String(); g != w {
  527. t.Errorf("body = %s, want %s", g, w)
  528. }
  529. if writer.Code != http.StatusOK {
  530. t.Errorf("header = %d, want %d", writer.Code, http.StatusOK)
  531. }
  532. }
  533. func TestPeersEndpoints(t *testing.T) {
  534. tests := []struct {
  535. peers Peers
  536. endpoints []string
  537. }{
  538. // single peer with a single address
  539. {
  540. peers: Peers(map[int64][]string{
  541. 1: []string{"192.0.2.1"},
  542. }),
  543. endpoints: []string{"http://192.0.2.1"},
  544. },
  545. // single peer with a single address with a port
  546. {
  547. peers: Peers(map[int64][]string{
  548. 1: []string{"192.0.2.1:8001"},
  549. }),
  550. endpoints: []string{"http://192.0.2.1:8001"},
  551. },
  552. // several peers explicitly unsorted
  553. {
  554. peers: Peers(map[int64][]string{
  555. 2: []string{"192.0.2.3", "192.0.2.4"},
  556. 3: []string{"192.0.2.5", "192.0.2.6"},
  557. 1: []string{"192.0.2.1", "192.0.2.2"},
  558. }),
  559. endpoints: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
  560. },
  561. // no peers
  562. {
  563. peers: Peers(map[int64][]string{}),
  564. endpoints: []string{},
  565. },
  566. // peer with no endpoints
  567. {
  568. peers: Peers(map[int64][]string{
  569. 3: []string{},
  570. }),
  571. endpoints: []string{},
  572. },
  573. }
  574. for i, tt := range tests {
  575. endpoints := tt.peers.Endpoints()
  576. if !reflect.DeepEqual(tt.endpoints, endpoints) {
  577. t.Errorf("#%d: peers.Endpoints() incorrect: want=%#v got=%#v", i, tt.endpoints, endpoints)
  578. }
  579. }
  580. }
  581. func TestAllowMethod(t *testing.T) {
  582. tests := []struct {
  583. m string
  584. ms []string
  585. w bool
  586. wh string
  587. }{
  588. // Accepted methods
  589. {
  590. m: "GET",
  591. ms: []string{"GET", "POST", "PUT"},
  592. w: true,
  593. },
  594. {
  595. m: "POST",
  596. ms: []string{"POST"},
  597. w: true,
  598. },
  599. // Made-up methods no good
  600. {
  601. m: "FAKE",
  602. ms: []string{"GET", "POST", "PUT"},
  603. w: false,
  604. wh: "GET,POST,PUT",
  605. },
  606. // Empty methods no good
  607. {
  608. m: "",
  609. ms: []string{"GET", "POST"},
  610. w: false,
  611. wh: "GET,POST",
  612. },
  613. // Empty accepted methods no good
  614. {
  615. m: "GET",
  616. ms: []string{""},
  617. w: false,
  618. wh: "",
  619. },
  620. // No methods accepted
  621. {
  622. m: "GET",
  623. ms: []string{},
  624. w: false,
  625. wh: "",
  626. },
  627. }
  628. for i, tt := range tests {
  629. rw := httptest.NewRecorder()
  630. g := allowMethod(rw, tt.m, tt.ms...)
  631. if g != tt.w {
  632. t.Errorf("#%d: got allowMethod()=%t, want %t", i, g, tt.w)
  633. }
  634. if !tt.w {
  635. if rw.Code != http.StatusMethodNotAllowed {
  636. t.Errorf("#%d: code=%d, want %d", i, rw.Code, http.StatusMethodNotAllowed)
  637. }
  638. gh := rw.Header().Get("Allow")
  639. if gh != tt.wh {
  640. t.Errorf("#%d: Allow header=%q, want %q", i, gh, tt.wh)
  641. }
  642. }
  643. }
  644. }
  645. // errServer implements the etcd.Server interface for testing.
  646. // It returns the given error from any Do/Process calls.
  647. type errServer struct {
  648. err error
  649. }
  650. func (fs *errServer) Do(ctx context.Context, r etcdserverpb.Request) (etcdserver.Response, error) {
  651. return etcdserver.Response{}, fs.err
  652. }
  653. func (fs *errServer) Process(ctx context.Context, m raftpb.Message) error {
  654. return fs.err
  655. }
  656. func (fs *errServer) Start() {}
  657. func (fs *errServer) Stop() {}
  658. // errReader implements io.Reader to facilitate a broken request.
  659. type errReader struct{}
  660. func (er *errReader) Read(_ []byte) (int, error) { return 0, errors.New("some error") }
  661. func mustMarshalMsg(t *testing.T, m raftpb.Message) []byte {
  662. json, err := m.Marshal()
  663. if err != nil {
  664. t.Fatalf("error marshalling raft Message: %#v", err)
  665. }
  666. return json
  667. }
  668. func TestServeRaft(t *testing.T) {
  669. testCases := []struct {
  670. method string
  671. body io.Reader
  672. serverErr error
  673. wcode int
  674. }{
  675. {
  676. // bad method
  677. "GET",
  678. bytes.NewReader(
  679. mustMarshalMsg(
  680. t,
  681. raftpb.Message{},
  682. ),
  683. ),
  684. nil,
  685. http.StatusMethodNotAllowed,
  686. },
  687. {
  688. // bad method
  689. "PUT",
  690. bytes.NewReader(
  691. mustMarshalMsg(
  692. t,
  693. raftpb.Message{},
  694. ),
  695. ),
  696. nil,
  697. http.StatusMethodNotAllowed,
  698. },
  699. {
  700. // bad method
  701. "DELETE",
  702. bytes.NewReader(
  703. mustMarshalMsg(
  704. t,
  705. raftpb.Message{},
  706. ),
  707. ),
  708. nil,
  709. http.StatusMethodNotAllowed,
  710. },
  711. {
  712. // bad request body
  713. "POST",
  714. &errReader{},
  715. nil,
  716. http.StatusBadRequest,
  717. },
  718. {
  719. // bad request protobuf
  720. "POST",
  721. strings.NewReader("malformed garbage"),
  722. nil,
  723. http.StatusBadRequest,
  724. },
  725. {
  726. // good request, etcdserver.Server error
  727. "POST",
  728. bytes.NewReader(
  729. mustMarshalMsg(
  730. t,
  731. raftpb.Message{},
  732. ),
  733. ),
  734. errors.New("some error"),
  735. http.StatusInternalServerError,
  736. },
  737. {
  738. // good request
  739. "POST",
  740. bytes.NewReader(
  741. mustMarshalMsg(
  742. t,
  743. raftpb.Message{},
  744. ),
  745. ),
  746. nil,
  747. http.StatusNoContent,
  748. },
  749. }
  750. for i, tt := range testCases {
  751. req, err := http.NewRequest(tt.method, "foo", tt.body)
  752. if err != nil {
  753. t.Fatalf("#%d: could not create request: %#v", i, err)
  754. }
  755. h := &serverHandler{
  756. timeout: time.Hour,
  757. server: &errServer{tt.serverErr},
  758. peers: nil,
  759. }
  760. rw := httptest.NewRecorder()
  761. h.serveRaft(rw, req)
  762. if rw.Code != tt.wcode {
  763. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  764. }
  765. }
  766. }
  767. // resServer implements the etcd.Server interface for testing.
  768. // It returns the given responsefrom any Do calls, and nil error
  769. type resServer struct {
  770. res etcdserver.Response
  771. }
  772. func (rs *resServer) Do(_ context.Context, _ etcdserverpb.Request) (etcdserver.Response, error) {
  773. return rs.res, nil
  774. }
  775. func (rs *resServer) Process(_ context.Context, _ raftpb.Message) error { return nil }
  776. func (rs *resServer) Start() {}
  777. func (rs *resServer) Stop() {}
  778. func mustMarshalEvent(t *testing.T, ev *store.Event) string {
  779. b := new(bytes.Buffer)
  780. if err := json.NewEncoder(b).Encode(ev); err != nil {
  781. t.Fatalf("error marshalling event %#v: %v", ev, err)
  782. }
  783. return b.String()
  784. }
  785. func TestBadServeKeys(t *testing.T) {
  786. testBadCases := []struct {
  787. req *http.Request
  788. server etcdserver.Server
  789. wcode int
  790. }{
  791. {
  792. // bad method
  793. &http.Request{
  794. Method: "CONNECT",
  795. },
  796. &resServer{},
  797. http.StatusMethodNotAllowed,
  798. },
  799. {
  800. // bad method
  801. &http.Request{
  802. Method: "TRACE",
  803. },
  804. &resServer{},
  805. http.StatusMethodNotAllowed,
  806. },
  807. {
  808. // parseRequest error
  809. &http.Request{
  810. Body: nil,
  811. Method: "PUT",
  812. },
  813. &resServer{},
  814. http.StatusBadRequest,
  815. },
  816. {
  817. // etcdserver.Server error
  818. mustNewRequest(t, "foo"),
  819. &errServer{
  820. errors.New("blah"),
  821. },
  822. http.StatusInternalServerError,
  823. },
  824. {
  825. // non-event/watcher response from etcdserver.Server
  826. mustNewRequest(t, "foo"),
  827. &resServer{
  828. etcdserver.Response{},
  829. },
  830. http.StatusInternalServerError,
  831. },
  832. }
  833. for i, tt := range testBadCases {
  834. h := &serverHandler{
  835. timeout: 0, // context times out immediately
  836. server: tt.server,
  837. peers: nil,
  838. }
  839. rw := httptest.NewRecorder()
  840. h.serveKeys(rw, tt.req)
  841. if rw.Code != tt.wcode {
  842. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  843. }
  844. }
  845. }
  846. func TestServeKeysEvent(t *testing.T) {
  847. req := mustNewRequest(t, "foo")
  848. server := &resServer{
  849. etcdserver.Response{
  850. Event: &store.Event{
  851. Action: store.Get,
  852. Node: &store.NodeExtern{},
  853. },
  854. },
  855. }
  856. h := &serverHandler{
  857. timeout: time.Hour,
  858. server: server,
  859. peers: nil,
  860. }
  861. rw := httptest.NewRecorder()
  862. h.serveKeys(rw, req)
  863. wcode := http.StatusOK
  864. wbody := mustMarshalEvent(
  865. t,
  866. &store.Event{
  867. Action: store.Get,
  868. Node: &store.NodeExtern{},
  869. },
  870. )
  871. if rw.Code != wcode {
  872. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  873. }
  874. g := rw.Body.String()
  875. if g != wbody {
  876. t.Errorf("got body=%#v, want %#v", g, wbody)
  877. }
  878. }
  879. func TestServeKeysWatch(t *testing.T) {
  880. req := mustNewRequest(t, "/foo/bar")
  881. ec := make(chan *store.Event)
  882. dw := &dummyWatcher{
  883. echan: ec,
  884. }
  885. server := &resServer{
  886. etcdserver.Response{
  887. Watcher: dw,
  888. },
  889. }
  890. h := &serverHandler{
  891. timeout: time.Hour,
  892. server: server,
  893. peers: nil,
  894. }
  895. go func() {
  896. ec <- &store.Event{
  897. Action: store.Get,
  898. Node: &store.NodeExtern{},
  899. }
  900. }()
  901. rw := httptest.NewRecorder()
  902. h.serveKeys(rw, req)
  903. wcode := http.StatusOK
  904. wbody := mustMarshalEvent(
  905. t,
  906. &store.Event{
  907. Action: store.Get,
  908. Node: &store.NodeExtern{},
  909. },
  910. )
  911. if rw.Code != wcode {
  912. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  913. }
  914. g := rw.Body.String()
  915. if g != wbody {
  916. t.Errorf("got body=%#v, want %#v", g, wbody)
  917. }
  918. }
  919. func TestHandleWatch(t *testing.T) {
  920. rw := httptest.NewRecorder()
  921. wa := &dummyWatcher{
  922. echan: make(chan *store.Event, 1),
  923. }
  924. wa.echan <- &store.Event{
  925. Action: store.Get,
  926. Node: &store.NodeExtern{},
  927. }
  928. handleWatch(context.Background(), rw, wa, false)
  929. wcode := http.StatusOK
  930. wct := "application/json"
  931. wbody := mustMarshalEvent(
  932. t,
  933. &store.Event{
  934. Action: store.Get,
  935. Node: &store.NodeExtern{},
  936. },
  937. )
  938. if rw.Code != wcode {
  939. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  940. }
  941. h := rw.Header()
  942. if ct := h.Get("Content-Type"); ct != wct {
  943. t.Errorf("Content-Type=%q, want %q", ct, wct)
  944. }
  945. g := rw.Body.String()
  946. if g != wbody {
  947. t.Errorf("got body=%#v, want %#v", g, wbody)
  948. }
  949. }
  950. func TestHandleWatchNoEvent(t *testing.T) {
  951. rw := httptest.NewRecorder()
  952. wa := &dummyWatcher{
  953. echan: make(chan *store.Event, 1),
  954. }
  955. close(wa.echan)
  956. handleWatch(context.Background(), rw, wa, false)
  957. wcode := http.StatusOK
  958. wct := "application/json"
  959. wbody := ""
  960. if rw.Code != wcode {
  961. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  962. }
  963. h := rw.Header()
  964. if ct := h.Get("Content-Type"); ct != wct {
  965. t.Errorf("Content-Type=%q, want %q", ct, wct)
  966. }
  967. g := rw.Body.String()
  968. if g != wbody {
  969. t.Errorf("got body=%#v, want %#v", g, wbody)
  970. }
  971. }
  972. type recordingCloseNotifier struct {
  973. *httptest.ResponseRecorder
  974. cn chan bool
  975. }
  976. func (rcn *recordingCloseNotifier) CloseNotify() <-chan bool {
  977. return rcn.cn
  978. }
  979. func TestHandleWatchCloseNotified(t *testing.T) {
  980. rw := &recordingCloseNotifier{
  981. ResponseRecorder: httptest.NewRecorder(),
  982. cn: make(chan bool, 1),
  983. }
  984. rw.cn <- true
  985. wa := &dummyWatcher{}
  986. handleWatch(context.Background(), rw, wa, false)
  987. wcode := http.StatusOK
  988. wct := "application/json"
  989. wbody := ""
  990. if rw.Code != wcode {
  991. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  992. }
  993. h := rw.Header()
  994. if ct := h.Get("Content-Type"); ct != wct {
  995. t.Errorf("Content-Type=%q, want %q", ct, wct)
  996. }
  997. g := rw.Body.String()
  998. if g != wbody {
  999. t.Errorf("got body=%#v, want %#v", g, wbody)
  1000. }
  1001. }
  1002. func TestHandleWatchTimeout(t *testing.T) {
  1003. rw := httptest.NewRecorder()
  1004. wa := &dummyWatcher{}
  1005. // Simulate a timed-out context
  1006. ctx, cancel := context.WithCancel(context.Background())
  1007. cancel()
  1008. handleWatch(ctx, rw, wa, false)
  1009. wcode := http.StatusOK
  1010. wct := "application/json"
  1011. wbody := ""
  1012. if rw.Code != wcode {
  1013. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  1014. }
  1015. h := rw.Header()
  1016. if ct := h.Get("Content-Type"); ct != wct {
  1017. t.Errorf("Content-Type=%q, want %q", ct, wct)
  1018. }
  1019. g := rw.Body.String()
  1020. if g != wbody {
  1021. t.Errorf("got body=%#v, want %#v", g, wbody)
  1022. }
  1023. }
  1024. // flushingRecorder provides a channel to allow users to block until the Recorder is Flushed()
  1025. type flushingRecorder struct {
  1026. *httptest.ResponseRecorder
  1027. ch chan struct{}
  1028. }
  1029. func (fr *flushingRecorder) Flush() {
  1030. fr.ResponseRecorder.Flush()
  1031. fr.ch <- struct{}{}
  1032. }
  1033. func TestHandleWatchStreaming(t *testing.T) {
  1034. rw := &flushingRecorder{
  1035. httptest.NewRecorder(),
  1036. make(chan struct{}, 1),
  1037. }
  1038. wa := &dummyWatcher{
  1039. echan: make(chan *store.Event),
  1040. }
  1041. // Launch the streaming handler in the background with a cancellable context
  1042. ctx, cancel := context.WithCancel(context.Background())
  1043. done := make(chan struct{})
  1044. go func() {
  1045. handleWatch(ctx, rw, wa, true)
  1046. close(done)
  1047. }()
  1048. // Expect one Flush for the headers etc.
  1049. select {
  1050. case <-rw.ch:
  1051. case <-time.After(time.Second):
  1052. t.Fatalf("timed out waiting for flush")
  1053. }
  1054. // Expect headers but no body
  1055. wcode := http.StatusOK
  1056. wct := "application/json"
  1057. wbody := ""
  1058. if rw.Code != wcode {
  1059. t.Errorf("got code=%d, want %d", rw.Code, wcode)
  1060. }
  1061. h := rw.Header()
  1062. if ct := h.Get("Content-Type"); ct != wct {
  1063. t.Errorf("Content-Type=%q, want %q", ct, wct)
  1064. }
  1065. g := rw.Body.String()
  1066. if g != wbody {
  1067. t.Errorf("got body=%#v, want %#v", g, wbody)
  1068. }
  1069. // Now send the first event
  1070. select {
  1071. case wa.echan <- &store.Event{
  1072. Action: store.Get,
  1073. Node: &store.NodeExtern{},
  1074. }:
  1075. case <-time.After(time.Second):
  1076. t.Fatal("timed out waiting for send")
  1077. }
  1078. // Wait for it to be flushed...
  1079. select {
  1080. case <-rw.ch:
  1081. case <-time.After(time.Second):
  1082. t.Fatalf("timed out waiting for flush")
  1083. }
  1084. // And check the body is as expected
  1085. wbody = mustMarshalEvent(
  1086. t,
  1087. &store.Event{
  1088. Action: store.Get,
  1089. Node: &store.NodeExtern{},
  1090. },
  1091. )
  1092. g = rw.Body.String()
  1093. if g != wbody {
  1094. t.Errorf("got body=%#v, want %#v", g, wbody)
  1095. }
  1096. // Rinse and repeat
  1097. select {
  1098. case wa.echan <- &store.Event{
  1099. Action: store.Get,
  1100. Node: &store.NodeExtern{},
  1101. }:
  1102. case <-time.After(time.Second):
  1103. t.Fatal("timed out waiting for send")
  1104. }
  1105. select {
  1106. case <-rw.ch:
  1107. case <-time.After(time.Second):
  1108. t.Fatalf("timed out waiting for flush")
  1109. }
  1110. // This time, we expect to see both events
  1111. wbody = wbody + wbody
  1112. g = rw.Body.String()
  1113. if g != wbody {
  1114. t.Errorf("got body=%#v, want %#v", g, wbody)
  1115. }
  1116. // Finally, time out the connection and ensure the serving goroutine returns
  1117. cancel()
  1118. select {
  1119. case <-done:
  1120. case <-time.After(time.Second):
  1121. t.Fatalf("timed out waiting for done")
  1122. }
  1123. }