github.com/cilium/cilium@v1.16.2/pkg/datapath/maps/map_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package maps 5 6 import ( 7 "sort" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types" 13 "github.com/cilium/cilium/pkg/datapath/types" 14 ) 15 16 type testEPManager struct { 17 endpoints map[uint16]struct{} 18 removedPaths []string 19 removedMappings []int 20 } 21 22 func (tm *testEPManager) EndpointExists(id uint16) bool { 23 _, exists := tm.endpoints[id] 24 return exists 25 } 26 27 func (tm *testEPManager) HasGlobalCT() bool { 28 return false 29 } 30 31 func (tm *testEPManager) RemoveDatapathMapping(id uint16) error { 32 tm.removedMappings = append(tm.removedMappings, int(id)) 33 return nil 34 } 35 36 func (tm *testEPManager) RemoveMapPath(path string) { 37 tm.removedPaths = append(tm.removedPaths, path) 38 } 39 40 func (tm *testEPManager) addEndpoint(id uint16) { 41 tm.endpoints[id] = struct{}{} 42 } 43 44 func newTestEPManager() *testEPManager { 45 return &testEPManager{ 46 endpoints: make(map[uint16]struct{}), 47 removedPaths: make([]string, 0), 48 removedMappings: make([]int, 0), 49 } 50 } 51 52 func newTestBWManager() types.BandwidthManager { 53 return &fakeTypes.BandwidthManager{} 54 } 55 56 func TestCollectStaleMapGarbage(t *testing.T) { 57 58 testCases := []struct { 59 name string 60 endpoints []uint16 61 paths []string 62 removedPaths []string 63 removedMappings []int 64 }{ 65 { 66 name: "No deletes", 67 endpoints: []uint16{ 68 1, 69 42, 70 }, 71 paths: []string{ 72 "cilium_policy_00001", 73 "cilium_policy_00001", 74 "cilium_policy_00042", 75 "cilium_ct6_00001", 76 "cilium_ct4_00001", 77 "cilium_ct_any6_00001", 78 "cilium_ct_any4_00001", 79 }, 80 removedPaths: []string{}, 81 removedMappings: []int{}, 82 }, 83 { 84 name: "Delete some endpoints", 85 endpoints: []uint16{ 86 42, 87 }, 88 paths: []string{ 89 "cilium_policy_00001", 90 "cilium_policy_00042", 91 "cilium_ct6_00001", 92 "cilium_ct4_00001", 93 "cilium_ct_any6_00001", 94 "cilium_ct_any4_00001", 95 }, 96 removedPaths: []string{ 97 "cilium_policy_00001", 98 "cilium_ct6_00001", 99 "cilium_ct4_00001", 100 "cilium_ct_any6_00001", 101 "cilium_ct_any4_00001", 102 }, 103 removedMappings: []int{ 104 1, 105 }, 106 }, 107 { 108 name: "Delete some endpoints", 109 endpoints: []uint16{ 110 1, 111 }, 112 paths: []string{ 113 "cilium_policy_00001", 114 "cilium_policy_00042", 115 "cilium_ct6_00001", 116 "cilium_ct4_00001", 117 "cilium_ct_any6_00001", 118 "cilium_ct_any4_00001", 119 }, 120 removedPaths: []string{ 121 "cilium_policy_00042", 122 }, 123 removedMappings: []int{ 124 42, 125 }, 126 }, 127 { 128 name: "Delete every map", 129 endpoints: []uint16{}, 130 paths: []string{ 131 "cilium_policy_00001", 132 "cilium_policy_00042", 133 "cilium_ct6_00001", 134 "cilium_ct4_00001", 135 "cilium_ct_any6_00001", 136 "cilium_ct_any4_00001", 137 }, 138 removedPaths: []string{ 139 "cilium_policy_00001", 140 "cilium_policy_00042", 141 "cilium_ct6_00001", 142 "cilium_ct4_00001", 143 "cilium_ct_any6_00001", 144 "cilium_ct_any4_00001", 145 }, 146 removedMappings: []int{ 147 1, 148 42, 149 }, 150 }, 151 { 152 name: "Delete maps with old path format", 153 endpoints: []uint16{ 154 1, 155 42, 156 }, 157 paths: []string{ 158 "cilium_policy_1", 159 "cilium_policy_42", 160 "cilium_ct6_1", 161 "cilium_ct4_1", 162 "cilium_ct_any6_1", 163 "cilium_ct_any4_1", 164 "cilium_policy_00001", 165 "cilium_policy_00042", 166 "cilium_ct6_00001", 167 "cilium_ct4_00001", 168 "cilium_ct_any6_00001", 169 "cilium_ct_any4_00001", 170 }, 171 removedPaths: []string{ 172 "cilium_policy_1", 173 "cilium_policy_42", 174 "cilium_ct6_1", 175 "cilium_ct4_1", 176 "cilium_ct_any6_1", 177 "cilium_ct_any4_1", 178 }, 179 removedMappings: []int{}, 180 }, 181 } 182 183 for _, tt := range testCases { 184 t.Run(tt.name, func(t *testing.T) { 185 testEPManager := newTestEPManager() 186 bwManager := newTestBWManager() 187 sweeper := NewMapSweeper(testEPManager, bwManager) 188 189 for _, ep := range tt.endpoints { 190 testEPManager.addEndpoint(ep) 191 } 192 for _, path := range tt.paths { 193 err := sweeper.walk(path, nil, nil) 194 require.Nil(t, err) 195 } 196 sort.Strings(tt.removedPaths) 197 sort.Strings(testEPManager.removedPaths) 198 sort.Ints(tt.removedMappings) 199 sort.Ints(testEPManager.removedMappings) 200 require.EqualValues(t, tt.removedPaths, testEPManager.removedPaths) 201 }) 202 } 203 }