peer_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2015 The etcd Authors
  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 etcdhttp
  15. import (
  16. "encoding/json"
  17. "io/ioutil"
  18. "net/http"
  19. "net/http/httptest"
  20. "path"
  21. "sort"
  22. "testing"
  23. "github.com/coreos/etcd/etcdserver/membership"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/rafthttp"
  27. "github.com/coreos/go-semver/semver"
  28. )
  29. type fakeCluster struct {
  30. id uint64
  31. clientURLs []string
  32. members map[uint64]*membership.Member
  33. }
  34. func (c *fakeCluster) ID() types.ID { return types.ID(c.id) }
  35. func (c *fakeCluster) ClientURLs() []string { return c.clientURLs }
  36. func (c *fakeCluster) Members() []*membership.Member {
  37. var ms membership.MembersByID
  38. for _, m := range c.members {
  39. ms = append(ms, m)
  40. }
  41. sort.Sort(ms)
  42. return []*membership.Member(ms)
  43. }
  44. func (c *fakeCluster) Member(id types.ID) *membership.Member { return c.members[uint64(id)] }
  45. func (c *fakeCluster) Version() *semver.Version { return nil }
  46. // TestNewPeerHandlerOnRaftPrefix tests that NewPeerHandler returns a handler that
  47. // handles raft-prefix requests well.
  48. func TestNewPeerHandlerOnRaftPrefix(t *testing.T) {
  49. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. w.Write([]byte("test data"))
  51. })
  52. ph := newPeerHandler(&fakeCluster{}, h, nil)
  53. srv := httptest.NewServer(ph)
  54. defer srv.Close()
  55. tests := []string{
  56. rafthttp.RaftPrefix,
  57. rafthttp.RaftPrefix + "/hello",
  58. }
  59. for i, tt := range tests {
  60. resp, err := http.Get(srv.URL + tt)
  61. if err != nil {
  62. t.Fatalf("unexpected http.Get error: %v", err)
  63. }
  64. body, err := ioutil.ReadAll(resp.Body)
  65. if err != nil {
  66. t.Fatalf("unexpected ioutil.ReadAll error: %v", err)
  67. }
  68. if w := "test data"; string(body) != w {
  69. t.Errorf("#%d: body = %s, want %s", i, body, w)
  70. }
  71. }
  72. }
  73. func TestServeMembersFails(t *testing.T) {
  74. tests := []struct {
  75. method string
  76. wcode int
  77. }{
  78. {
  79. "POST",
  80. http.StatusMethodNotAllowed,
  81. },
  82. {
  83. "DELETE",
  84. http.StatusMethodNotAllowed,
  85. },
  86. {
  87. "BAD",
  88. http.StatusMethodNotAllowed,
  89. },
  90. }
  91. for i, tt := range tests {
  92. rw := httptest.NewRecorder()
  93. h := &peerMembersHandler{cluster: nil}
  94. h.ServeHTTP(rw, &http.Request{Method: tt.method})
  95. if rw.Code != tt.wcode {
  96. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  97. }
  98. }
  99. }
  100. func TestServeMembersGet(t *testing.T) {
  101. memb1 := membership.Member{ID: 1, Attributes: membership.Attributes{ClientURLs: []string{"http://localhost:8080"}}}
  102. memb2 := membership.Member{ID: 2, Attributes: membership.Attributes{ClientURLs: []string{"http://localhost:8081"}}}
  103. cluster := &fakeCluster{
  104. id: 1,
  105. members: map[uint64]*membership.Member{1: &memb1, 2: &memb2},
  106. }
  107. h := &peerMembersHandler{cluster: cluster}
  108. msb, err := json.Marshal([]membership.Member{memb1, memb2})
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. wms := string(msb) + "\n"
  113. tests := []struct {
  114. path string
  115. wcode int
  116. wct string
  117. wbody string
  118. }{
  119. {peerMembersPrefix, http.StatusOK, "application/json", wms},
  120. {path.Join(peerMembersPrefix, "bad"), http.StatusBadRequest, "text/plain; charset=utf-8", "bad path\n"},
  121. }
  122. for i, tt := range tests {
  123. req, err := http.NewRequest("GET", testutil.MustNewURL(t, tt.path).String(), nil)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. rw := httptest.NewRecorder()
  128. h.ServeHTTP(rw, req)
  129. if rw.Code != tt.wcode {
  130. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  131. }
  132. if gct := rw.Header().Get("Content-Type"); gct != tt.wct {
  133. t.Errorf("#%d: content-type = %s, want %s", i, gct, tt.wct)
  134. }
  135. if rw.Body.String() != tt.wbody {
  136. t.Errorf("#%d: body = %s, want %s", i, rw.Body.String(), tt.wbody)
  137. }
  138. gcid := rw.Header().Get("X-Etcd-Cluster-ID")
  139. wcid := cluster.ID().String()
  140. if gcid != wcid {
  141. t.Errorf("#%d: cid = %s, want %s", i, gcid, wcid)
  142. }
  143. }
  144. }