github.com/elfadel/cilium@v1.6.12/pkg/datapath/maps/map_test.go (about)

     1  // Copyright 2019 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 maps
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  type MapTestSuite struct{}
    30  
    31  var _ = Suite(&MapTestSuite{})
    32  
    33  func Test(t *testing.T) {
    34  	TestingT(t)
    35  }
    36  
    37  type testEPManager struct {
    38  	endpoints       map[uint16]struct{}
    39  	removedPaths    []string
    40  	removedMappings []int
    41  }
    42  
    43  func (tm *testEPManager) endpointExists(id uint16) bool {
    44  	_, exists := tm.endpoints[id]
    45  	return exists
    46  }
    47  
    48  func (tm *testEPManager) removeDatapathMapping(id uint16) error {
    49  	tm.removedMappings = append(tm.removedMappings, int(id))
    50  	return nil
    51  }
    52  
    53  func (tm *testEPManager) removeMapPath(path string) {
    54  	tm.removedPaths = append(tm.removedPaths, path)
    55  }
    56  
    57  func (tm *testEPManager) addEndpoint(id uint16) {
    58  	tm.endpoints[id] = struct{}{}
    59  }
    60  
    61  func newTestEPManager() *testEPManager {
    62  	return &testEPManager{
    63  		endpoints:       make(map[uint16]struct{}),
    64  		removedPaths:    make([]string, 0),
    65  		removedMappings: make([]int, 0),
    66  	}
    67  }
    68  
    69  func (s *MapTestSuite) TestCollectStaleMapGarbage(c *C) {
    70  
    71  	testCases := []struct {
    72  		name            string
    73  		endpoints       []uint16
    74  		paths           []string
    75  		removedPaths    []string
    76  		removedMappings []int
    77  	}{
    78  		{
    79  			name: "No deletes",
    80  			endpoints: []uint16{
    81  				1,
    82  				42,
    83  			},
    84  			paths: []string{
    85  				"cilium_policy_00001",
    86  				"cilium_policy_00001",
    87  				"cilium_policy_00042",
    88  				"cilium_ct6_00001",
    89  				"cilium_ct4_00001",
    90  				"cilium_ct_any6_00001",
    91  				"cilium_ct_any4_00001",
    92  				"cilium_ep_config_00001",
    93  			},
    94  			removedPaths:    []string{},
    95  			removedMappings: []int{},
    96  		},
    97  		{
    98  			name: "Delete some endpoints",
    99  			endpoints: []uint16{
   100  				42,
   101  			},
   102  			paths: []string{
   103  				"cilium_policy_00001",
   104  				"cilium_policy_00042",
   105  				"cilium_ct6_00001",
   106  				"cilium_ct4_00001",
   107  				"cilium_ct_any6_00001",
   108  				"cilium_ct_any4_00001",
   109  				"cilium_ep_config_00001",
   110  			},
   111  			removedPaths: []string{
   112  				"cilium_policy_00001",
   113  				"cilium_ct6_00001",
   114  				"cilium_ct4_00001",
   115  				"cilium_ct_any6_00001",
   116  				"cilium_ct_any4_00001",
   117  				"cilium_ep_config_00001",
   118  			},
   119  			removedMappings: []int{
   120  				1,
   121  			},
   122  		},
   123  		{
   124  			name: "Delete some endpoints",
   125  			endpoints: []uint16{
   126  				1,
   127  			},
   128  			paths: []string{
   129  				"cilium_policy_00001",
   130  				"cilium_policy_00042",
   131  				"cilium_ct6_00001",
   132  				"cilium_ct4_00001",
   133  				"cilium_ct_any6_00001",
   134  				"cilium_ct_any4_00001",
   135  				"cilium_ep_config_00001",
   136  			},
   137  			removedPaths: []string{
   138  				"cilium_policy_00042",
   139  			},
   140  			removedMappings: []int{
   141  				42,
   142  			},
   143  		},
   144  		{
   145  			name:      "Delete every map",
   146  			endpoints: []uint16{},
   147  			paths: []string{
   148  				"cilium_policy_00001",
   149  				"cilium_policy_00042",
   150  				"cilium_ct6_00001",
   151  				"cilium_ct4_00001",
   152  				"cilium_ct_any6_00001",
   153  				"cilium_ct_any4_00001",
   154  				"cilium_ep_config_00001",
   155  			},
   156  			removedPaths: []string{
   157  				"cilium_policy_00001",
   158  				"cilium_policy_00042",
   159  				"cilium_ct6_00001",
   160  				"cilium_ct4_00001",
   161  				"cilium_ct_any6_00001",
   162  				"cilium_ct_any4_00001",
   163  				"cilium_ep_config_00001",
   164  			},
   165  			removedMappings: []int{
   166  				1,
   167  				42,
   168  			},
   169  		},
   170  		{
   171  			name: "Delete maps with old path format",
   172  			endpoints: []uint16{
   173  				1,
   174  				42,
   175  			},
   176  			paths: []string{
   177  				"cilium_policy_1",
   178  				"cilium_policy_42",
   179  				"cilium_ct6_1",
   180  				"cilium_ct4_1",
   181  				"cilium_ct_any6_1",
   182  				"cilium_ct_any4_1",
   183  				"cilium_ep_config_1",
   184  				"cilium_policy_00001",
   185  				"cilium_policy_00042",
   186  				"cilium_ct6_00001",
   187  				"cilium_ct4_00001",
   188  				"cilium_ct_any6_00001",
   189  				"cilium_ct_any4_00001",
   190  				"cilium_ep_config_00001",
   191  			},
   192  			removedPaths: []string{
   193  				"cilium_policy_1",
   194  				"cilium_policy_42",
   195  				"cilium_ct6_1",
   196  				"cilium_ct4_1",
   197  				"cilium_ct_any6_1",
   198  				"cilium_ct_any4_1",
   199  				"cilium_ep_config_1",
   200  			},
   201  			removedMappings: []int{},
   202  		},
   203  	}
   204  
   205  	for _, tt := range testCases {
   206  		c.Log(tt.name)
   207  		testEPManager := newTestEPManager()
   208  		sweeper := newMapSweeper(testEPManager)
   209  
   210  		for _, ep := range tt.endpoints {
   211  			testEPManager.addEndpoint(ep)
   212  		}
   213  		for _, path := range tt.paths {
   214  			err := sweeper.walk(path, nil, nil)
   215  			c.Assert(err, IsNil)
   216  		}
   217  		sort.Strings(tt.removedPaths)
   218  		sort.Strings(testEPManager.removedPaths)
   219  		sort.Ints(tt.removedMappings)
   220  		sort.Ints(testEPManager.removedMappings)
   221  		c.Assert(testEPManager.removedPaths, checker.DeepEquals, tt.removedPaths)
   222  	}
   223  }