k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/proxy/util/localdetector_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestNoOpLocalDetector(t *testing.T) {
    25  	localDetector := NewNoOpLocalDetector()
    26  	if localDetector.IsImplemented() {
    27  		t.Error("NoOpLocalDetector returns true for IsImplemented")
    28  	}
    29  
    30  	ifLocal := localDetector.IfLocal()
    31  	if len(ifLocal) != 0 {
    32  		t.Errorf("NoOpLocalDetector returns %v for IsLocal (expected nil)", ifLocal)
    33  	}
    34  
    35  	ifNotLocal := localDetector.IfNotLocal()
    36  	if len(ifNotLocal) != 0 {
    37  		t.Errorf("NoOpLocalDetector returns %v for IsNotLocal (expected nil)", ifNotLocal)
    38  	}
    39  }
    40  
    41  func TestDetectLocalByCIDR(t *testing.T) {
    42  	cases := []struct {
    43  		cidr                     string
    44  		expectedIfLocalOutput    []string
    45  		expectedIfNotLocalOutput []string
    46  	}{
    47  		{
    48  			cidr:                     "10.0.0.0/14",
    49  			expectedIfLocalOutput:    []string{"-s", "10.0.0.0/14"},
    50  			expectedIfNotLocalOutput: []string{"!", "-s", "10.0.0.0/14"},
    51  		},
    52  		{
    53  			cidr:                     "2002:0:0:1234::/64",
    54  			expectedIfLocalOutput:    []string{"-s", "2002:0:0:1234::/64"},
    55  			expectedIfNotLocalOutput: []string{"!", "-s", "2002:0:0:1234::/64"},
    56  		},
    57  	}
    58  	for _, c := range cases {
    59  		localDetector := NewDetectLocalByCIDR(c.cidr)
    60  		if !localDetector.IsImplemented() {
    61  			t.Error("DetectLocalByCIDR returns false for IsImplemented")
    62  		}
    63  
    64  		ifLocal := localDetector.IfLocal()
    65  		ifNotLocal := localDetector.IfNotLocal()
    66  
    67  		if !reflect.DeepEqual(ifLocal, c.expectedIfLocalOutput) {
    68  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedIfLocalOutput, ifLocal)
    69  		}
    70  
    71  		if !reflect.DeepEqual(ifNotLocal, c.expectedIfNotLocalOutput) {
    72  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedIfNotLocalOutput, ifNotLocal)
    73  		}
    74  	}
    75  }
    76  
    77  func TestDetectLocalByBridgeInterface(t *testing.T) {
    78  	cases := []struct {
    79  		ifaceName               string
    80  		expectedJumpIfOutput    []string
    81  		expectedJumpIfNotOutput []string
    82  	}{
    83  		{
    84  			ifaceName:               "eth0",
    85  			expectedJumpIfOutput:    []string{"-i", "eth0"},
    86  			expectedJumpIfNotOutput: []string{"!", "-i", "eth0"},
    87  		},
    88  	}
    89  	for _, c := range cases {
    90  		localDetector := NewDetectLocalByBridgeInterface(c.ifaceName)
    91  		if !localDetector.IsImplemented() {
    92  			t.Error("DetectLocalByBridgeInterface returns false for IsImplemented")
    93  		}
    94  
    95  		ifLocal := localDetector.IfLocal()
    96  		ifNotLocal := localDetector.IfNotLocal()
    97  
    98  		if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
    99  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
   100  		}
   101  
   102  		if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
   103  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
   104  		}
   105  	}
   106  }
   107  
   108  func TestDetectLocalByInterfaceNamePrefix(t *testing.T) {
   109  	cases := []struct {
   110  		ifacePrefix             string
   111  		chain                   string
   112  		args                    []string
   113  		expectedJumpIfOutput    []string
   114  		expectedJumpIfNotOutput []string
   115  	}{
   116  		{
   117  			ifacePrefix:             "eth0",
   118  			expectedJumpIfOutput:    []string{"-i", "eth0+"},
   119  			expectedJumpIfNotOutput: []string{"!", "-i", "eth0+"},
   120  		},
   121  	}
   122  	for _, c := range cases {
   123  		localDetector := NewDetectLocalByInterfaceNamePrefix(c.ifacePrefix)
   124  		if !localDetector.IsImplemented() {
   125  			t.Error("DetectLocalByInterfaceNamePrefix returns false for IsImplemented")
   126  		}
   127  
   128  		ifLocal := localDetector.IfLocal()
   129  		ifNotLocal := localDetector.IfNotLocal()
   130  
   131  		if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
   132  			t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
   133  		}
   134  
   135  		if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
   136  			t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
   137  		}
   138  	}
   139  }