github.com/zhyoulun/cilium@v1.6.12/pkg/node/node_address_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 node
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"os"
    23  	"path/filepath"
    24  	"reflect"
    25  
    26  	"github.com/cilium/cilium/pkg/checker"
    27  	"github.com/cilium/cilium/pkg/cidr"
    28  
    29  	. "gopkg.in/check.v1"
    30  )
    31  
    32  func (s *NodeSuite) TestMaskCheck(c *C) {
    33  	InitDefaultPrefix("")
    34  	SetIPv4ClusterCidrMaskSize(24)
    35  
    36  	allocCIDR := cidr.MustParseCIDR("1.1.1.1/16")
    37  	SetIPv4AllocRange(allocCIDR)
    38  
    39  	// must fail, cluster /24 > per node alloc prefix /16
    40  	c.Assert(ValidatePostInit(), Not(IsNil))
    41  
    42  	SetInternalIPv4(allocCIDR.IP)
    43  
    44  	// OK, cluster /16 == per node alloc prefix /16
    45  	SetIPv4ClusterCidrMaskSize(16)
    46  	c.Assert(ValidatePostInit(), IsNil)
    47  
    48  	// OK, cluster /8 < per node alloc prefix /16
    49  	SetIPv4ClusterCidrMaskSize(8)
    50  	c.Assert(ValidatePostInit(), IsNil)
    51  
    52  	c.Assert(IsHostIPv4(GetInternalIPv4()), Equals, true)
    53  	c.Assert(IsHostIPv4(GetExternalIPv4()), Equals, true)
    54  	c.Assert(IsHostIPv6(GetIPv6()), Equals, true)
    55  }
    56  
    57  func (s *NodeSuite) Test_getCiliumHostIPsFromFile(c *C) {
    58  	tmpDir := c.MkDir()
    59  	allIPsCorrect := filepath.Join(tmpDir, "node_config.h")
    60  	f, err := os.Create(allIPsCorrect)
    61  	c.Assert(err, IsNil)
    62  	defer f.Close()
    63  	fmt.Fprintf(f, `/*
    64   * Node-IPv6: fd01::b
    65   * Router-IPv6: f00d::a00:0:0:a4ad
    66   * Host-IPv4: 10.0.0.1
    67   */
    68  
    69  #define ENABLE_IPV4 1
    70  #define ROUTER_IP 0xf0, 0xd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa4, 0xad
    71  #define IPV4_GATEWAY 0x100000a
    72  #define IPV4_LOOPBACK 0x5dd0000a
    73  #define IPV4_MASK 0xffff
    74  #define NAT46_PREFIX { .addr = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0 } }
    75  #define HOST_IP 0xfd, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
    76  #define HOST_ID 1
    77  #define WORLD_ID 2
    78  #define LB_RR_MAX_SEQ 31
    79  #define CILIUM_LB_MAP_MAX_ENTRIES 65536
    80  #define TUNNEL_ENDPOINT_MAP_SIZE 65536
    81  #define ENDPOINTS_MAP_SIZE 65535
    82  #define LPM_MAP_SIZE 16384
    83  #define POLICY_MAP_SIZE 16384
    84  #define IPCACHE_MAP_SIZE 512000
    85  #define POLICY_PROG_MAP_SIZE 65535
    86  #define TRACE_PAYLOAD_LEN 128ULL
    87  #ifndef CILIUM_NET_MAC
    88  #define CILIUM_NET_MAC { .addr = {0x26,0x11,0x70,0xcc,0xca,0x0c}}
    89  #endif /* CILIUM_NET_MAC */
    90  #define HOST_IFINDEX 356
    91  #define HOST_IFINDEX_MAC { .addr = {0x3e,0x28,0xb4,0x4b,0x95,0x25}}
    92  #define ENCAP_VXLAN 1
    93  #define ENCAP_IFINDEX 358
    94  `)
    95  
    96  	type args struct {
    97  		nodeConfig string
    98  	}
    99  	tests := []struct {
   100  		name            string
   101  		args            args
   102  		wantIpv4GW      net.IP
   103  		wantIpv6Router  net.IP
   104  		wantIpv6Address net.IP
   105  	}{
   106  		{
   107  			name: "every-ip-correct",
   108  			args: args{
   109  				nodeConfig: allIPsCorrect,
   110  			},
   111  			wantIpv4GW:      net.ParseIP("10.0.0.1"),
   112  			wantIpv6Router:  net.ParseIP("f00d::a00:0:0:a4ad"),
   113  			wantIpv6Address: net.ParseIP("fd01::b"),
   114  		},
   115  		{
   116  			name: "file-not-present",
   117  			args: args{
   118  				nodeConfig: "",
   119  			},
   120  			wantIpv4GW:     nil,
   121  			wantIpv6Router: nil,
   122  		},
   123  	}
   124  	for _, tt := range tests {
   125  		gotIpv4GW, gotIpv6Router := getCiliumHostIPsFromFile(tt.args.nodeConfig)
   126  		if !reflect.DeepEqual(gotIpv4GW, tt.wantIpv4GW) {
   127  			c.Assert(gotIpv4GW, checker.DeepEquals, tt.wantIpv4GW)
   128  		}
   129  		if !reflect.DeepEqual(gotIpv6Router, tt.wantIpv6Router) {
   130  			c.Assert(gotIpv6Router, checker.DeepEquals, tt.wantIpv6Router)
   131  		}
   132  	}
   133  }