github.phpd.cn/cilium/cilium@v1.6.12/pkg/labels/cidr/cidr_test.go (about)

     1  // Copyright 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 cidr
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  	"github.com/cilium/cilium/pkg/labels"
    25  
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  // Hook up gocheck into the "go test" runner.
    30  func Test(t *testing.T) {
    31  	TestingT(t)
    32  }
    33  
    34  type CIDRLabelsSuite struct{}
    35  
    36  var _ = Suite(&CIDRLabelsSuite{})
    37  
    38  // TestGetCIDRLabels checks that GetCIDRLabels returns a sane set of labels for
    39  // given CIDRs.
    40  func (s *CIDRLabelsSuite) TestGetCIDRLabels(c *C) {
    41  	_, cidr, err := net.ParseCIDR("192.0.2.3/32")
    42  	c.Assert(err, IsNil)
    43  	expected := labels.ParseLabelArray(
    44  		"cidr:0.0.0.0/0",
    45  		"cidr:128.0.0.0/1",
    46  		"cidr:192.0.0.0/8",
    47  		"cidr:192.0.2.0/24",
    48  		"cidr:192.0.2.3/32",
    49  		"reserved:world",
    50  	)
    51  
    52  	lbls := GetCIDRLabels(cidr)
    53  	lblArray := lbls.LabelArray()
    54  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
    55  	// IPs should be masked as the labels are generated
    56  	c.Assert(lblArray.Has("cidr:192.0.2.3/24"), Equals, false)
    57  
    58  	_, cidr, err = net.ParseCIDR("192.0.2.0/24")
    59  	c.Assert(err, IsNil)
    60  	expected = labels.ParseLabelArray(
    61  		"cidr:0.0.0.0/0",
    62  		"cidr:192.0.2.0/24",
    63  		"reserved:world",
    64  	)
    65  
    66  	lbls = GetCIDRLabels(cidr)
    67  	lblArray = lbls.LabelArray()
    68  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
    69  	// CIDRs that are covered by the prefix should not be in the labels
    70  	c.Assert(lblArray.Has("cidr.192.0.2.3/32"), Equals, false)
    71  
    72  	// Zero-length prefix / default route should become reserved:world.
    73  	_, cidr, err = net.ParseCIDR("0.0.0.0/0")
    74  	c.Assert(err, IsNil)
    75  	expected = labels.ParseLabelArray(
    76  		"reserved:world",
    77  	)
    78  
    79  	lbls = GetCIDRLabels(cidr)
    80  	lblArray = lbls.LabelArray()
    81  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
    82  	c.Assert(lblArray.Has("cidr.0.0.0.0/0"), Equals, false)
    83  
    84  	// Note that we convert the colons in IPv6 addresses into dashes when
    85  	// translating into labels, because endpointSelectors don't support
    86  	// colons.
    87  	_, cidr, err = net.ParseCIDR("2001:DB8::1/128")
    88  	c.Assert(err, IsNil)
    89  	expected = labels.ParseLabelArray(
    90  		"cidr:0--0/0",
    91  		"cidr:2000--0/3",
    92  		"cidr:2001--0/16",
    93  		"cidr:2001-d00--0/24",
    94  		"cidr:2001-db8--0/32",
    95  		"cidr:2001-db8--1/128",
    96  		"reserved:world",
    97  	)
    98  
    99  	lbls = GetCIDRLabels(cidr)
   100  	lblArray = lbls.LabelArray()
   101  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
   102  	// IPs should be masked as the labels are generated
   103  	c.Assert(lblArray.Has("cidr.2001-db8--1/24"), Equals, false)
   104  }
   105  
   106  // TestGetCIDRLabelsInCluster checks that the cluster label is properly added
   107  // when getting labels for CIDRs that are equal to or within the cluster range.
   108  func (s *CIDRLabelsSuite) TestGetCIDRLabelsInCluster(c *C) {
   109  	_, cidr, err := net.ParseCIDR("10.0.0.0/16")
   110  	c.Assert(err, IsNil)
   111  	expected := labels.ParseLabelArray(
   112  		"cidr:0.0.0.0/0",
   113  		"cidr:10.0.0.0/16",
   114  		"reserved:world",
   115  	)
   116  	lbls := GetCIDRLabels(cidr)
   117  	lblArray := lbls.LabelArray()
   118  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
   119  
   120  	// This case is firmly within the cluster range
   121  	_, cidr, err = net.ParseCIDR("2001:db8:cafe::cab:4:b0b:0/112")
   122  	c.Assert(err, IsNil)
   123  	expected = labels.ParseLabelArray(
   124  		"cidr:0--0/0",
   125  		"cidr:2001-db8-cafe--0/64",
   126  		"cidr:2001-db8-cafe-0-cab-4--0/96",
   127  		"cidr:2001-db8-cafe-0-cab-4-b0b-0/112",
   128  		"reserved:world",
   129  	)
   130  	lbls = GetCIDRLabels(cidr)
   131  	lblArray = lbls.LabelArray()
   132  	c.Assert(lblArray.Lacks(expected), checker.DeepEquals, labels.LabelArray{})
   133  }
   134  
   135  func (s *CIDRLabelsSuite) TestIPStringToLabel(c *C) {
   136  	ipToLabels := map[string]string{
   137  		"0.0.0.0/0":    "cidr:0.0.0.0/0",
   138  		"192.0.2.3":    "cidr:192.0.2.3/32",
   139  		"192.0.2.3/32": "cidr:192.0.2.3/32",
   140  		"192.0.2.3/24": "cidr:192.0.2.0/24",
   141  		"192.0.2.0/24": "cidr:192.0.2.0/24",
   142  		"::/0":         "cidr:0--0/0",
   143  		"fdff::ff":     "cidr:fdff--ff/128",
   144  	}
   145  	for ip, labelStr := range ipToLabels {
   146  		lbl, err := IPStringToLabel(ip)
   147  		c.Assert(err, IsNil)
   148  		c.Assert(lbl.String(), checker.DeepEquals, labelStr)
   149  	}
   150  }