github.com/cilium/cilium@v1.16.2/pkg/ipam/allocator/clusterpool/cidralloc/cidralloc_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package cidralloc
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  )
    10  
    11  func TestNewCIDRSets(t *testing.T) {
    12  	type args struct {
    13  		isV6     bool
    14  		strCIDRs []string
    15  		maskSize int
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantErr error
    21  	}{
    22  		{
    23  			name: "test-1",
    24  			args: args{
    25  				isV6:     false,
    26  				strCIDRs: []string{"10.0.0.0/16"},
    27  				maskSize: 24,
    28  			},
    29  			wantErr: nil,
    30  		},
    31  		{
    32  			name: "test-2 - CIDRs collide",
    33  			args: args{
    34  				isV6:     false,
    35  				strCIDRs: []string{"10.0.0.0/16", "10.0.0.0/8"},
    36  				maskSize: 24,
    37  			},
    38  			wantErr: &ErrCIDRCollision{
    39  				cidr: "10.0.0.0/8",
    40  			},
    41  		},
    42  		{
    43  			name: "test-2 - CIDRs collide",
    44  			args: args{
    45  				isV6:     false,
    46  				strCIDRs: []string{"10.0.0.0/8"},
    47  				maskSize: 24,
    48  			},
    49  			wantErr: nil,
    50  		},
    51  		{
    52  			name: "test-4 - CIDRs collide",
    53  			args: args{
    54  				isV6:     true,
    55  				strCIDRs: []string{"fd00::/100", "fd00::/96"},
    56  				maskSize: 112,
    57  			},
    58  			wantErr: &ErrCIDRCollision{
    59  				cidr: "fd00::/96",
    60  			},
    61  		},
    62  		{
    63  			name: "test-5 - CIDRs do not collide",
    64  			args: args{
    65  				isV6:     true,
    66  				strCIDRs: []string{"fd00::/100", "fd00::1:0000:0000/96"},
    67  				maskSize: 112,
    68  			},
    69  			wantErr: nil,
    70  		},
    71  		{
    72  			name: "test-6 - CIDR does not collide",
    73  			args: args{
    74  				isV6:     true,
    75  				strCIDRs: []string{"fd00::/104"},
    76  				maskSize: 120,
    77  			},
    78  			wantErr: nil,
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			_, err := NewCIDRSets(tt.args.isV6, tt.args.strCIDRs, tt.args.maskSize)
    84  			if (err != nil) != (tt.wantErr != nil) {
    85  				t.Errorf("newCIDRSets() error = %v, wantErr %v", err, tt.wantErr)
    86  				return
    87  			}
    88  			if tt.wantErr != nil && !errors.Is(err, tt.wantErr) {
    89  				t.Errorf("newCIDRSets() error = %v, wantErr %v", err, tt.wantErr)
    90  				return
    91  			}
    92  		})
    93  	}
    94  }