From bf7a62e0e04e5fc56ff17715156e6a789e869bf0 Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Fri, 8 Jun 2018 13:10:06 +0100 Subject: [PATCH] Sort intention list by precedence --- agent/consul/state/intention.go | 5 +++++ agent/intentions_endpoint_test.go | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/agent/consul/state/intention.go b/agent/consul/state/intention.go index 91a61ffe1c..9bbf17f709 100644 --- a/agent/consul/state/intention.go +++ b/agent/consul/state/intention.go @@ -144,6 +144,11 @@ func (s *Store) Intentions(ws memdb.WatchSet) (uint64, structs.Intentions, error for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { results = append(results, ixn.(*structs.Intention)) } + + // Sort by precedence just because that's nicer and probably what most clients + // want for presentation. + sort.Sort(structs.IntentionPrecedenceSorter(results)) + return idx, results, nil } diff --git a/agent/intentions_endpoint_test.go b/agent/intentions_endpoint_test.go index a084edacc2..4eb6c985c1 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "sort" "testing" "github.com/hashicorp/consul/agent/structs" @@ -37,8 +36,9 @@ func TestIntentionsList_values(t *testing.T) { a := NewTestAgent(t.Name(), "") defer a.Shutdown() - // Create some intentions - for _, v := range []string{"foo", "bar"} { + // Create some intentions, note we create the lowest precedence first to test + // sorting. + for _, v := range []string{"*", "foo", "bar"} { req := structs.IntentionRequest{ Datacenter: "dc1", Op: structs.IntentionOpCreate, @@ -54,14 +54,17 @@ func TestIntentionsList_values(t *testing.T) { req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil) resp := httptest.NewRecorder() obj, err := a.srv.IntentionList(resp, req) - assert.Nil(err) + assert.NoError(err) value := obj.(structs.Intentions) - assert.Len(value, 2) + assert.Len(value, 3) - expected := []string{"bar", "foo"} - actual := []string{value[0].SourceName, value[1].SourceName} - sort.Strings(actual) + expected := []string{"bar", "foo", "*"} + actual := []string{ + value[0].SourceName, + value[1].SourceName, + value[2].SourceName, + } assert.Equal(expected, actual) }