From e02f10311c6e47d051bc4824d8ea783607fc0895 Mon Sep 17 00:00:00 2001 From: Nic Jackson Date: Thu, 30 Jul 2020 11:26:15 +0100 Subject: [PATCH] Modify Status Leader API to return Status 500 when there is no leader --- .changelog/8408.txt | 4 ++++ agent/status_endpoint.go | 6 ++++++ agent/status_endpoint_test.go | 13 +++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 .changelog/8408.txt diff --git a/.changelog/8408.txt b/.changelog/8408.txt new file mode 100644 index 000000000000..396e3ec5a89b --- /dev/null +++ b/.changelog/8408.txt @@ -0,0 +1,4 @@ +```release-note:bug +api: when the cluster has no leader return an error from `/v1/status/leader` to be consistent with all other endpoints. +``` + diff --git a/agent/status_endpoint.go b/agent/status_endpoint.go index 5cc329ac9e3a..76851971f87c 100644 --- a/agent/status_endpoint.go +++ b/agent/status_endpoint.go @@ -16,6 +16,12 @@ func (s *HTTPHandlers) StatusLeader(resp http.ResponseWriter, req *http.Request) if err := s.agent.RPC("Status.Leader", &args, &out); err != nil { return nil, err } + + // check there is a leader, if not return a status no content + if out == "" { + return nil, structs.ErrNoLeader + } + return out, nil } diff --git a/agent/status_endpoint_test.go b/agent/status_endpoint_test.go index 93768618c322..f0b6dddadda1 100644 --- a/agent/status_endpoint_test.go +++ b/agent/status_endpoint_test.go @@ -5,6 +5,7 @@ import ( "net/http" "testing" + "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/testrpc" "github.com/stretchr/testify/require" @@ -31,6 +32,18 @@ func TestStatusLeader(t *testing.T) { } } +func TestStatusLeaderNoLeaderReturnsStatus500(t *testing.T) { + t.Parallel() + a := NewTestAgent(t, "bootstrap_expect=2 bootstrap=false") + defer a.Shutdown() + + req, _ := http.NewRequest("GET", "/v1/status/leader", nil) + _, err := a.srv.StatusLeader(nil, req) + + require.Error(t, err) + require.Equal(t, structs.ErrNoLeader, err) +} + func TestStatusLeaderSecondary(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short")