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

     1  // Copyright 2018-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 eppolicymap
    18  
    19  import (
    20  	"net"
    21  	"os"
    22  	"testing"
    23  	"unsafe"
    24  
    25  	"github.com/cilium/cilium/pkg/bpf"
    26  	"github.com/cilium/cilium/pkg/maps/lxcmap"
    27  	"github.com/cilium/cilium/pkg/maps/policymap"
    28  	"github.com/cilium/cilium/pkg/option"
    29  
    30  	. "gopkg.in/check.v1"
    31  )
    32  
    33  // Hook up gocheck into the "go test" runner.
    34  func Test(t *testing.T) {
    35  	TestingT(t)
    36  }
    37  
    38  type EPPolicyMapTestSuite struct{}
    39  
    40  var _ = Suite(&EPPolicyMapTestSuite{})
    41  
    42  func (e *EPPolicyMapTestSuite) SetUpTest(c *C) {
    43  	MapName = "unit_test_ep_to_policy"
    44  	innerMapName = "unit_test_ep_policy_inner_map"
    45  	err := bpf.ConfigureResourceLimits()
    46  	c.Assert(err, IsNil)
    47  }
    48  
    49  func (e *EPPolicyMapTestSuite) TearDownTest(c *C) {
    50  	os.Remove(MapName)
    51  	os.Remove(innerMapName)
    52  }
    53  
    54  func (e *EPPolicyMapTestSuite) TestCreateEPPolicy(c *C) {
    55  	bpf.CheckOrMountFS("")
    56  	CreateEPPolicyMap()
    57  }
    58  
    59  func (e *EPPolicyMapTestSuite) TestWriteEndpoint(c *C) {
    60  	option.Config.SockopsEnable = true
    61  	bpf.CheckOrMountFS("")
    62  	keys := make([]*lxcmap.EndpointKey, 1)
    63  	many := make([]*lxcmap.EndpointKey, 256)
    64  	fd, err := bpf.CreateMap(bpf.BPF_MAP_TYPE_HASH,
    65  		uint32(unsafe.Sizeof(policymap.PolicyKey{})),
    66  		uint32(unsafe.Sizeof(policymap.PolicyEntry{})), 1024, 0, 0,
    67  		"ep-policy-inner-map")
    68  	c.Assert(err, IsNil)
    69  
    70  	keys[0] = lxcmap.NewEndpointKey(net.ParseIP("1.2.3.4"))
    71  	for i := 0; i < 256; i++ {
    72  		ip := net.ParseIP("1.2.3." + string(i))
    73  		many[i] = lxcmap.NewEndpointKey(ip)
    74  	}
    75  
    76  	CreateEPPolicyMap()
    77  	err = writeEndpoint(keys, fd)
    78  	c.Assert(err, Not(IsNil))
    79  	err = writeEndpoint(many, fd)
    80  	c.Assert(err, Not(IsNil))
    81  }
    82  
    83  // We allow calls into WriteEndpoint with invalid fd allowing users of the
    84  // API to avoid doing if enabled { WriteEndpoint() } and simply passing
    85  // in invalid fd in if its disabled.
    86  func (e *EPPolicyMapTestSuite) TestWriteEndpointFails(c *C) {
    87  	option.Config.SockopsEnable = true
    88  	bpf.CheckOrMountFS("")
    89  	keys := make([]*lxcmap.EndpointKey, 1)
    90  	_, err := bpf.CreateMap(bpf.BPF_MAP_TYPE_HASH,
    91  		uint32(unsafe.Sizeof(policymap.PolicyKey{})),
    92  		uint32(unsafe.Sizeof(policymap.PolicyEntry{})), 1024, 0, 0,
    93  		"ep-policy-inner-map")
    94  	c.Assert(err, IsNil)
    95  
    96  	keys[0] = lxcmap.NewEndpointKey(net.ParseIP("1.2.3.4"))
    97  	CreateEPPolicyMap()
    98  	err = writeEndpoint(keys, -1)
    99  	c.Assert(err, Not(IsNil))
   100  }
   101  
   102  func (e *EPPolicyMapTestSuite) TestWriteEndpointDisabled(c *C) {
   103  	option.Config.SockopsEnable = false
   104  	bpf.CheckOrMountFS("")
   105  	keys := make([]*lxcmap.EndpointKey, 1)
   106  	fd, err := bpf.CreateMap(bpf.BPF_MAP_TYPE_HASH,
   107  		uint32(unsafe.Sizeof(policymap.PolicyKey{})),
   108  		uint32(unsafe.Sizeof(policymap.PolicyEntry{})), 1024, 0, 0,
   109  		"ep-policy-inner-map")
   110  	c.Assert(err, IsNil)
   111  
   112  	keys[0] = lxcmap.NewEndpointKey(net.ParseIP("1.2.3.4"))
   113  	CreateEPPolicyMap()
   114  	err = writeEndpoint(keys, fd)
   115  	c.Assert(err, IsNil)
   116  }