|
|
@@ -22,6 +22,7 @@ import (
|
|
|
"net/http/httptest"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
+ "time"
|
|
|
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
|
"github.com/coreos/etcd/pkg/pbutil"
|
|
|
@@ -45,7 +46,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusMethodNotAllowed,
|
|
|
},
|
|
|
@@ -55,7 +56,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusMethodNotAllowed,
|
|
|
},
|
|
|
@@ -65,7 +66,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusMethodNotAllowed,
|
|
|
},
|
|
|
@@ -73,7 +74,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
// bad request body
|
|
|
"POST",
|
|
|
&errReader{},
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusBadRequest,
|
|
|
},
|
|
|
@@ -81,7 +82,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
// bad request protobuf
|
|
|
"POST",
|
|
|
strings.NewReader("malformed garbage"),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusBadRequest,
|
|
|
},
|
|
|
@@ -91,7 +92,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"1",
|
|
|
http.StatusPreconditionFailed,
|
|
|
},
|
|
|
@@ -101,7 +102,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &errProcessor{
|
|
|
+ &fakeRaft{
|
|
|
err: &resWriterToError{code: http.StatusForbidden},
|
|
|
},
|
|
|
"0",
|
|
|
@@ -113,7 +114,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &errProcessor{
|
|
|
+ &fakeRaft{
|
|
|
err: &resWriterToError{code: http.StatusInternalServerError},
|
|
|
},
|
|
|
"0",
|
|
|
@@ -125,7 +126,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &errProcessor{err: errors.New("blah")},
|
|
|
+ &fakeRaft{err: errors.New("blah")},
|
|
|
"0",
|
|
|
http.StatusInternalServerError,
|
|
|
},
|
|
|
@@ -135,7 +136,7 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
bytes.NewReader(
|
|
|
pbutil.MustMarshal(&raftpb.Message{}),
|
|
|
),
|
|
|
- &nopProcessor{},
|
|
|
+ &fakeRaft{},
|
|
|
"0",
|
|
|
http.StatusNoContent,
|
|
|
},
|
|
|
@@ -155,24 +156,177 @@ func TestServeRaftPrefix(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestServeRaftStreamPrefix(t *testing.T) {
|
|
|
+ tests := []struct {
|
|
|
+ path string
|
|
|
+ wtype streamType
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ streamTypeMessage,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ RaftStreamPrefix + "/msgapp/1",
|
|
|
+ streamTypeMsgApp,
|
|
|
+ },
|
|
|
+ // backward compatibility
|
|
|
+ {
|
|
|
+ RaftStreamPrefix + "/1",
|
|
|
+ streamTypeMsgApp,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for i, tt := range tests {
|
|
|
+ req, err := http.NewRequest("GET", "http://localhost:7001"+tt.path, nil)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("#%d: could not create request: %#v", i, err)
|
|
|
+ }
|
|
|
+ req.Header.Set("X-Etcd-Cluster-ID", "1")
|
|
|
+ req.Header.Set("X-Raft-To", "2")
|
|
|
+ wterm := "1"
|
|
|
+ req.Header.Set("X-Raft-Term", wterm)
|
|
|
+
|
|
|
+ peer := newFakePeer()
|
|
|
+ peerGetter := &fakePeerGetter{peers: map[types.ID]Peer{types.ID(1): peer}}
|
|
|
+ h := newStreamHandler(peerGetter, types.ID(2), types.ID(1))
|
|
|
+
|
|
|
+ rw := httptest.NewRecorder()
|
|
|
+ go h.ServeHTTP(rw, req)
|
|
|
+
|
|
|
+ var conn *outgoingConn
|
|
|
+ select {
|
|
|
+ case conn = <-peer.connc:
|
|
|
+ case <-time.After(time.Second):
|
|
|
+ t.Fatalf("#%d: failed to attach outgoingConn", i)
|
|
|
+ }
|
|
|
+ if conn.t != tt.wtype {
|
|
|
+ t.Errorf("$%d: type = %s, want %s", i, conn.t, tt.wtype)
|
|
|
+ }
|
|
|
+ if conn.termStr != wterm {
|
|
|
+ t.Errorf("$%d: term = %s, want %s", i, conn.termStr, wterm)
|
|
|
+ }
|
|
|
+ conn.Close()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestServeRaftStreamPrefixBad(t *testing.T) {
|
|
|
+ tests := []struct {
|
|
|
+ method string
|
|
|
+ path string
|
|
|
+ clusterID string
|
|
|
+ remote string
|
|
|
+
|
|
|
+ wcode int
|
|
|
+ }{
|
|
|
+ // bad method
|
|
|
+ {
|
|
|
+ "PUT",
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusMethodNotAllowed,
|
|
|
+ },
|
|
|
+ // bad method
|
|
|
+ {
|
|
|
+ "POST",
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusMethodNotAllowed,
|
|
|
+ },
|
|
|
+ // bad method
|
|
|
+ {
|
|
|
+ "DELETE",
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusMethodNotAllowed,
|
|
|
+ },
|
|
|
+ // bad path
|
|
|
+ {
|
|
|
+ "GET",
|
|
|
+ RaftStreamPrefix + "/strange/1",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusNotFound,
|
|
|
+ },
|
|
|
+ // bad path
|
|
|
+ {
|
|
|
+ "GET",
|
|
|
+ RaftStreamPrefix + "/strange",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusNotFound,
|
|
|
+ },
|
|
|
+ // non-existant peer
|
|
|
+ {
|
|
|
+ "GET",
|
|
|
+ RaftStreamPrefix + "/message/2",
|
|
|
+ "1",
|
|
|
+ "1",
|
|
|
+ http.StatusNotFound,
|
|
|
+ },
|
|
|
+ // wrong cluster ID
|
|
|
+ {
|
|
|
+ "GET",
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ "2",
|
|
|
+ "1",
|
|
|
+ http.StatusPreconditionFailed,
|
|
|
+ },
|
|
|
+ // wrong remote id
|
|
|
+ {
|
|
|
+ "GET",
|
|
|
+ RaftStreamPrefix + "/message/1",
|
|
|
+ "1",
|
|
|
+ "2",
|
|
|
+ http.StatusPreconditionFailed,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for i, tt := range tests {
|
|
|
+ req, err := http.NewRequest(tt.method, "http://localhost:7001"+tt.path, nil)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("#%d: could not create request: %#v", i, err)
|
|
|
+ }
|
|
|
+ req.Header.Set("X-Etcd-Cluster-ID", tt.clusterID)
|
|
|
+ req.Header.Set("X-Raft-To", tt.remote)
|
|
|
+ rw := httptest.NewRecorder()
|
|
|
+ peerGetter := &fakePeerGetter{peers: map[types.ID]Peer{types.ID(1): newFakePeer()}}
|
|
|
+ h := newStreamHandler(peerGetter, types.ID(1), types.ID(1))
|
|
|
+ h.ServeHTTP(rw, req)
|
|
|
+
|
|
|
+ if rw.Code != tt.wcode {
|
|
|
+ t.Errorf("#%d: code = %d, want %d", i, rw.Code, tt.wcode)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestCloseNotifier(t *testing.T) {
|
|
|
+ c := newCloseNotifier()
|
|
|
+ select {
|
|
|
+ case <-c.closeNotify():
|
|
|
+ t.Fatalf("received unexpected close notification")
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ c.Close()
|
|
|
+ select {
|
|
|
+ case <-c.closeNotify():
|
|
|
+ default:
|
|
|
+ t.Fatalf("failed to get close notification")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// errReader implements io.Reader to facilitate a broken request.
|
|
|
type errReader struct{}
|
|
|
|
|
|
func (er *errReader) Read(_ []byte) (int, error) { return 0, errors.New("some error") }
|
|
|
|
|
|
-type nopProcessor struct{}
|
|
|
-
|
|
|
-func (p *nopProcessor) Process(ctx context.Context, m raftpb.Message) error { return nil }
|
|
|
-func (p *nopProcessor) ReportUnreachable(id uint64) {}
|
|
|
-func (p *nopProcessor) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
|
|
|
-
|
|
|
-type errProcessor struct {
|
|
|
+type fakeRaft struct {
|
|
|
err error
|
|
|
}
|
|
|
|
|
|
-func (p *errProcessor) Process(ctx context.Context, m raftpb.Message) error { return p.err }
|
|
|
-func (p *errProcessor) ReportUnreachable(id uint64) {}
|
|
|
-func (p *errProcessor) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
|
|
|
+func (p *fakeRaft) Process(ctx context.Context, m raftpb.Message) error { return p.err }
|
|
|
+func (p *fakeRaft) ReportUnreachable(id uint64) {}
|
|
|
+func (p *fakeRaft) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
|
|
|
|
|
|
type resWriterToError struct {
|
|
|
code int
|
|
|
@@ -180,3 +334,26 @@ type resWriterToError struct {
|
|
|
|
|
|
func (e *resWriterToError) Error() string { return "" }
|
|
|
func (e *resWriterToError) WriteTo(w http.ResponseWriter) { w.WriteHeader(e.code) }
|
|
|
+
|
|
|
+type fakePeerGetter struct {
|
|
|
+ peers map[types.ID]Peer
|
|
|
+}
|
|
|
+
|
|
|
+func (pg *fakePeerGetter) Get(id types.ID) Peer { return pg.peers[id] }
|
|
|
+
|
|
|
+type fakePeer struct {
|
|
|
+ msgs []raftpb.Message
|
|
|
+ u string
|
|
|
+ connc chan *outgoingConn
|
|
|
+}
|
|
|
+
|
|
|
+func newFakePeer() *fakePeer {
|
|
|
+ return &fakePeer{
|
|
|
+ connc: make(chan *outgoingConn, 1),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (pr *fakePeer) Send(m raftpb.Message) { pr.msgs = append(pr.msgs, m) }
|
|
|
+func (pr *fakePeer) Update(u string) { pr.u = u }
|
|
|
+func (pr *fakePeer) attachOutgoingConn(conn *outgoingConn) { pr.connc <- conn }
|
|
|
+func (pr *fakePeer) Stop() {}
|