peer_test.go 4.3 KB

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