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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package identity
     5  
     6  import (
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
    14  )
    15  
    16  func TestLocalIdentity(t *testing.T) {
    17  	localID := NumericIdentity(IdentityScopeLocal | 1)
    18  	require.True(t, localID.HasLocalScope())
    19  
    20  	maxClusterID := NumericIdentity(cmtypes.ClusterIDMax | 1)
    21  	require.Equal(t, false, maxClusterID.HasLocalScope())
    22  
    23  	require.Equal(t, false, ReservedIdentityWorld.HasLocalScope())
    24  }
    25  
    26  func TestClusterID(t *testing.T) {
    27  	tbl := []struct {
    28  		identity  uint32
    29  		clusterID uint32
    30  	}{
    31  		{
    32  			identity:  0x000000,
    33  			clusterID: 0,
    34  		},
    35  		{
    36  			identity:  0x010000,
    37  			clusterID: 1,
    38  		},
    39  		{
    40  			identity:  0x2A0000,
    41  			clusterID: 42,
    42  		},
    43  		{
    44  			identity:  0xFF0000,
    45  			clusterID: 255,
    46  		},
    47  		{ // make sure we support min/max configuration values
    48  			identity:  cmtypes.ClusterIDMin << 16,
    49  			clusterID: cmtypes.ClusterIDMin,
    50  		},
    51  		{
    52  			identity:  cmtypes.ClusterIDMax << 16,
    53  			clusterID: cmtypes.ClusterIDMax,
    54  		},
    55  	}
    56  
    57  	for _, item := range tbl {
    58  		require.Equal(t, item.clusterID, NumericIdentity(item.identity).ClusterID())
    59  	}
    60  }
    61  
    62  func TestGetAllReservedIdentities(t *testing.T) {
    63  	allReservedIdentities := GetAllReservedIdentities()
    64  	require.NotNil(t, allReservedIdentities)
    65  	require.Len(t, allReservedIdentities, len(reservedIdentities))
    66  	for i, id := range allReservedIdentities {
    67  		// NOTE: identity 0 is unknown, so the reserved identities start at 1
    68  		// hence the plus one here.
    69  		require.Equal(t, uint32(i+1), id.Uint32())
    70  	}
    71  }
    72  
    73  func TestAsUint32Slice(t *testing.T) {
    74  	nids := NumericIdentitySlice{2, 42, 42, 1, 1024, 1}
    75  	uint32Slice := nids.AsUint32Slice()
    76  	require.NotNil(t, uint32Slice)
    77  	require.Len(t, uint32Slice, len(nids))
    78  	for i, nid := range nids {
    79  		require.Equal(t, nid.Uint32(), uint32Slice[i])
    80  	}
    81  }
    82  
    83  func TestGetClusterIDShift(t *testing.T) {
    84  	resetClusterIDInit := func() { clusterIDInit = sync.Once{} }
    85  
    86  	tests := []struct {
    87  		name                   string
    88  		maxConnectedClusters   uint32
    89  		expectedClusterIDShift uint32
    90  	}{
    91  		{
    92  			name:                   "clustermesh255",
    93  			maxConnectedClusters:   255,
    94  			expectedClusterIDShift: 16,
    95  		},
    96  		{
    97  			name:                   "clustermesh511",
    98  			maxConnectedClusters:   511,
    99  			expectedClusterIDShift: 15,
   100  		},
   101  	}
   102  
   103  	// cleanup state from any previous tests
   104  	resetClusterIDInit()
   105  
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			t.Cleanup(resetClusterIDInit)
   109  			cinfo := cmtypes.ClusterInfo{MaxConnectedClusters: tt.maxConnectedClusters}
   110  			cinfo.InitClusterIDMax()
   111  			assert.Equal(t, tt.expectedClusterIDShift, GetClusterIDShift())
   112  
   113  			// ensure we cannot change the clusterIDShift after it has been initialized
   114  			for _, tc := range tests {
   115  				if tc.name == tt.name {
   116  					// skip the current test case itself
   117  					continue
   118  				}
   119  				newCinfo := cmtypes.ClusterInfo{MaxConnectedClusters: tc.maxConnectedClusters}
   120  				newCinfo.InitClusterIDMax()
   121  				assert.NotEqual(t, tc.expectedClusterIDShift, GetClusterIDShift())
   122  			}
   123  		})
   124  	}
   125  }