mirror of https://github.com/status-im/consul.git
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
This commit is contained in:
parent
7d2aa180a4
commit
85ef7ba943
|
@ -11,9 +11,13 @@ func (c *Client) Status() *Status {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leader is used to query for a known leader
|
// 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 := s.c.newRequest("GET", "/v1/status/leader")
|
||||||
|
|
||||||
|
if q != nil {
|
||||||
r.setQueryOptions(q)
|
r.setQueryOptions(q)
|
||||||
|
}
|
||||||
|
|
||||||
_, resp, err := requireOK(s.c.doRequest(r))
|
_, resp, err := requireOK(s.c.doRequest(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -27,10 +31,18 @@ func (s *Status) Leader(q *QueryOptions) (string, error) {
|
||||||
return leader, nil
|
return leader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Status) Leader() (string, error) {
|
||||||
|
return s.LeaderWithQueryOptions(nil)
|
||||||
|
}
|
||||||
|
|
||||||
// Peers is used to query for a known raft peers
|
// 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 := s.c.newRequest("GET", "/v1/status/peers")
|
||||||
|
|
||||||
|
if q != nil {
|
||||||
r.setQueryOptions(q)
|
r.setQueryOptions(q)
|
||||||
|
}
|
||||||
|
|
||||||
_, resp, err := requireOK(s.c.doRequest(r))
|
_, resp, err := requireOK(s.c.doRequest(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -43,3 +55,7 @@ func (s *Status) Peers(q *QueryOptions) ([]string, error) {
|
||||||
}
|
}
|
||||||
return peers, nil
|
return peers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Status) Peers() ([]string, error) {
|
||||||
|
return s.PeersWithQueryOptions(nil)
|
||||||
|
}
|
||||||
|
|
|
@ -13,16 +13,33 @@ func TestAPI_StatusLeader(t *testing.T) {
|
||||||
|
|
||||||
status := c.Status()
|
status := c.Status()
|
||||||
|
|
||||||
opts := QueryOptions{
|
leader, err := status.Leader()
|
||||||
Datacenter: "dc1",
|
|
||||||
}
|
|
||||||
|
|
||||||
leader, err := status.Leader(&opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
if leader == "" {
|
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()
|
status := c.Status()
|
||||||
|
|
||||||
opts := QueryOptions{
|
peers, err := status.Peers()
|
||||||
Datacenter: "dc1",
|
|
||||||
}
|
|
||||||
peers, err := status.Peers(&opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
if len(peers) == 0 {
|
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{
|
opts := QueryOptions{
|
||||||
Datacenter: "wrong_dc1",
|
Datacenter: "wrong_dc1",
|
||||||
}
|
}
|
||||||
_, err := status.Leader(&opts)
|
|
||||||
|
_, err := status.LeaderWithQueryOptions(&opts)
|
||||||
require.Error(err)
|
require.Error(err)
|
||||||
require.Contains(err.Error(), "No path to datacenter")
|
require.Contains(err.Error(), "No path to datacenter")
|
||||||
}
|
}
|
||||||
|
@ -77,7 +113,7 @@ func TestAPI_StatusPeers_WrongDC(t *testing.T) {
|
||||||
opts := QueryOptions{
|
opts := QueryOptions{
|
||||||
Datacenter: "wrong_dc1",
|
Datacenter: "wrong_dc1",
|
||||||
}
|
}
|
||||||
_, err := status.Peers(&opts)
|
_, err := status.PeersWithQueryOptions(&opts)
|
||||||
require.Error(err)
|
require.Error(err)
|
||||||
require.Contains(err.Error(), "No path to datacenter")
|
require.Contains(err.Error(), "No path to datacenter")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue