|
|
@@ -92,9 +92,10 @@ type serverRecorder struct {
|
|
|
actions []action
|
|
|
}
|
|
|
|
|
|
-func (s *serverRecorder) Start() {}
|
|
|
-func (s *serverRecorder) Stop() {}
|
|
|
-func (s *serverRecorder) ID() types.ID { return types.ID(1) }
|
|
|
+func (s *serverRecorder) Start() {}
|
|
|
+func (s *serverRecorder) Stop() {}
|
|
|
+func (s *serverRecorder) Leader() types.ID { return types.ID(1) }
|
|
|
+func (s *serverRecorder) ID() types.ID { return types.ID(1) }
|
|
|
func (s *serverRecorder) Do(_ context.Context, r etcdserverpb.Request) (etcdserver.Response, error) {
|
|
|
s.actions = append(s.actions, action{name: "Do", params: []interface{}{r}})
|
|
|
return etcdserver.Response{}, nil
|
|
|
@@ -139,9 +140,10 @@ type resServer struct {
|
|
|
res etcdserver.Response
|
|
|
}
|
|
|
|
|
|
-func (rs *resServer) Start() {}
|
|
|
-func (rs *resServer) Stop() {}
|
|
|
-func (rs *resServer) ID() types.ID { return types.ID(1) }
|
|
|
+func (rs *resServer) Start() {}
|
|
|
+func (rs *resServer) Stop() {}
|
|
|
+func (rs *resServer) ID() types.ID { return types.ID(1) }
|
|
|
+func (rs *resServer) Leader() types.ID { return types.ID(1) }
|
|
|
func (rs *resServer) Do(_ context.Context, _ etcdserverpb.Request) (etcdserver.Response, error) {
|
|
|
return rs.res, nil
|
|
|
}
|
|
|
@@ -604,6 +606,57 @@ func TestServeMembers(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// TODO: consolidate **ALL** fake server implementations and add no leader test case.
|
|
|
+func TestServeLeader(t *testing.T) {
|
|
|
+ memb1 := etcdserver.Member{ID: 1, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8080"}}}
|
|
|
+ memb2 := etcdserver.Member{ID: 2, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8081"}}}
|
|
|
+ cluster := &fakeCluster{
|
|
|
+ id: 1,
|
|
|
+ members: map[uint64]*etcdserver.Member{1: &memb1, 2: &memb2},
|
|
|
+ }
|
|
|
+ h := &membersHandler{
|
|
|
+ server: &serverRecorder{},
|
|
|
+ clock: clockwork.NewFakeClock(),
|
|
|
+ clusterInfo: cluster,
|
|
|
+ }
|
|
|
+
|
|
|
+ wmc := string(`{"id":"1","name":"","peerURLs":[],"clientURLs":["http://localhost:8080"]}`)
|
|
|
+
|
|
|
+ tests := []struct {
|
|
|
+ path string
|
|
|
+ wcode int
|
|
|
+ wct string
|
|
|
+ wbody string
|
|
|
+ }{
|
|
|
+ {membersPrefix + "leader", http.StatusOK, "application/json", wmc + "\n"},
|
|
|
+ // TODO: add no leader case
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, tt := range tests {
|
|
|
+ req, err := http.NewRequest("GET", mustNewURL(t, tt.path).String(), nil)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ rw := httptest.NewRecorder()
|
|
|
+ h.ServeHTTP(rw, req)
|
|
|
+
|
|
|
+ if rw.Code != tt.wcode {
|
|
|
+ t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
|
|
|
+ }
|
|
|
+ if gct := rw.Header().Get("Content-Type"); gct != tt.wct {
|
|
|
+ t.Errorf("#%d: content-type = %s, want %s", i, gct, tt.wct)
|
|
|
+ }
|
|
|
+ gcid := rw.Header().Get("X-Etcd-Cluster-ID")
|
|
|
+ wcid := cluster.ID().String()
|
|
|
+ if gcid != wcid {
|
|
|
+ t.Errorf("#%d: cid = %s, want %s", i, gcid, wcid)
|
|
|
+ }
|
|
|
+ if rw.Body.String() != tt.wbody {
|
|
|
+ t.Errorf("#%d: body = %q, want %q", i, rw.Body.String(), tt.wbody)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestServeMembersCreate(t *testing.T) {
|
|
|
u := mustNewURL(t, membersPrefix)
|
|
|
b := []byte(`{"peerURLs":["http://127.0.0.1:1"]}`)
|