github.phpd.cn/cilium/cilium@v1.6.12/pkg/allocator/localkeys_test.go (about)

     1  // Copyright 2016-2017 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 allocator
    18  
    19  import (
    20  	"github.com/cilium/cilium/pkg/idpool"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  func (s *AllocatorSuite) TestLocalKeys(c *C) {
    26  	k := newLocalKeys()
    27  	key, val := TestAllocatorKey("foo"), idpool.ID(200)
    28  	key2, val2 := TestAllocatorKey("bar"), idpool.ID(300)
    29  
    30  	v := k.use(key.GetKey())
    31  	c.Assert(v, Equals, idpool.NoID)
    32  
    33  	v, err := k.allocate(key.GetKey(), key, val) // refcnt=1
    34  	c.Assert(err, IsNil)
    35  	c.Assert(v, Equals, val)
    36  
    37  	c.Assert(k.verify(key.GetKey()), IsNil)
    38  
    39  	v = k.use(key.GetKey()) // refcnt=2
    40  	c.Assert(v, Equals, val)
    41  	k.release(key.GetKey()) // refcnt=1
    42  
    43  	v, err = k.allocate(key.GetKey(), key, val) // refcnt=2
    44  	c.Assert(err, IsNil)
    45  	c.Assert(v, Equals, val)
    46  
    47  	v, err = k.allocate(key2.GetKey(), key2, val2) // refcnt=1
    48  	c.Assert(err, IsNil)
    49  	c.Assert(v, Equals, val2)
    50  
    51  	// only one of the two keys is verified yet
    52  	ids := k.getVerifiedIDs()
    53  	c.Assert(len(ids), Equals, 1)
    54  
    55  	// allocate with different value must fail
    56  	_, err = k.allocate(key2.GetKey(), key2, val)
    57  	c.Assert(err, Not(IsNil))
    58  
    59  	k.release(key.GetKey()) // refcnt=1
    60  	v = k.use(key.GetKey()) // refcnt=2
    61  	c.Assert(v, Equals, val)
    62  
    63  	k.release(key.GetKey()) // refcnt=1
    64  	k.release(key.GetKey()) // refcnt=0
    65  	v = k.use(key.GetKey())
    66  	c.Assert(v, Equals, idpool.NoID)
    67  
    68  	k.release(key2.GetKey()) // refcnt=0
    69  	v = k.use(key2.GetKey())
    70  	c.Assert(v, Equals, idpool.NoID)
    71  }