From 135c4095731e3da9d5d54fd1f1b502ac262beec4 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sat, 5 Jul 2014 09:49:10 -0700 Subject: [PATCH] agent: Fixing passing filter. Fixes #241 --- command/agent/health_endpoint.go | 2 ++ command/agent/health_endpoint_test.go | 37 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/command/agent/health_endpoint.go b/command/agent/health_endpoint.go index 8462e0a4b2..3ee02ac5e6 100644 --- a/command/agent/health_endpoint.go +++ b/command/agent/health_endpoint.go @@ -117,6 +117,7 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ // filterNonPassing is used to filter out any nodes that have check that are not passing func filterNonPassing(nodes structs.CheckServiceNodes) structs.CheckServiceNodes { n := len(nodes) +OUTER: for i := 0; i < n; i++ { node := nodes[i] for _, check := range node.Checks { @@ -124,6 +125,7 @@ func filterNonPassing(nodes structs.CheckServiceNodes) structs.CheckServiceNodes nodes[i], nodes[n-1] = nodes[n-1], structs.CheckServiceNode{} n-- i-- + continue OUTER } } } diff --git a/command/agent/health_endpoint_test.go b/command/agent/health_endpoint_test.go index c1f75a2698..40ceedb0f3 100644 --- a/command/agent/health_endpoint_test.go +++ b/command/agent/health_endpoint_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "reflect" "testing" ) @@ -182,3 +183,39 @@ func TestHealthServiceNodes_PassingFilter(t *testing.T) { t.Fatalf("bad: %v", obj) } } + +func TestFilterNonPassing(t *testing.T) { + nodes := structs.CheckServiceNodes{ + structs.CheckServiceNode{ + Checks: structs.HealthChecks{ + &structs.HealthCheck{ + Status: structs.HealthCritical, + }, + &structs.HealthCheck{ + Status: structs.HealthCritical, + }, + }, + }, + structs.CheckServiceNode{ + Checks: structs.HealthChecks{ + &structs.HealthCheck{ + Status: structs.HealthCritical, + }, + &structs.HealthCheck{ + Status: structs.HealthCritical, + }, + }, + }, + structs.CheckServiceNode{ + Checks: structs.HealthChecks{ + &structs.HealthCheck{ + Status: structs.HealthPassing, + }, + }, + }, + } + out := filterNonPassing(nodes) + if len(out) != 1 && reflect.DeepEqual(out[0], nodes[2]) { + t.Fatalf("bad: %v", out) + } +}