github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/ctmap/ctmap_test.go (about)

     1  // Copyright 2016-2018 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 ctmap
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  	"time"
    23  	"unsafe"
    24  
    25  	"github.com/cilium/cilium/pkg/bpf"
    26  	"github.com/cilium/cilium/pkg/defaults"
    27  	"github.com/cilium/cilium/pkg/option"
    28  	"github.com/cilium/cilium/pkg/tuple"
    29  
    30  	. "gopkg.in/check.v1"
    31  )
    32  
    33  // Hook up gocheck into the "go test" runner.
    34  type CTMapTestSuite struct{}
    35  
    36  var _ = Suite(&CTMapTestSuite{})
    37  
    38  func Test(t *testing.T) {
    39  	TestingT(t)
    40  }
    41  
    42  func (t *CTMapTestSuite) TestInit(c *C) {
    43  	InitMapInfo(option.CTMapEntriesGlobalTCPDefault, option.CTMapEntriesGlobalAnyDefault, true, true)
    44  	for mapType := MapType(0); mapType < MapTypeMax; mapType++ {
    45  		info := mapInfo[mapType]
    46  		if mapType.isIPv6() {
    47  			c.Assert(info.keySize, Equals, int(unsafe.Sizeof(tuple.TupleKey6{})))
    48  			c.Assert(strings.Contains(info.bpfDefine, "6"), Equals, true)
    49  		}
    50  		if mapType.isIPv4() {
    51  			c.Assert(info.keySize, Equals, int(unsafe.Sizeof(tuple.TupleKey4{})))
    52  			c.Assert(strings.Contains(info.bpfDefine, "4"), Equals, true)
    53  		}
    54  		if mapType.isTCP() {
    55  			c.Assert(strings.Contains(info.bpfDefine, "TCP"), Equals, true)
    56  		} else {
    57  			c.Assert(strings.Contains(info.bpfDefine, "ANY"), Equals, true)
    58  		}
    59  		if mapType.isLocal() {
    60  			c.Assert(info.maxEntries, Equals, MapNumEntriesLocal)
    61  		}
    62  		if mapType.isGlobal() {
    63  			if mapType.isTCP() {
    64  				c.Assert(info.maxEntries, Equals, option.CTMapEntriesGlobalTCPDefault)
    65  			} else {
    66  				c.Assert(info.maxEntries, Equals, option.CTMapEntriesGlobalAnyDefault)
    67  			}
    68  		}
    69  	}
    70  }
    71  
    72  func (t *CTMapTestSuite) TestCalculateInterval(c *C) {
    73  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.1), Equals, time.Minute)  // no change
    74  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.2), Equals, time.Minute)  // no change
    75  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.25), Equals, time.Minute) // no change
    76  
    77  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.40), Equals, 36*time.Second)
    78  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, time.Minute, 0.60), Equals, 24*time.Second)
    79  
    80  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, 10*time.Second, 0.01), Equals, 15*time.Second)
    81  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, 10*time.Second, 0.04), Equals, 15*time.Second)
    82  
    83  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, 1*time.Second, 0.9), Equals, defaults.ConntrackGCMinInterval)
    84  	c.Assert(calculateInterval(bpf.MapTypeHash, 1*time.Second, 0.9), Equals, defaults.ConntrackGCMinInterval)
    85  
    86  	c.Assert(calculateInterval(bpf.MapTypeLRUHash, 24*time.Hour, 0.01), Equals, defaults.ConntrackGCMaxLRUInterval)
    87  	c.Assert(calculateInterval(bpf.MapTypeHash, 24*time.Hour, 0.01), Equals, defaults.ConntrackGCMaxInterval)
    88  }