github.com/fafucoder/cilium@v1.6.11/pkg/cidr/diff_test.go (about)

     1  // Copyright 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 cidr
    18  
    19  import (
    20  	"github.com/cilium/cilium/pkg/checker"
    21  
    22  	"gopkg.in/check.v1"
    23  )
    24  
    25  func (s *CidrTestSuite) TestDiffIPNetLists(c *check.C) {
    26  	net1 := MustParseCIDR("1.1.1.1/32")
    27  	net2 := MustParseCIDR("1.1.1.1/24")
    28  	net3 := MustParseCIDR("cafe::1/128")
    29  	net4 := MustParseCIDR("cafe::2/16")
    30  
    31  	type testExpectation struct {
    32  		old    []*CIDR
    33  		new    []*CIDR
    34  		add    []*CIDR
    35  		remove []*CIDR
    36  	}
    37  
    38  	expectations := []testExpectation{
    39  		{old: []*CIDR{nil}, new: []*CIDR{net1, net2, net3, net4}, add: []*CIDR{net1, net2, net3, net4}, remove: nil},
    40  		{old: []*CIDR{}, new: []*CIDR{net1, net2, net3, net4}, add: []*CIDR{net1, net2, net3, net4}, remove: nil},
    41  		{old: []*CIDR{net1, net2, net3, net4}, new: []*CIDR{}, add: nil, remove: []*CIDR{net1, net2, net3, net4}},
    42  		{old: []*CIDR{net1, net2}, new: []*CIDR{net3, net4}, add: []*CIDR{net3, net4}, remove: []*CIDR{net1, net2}},
    43  		{old: []*CIDR{net1, net2}, new: []*CIDR{net2, net3}, add: []*CIDR{net3}, remove: []*CIDR{net1}},
    44  		{old: []*CIDR{net1, net2, net3, net4}, new: []*CIDR{net1, net2, net3, net4}, add: nil, remove: nil},
    45  	}
    46  
    47  	for i, t := range expectations {
    48  		add, remove := DiffCIDRLists(t.old, t.new)
    49  		c.Assert(add, checker.DeepEquals, t.add, check.Commentf("test index: %d", i))
    50  		c.Assert(remove, checker.DeepEquals, t.remove, check.Commentf("test index: %d", i))
    51  	}
    52  }