k8s.io/kubernetes@v1.29.3/pkg/proxy/ipvs/testing/fake_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 testing
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  	"k8s.io/kubernetes/pkg/proxy/ipvs"
    24  )
    25  
    26  // (I am unsure if this test has any value since it only tests the fake implementation)
    27  func TestSetGetLocalAddresses(t *testing.T) {
    28  	fake := NewFakeNetlinkHandle(false)
    29  	_ = ipvs.NetLinkHandle(fake) // Ensure that the interface is honored
    30  	fake.SetLocalAddresses("eth0", "1.2.3.4")
    31  	var expected, addr sets.Set[string]
    32  	expected = sets.New("1.2.3.4")
    33  	addr, _ = fake.GetLocalAddresses("eth0")
    34  	if !addr.Equal(expected) {
    35  		t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
    36  	}
    37  	addr, _ = fake.GetAllLocalAddresses()
    38  	if !addr.Equal(expected) {
    39  		t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
    40  	}
    41  	fake.SetLocalAddresses("lo", "127.0.0.1")
    42  	expected = nil
    43  	addr, _ = fake.GetLocalAddresses("lo")
    44  	if !addr.Equal(expected) {
    45  		t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
    46  	}
    47  	fake.SetLocalAddresses("kube-ipvs0", "1.2.3.4", "4.3.2.1")
    48  	addr, _ = fake.GetAllLocalAddresses()
    49  	expected = sets.New("1.2.3.4", "4.3.2.1")
    50  	if !addr.Equal(expected) {
    51  		t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
    52  	}
    53  	addr, _ = fake.GetAllLocalAddressesExcept("kube-ipvs0")
    54  	expected = sets.New("1.2.3.4")
    55  	if !addr.Equal(expected) {
    56  		t.Errorf("Unexpected mismatch, expected: %v, got: %v", expected, addr)
    57  	}
    58  }