github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/identity/identitymanager/manager_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package identitymanager
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cilium/cilium/pkg/checker"
    23  	"github.com/cilium/cilium/pkg/identity"
    24  	"github.com/cilium/cilium/pkg/labels"
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  func Test(t *testing.T) {
    30  	TestingT(t)
    31  }
    32  
    33  type IdentityManagerTestSuite struct{}
    34  
    35  var (
    36  	_ = Suite(&IdentityManagerTestSuite{})
    37  
    38  	idFooSelectLabels = labels.NewLabelsFromModel([]string{"id=foo"})
    39  	idBarSelectLabels = labels.NewLabelsFromModel([]string{"id=bar"})
    40  	fooIdentity       = identity.NewIdentity(identity.NumericIdentity(12345), idFooSelectLabels)
    41  	fooIdentity2      = identity.NewIdentity(identity.NumericIdentity(12345), idFooSelectLabels)
    42  	barIdentity       = identity.NewIdentity(identity.NumericIdentity(54321), idBarSelectLabels)
    43  )
    44  
    45  func (s *IdentityManagerTestSuite) TestIdentityManagerLifecycle(c *C) {
    46  	idm := NewIdentityManager()
    47  	c.Assert(idm.identities, Not(IsNil))
    48  
    49  	_, exists := idm.identities[fooIdentity.ID]
    50  	c.Assert(exists, Equals, false)
    51  
    52  	idm.Add(fooIdentity)
    53  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(1))
    54  
    55  	idm.Add(fooIdentity)
    56  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(2))
    57  
    58  	idm.Add(barIdentity)
    59  	c.Assert(idm.identities[barIdentity.ID].refCount, Equals, uint(1))
    60  
    61  	idm.Remove(fooIdentity)
    62  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(1))
    63  
    64  	idm.Remove(fooIdentity)
    65  	_, exists = idm.identities[fooIdentity.ID]
    66  	c.Assert(exists, Equals, false)
    67  
    68  	_, exists = idm.identities[barIdentity.ID]
    69  	c.Assert(exists, Equals, true)
    70  
    71  	idm.Remove(barIdentity)
    72  	_, exists = idm.identities[barIdentity.ID]
    73  	c.Assert(exists, Equals, false)
    74  
    75  	idm.Add(fooIdentity)
    76  	_, exists = idm.identities[fooIdentity.ID]
    77  	c.Assert(exists, Equals, true)
    78  	idm.RemoveOldAddNew(fooIdentity, barIdentity)
    79  	_, exists = idm.identities[fooIdentity.ID]
    80  	c.Assert(exists, Equals, false)
    81  	_, exists = idm.identities[barIdentity.ID]
    82  	c.Assert(exists, Equals, true)
    83  	c.Assert(idm.identities[barIdentity.ID].refCount, Equals, uint(1))
    84  
    85  	idm.RemoveOldAddNew(nil, barIdentity)
    86  	c.Assert(idm.identities[barIdentity.ID].refCount, Equals, uint(2))
    87  }
    88  
    89  type identityManagerObserver struct {
    90  	added   []identity.NumericIdentity
    91  	removed []identity.NumericIdentity
    92  }
    93  
    94  func newIdentityManagerObserver(trackAdd, trackRemove []identity.NumericIdentity) *identityManagerObserver {
    95  	return &identityManagerObserver{
    96  		added:   trackAdd,
    97  		removed: trackRemove,
    98  	}
    99  }
   100  
   101  func (i *identityManagerObserver) LocalEndpointIdentityAdded(identity *identity.Identity) {
   102  	if i.added != nil {
   103  		i.added = append(i.added, identity.ID)
   104  	}
   105  }
   106  
   107  func (i *identityManagerObserver) LocalEndpointIdentityRemoved(identity *identity.Identity) {
   108  	if i.removed != nil {
   109  		i.removed = append(i.removed, identity.ID)
   110  	}
   111  }
   112  
   113  func (s *IdentityManagerTestSuite) TestLocalEndpointIdentityAdded(c *C) {
   114  	idm := NewIdentityManager()
   115  	observer := newIdentityManagerObserver([]identity.NumericIdentity{}, []identity.NumericIdentity{})
   116  	idm.subscribe(observer)
   117  
   118  	// No-op: nil Identity.
   119  	idm.Add(nil)
   120  	expectedObserver := newIdentityManagerObserver([]identity.NumericIdentity{}, []identity.NumericIdentity{})
   121  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   122  
   123  	// First add triggers an "IdentityAdded" event.
   124  	idm.Add(fooIdentity)
   125  	expectedObserver = newIdentityManagerObserver([]identity.NumericIdentity{fooIdentity.ID}, []identity.NumericIdentity{})
   126  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   127  
   128  	// Second does not.
   129  	idm.Add(fooIdentity)
   130  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   131  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(2))
   132  
   133  	// Duplicate identity with the same ID does not trigger events.
   134  	idm.Add(fooIdentity2)
   135  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   136  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(3))
   137  
   138  	// Unrelated add should also trigger.
   139  	idm.Add(barIdentity)
   140  	expectedObserver = newIdentityManagerObserver([]identity.NumericIdentity{fooIdentity.ID, barIdentity.ID}, []identity.NumericIdentity{})
   141  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   142  	c.Assert(idm.identities[barIdentity.ID].refCount, Equals, uint(1))
   143  
   144  	// Removing both then re-adding should trigger the event again.
   145  	idm.Remove(fooIdentity)
   146  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(2))
   147  	idm.Remove(fooIdentity)
   148  	c.Assert(idm.identities[fooIdentity.ID].refCount, Equals, uint(1))
   149  	idm.Remove(fooIdentity)
   150  	c.Assert(observer.added, HasLen, 2)
   151  	c.Assert(observer.removed, HasLen, 1)
   152  	idm.Add(fooIdentity)
   153  	expectedObserver = newIdentityManagerObserver([]identity.NumericIdentity{fooIdentity.ID, barIdentity.ID, fooIdentity.ID}, []identity.NumericIdentity{fooIdentity.ID})
   154  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   155  
   156  	// RemoveOldAddNew with the same ID is a no-op
   157  	idm.RemoveOldAddNew(fooIdentity, fooIdentity2)
   158  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   159  
   160  	// RemoveOldAddNew from an existing ID to another triggers removal of the old
   161  	idm.RemoveOldAddNew(barIdentity, fooIdentity)
   162  	expectedObserver = newIdentityManagerObserver([]identity.NumericIdentity{fooIdentity.ID, barIdentity.ID, fooIdentity.ID}, []identity.NumericIdentity{fooIdentity.ID, barIdentity.ID})
   163  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   164  }
   165  
   166  func (s *IdentityManagerTestSuite) TestLocalEndpointIdentityRemoved(c *C) {
   167  	idm := NewIdentityManager()
   168  	c.Assert(idm.identities, NotNil)
   169  	observer := newIdentityManagerObserver([]identity.NumericIdentity{}, []identity.NumericIdentity{})
   170  	idm.subscribe(observer)
   171  
   172  	// No-ops:
   173  	// - nil Identity.
   174  	// - Identity that isn't managed
   175  	idm.Remove(nil)
   176  	// This will log a warnign!
   177  	idm.Remove(fooIdentity)
   178  
   179  	// Basic remove
   180  	idm.Add(fooIdentity)
   181  	idm.Remove(fooIdentity)
   182  	expectedObserver := newIdentityManagerObserver([]identity.NumericIdentity{fooIdentity.ID}, []identity.NumericIdentity{fooIdentity.ID})
   183  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   184  
   185  	idm = NewIdentityManager()
   186  	c.Assert(idm.identities, NotNil)
   187  	observer = newIdentityManagerObserver(nil, []identity.NumericIdentity{})
   188  	idm.subscribe(observer)
   189  
   190  	// Refcount remove
   191  	idm.Add(fooIdentity)    // foo = 1
   192  	idm.Add(fooIdentity)    // foo = 2
   193  	idm.Add(barIdentity)    // bar = 1
   194  	idm.Remove(fooIdentity) // foo = 1
   195  	expectedObserver = newIdentityManagerObserver(nil, []identity.NumericIdentity{})
   196  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   197  	idm.Remove(fooIdentity) // foo = 0
   198  	expectedObserver = newIdentityManagerObserver(nil, []identity.NumericIdentity{fooIdentity.ID})
   199  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   200  	idm.Remove(barIdentity) // bar = 0
   201  	expectedObserver = newIdentityManagerObserver(nil, []identity.NumericIdentity{fooIdentity.ID, barIdentity.ID})
   202  	c.Assert(observer, checker.DeepEquals, expectedObserver)
   203  }