From 6980ab0a816ea1901240c89d5e3735793faa46b9 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Fri, 1 Nov 2019 13:58:22 -0400 Subject: [PATCH] Prevent nomad alloc status output inconsistency Prevent random map ordering and sort alphabetically better variable name --- command/alloc_status_test.go | 3 +++ command/monitor.go | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index b3c7fef2070e..0c0bf38e9c9e 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -286,6 +286,9 @@ func TestAllocStatusCommand_ScoreMetrics(t *testing.T) { require.Contains(out, "Placement Metrics") require.Contains(out, mockNode1.ID) require.Contains(out, mockNode2.ID) + + // assert we sort headers alphabetically + require.Contains(out, "binpack node-affinity") require.Contains(out, "final score") } diff --git a/command/monitor.go b/command/monitor.go index 52cee4b80b84..c7f0f9d55567 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "sort" "strings" "sync" "time" @@ -380,7 +381,16 @@ func formatAllocMetrics(metrics *api.AllocationMetric, scores bool, prefix strin // Add header as first row if i == 0 { scoreOutput[0] = "Node|" - for scorerName := range scoreMeta.Scores { + + // sort scores alphabetically + scores := make([]string, 0, len(scoreMeta.Scores)) + for score := range scoreMeta.Scores { + scores = append(scores, score) + } + sort.Strings(scores) + + // build score header output + for _, scorerName := range scores { scoreOutput[0] += fmt.Sprintf("%v|", scorerName) scorerNames = append(scorerNames, scorerName) }