github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/node_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 linux
    18  
    19  import (
    20  	"net"
    21  
    22  	"github.com/cilium/cilium/pkg/checker"
    23  	"github.com/cilium/cilium/pkg/cidr"
    24  	"github.com/cilium/cilium/pkg/datapath/fake"
    25  
    26  	"gopkg.in/check.v1"
    27  )
    28  
    29  func (s *linuxTestSuite) TestTunnelCIDRUpdateRequired(c *check.C) {
    30  	c1 := cidr.MustParseCIDR("10.1.0.0/16")
    31  	c2 := cidr.MustParseCIDR("10.2.0.0/16")
    32  	ip1 := net.ParseIP("1.1.1.1")
    33  	ip2 := net.ParseIP("2.2.2.2")
    34  
    35  	c.Assert(cidrNodeMappingUpdateRequired(nil, nil, ip1, ip1, 0, 0), check.Equals, false) // disabled -> disabled
    36  	c.Assert(cidrNodeMappingUpdateRequired(nil, c1, ip1, ip1, 0, 0), check.Equals, true)   // disabled -> c1
    37  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 0, 0), check.Equals, false)   // c1 -> c1
    38  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip2, 0, 0), check.Equals, true)    // c1 -> c1 (changed host IP)
    39  	c.Assert(cidrNodeMappingUpdateRequired(c1, c2, ip2, ip2, 0, 0), check.Equals, true)    // c1 -> c2
    40  	c.Assert(cidrNodeMappingUpdateRequired(c2, nil, ip2, ip2, 0, 0), check.Equals, false)  // c2 -> disabled
    41  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 0, 1), check.Equals, true)    // key upgrade 0 -> 1
    42  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 1, 0), check.Equals, true)    // key downgrade 1 -> 0
    43  
    44  	c1 = cidr.MustParseCIDR("f00d::a0a:0:0:0/96")
    45  	c2 = cidr.MustParseCIDR("f00d::b0b:0:0:0/96")
    46  	ip1 = net.ParseIP("cafe::1")
    47  	ip2 = net.ParseIP("cafe::2")
    48  
    49  	c.Assert(cidrNodeMappingUpdateRequired(nil, nil, ip1, ip1, 0, 0), check.Equals, false) // disabled -> disabled
    50  	c.Assert(cidrNodeMappingUpdateRequired(nil, c1, ip1, ip1, 0, 0), check.Equals, true)   // disabled -> c1
    51  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 0, 0), check.Equals, false)   // c1 -> c1
    52  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip2, 0, 0), check.Equals, true)    // c1 -> c1 (changed host IP)
    53  	c.Assert(cidrNodeMappingUpdateRequired(c1, c2, ip2, ip2, 0, 0), check.Equals, true)    // c1 -> c2
    54  	c.Assert(cidrNodeMappingUpdateRequired(c2, nil, ip2, ip2, 0, 0), check.Equals, false)  // c2 -> disabled
    55  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 0, 1), check.Equals, true)    // key upgrade 0 -> 1
    56  	c.Assert(cidrNodeMappingUpdateRequired(c1, c1, ip1, ip1, 1, 0), check.Equals, true)    // key downgrade 1 -> 0
    57  }
    58  
    59  func (s *linuxTestSuite) TestCreateNodeRoute(c *check.C) {
    60  	dpConfig := DatapathConfiguration{
    61  		HostDevice: "host_device",
    62  	}
    63  
    64  	fakeNodeAddressing := fake.NewNodeAddressing()
    65  
    66  	nodeHandler := NewNodeHandler(dpConfig, fakeNodeAddressing)
    67  
    68  	c1 := cidr.MustParseCIDR("10.10.0.0/16")
    69  	generatedRoute, err := nodeHandler.(*linuxNodeHandler).createNodeRoute(c1)
    70  	c.Assert(err, check.IsNil)
    71  	c.Assert(generatedRoute.Prefix, checker.DeepEquals, *c1.IPNet)
    72  	c.Assert(generatedRoute.Device, check.Equals, dpConfig.HostDevice)
    73  	c.Assert(*generatedRoute.Nexthop, checker.DeepEquals, fakeNodeAddressing.IPv4().Router())
    74  	c.Assert(generatedRoute.Local, checker.DeepEquals, fakeNodeAddressing.IPv4().Router())
    75  
    76  	c1 = cidr.MustParseCIDR("beef:beef::/48")
    77  	generatedRoute, err = nodeHandler.(*linuxNodeHandler).createNodeRoute(c1)
    78  	c.Assert(err, check.IsNil)
    79  	c.Assert(generatedRoute.Prefix, checker.DeepEquals, *c1.IPNet)
    80  	c.Assert(generatedRoute.Device, check.Equals, dpConfig.HostDevice)
    81  	c.Assert(*generatedRoute.Nexthop, checker.DeepEquals, fakeNodeAddressing.IPv6().Router())
    82  	c.Assert(generatedRoute.Local, checker.DeepEquals, fakeNodeAddressing.IPv6().PrimaryExternal())
    83  }