github.com/noironetworks/cilium-net@v1.6.12/pkg/loadbalancer/loadbalancer_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 loadbalancer
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"gopkg.in/check.v1"
    24  )
    25  
    26  // Hook up gocheck into the "go test" runner.
    27  func Test(t *testing.T) {
    28  	check.TestingT(t)
    29  }
    30  
    31  type TypesSuite struct{}
    32  
    33  var _ = check.Suite(&TypesSuite{})
    34  
    35  func TestL4Addr_Equals(t *testing.T) {
    36  	type args struct {
    37  		o *L4Addr
    38  	}
    39  	tests := []struct {
    40  		name   string
    41  		fields *L4Addr
    42  		args   args
    43  		want   bool
    44  	}{
    45  		{
    46  			name: "both equal",
    47  			fields: &L4Addr{
    48  				Protocol: NONE,
    49  				Port:     1,
    50  			},
    51  			args: args{
    52  				o: &L4Addr{
    53  					Protocol: NONE,
    54  					Port:     1,
    55  				},
    56  			},
    57  			want: true,
    58  		},
    59  		{
    60  			name: "both different",
    61  			fields: &L4Addr{
    62  				Protocol: NONE,
    63  				Port:     0,
    64  			},
    65  			args: args{
    66  				o: &L4Addr{
    67  					Protocol: NONE,
    68  					Port:     1,
    69  				},
    70  			},
    71  			want: false,
    72  		},
    73  		{
    74  			name: "both nil",
    75  			args: args{},
    76  			want: true,
    77  		},
    78  		{
    79  			name: "other nil",
    80  			fields: &L4Addr{
    81  				Protocol: NONE,
    82  				Port:     1,
    83  			},
    84  			args: args{},
    85  			want: false,
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			l := tt.fields
    91  			if got := l.Equals(tt.args.o); got != tt.want {
    92  				t.Errorf("L4Addr.Equals() = %v, want %v", got, tt.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestFEPort_EqualsIgnoreID(t *testing.T) {
    99  	type args struct {
   100  		o *FEPort
   101  	}
   102  	tests := []struct {
   103  		name   string
   104  		fields *FEPort
   105  		args   args
   106  		want   bool
   107  	}{
   108  		{
   109  			name: "both equal",
   110  			fields: &FEPort{
   111  				L4Addr: &L4Addr{
   112  					Protocol: NONE,
   113  					Port:     1,
   114  				},
   115  				ID: 1,
   116  			},
   117  			args: args{
   118  				o: &FEPort{
   119  					L4Addr: &L4Addr{
   120  						Protocol: NONE,
   121  						Port:     1,
   122  					},
   123  					ID: 1,
   124  				},
   125  			},
   126  			want: true,
   127  		},
   128  		{
   129  			name: "IDs different are considered equal",
   130  			fields: &FEPort{
   131  				L4Addr: &L4Addr{
   132  					Protocol: NONE,
   133  					Port:     1,
   134  				},
   135  				ID: 1,
   136  			},
   137  			args: args{
   138  				o: &FEPort{
   139  					L4Addr: &L4Addr{
   140  						Protocol: NONE,
   141  						Port:     1,
   142  					},
   143  					ID: 1001,
   144  				},
   145  			},
   146  			want: true,
   147  		},
   148  		{
   149  			name: "both nil",
   150  			args: args{},
   151  			want: true,
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		t.Run(tt.name, func(t *testing.T) {
   156  			f := tt.fields
   157  			if got := f.EqualsIgnoreID(tt.args.o); got != tt.want {
   158  				t.Errorf("FEPort.EqualsIgnoreID() = %v, want %v", got, tt.want)
   159  			}
   160  		})
   161  	}
   162  }
   163  
   164  func TestFEPort_Equals(t *testing.T) {
   165  	type args struct {
   166  		o *FEPort
   167  	}
   168  	tests := []struct {
   169  		name   string
   170  		fields *FEPort
   171  		args   args
   172  		want   bool
   173  	}{
   174  		{
   175  			name: "both equal",
   176  			fields: &FEPort{
   177  				L4Addr: &L4Addr{
   178  					Protocol: NONE,
   179  					Port:     1,
   180  				},
   181  				ID: 1,
   182  			},
   183  			args: args{
   184  				o: &FEPort{
   185  					L4Addr: &L4Addr{
   186  						Protocol: NONE,
   187  						Port:     1,
   188  					},
   189  					ID: 1,
   190  				},
   191  			},
   192  			want: true,
   193  		},
   194  		{
   195  			name: "IDs different are considered different",
   196  			fields: &FEPort{
   197  				L4Addr: &L4Addr{
   198  					Protocol: NONE,
   199  					Port:     1,
   200  				},
   201  				ID: 1,
   202  			},
   203  			args: args{
   204  				o: &FEPort{
   205  					L4Addr: &L4Addr{
   206  						Protocol: NONE,
   207  						Port:     1,
   208  					},
   209  					ID: 1001,
   210  				},
   211  			},
   212  			want: false,
   213  		},
   214  		{
   215  			name: "both nil",
   216  			args: args{},
   217  			want: true,
   218  		},
   219  	}
   220  	for _, tt := range tests {
   221  		t.Run(tt.name, func(t *testing.T) {
   222  			f := tt.fields
   223  			if got := f.Equals(tt.args.o); got != tt.want {
   224  				t.Errorf("FEPort.Equals() = %v, want %v", got, tt.want)
   225  			}
   226  		})
   227  	}
   228  }
   229  
   230  func TestL3n4AddrID_Equals(t *testing.T) {
   231  	type args struct {
   232  		o *L3n4AddrID
   233  	}
   234  	tests := []struct {
   235  		name   string
   236  		fields *L3n4AddrID
   237  		args   args
   238  		want   bool
   239  	}{
   240  		{
   241  			name: "both equal",
   242  			fields: &L3n4AddrID{
   243  				L3n4Addr: L3n4Addr{
   244  					L4Addr: L4Addr{
   245  						Protocol: NONE,
   246  						Port:     1,
   247  					},
   248  					IP: net.IPv4(1, 1, 1, 1),
   249  				},
   250  				ID: 1,
   251  			},
   252  			args: args{
   253  				o: &L3n4AddrID{
   254  					L3n4Addr: L3n4Addr{
   255  						L4Addr: L4Addr{
   256  							Protocol: NONE,
   257  							Port:     1,
   258  						},
   259  						IP: net.IPv4(1, 1, 1, 1),
   260  					},
   261  					ID: 1,
   262  				},
   263  			},
   264  			want: true,
   265  		},
   266  		{
   267  			name: "IDs different",
   268  			fields: &L3n4AddrID{
   269  				L3n4Addr: L3n4Addr{
   270  					L4Addr: L4Addr{
   271  						Protocol: NONE,
   272  						Port:     1,
   273  					},
   274  					IP: net.IPv4(1, 1, 1, 1),
   275  				},
   276  				ID: 1,
   277  			},
   278  			args: args{
   279  				o: &L3n4AddrID{
   280  					L3n4Addr: L3n4Addr{
   281  						L4Addr: L4Addr{
   282  							Protocol: NONE,
   283  							Port:     1,
   284  						},
   285  						IP: net.IPv4(1, 1, 1, 1),
   286  					},
   287  					ID: 2,
   288  				},
   289  			},
   290  			want: false,
   291  		},
   292  		{
   293  			name: "IPs different",
   294  			fields: &L3n4AddrID{
   295  				L3n4Addr: L3n4Addr{
   296  					L4Addr: L4Addr{
   297  						Protocol: NONE,
   298  						Port:     1,
   299  					},
   300  					IP: net.IPv4(2, 2, 2, 2),
   301  				},
   302  				ID: 1,
   303  			},
   304  			args: args{
   305  				o: &L3n4AddrID{
   306  					L3n4Addr: L3n4Addr{
   307  						L4Addr: L4Addr{
   308  							Protocol: NONE,
   309  							Port:     1,
   310  						},
   311  						IP: net.IPv4(1, 1, 1, 1),
   312  					},
   313  					ID: 1,
   314  				},
   315  			},
   316  			want: false,
   317  		},
   318  		{
   319  			name: "Ports different",
   320  			fields: &L3n4AddrID{
   321  				L3n4Addr: L3n4Addr{
   322  					L4Addr: L4Addr{
   323  						Protocol: NONE,
   324  						Port:     2,
   325  					},
   326  					IP: net.IPv4(1, 1, 1, 1),
   327  				},
   328  				ID: 1,
   329  			},
   330  			args: args{
   331  				o: &L3n4AddrID{
   332  					L3n4Addr: L3n4Addr{
   333  						L4Addr: L4Addr{
   334  							Protocol: NONE,
   335  							Port:     1,
   336  						},
   337  						IP: net.IPv4(1, 1, 1, 1),
   338  					},
   339  					ID: 1,
   340  				},
   341  			},
   342  			want: false,
   343  		},
   344  		{
   345  			name: "both nil",
   346  			args: args{},
   347  			want: true,
   348  		},
   349  	}
   350  	for _, tt := range tests {
   351  		t.Run(tt.name, func(t *testing.T) {
   352  			f := tt.fields
   353  			if got := f.Equals(tt.args.o); got != tt.want {
   354  				t.Errorf("L3n4AddrID.Equals() = %v, want %v", got, tt.want)
   355  			}
   356  		})
   357  	}
   358  }