Sort intention list by precedence

This commit is contained in:
Paul Banks 2018-06-08 13:10:06 +01:00 committed by Jack Pearkes
parent efa2bdb88b
commit e2938138f6
2 changed files with 16 additions and 8 deletions

View File

@ -144,6 +144,11 @@ func (s *Store) Intentions(ws memdb.WatchSet) (uint64, structs.Intentions, error
for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { for ixn := iter.Next(); ixn != nil; ixn = iter.Next() {
results = append(results, ixn.(*structs.Intention)) 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 return idx, results, nil
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"sort"
"testing" "testing"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
@ -37,8 +36,9 @@ func TestIntentionsList_values(t *testing.T) {
a := NewTestAgent(t.Name(), "") a := NewTestAgent(t.Name(), "")
defer a.Shutdown() defer a.Shutdown()
// Create some intentions // Create some intentions, note we create the lowest precedence first to test
for _, v := range []string{"foo", "bar"} { // sorting.
for _, v := range []string{"*", "foo", "bar"} {
req := structs.IntentionRequest{ req := structs.IntentionRequest{
Datacenter: "dc1", Datacenter: "dc1",
Op: structs.IntentionOpCreate, Op: structs.IntentionOpCreate,
@ -54,14 +54,17 @@ func TestIntentionsList_values(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil) req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := a.srv.IntentionList(resp, req) obj, err := a.srv.IntentionList(resp, req)
assert.Nil(err) assert.NoError(err)
value := obj.(structs.Intentions) value := obj.(structs.Intentions)
assert.Len(value, 2) assert.Len(value, 3)
expected := []string{"bar", "foo"} expected := []string{"bar", "foo", "*"}
actual := []string{value[0].SourceName, value[1].SourceName} actual := []string{
sort.Strings(actual) value[0].SourceName,
value[1].SourceName,
value[2].SourceName,
}
assert.Equal(expected, actual) assert.Equal(expected, actual)
} }