|
@@ -114,6 +114,23 @@ func TestMembersAPIActionRemove(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func TestMembersAPIActionLeader(t *testing.T) {
|
|
|
|
|
+ ep := url.URL{Scheme: "http", Host: "example.com"}
|
|
|
|
|
+ act := &membersAPIActionLeader{}
|
|
|
|
|
+
|
|
|
|
|
+ wantURL := &url.URL{
|
|
|
|
|
+ Scheme: "http",
|
|
|
|
|
+ Host: "example.com",
|
|
|
|
|
+ Path: "/v2/members/leader",
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ got := *act.HTTPRequest(ep)
|
|
|
|
|
+ err := assertRequest(got, "GET", wantURL, http.Header{}, nil)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Error(err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func TestAssertStatusCode(t *testing.T) {
|
|
func TestAssertStatusCode(t *testing.T) {
|
|
|
if err := assertStatusCode(404, 400); err == nil {
|
|
if err := assertStatusCode(404, 400); err == nil {
|
|
|
t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
|
|
t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
|
|
@@ -520,3 +537,63 @@ func TestHTTPMembersAPIListError(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestHTTPMembersAPILeaderSuccess(t *testing.T) {
|
|
|
|
|
+ wantAction := &membersAPIActionLeader{}
|
|
|
|
|
+ mAPI := &httpMembersAPI{
|
|
|
|
|
+ client: &actionAssertingHTTPClient{
|
|
|
|
|
+ t: t,
|
|
|
|
|
+ act: wantAction,
|
|
|
|
|
+ resp: http.Response{
|
|
|
|
|
+ StatusCode: http.StatusOK,
|
|
|
|
|
+ },
|
|
|
|
|
+ body: []byte(`{"id":"94088180e21eb87b","name":"node2","peerURLs":["http://127.0.0.1:7002"],"clientURLs":["http://127.0.0.1:4002"]}`),
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ wantResponseMember := &Member{
|
|
|
|
|
+ ID: "94088180e21eb87b",
|
|
|
|
|
+ Name: "node2",
|
|
|
|
|
+ PeerURLs: []string{"http://127.0.0.1:7002"},
|
|
|
|
|
+ ClientURLs: []string{"http://127.0.0.1:4002"},
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ m, err := mAPI.Leader(context.Background())
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Errorf("err = %v, want %v", err, nil)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !reflect.DeepEqual(wantResponseMember, m) {
|
|
|
|
|
+ t.Errorf("incorrect member: member = %v, want %v", wantResponseMember, m)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestHTTPMembersAPILeaderError(t *testing.T) {
|
|
|
|
|
+ tests := []httpClient{
|
|
|
|
|
+ // generic httpClient failure
|
|
|
|
|
+ &staticHTTPClient{err: errors.New("fail!")},
|
|
|
|
|
+
|
|
|
|
|
+ // unrecognized HTTP status code
|
|
|
|
|
+ &staticHTTPClient{
|
|
|
|
|
+ resp: http.Response{StatusCode: http.StatusTeapot},
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ // fail to unmarshal body on StatusOK
|
|
|
|
|
+ &staticHTTPClient{
|
|
|
|
|
+ resp: http.Response{
|
|
|
|
|
+ StatusCode: http.StatusOK,
|
|
|
|
|
+ },
|
|
|
|
|
+ body: []byte(`[{"id":"XX`),
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for i, tt := range tests {
|
|
|
|
|
+ mAPI := &httpMembersAPI{client: tt}
|
|
|
|
|
+ m, err := mAPI.Leader(context.Background())
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Errorf("#%d: err = nil, want not nil", i)
|
|
|
|
|
+ }
|
|
|
|
|
+ if m != nil {
|
|
|
|
|
+ t.Errorf("member slice = %v, want nil", m)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|