Skip to content

Commit

Permalink
api: restore Leader() and Peers() to avoid breaking function signatur…
Browse files Browse the repository at this point in the history
…es (#8395)

api: add TestAPI_StatusLeaderWithQueryOptions and TestAPI_StatusPeersWithQueryOptions
api: make TestAPI_Status* error messages more verbose
  • Loading branch information
mikemorris authored Jul 29, 2020
1 parent 7d2aa18 commit 85ef7ba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
24 changes: 20 additions & 4 deletions api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ func (c *Client) Status() *Status {
}

// Leader is used to query for a known leader
func (s *Status) Leader(q *QueryOptions) (string, error) {
func (s *Status) LeaderWithQueryOptions(q *QueryOptions) (string, error) {
r := s.c.newRequest("GET", "/v1/status/leader")
r.setQueryOptions(q)

if q != nil {
r.setQueryOptions(q)
}

_, resp, err := requireOK(s.c.doRequest(r))
if err != nil {
return "", err
Expand All @@ -27,10 +31,18 @@ func (s *Status) Leader(q *QueryOptions) (string, error) {
return leader, nil
}

func (s *Status) Leader() (string, error) {
return s.LeaderWithQueryOptions(nil)
}

// Peers is used to query for a known raft peers
func (s *Status) Peers(q *QueryOptions) ([]string, error) {
func (s *Status) PeersWithQueryOptions(q *QueryOptions) ([]string, error) {
r := s.c.newRequest("GET", "/v1/status/peers")
r.setQueryOptions(q)

if q != nil {
r.setQueryOptions(q)
}

_, resp, err := requireOK(s.c.doRequest(r))
if err != nil {
return nil, err
Expand All @@ -43,3 +55,7 @@ func (s *Status) Peers(q *QueryOptions) ([]string, error) {
}
return peers, nil
}

func (s *Status) Peers() ([]string, error) {
return s.PeersWithQueryOptions(nil)
}
48 changes: 42 additions & 6 deletions api/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,33 @@ func TestAPI_StatusLeader(t *testing.T) {

status := c.Status()

leader, err := status.Leader()
if err != nil {
t.Fatalf("err: %v", err)
}
if leader == "" {
t.Fatalf("Expected leader, found empty string")
}
}

func TestAPI_StatusLeaderWithQueryOptions(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
defer s.Stop()
s.WaitForSerfCheck(t)

status := c.Status()

opts := QueryOptions{
Datacenter: "dc1",
}

leader, err := status.Leader(&opts)
leader, err := status.LeaderWithQueryOptions(&opts)
if err != nil {
t.Fatalf("err: %v", err)
}
if leader == "" {
t.Fatalf("Expected leader")
t.Fatalf("Expected leader, found empty string")
}
}

Expand All @@ -34,15 +51,33 @@ func TestAPI_StatusPeers(t *testing.T) {

status := c.Status()

peers, err := status.Peers()
if err != nil {
t.Fatalf("err: %v", err)
}
if len(peers) == 0 {
t.Fatalf("Expected peers, found %d", len(peers))
}
}

func TestAPI_StatusPeersWithQueryOptions(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
defer s.Stop()
s.WaitForSerfCheck(t)

status := c.Status()

opts := QueryOptions{
Datacenter: "dc1",
}
peers, err := status.Peers(&opts)

peers, err := status.PeersWithQueryOptions(&opts)
if err != nil {
t.Fatalf("err: %v", err)
}
if len(peers) == 0 {
t.Fatalf("Expected peers ")
t.Fatalf("Expected peers, found %d", len(peers))
}
}

Expand All @@ -59,7 +94,8 @@ func TestAPI_StatusLeader_WrongDC(t *testing.T) {
opts := QueryOptions{
Datacenter: "wrong_dc1",
}
_, err := status.Leader(&opts)

_, err := status.LeaderWithQueryOptions(&opts)
require.Error(err)
require.Contains(err.Error(), "No path to datacenter")
}
Expand All @@ -77,7 +113,7 @@ func TestAPI_StatusPeers_WrongDC(t *testing.T) {
opts := QueryOptions{
Datacenter: "wrong_dc1",
}
_, err := status.Peers(&opts)
_, err := status.PeersWithQueryOptions(&opts)
require.Error(err)
require.Contains(err.Error(), "No path to datacenter")
}

0 comments on commit 85ef7ba

Please sign in to comment.