From bad3b6e3055fea8dedf26f4fbd9e6924934c161f Mon Sep 17 00:00:00 2001 From: spooner Date: Tue, 28 Jul 2020 19:26:50 +0300 Subject: [PATCH] Added QueryOptions for status api (#7818) * Added QueryOptions & Tests for status api --- api/status.go | 6 ++++-- api/status_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/api/status.go b/api/status.go index 74ef61a67..1b195a1d2 100644 --- a/api/status.go +++ b/api/status.go @@ -11,8 +11,9 @@ func (c *Client) Status() *Status { } // Leader is used to query for a known leader -func (s *Status) Leader() (string, error) { +func (s *Status) Leader(q *QueryOptions) (string, error) { r := s.c.newRequest("GET", "/v1/status/leader") + r.setQueryOptions(q) _, resp, err := requireOK(s.c.doRequest(r)) if err != nil { return "", err @@ -27,8 +28,9 @@ func (s *Status) Leader() (string, error) { } // Peers is used to query for a known raft peers -func (s *Status) Peers() ([]string, error) { +func (s *Status) Peers(q *QueryOptions) ([]string, error) { r := s.c.newRequest("GET", "/v1/status/peers") + r.setQueryOptions(q) _, resp, err := requireOK(s.c.doRequest(r)) if err != nil { return nil, err diff --git a/api/status_test.go b/api/status_test.go index 62c8a193f..175eccc62 100644 --- a/api/status_test.go +++ b/api/status_test.go @@ -1,6 +1,7 @@ package api import ( + "github.com/stretchr/testify/require" "testing" ) @@ -12,7 +13,11 @@ func TestAPI_StatusLeader(t *testing.T) { status := c.Status() - leader, err := status.Leader() + opts := QueryOptions{ + Datacenter: "dc1", + } + + leader, err := status.Leader(&opts) if err != nil { t.Fatalf("err: %v", err) } @@ -29,7 +34,10 @@ func TestAPI_StatusPeers(t *testing.T) { status := c.Status() - peers, err := status.Peers() + opts := QueryOptions{ + Datacenter: "dc1", + } + peers, err := status.Peers(&opts) if err != nil { t.Fatalf("err: %v", err) } @@ -37,3 +45,39 @@ func TestAPI_StatusPeers(t *testing.T) { t.Fatalf("Expected peers ") } } + +func TestAPI_StatusLeader_WrongDC(t *testing.T) { + t.Parallel() + require := require.New(t) + + c, s := makeClient(t) + defer s.Stop() + s.WaitForSerfCheck(t) + + status := c.Status() + + opts := QueryOptions{ + Datacenter: "wrong_dc1", + } + _, err := status.Leader(&opts) + require.Error(err) + require.Contains(err.Error(), "No path to datacenter") +} + +func TestAPI_StatusPeers_WrongDC(t *testing.T) { + t.Parallel() + require := require.New(t) + + c, s := makeClient(t) + defer s.Stop() + s.WaitForSerfCheck(t) + + status := c.Status() + + opts := QueryOptions{ + Datacenter: "wrong_dc1", + } + _, err := status.Peers(&opts) + require.Error(err) + require.Contains(err.Error(), "No path to datacenter") +}