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() {
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
}

View File

@ -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)
}