github.com/cilium/cilium@v1.16.2/pkg/testutils/identity/allocator_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package testidentity
     5  
     6  import (
     7  	"context"
     8  	"net/netip"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/cilium/cilium/pkg/identity"
    14  	"github.com/cilium/cilium/pkg/labels"
    15  )
    16  
    17  func TestOldNID(t *testing.T) {
    18  	a := NewMockIdentityAllocator(nil)
    19  	ctx := context.Background()
    20  
    21  	// Request identity, it should work
    22  	l := labels.GetCIDRLabels(netip.MustParsePrefix("1.1.1.1/32"))
    23  	id, _, _ := a.AllocateIdentity(ctx, l, false, 16777216)
    24  	assert.NotNil(t, id)
    25  	assert.EqualValues(t, 16777216, id.ID)
    26  
    27  	// Re-request identity, it should not
    28  	l = labels.GetCIDRLabels(netip.MustParsePrefix("1.1.1.2/32"))
    29  	id, _, _ = a.AllocateIdentity(ctx, l, false, 16777216)
    30  	assert.NotNil(t, id)
    31  	assert.EqualValues(t, 16777217, id.ID)
    32  
    33  	// Withhold the next identity, it should be skipped
    34  	a.WithholdLocalIdentities([]identity.NumericIdentity{16777218})
    35  
    36  	l = labels.GetCIDRLabels(netip.MustParsePrefix("1.1.1.3/32"))
    37  	id, _, _ = a.AllocateIdentity(ctx, l, false, 0)
    38  	assert.NotNil(t, id)
    39  	assert.EqualValues(t, 16777219, id.ID)
    40  
    41  	// Request a withheld identity, it should succeed
    42  	l = labels.GetCIDRLabels(netip.MustParsePrefix("1.1.1.4/32"))
    43  	id, _, _ = a.AllocateIdentity(ctx, l, false, 16777218)
    44  	assert.NotNil(t, id)
    45  	assert.EqualValues(t, 16777218, id.ID)
    46  
    47  	// Request a withheld and allocated identity, it should be ignored
    48  	l = labels.GetCIDRLabels(netip.MustParsePrefix("1.1.1.5/32"))
    49  	id, _, _ = a.AllocateIdentity(ctx, l, false, 16777218)
    50  	assert.NotNil(t, id)
    51  	assert.EqualValues(t, 16777220, id.ID)
    52  }