From 4a54f8f7e361878b12c2ee39f908bbce35c44690 Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Fri, 8 Jun 2018 13:58:46 +0100 Subject: [PATCH] Fix some tests failures caused by the sorting change and some cuased by previous UpdatePrecedence() change --- agent/consul/state/intention.go | 13 +++++++++++-- agent/consul/state/intention_test.go | 13 ++++++++++++- agent/structs/intention.go | 4 ++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/agent/consul/state/intention.go b/agent/consul/state/intention.go index 9bbf17f709..80a150cfa1 100644 --- a/agent/consul/state/intention.go +++ b/agent/consul/state/intention.go @@ -106,6 +106,11 @@ func (s *Snapshot) Intentions() (structs.Intentions, error) { var ret structs.Intentions for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() { + // Update precedence values for consistency (i.e. the object returned should + // be usable/comparable to the ones returned from Intentions() and Match() + // etc.) + ixn := wrapped.(*structs.Intention) + ixn.UpdatePrecedence() ret = append(ret, wrapped.(*structs.Intention)) } @@ -142,7 +147,9 @@ func (s *Store) Intentions(ws memdb.WatchSet) (uint64, structs.Intentions, error var results structs.Intentions for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { - results = append(results, ixn.(*structs.Intention)) + ixnReal := ixn.(*structs.Intention) + ixnReal.UpdatePrecedence() + results = append(results, ixnReal) } // Sort by precedence just because that's nicer and probably what most clients @@ -317,7 +324,9 @@ func (s *Store) IntentionMatch(ws memdb.WatchSet, args *structs.IntentionQueryMa ws.Add(iter.WatchCh()) for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { - ixns = append(ixns, ixn.(*structs.Intention)) + ixnReal := ixn.(*structs.Intention) + ixnReal.UpdatePrecedence() + ixns = append(ixns, ixnReal) } } diff --git a/agent/consul/state/intention_test.go b/agent/consul/state/intention_test.go index fbf43c19bd..fc161cddb4 100644 --- a/agent/consul/state/intention_test.go +++ b/agent/consul/state/intention_test.go @@ -273,6 +273,9 @@ func TestStore_IntentionsList(t *testing.T) { }, }, } + for i := range expected { + expected[i].UpdatePrecedence() // to match what is returned... + } idx, actual, err := s.Intentions(nil) assert.NoError(err) assert.Equal(idx, uint64(2)) @@ -496,6 +499,8 @@ func TestStore_Intention_Snapshot_Restore(t *testing.T) { // Verify the snapshot. assert.Equal(snap.LastIndex(), uint64(6)) + + // Expect them sorted in insertion order expected := structs.Intentions{ &structs.Intention{ ID: ixns[0].ID, @@ -525,6 +530,9 @@ func TestStore_Intention_Snapshot_Restore(t *testing.T) { }, }, } + for i := range expected { + expected[i].UpdatePrecedence() // to match what is returned... + } dump, err := snap.Intentions() assert.NoError(err) assert.Equal(expected, dump) @@ -538,7 +546,10 @@ func TestStore_Intention_Snapshot_Restore(t *testing.T) { } restore.Commit() - // Read the restored values back out and verify that they match. + // Read the restored values back out and verify that they match. Note that + // Intentions are returned precedence sorted unlike the snapshot so we need + // to rearrange the expected slice some. + expected[0], expected[1], expected[2] = expected[1], expected[2], expected[0] idx, actual, err := s.Intentions(nil) assert.NoError(err) assert.Equal(idx, uint64(6)) diff --git a/agent/structs/intention.go b/agent/structs/intention.go index f9df47b6ac..77b5f951ee 100644 --- a/agent/structs/intention.go +++ b/agent/structs/intention.go @@ -217,11 +217,11 @@ func (x *Intention) GetACLPrefix() (string, bool) { // String returns a human-friendly string for this intention. func (x *Intention) String() string { - return fmt.Sprintf("%s %s/%s => %s/%s (ID: %s)", + return fmt.Sprintf("%s %s/%s => %s/%s (ID: %s, Precedence: %d)", strings.ToUpper(string(x.Action)), x.SourceNS, x.SourceName, x.DestinationNS, x.DestinationName, - x.ID) + x.ID, x.Precedence) } // IntentionAction is the action that the intention represents. This