github.phpd.cn/cilium/cilium@v1.6.12/pkg/identity/cache/local_test.go (about) 1 // Copyright 2018 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 cache 18 19 import ( 20 "fmt" 21 22 "github.com/cilium/cilium/pkg/checker" 23 "github.com/cilium/cilium/pkg/identity" 24 "github.com/cilium/cilium/pkg/labels" 25 26 . "gopkg.in/check.v1" 27 ) 28 29 func (s *IdentityCacheTestSuite) TestBumpNextNumericIdentity(c *C) { 30 minID, maxID := identity.NumericIdentity(1), identity.NumericIdentity(5) 31 cache := newLocalIdentityCache(minID, maxID, nil) 32 33 for i := minID; i <= maxID; i++ { 34 c.Assert(cache.nextNumericIdentity, Equals, i) 35 cache.bumpNextNumericIdentity() 36 } 37 38 // ID must have overflowed and must be back to minID 39 c.Assert(cache.nextNumericIdentity, Equals, minID) 40 } 41 42 func (s *IdentityCacheTestSuite) TestLocalIdentityCache(c *C) { 43 minID, maxID := identity.NumericIdentity(1), identity.NumericIdentity(5) 44 cache := newLocalIdentityCache(minID, maxID, nil) 45 46 identities := map[identity.NumericIdentity]*identity.Identity{} 47 48 // allocate identities for all available numeric identities with a 49 // unique label 50 for i := minID; i <= maxID; i++ { 51 id, isNew, err := cache.lookupOrCreate(labels.NewLabelsFromModel([]string{fmt.Sprintf("%d", i)})) 52 c.Assert(err, IsNil) 53 c.Assert(isNew, Equals, true) 54 identities[id.ID] = id 55 } 56 57 // allocate the same labels again. This must be successful and the same 58 // identities must be returned. 59 for i := minID; i <= maxID; i++ { 60 id, isNew, err := cache.lookupOrCreate(labels.NewLabelsFromModel([]string{fmt.Sprintf("%d", i)})) 61 c.Assert(isNew, Equals, false) 62 c.Assert(err, IsNil) 63 64 // The returned identity must be identical 65 c.Assert(id, checker.DeepEquals, identities[id.ID]) 66 } 67 68 // Allocation must fail as we are out of IDs 69 id, _, err := cache.lookupOrCreate(labels.NewLabelsFromModel([]string{"foo"})) 70 c.Assert(err, Not(IsNil)) 71 72 // release all identities, this must decrement the reference count but not release the identities yet 73 for _, id := range identities { 74 c.Assert(cache.release(id), Equals, false) 75 } 76 77 // lookup must still be successful 78 for i := minID; i <= maxID; i++ { 79 c.Assert(cache.lookup(labels.NewLabelsFromModel([]string{fmt.Sprintf("%d", i)})), Not(IsNil)) 80 c.Assert(cache.lookupByID(i|identity.LocalIdentityFlag), Not(IsNil)) 81 } 82 83 // release the identities a second time, this must cause the identity 84 // to be forgotten 85 for _, id := range identities { 86 c.Assert(cache.release(id), Equals, true) 87 } 88 89 // allocate all identities again 90 for i := minID; i <= maxID; i++ { 91 id, isNew, err := cache.lookupOrCreate(labels.NewLabelsFromModel([]string{fmt.Sprintf("%d", i)})) 92 c.Assert(err, IsNil) 93 c.Assert(isNew, Equals, true) 94 identities[id.ID] = id 95 } 96 97 // release a random identity in the middle 98 randomID := identity.NumericIdentity(3) | identity.LocalIdentityFlag 99 c.Assert(cache.release(identities[randomID]), Equals, true) 100 101 id, isNew, err := cache.lookupOrCreate(labels.NewLabelsFromModel([]string{"foo"})) 102 c.Assert(err, IsNil) 103 c.Assert(isNew, Equals, true) 104 // the selected numeric identity must be the one released before 105 c.Assert(id.ID, Equals, randomID) 106 }