Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Status Leader API to return Status 500 when there is no leader #8408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/8408.txt
Original file line number Diff line number Diff line change
@@ -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.
```

6 changes: 6 additions & 0 deletions agent/status_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions agent/status_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down