Skip to content

Commit

Permalink
Sort /v1/agent/servers output
Browse files Browse the repository at this point in the history
This PR sorts the output of the endpoint since its results are used as
part of Consul checks to avoid the value changing unnecessarily.

Fixes #3211
  • Loading branch information
dadgar committed Sep 14, 2017
1 parent f9a914b commit e25dff5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
2 changes: 2 additions & 0 deletions command/agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"net"
"net/http"
"sort"
"strings"

"github.com/hashicorp/nomad/nomad/structs"
Expand Down Expand Up @@ -148,6 +149,7 @@ func (s *HTTPServer) listServers(resp http.ResponseWriter, req *http.Request) (i
}

peers := s.agent.client.GetServers()
sort.Strings(peers)
return peers, nil
}

Expand Down
24 changes: 7 additions & 17 deletions command/agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -175,10 +176,10 @@ func TestHTTP_AgentSetServers(t *testing.T) {
respW = httptest.NewRecorder()

// Make the request and check the result
expected := map[string]bool{
"127.0.0.1:4647": true,
"127.0.0.2:4647": true,
"127.0.0.3:4647": true,
expected := []string{
"127.0.0.1:4647",
"127.0.0.2:4647",
"127.0.0.3:4647",
}
out, err := s.Server.AgentServersRequest(respW, req)
if err != nil {
Expand All @@ -188,19 +189,8 @@ func TestHTTP_AgentSetServers(t *testing.T) {
if n := len(servers); n != len(expected) {
t.Fatalf("expected %d servers, got: %d: %v", len(expected), n, servers)
}
received := make(map[string]bool, len(servers))
for _, server := range servers {
received[server] = true
}
foundCount := 0
for k, _ := range received {
_, found := expected[k]
if found {
foundCount++
}
}
if foundCount != len(expected) {
t.Fatalf("bad servers result")
if !reflect.DeepEqual(servers, expected) {
t.Fatalf("got %v; want %v", servers, expected)
}
})
}
Expand Down

0 comments on commit e25dff5

Please sign in to comment.