...

Source file src/k8s.io/kubernetes/pkg/proxy/ipvs/util/ipvs_linux_test.go

Documentation: k8s.io/kubernetes/pkg/proxy/ipvs/util

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2017 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package ipvs
    21  
    22  import (
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	netutils "k8s.io/utils/net"
    28  
    29  	libipvs "github.com/moby/ipvs"
    30  
    31  	"golang.org/x/sys/unix"
    32  )
    33  
    34  func Test_toVirtualServer(t *testing.T) {
    35  	Tests := []struct {
    36  		ipvsService   libipvs.Service
    37  		virtualServer VirtualServer
    38  		expectError   bool
    39  		reason        string
    40  	}{
    41  		{
    42  			libipvs.Service{
    43  				Flags: 0x0,
    44  			},
    45  			VirtualServer{},
    46  			true,
    47  			fmt.Sprintf("IPVS Service Flags should include %x, got 0x0", FlagHashed),
    48  		},
    49  		{
    50  			libipvs.Service{
    51  				Flags: 0x1,
    52  			},
    53  			VirtualServer{},
    54  			true,
    55  			fmt.Sprintf("IPVS Service Flags should include %x, got 0x1", FlagHashed),
    56  		},
    57  		{
    58  			libipvs.Service{
    59  				Protocol:      unix.IPPROTO_TCP,
    60  				Port:          80,
    61  				FWMark:        0,
    62  				SchedName:     "",
    63  				Flags:         uint32(FlagPersistent + FlagHashed),
    64  				Timeout:       0,
    65  				Netmask:       0xffffffff,
    66  				AddressFamily: unix.AF_INET,
    67  				Address:       nil,
    68  				PEName:        "",
    69  			},
    70  			VirtualServer{
    71  				Address:   netutils.ParseIPSloppy("0.0.0.0"),
    72  				Protocol:  "TCP",
    73  				Port:      80,
    74  				Scheduler: "",
    75  				Flags:     ServiceFlags(FlagPersistent),
    76  				Timeout:   0,
    77  			},
    78  			false,
    79  			"",
    80  		},
    81  		{
    82  			libipvs.Service{
    83  				Protocol:      unix.IPPROTO_UDP,
    84  				Port:          33434,
    85  				FWMark:        0,
    86  				SchedName:     "wlc",
    87  				Flags:         uint32(0 + FlagHashed),
    88  				Timeout:       100,
    89  				Netmask:       128,
    90  				AddressFamily: unix.AF_INET6,
    91  				Address:       netutils.ParseIPSloppy("2012::beef"),
    92  				PEName:        "",
    93  			},
    94  			VirtualServer{
    95  				Address:   netutils.ParseIPSloppy("2012::beef"),
    96  				Protocol:  "UDP",
    97  				Port:      33434,
    98  				Scheduler: "wlc",
    99  				Flags:     ServiceFlags(0),
   100  				Timeout:   100,
   101  			},
   102  			false,
   103  			"",
   104  		},
   105  		{
   106  			libipvs.Service{
   107  				Protocol:      0,
   108  				Port:          0,
   109  				FWMark:        0,
   110  				SchedName:     "lc",
   111  				Flags:         uint32(0 + FlagHashed),
   112  				Timeout:       0,
   113  				Netmask:       0xffffffff,
   114  				AddressFamily: unix.AF_INET,
   115  				Address:       netutils.ParseIPSloppy("1.2.3.4"),
   116  				PEName:        "",
   117  			},
   118  			VirtualServer{
   119  				Address:   netutils.ParseIPSloppy("1.2.3.4"),
   120  				Protocol:  "",
   121  				Port:      0,
   122  				Scheduler: "lc",
   123  				Flags:     ServiceFlags(0),
   124  				Timeout:   0,
   125  			},
   126  			false,
   127  			"",
   128  		},
   129  		{
   130  			libipvs.Service{
   131  				Protocol:      0,
   132  				Port:          0,
   133  				FWMark:        0,
   134  				SchedName:     "wrr",
   135  				Flags:         uint32(FlagPersistent + FlagHashed),
   136  				Timeout:       0,
   137  				Netmask:       128,
   138  				AddressFamily: unix.AF_INET6,
   139  				Address:       nil,
   140  				PEName:        "",
   141  			},
   142  			VirtualServer{
   143  				Address:   netutils.ParseIPSloppy("::0"),
   144  				Protocol:  "",
   145  				Port:      0,
   146  				Scheduler: "wrr",
   147  				Flags:     ServiceFlags(FlagPersistent),
   148  				Timeout:   0,
   149  			},
   150  			false,
   151  			"",
   152  		},
   153  		{
   154  			libipvs.Service{
   155  				Protocol:      0,
   156  				Port:          0,
   157  				FWMark:        0,
   158  				SchedName:     "mh",
   159  				Flags:         uint32(FlagPersistent + FlagHashed + FlagSourceHash),
   160  				Timeout:       0,
   161  				Netmask:       0xffffffff,
   162  				AddressFamily: unix.AF_INET,
   163  				Address:       netutils.ParseIPSloppy("1.2.3.4"),
   164  				PEName:        "",
   165  			},
   166  			VirtualServer{
   167  				Address:   netutils.ParseIPSloppy("1.2.3.4"),
   168  				Protocol:  "",
   169  				Port:      0,
   170  				Scheduler: "mh",
   171  				Flags:     ServiceFlags(FlagPersistent + FlagSourceHash),
   172  				Timeout:   0,
   173  			},
   174  			false,
   175  			"",
   176  		},
   177  		{
   178  			libipvs.Service{
   179  				Protocol:      unix.IPPROTO_SCTP,
   180  				Port:          80,
   181  				FWMark:        0,
   182  				SchedName:     "",
   183  				Flags:         uint32(FlagPersistent + FlagHashed),
   184  				Timeout:       0,
   185  				Netmask:       0xffffffff,
   186  				AddressFamily: unix.AF_INET,
   187  				Address:       nil,
   188  				PEName:        "",
   189  			},
   190  			VirtualServer{
   191  				Address:   netutils.ParseIPSloppy("0.0.0.0"),
   192  				Protocol:  "SCTP",
   193  				Port:      80,
   194  				Scheduler: "",
   195  				Flags:     ServiceFlags(FlagPersistent),
   196  				Timeout:   0,
   197  			},
   198  			false,
   199  			"",
   200  		},
   201  	}
   202  
   203  	for i := range Tests {
   204  		got, err := toVirtualServer(&Tests[i].ipvsService)
   205  		if Tests[i].expectError && err == nil {
   206  			t.Errorf("case: %d, expected error: %s, got nil", i, Tests[i].reason)
   207  		}
   208  		if !Tests[i].expectError && err != nil {
   209  			t.Errorf("case: %d, unexpected error: %v", i, err)
   210  		}
   211  		if got != nil {
   212  			if !reflect.DeepEqual(*got, Tests[i].virtualServer) {
   213  				t.Errorf("case: %d, got %#v, want %#v", i, *got, Tests[i].virtualServer)
   214  			}
   215  		}
   216  	}
   217  }
   218  
   219  func Test_toIPVSService(t *testing.T) {
   220  	Tests := []struct {
   221  		ipvsService   libipvs.Service
   222  		virtualServer VirtualServer
   223  	}{
   224  		{
   225  			libipvs.Service{
   226  				Protocol:      unix.IPPROTO_TCP,
   227  				Port:          80,
   228  				FWMark:        0,
   229  				SchedName:     "",
   230  				Flags:         0,
   231  				Timeout:       0,
   232  				Netmask:       0xffffffff,
   233  				AddressFamily: unix.AF_INET,
   234  				Address:       netutils.ParseIPSloppy("0.0.0.0"),
   235  				PEName:        "",
   236  			},
   237  			VirtualServer{
   238  				Address:   netutils.ParseIPSloppy("0.0.0.0"),
   239  				Protocol:  "TCP",
   240  				Port:      80,
   241  				Scheduler: "",
   242  				Flags:     0,
   243  				Timeout:   0,
   244  			},
   245  		},
   246  		{
   247  			libipvs.Service{
   248  				Protocol:      unix.IPPROTO_UDP,
   249  				Port:          33434,
   250  				FWMark:        0,
   251  				SchedName:     "wlc",
   252  				Flags:         1234,
   253  				Timeout:       100,
   254  				Netmask:       128,
   255  				AddressFamily: unix.AF_INET6,
   256  				Address:       netutils.ParseIPSloppy("2012::beef"),
   257  				PEName:        "",
   258  			},
   259  			VirtualServer{
   260  				Address:   netutils.ParseIPSloppy("2012::beef"),
   261  				Protocol:  "UDP",
   262  				Port:      33434,
   263  				Scheduler: "wlc",
   264  				Flags:     1234,
   265  				Timeout:   100,
   266  			},
   267  		},
   268  		{
   269  			libipvs.Service{
   270  				Protocol:      0,
   271  				Port:          0,
   272  				FWMark:        0,
   273  				SchedName:     "lc",
   274  				Flags:         0,
   275  				Timeout:       0,
   276  				Netmask:       0xffffffff,
   277  				AddressFamily: unix.AF_INET,
   278  				Address:       netutils.ParseIPSloppy("1.2.3.4"),
   279  				PEName:        "",
   280  			},
   281  			VirtualServer{
   282  				Address:   netutils.ParseIPSloppy("1.2.3.4"),
   283  				Protocol:  "",
   284  				Port:      0,
   285  				Scheduler: "lc",
   286  				Flags:     0,
   287  				Timeout:   0,
   288  			},
   289  		},
   290  		{
   291  			libipvs.Service{
   292  				Protocol:      0,
   293  				Port:          0,
   294  				FWMark:        0,
   295  				SchedName:     "wrr",
   296  				Flags:         0,
   297  				Timeout:       0,
   298  				Netmask:       128,
   299  				AddressFamily: unix.AF_INET6,
   300  				Address:       netutils.ParseIPSloppy("::0"),
   301  				PEName:        "",
   302  			},
   303  			VirtualServer{
   304  				Address:   netutils.ParseIPSloppy("::0"),
   305  				Protocol:  "",
   306  				Port:      0,
   307  				Scheduler: "wrr",
   308  				Flags:     0,
   309  				Timeout:   0,
   310  			},
   311  		},
   312  	}
   313  
   314  	for i := range Tests {
   315  		got, err := toIPVSService(&Tests[i].virtualServer)
   316  		if err != nil {
   317  			t.Errorf("case: %d, unexpected error: %v", i, err)
   318  		}
   319  		if !reflect.DeepEqual(*got, Tests[i].ipvsService) {
   320  			t.Errorf("case: %d - got %#v, want %#v", i, *got, Tests[i].ipvsService)
   321  		}
   322  	}
   323  }
   324  
   325  func Test_toRealServer(t *testing.T) {
   326  	Tests := []struct {
   327  		ipvsDestination libipvs.Destination
   328  		realServer      RealServer
   329  	}{
   330  		{
   331  			libipvs.Destination{
   332  				Port:            54321,
   333  				ConnectionFlags: 0,
   334  				Weight:          1,
   335  				Address:         netutils.ParseIPSloppy("1.2.3.4"),
   336  			},
   337  			RealServer{
   338  				Address: netutils.ParseIPSloppy("1.2.3.4"),
   339  				Port:    54321,
   340  				Weight:  1,
   341  			},
   342  		},
   343  		{
   344  			libipvs.Destination{
   345  				Port:            53,
   346  				ConnectionFlags: 0,
   347  				Weight:          1,
   348  				Address:         netutils.ParseIPSloppy("2002::cafe"),
   349  			},
   350  			RealServer{
   351  				Address: netutils.ParseIPSloppy("2002::cafe"),
   352  				Port:    53,
   353  				Weight:  1,
   354  			},
   355  		},
   356  	}
   357  	for i := range Tests {
   358  		got, err := toRealServer(&Tests[i].ipvsDestination)
   359  		if err != nil {
   360  			t.Errorf("case %d unexpected error: %v", i, err)
   361  		}
   362  		if !reflect.DeepEqual(*got, Tests[i].realServer) {
   363  			t.Errorf("case %d Failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].realServer)
   364  		}
   365  	}
   366  }
   367  
   368  func Test_toIPVSDestination(t *testing.T) {
   369  	Tests := []struct {
   370  		realServer      RealServer
   371  		ipvsDestination libipvs.Destination
   372  	}{
   373  		{
   374  			RealServer{
   375  				Address: netutils.ParseIPSloppy("1.2.3.4"),
   376  				Port:    54321,
   377  				Weight:  1,
   378  			},
   379  			libipvs.Destination{
   380  				Port:            54321,
   381  				ConnectionFlags: 0,
   382  				Weight:          1,
   383  				Address:         netutils.ParseIPSloppy("1.2.3.4"),
   384  			},
   385  		},
   386  		{
   387  			RealServer{
   388  				Address: netutils.ParseIPSloppy("2002::cafe"),
   389  				Port:    53,
   390  				Weight:  1,
   391  			},
   392  			libipvs.Destination{
   393  				Port:            53,
   394  				ConnectionFlags: 0,
   395  				Weight:          1,
   396  				Address:         netutils.ParseIPSloppy("2002::cafe"),
   397  			},
   398  		},
   399  	}
   400  	for i := range Tests {
   401  		got, err := toIPVSDestination(&Tests[i].realServer)
   402  		if err != nil {
   403  			t.Errorf("case %d unexpected error: %v", i, err)
   404  		}
   405  		if !reflect.DeepEqual(*got, Tests[i].ipvsDestination) {
   406  			t.Errorf("case %d failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].ipvsDestination)
   407  		}
   408  	}
   409  }
   410  
   411  func Test_stringToProtocol(t *testing.T) {
   412  	tests := []string{
   413  		"TCP", "UDP", "ICMP", "SCTP",
   414  	}
   415  	expected := []uint16{
   416  		uint16(unix.IPPROTO_TCP), uint16(unix.IPPROTO_UDP), uint16(0), uint16(unix.IPPROTO_SCTP),
   417  	}
   418  	for i := range tests {
   419  		got := stringToProtocol(tests[i])
   420  		if got != expected[i] {
   421  			t.Errorf("stringToProtocol() failed - got %#v, want %#v",
   422  				got, expected[i])
   423  		}
   424  	}
   425  }
   426  
   427  func Test_protocolToString(t *testing.T) {
   428  	tests := []Protocol{
   429  		unix.IPPROTO_TCP, unix.IPPROTO_UDP, Protocol(0), unix.IPPROTO_SCTP,
   430  	}
   431  	expected := []string{
   432  		"TCP", "UDP", "", "SCTP",
   433  	}
   434  	for i := range tests {
   435  		got := protocolToString(tests[i])
   436  		if got != expected[i] {
   437  			t.Errorf("protocolToString() failed - got %#v, want %#v",
   438  				got, expected[i])
   439  		}
   440  	}
   441  }
   442  

View as plain text