github.com/cilium/cilium@v1.16.2/pkg/clustermesh/types/addressing_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"net/netip"
     8  	"testing"
     9  )
    10  
    11  func TestParseAddrCluster(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		arg     string
    15  		wantErr bool
    16  	}{
    17  		{"valid bare IPv4 address", "10.0.0.1", false},
    18  		{"invalid bare IPv4 address", "257.0.0.1", true},
    19  
    20  		{"valid IPv4 address and valid cluster-id", "10.0.0.1@1", false},
    21  		{"invalid IPv4 address and valid cluster-id", "257.0.0.1@1", true},
    22  		{"valid IPv4 address and invalid cluster-id", "10.0.0.1@foo", true},
    23  		{"valid IPv4 address and enpty cluster-id", "10.0.0.1@", true},
    24  
    25  		{"valid bare IPv6 address", "a::1", false},
    26  		{"invalid bare IPv6 address", "g::1", true},
    27  
    28  		{"valid IPv6 address and valid cluster-id", "a::1@1", false},
    29  		{"invalid IPv6 address and valid cluster-id", "g::1@1", true},
    30  		{"valid IPv6 address and invalid cluster-id", "a::1@foo", true},
    31  		{"valid IPv6 address and enpty cluster-id", "a::1@", true},
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			_, err := ParseAddrCluster(tt.arg)
    36  			if (err != nil) != tt.wantErr {
    37  				t.Errorf("ParseAddrCluster() error = %v, wantErr %v", err, tt.wantErr)
    38  				return
    39  			}
    40  		})
    41  	}
    42  
    43  	// Ensure bare IP address equals to ClusterID = 0
    44  	if MustParseAddrCluster("10.0.0.1") != MustParseAddrCluster("10.0.0.1@0") {
    45  		t.Errorf("ParseAddrCluster() returns different results for bare IP address and IP address with zero ClusterID")
    46  		return
    47  	}
    48  }
    49  
    50  func TestAddrCluster_Equal(t *testing.T) {
    51  	type fields struct {
    52  		addr      netip.Addr
    53  		clusterID uint32
    54  	}
    55  	type args struct {
    56  		ac1 AddrCluster
    57  	}
    58  	tests := []struct {
    59  		name   string
    60  		fields fields
    61  		args   args
    62  		want   bool
    63  	}{
    64  		{
    65  			"same IP and same ClusterID",
    66  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
    67  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1}},
    68  			true,
    69  		},
    70  		{
    71  			"same IP and different ClusterID",
    72  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
    73  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 2}},
    74  			false,
    75  		},
    76  		{
    77  			"different IP and same ClusterID",
    78  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
    79  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.2"), clusterID: 1}},
    80  			false,
    81  		},
    82  		{
    83  			"different IP and different ClusterID",
    84  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
    85  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.2"), clusterID: 2}},
    86  			false,
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			ac0 := AddrCluster{
    92  				addr:      tt.fields.addr,
    93  				clusterID: tt.fields.clusterID,
    94  			}
    95  			if got := ac0.Equal(tt.args.ac1); got != tt.want {
    96  				t.Errorf("AddrCluster.Equal() = %v, want %v", got, tt.want)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func TestAddrCluster_Less(t *testing.T) {
   103  	type fields struct {
   104  		addr      netip.Addr
   105  		clusterID uint32
   106  	}
   107  	type args struct {
   108  		ac1 AddrCluster
   109  	}
   110  	tests := []struct {
   111  		name   string
   112  		fields fields
   113  		args   args
   114  		want   bool
   115  	}{
   116  		{
   117  			"same IP and same ClusterID",
   118  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
   119  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1}},
   120  			false,
   121  		},
   122  		{
   123  			"larger IP and same ClusterID",
   124  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
   125  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.2"), clusterID: 1}},
   126  			true,
   127  		},
   128  		{
   129  			"smaller IP and smaller ClusterID",
   130  			fields{addr: netip.MustParseAddr("10.0.0.2"), clusterID: 1},
   131  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1}},
   132  			false,
   133  		},
   134  		{
   135  			"same IP and larger ClusterID",
   136  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
   137  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 2}},
   138  			true,
   139  		},
   140  		{
   141  			"same IP and smaller ClusterID",
   142  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 2},
   143  			args{ac1: AddrCluster{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1}},
   144  			false,
   145  		},
   146  	}
   147  	for _, tt := range tests {
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			ac0 := AddrCluster{
   150  				addr:      tt.fields.addr,
   151  				clusterID: tt.fields.clusterID,
   152  			}
   153  			if got := ac0.Less(tt.args.ac1); got != tt.want {
   154  				t.Errorf("AddrCluster.Less() = %v, want %v", got, tt.want)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  func TestAddrCluster_String(t *testing.T) {
   161  	type fields struct {
   162  		addr      netip.Addr
   163  		clusterID uint32
   164  	}
   165  	tests := []struct {
   166  		name   string
   167  		fields fields
   168  		want   string
   169  	}{
   170  		{
   171  			"zero ClusterID",
   172  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 0},
   173  			"10.0.0.1",
   174  		},
   175  		{
   176  			"non-zero ClusterID",
   177  			fields{addr: netip.MustParseAddr("10.0.0.1"), clusterID: 1},
   178  			"10.0.0.1@1",
   179  		},
   180  	}
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			ac := AddrCluster{
   184  				addr:      tt.fields.addr,
   185  				clusterID: tt.fields.clusterID,
   186  			}
   187  			if got := ac.String(); got != tt.want {
   188  				t.Errorf("AddrCluster.String() = %v, want %v", got, tt.want)
   189  			}
   190  		})
   191  	}
   192  }
   193  
   194  func TestParsePrefixCluster(t *testing.T) {
   195  	tests := []struct {
   196  		name    string
   197  		arg     string
   198  		wantErr bool
   199  	}{
   200  		{"valid bare IPv4 prefix", "10.0.0.0/24", false},
   201  		{"invalid bare IPv4 prefix 1", "10.0.0.0", true},
   202  		{"invalid bare IPv4 prefix 2", "257.0.0.0/24", true},
   203  
   204  		{"valid IPv4 prefix and valid cluster-id", "10.0.0.0/24@1", false},
   205  		{"invalid IPv4 prefix and valid cluster-id", "257.0.0.0/24@1", true},
   206  		{"valid IPv4 prefix and invalid cluster-id", "10.0.0.0/24@foo", true},
   207  		{"valid IPv4 prefix and empty cluster-id", "10.0.0.0/24@", true},
   208  
   209  		{"valid bare IPv6 prefix", "a::/64", false},
   210  		{"invalid bare IPv6 prefix", "g::/64", true},
   211  
   212  		{"valid IPv6 prefix and valid cluster-id", "a::/64@1", false},
   213  		{"invalid IPv6 prefix and valid cluster-id", "g::/64@1", true},
   214  		{"valid IPv6 prefix and invalid cluster-id", "a::/64@foo", true},
   215  		{"valid IPv6 prefix and empty cluster-id", "a::/64@", true},
   216  	}
   217  	for _, tt := range tests {
   218  		t.Run(tt.name, func(t *testing.T) {
   219  			_, err := ParsePrefixCluster(tt.arg)
   220  			if (err != nil) != tt.wantErr {
   221  				t.Errorf("ParsePrefixCluster() error = %v, wantErr %v", err, tt.wantErr)
   222  				return
   223  			}
   224  		})
   225  	}
   226  
   227  	// Ensure bare IP address equals to ClusterID = 0
   228  	if MustParsePrefixCluster("10.0.0.0/24") != MustParsePrefixCluster("10.0.0.0/24@0") {
   229  		t.Errorf("ParsePrefixCluster() returns different results for bare IP prefix and IP prefix with zero ClusterID")
   230  		return
   231  	}
   232  }