From c44b54dea73517e7230c05078202c74c3b023b75 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 29 Jul 2020 12:09:15 -0400 Subject: [PATCH] api: restore Leader() and Peers() to avoid breaking function signatures (#8395) api: add TestAPI_StatusLeaderWithQueryOptions and TestAPI_StatusPeersWithQueryOptions api: make TestAPI_Status* error messages more verbose --- api/status.go | 24 +++++++++++++++--- api/status_test.go | 62 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/api/status.go b/api/status.go index 1b195a1d2..57f379c7b 100644 --- a/api/status.go +++ b/api/status.go @@ -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 @@ -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 @@ -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) +} diff --git a/api/status_test.go b/api/status_test.go index 175eccc62..8802cc561 100644 --- a/api/status_test.go +++ b/api/status_test.go @@ -13,16 +13,33 @@ func TestAPI_StatusLeader(t *testing.T) { status := c.Status() - opts := QueryOptions{ - Datacenter: "dc1", - } - - leader, err := status.Leader(&opts) + leader, err := status.Leader() if err != nil { t.Fatalf("err: %v", err) } if leader == "" { - t.Fatalf("Expected 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.LeaderWithQueryOptions(&opts) + if err != nil { + t.Fatalf("err: %v", err) + } + if leader == "" { + t.Fatalf("Expected leader, found empty string") } } @@ -34,15 +51,33 @@ func TestAPI_StatusPeers(t *testing.T) { status := c.Status() - opts := QueryOptions{ - Datacenter: "dc1", - } - peers, err := status.Peers(&opts) + peers, err := status.Peers() if err != nil { t.Fatalf("err: %v", err) } if len(peers) == 0 { - t.Fatalf("Expected peers ") + 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.PeersWithQueryOptions(&opts) + if err != nil { + t.Fatalf("err: %v", err) + } + if len(peers) == 0 { + t.Fatalf("Expected peers, found %d", len(peers)) } } @@ -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") } @@ -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") }