github.com/cilium/cilium@v1.16.2/pkg/maps/ctmap/types_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package ctmap 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/cilium/cilium/pkg/option" 12 ) 13 14 func TestMapKey(t *testing.T) { 15 for mapType := mapType(0); mapType < mapTypeMax; mapType++ { 16 assert.NotNil(t, mapType.key()) 17 } 18 19 assert.Panics(t, func() { mapType(-1).key() }) 20 assert.Panics(t, func() { mapTypeMax.key() }) 21 } 22 23 func TestMapBPFDefine(t *testing.T) { 24 for mapType := mapType(0); mapType < mapTypeMax; mapType++ { 25 if mapType.isIPv6() { 26 assert.Contains(t, mapType.bpfDefine(), "6") 27 } 28 if mapType.isIPv4() { 29 assert.Contains(t, mapType.bpfDefine(), "4") 30 } 31 32 if mapType.isTCP() { 33 assert.Contains(t, mapType.bpfDefine(), "TCP") 34 } else { 35 assert.Contains(t, mapType.bpfDefine(), "ANY") 36 } 37 } 38 39 assert.Panics(t, func() { mapType(-1).bpfDefine() }) 40 assert.Panics(t, func() { mapTypeMax.bpfDefine() }) 41 } 42 43 func TestMaxEntries(t *testing.T) { 44 tests := []struct { 45 name string 46 tcp, any int 47 etcp, eany int 48 }{ 49 { 50 name: "defaults", 51 etcp: option.CTMapEntriesGlobalTCPDefault, 52 eany: option.CTMapEntriesGlobalAnyDefault, 53 }, 54 { 55 name: "configured", 56 tcp: 0x12345, 57 etcp: 0x12345, 58 any: 0x67890, 59 eany: 0x67890, 60 }, 61 } 62 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 option.Config.CTMapEntriesGlobalTCP = tt.tcp 66 option.Config.CTMapEntriesGlobalAny = tt.any 67 68 for mapType := mapType(0); mapType < mapTypeMax; mapType++ { 69 if mapType.isLocal() { 70 assert.Equal(t, mapNumEntriesLocal, mapType.maxEntries()) 71 } 72 73 if mapType.isGlobal() { 74 if mapType.isTCP() { 75 assert.Equal(t, tt.etcp, mapType.maxEntries()) 76 } else { 77 assert.Equal(t, tt.eany, mapType.maxEntries()) 78 } 79 } 80 } 81 82 assert.Panics(t, func() { mapType(-1).maxEntries() }) 83 assert.Panics(t, func() { mapTypeMax.maxEntries() }) 84 }) 85 } 86 }