Sort /v1/agent/servers output

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 https://github.com/hashicorp/nomad/issues/3211
This commit is contained in:
Alex Dadgar
2017-09-14 10:04:48 -07:00
parent f9a914b7e2
commit e25dff5a28
2 changed files with 9 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ package agent
import (
"net"
"net/http"
"sort"
"strings"
"github.com/hashicorp/nomad/nomad/structs"
@@ -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
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
@@ -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 {
@@ -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)
}
})
}