Move a kube-proxy unit test to the correct file

This commit is contained in:
Dan Winship 2023-04-06 17:44:05 -04:00
parent 5a5ebfd88b
commit b69b34bec6
2 changed files with 139 additions and 138 deletions

View File

@ -21,7 +21,6 @@ package app
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -107,111 +106,6 @@ func Test_platformApplyDefaults(t *testing.T) {
} }
} }
func Test_detectNodeIP(t *testing.T) {
cases := []struct {
name string
nodeInfo *v1.Node
hostname string
bindAddress string
expectedIP net.IP
}{
{
name: "Bind address IPv4 unicast address and no Node object",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "10.0.0.1",
expectedIP: netutils.ParseIPSloppy("10.0.0.1"),
},
{
name: "Bind address IPv6 unicast address and no Node object",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "fd00:4321::2",
expectedIP: netutils.ParseIPSloppy("fd00:4321::2"),
},
{
name: "No Valid IP found",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "",
expectedIP: netutils.ParseIPSloppy("127.0.0.1"),
},
// Disabled because the GetNodeIP method has a backoff retry mechanism
// and the test takes more than 30 seconds
// ok k8s.io/kubernetes/cmd/kube-proxy/app 34.136s
// {
// name: "No Valid IP found and unspecified bind address",
// nodeInfo: makeNodeWithAddresses("", "", ""),
// hostname: "fakeHost",
// bindAddress: "0.0.0.0",
// expectedIP: net.IP{127,0,0,1),
// },
{
name: "Bind address 0.0.0.0 and node with IPv4 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("192.168.1.1"),
},
{
name: "Bind address :: and node with IPv4 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("192.168.1.1"),
},
{
name: "Bind address 0.0.0.0 and node with IPv6 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("fd00:1234::1"),
},
{
name: "Bind address :: and node with IPv6 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("fd00:1234::1"),
},
{
name: "Bind address 0.0.0.0 and node with only IPv4 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("90.90.90.90"),
},
{
name: "Bind address :: and node with only IPv4 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("90.90.90.90"),
},
{
name: "Bind address 0.0.0.0 and node with only IPv6 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("2001:db8::2"),
},
{
name: "Bind address :: and node with only IPv6 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("2001:db8::2"),
},
}
for _, c := range cases {
client := clientsetfake.NewSimpleClientset(c.nodeInfo)
ip := detectNodeIP(client, c.hostname, c.bindAddress)
if !ip.Equal(c.expectedIP) {
t.Errorf("Case[%s] Expected IP %q got %q", c.name, c.expectedIP, ip)
}
}
}
func Test_getLocalDetector(t *testing.T) { func Test_getLocalDetector(t *testing.T) {
cases := []struct { cases := []struct {
mode proxyconfigapi.LocalMode mode proxyconfigapi.LocalMode
@ -502,35 +396,6 @@ func Test_getDualStackLocalDetectorTuple(t *testing.T) {
} }
} }
func makeNodeWithAddresses(name, internal, external string) *v1.Node {
if name == "" {
return &v1.Node{}
}
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{},
},
}
if internal != "" {
node.Status.Addresses = append(node.Status.Addresses,
v1.NodeAddress{Type: v1.NodeInternalIP, Address: internal},
)
}
if external != "" {
node.Status.Addresses = append(node.Status.Addresses,
v1.NodeAddress{Type: v1.NodeExternalIP, Address: external},
)
}
return node
}
func makeNodeWithPodCIDRs(cidrs ...string) *v1.Node { func makeNodeWithPodCIDRs(cidrs ...string) *v1.Node {
if len(cidrs) == 0 { if len(cidrs) == 0 {
return &v1.Node{} return &v1.Node{}

View File

@ -19,19 +19,21 @@ package app
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"reflect" "reflect"
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"k8s.io/utils/pointer" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientsetfake "k8s.io/client-go/kubernetes/fake"
componentbaseconfig "k8s.io/component-base/config" componentbaseconfig "k8s.io/component-base/config"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config" kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
netutils "k8s.io/utils/net"
"k8s.io/utils/pointer"
) )
// TestLoadConfig tests proper operation of loadConfig() // TestLoadConfig tests proper operation of loadConfig()
@ -464,3 +466,137 @@ func TestAddressFromDeprecatedFlags(t *testing.T) {
} }
} }
func makeNodeWithAddresses(name, internal, external string) *v1.Node {
if name == "" {
return &v1.Node{}
}
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{},
},
}
if internal != "" {
node.Status.Addresses = append(node.Status.Addresses,
v1.NodeAddress{Type: v1.NodeInternalIP, Address: internal},
)
}
if external != "" {
node.Status.Addresses = append(node.Status.Addresses,
v1.NodeAddress{Type: v1.NodeExternalIP, Address: external},
)
}
return node
}
func Test_detectNodeIP(t *testing.T) {
cases := []struct {
name string
nodeInfo *v1.Node
hostname string
bindAddress string
expectedIP net.IP
}{
{
name: "Bind address IPv4 unicast address and no Node object",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "10.0.0.1",
expectedIP: netutils.ParseIPSloppy("10.0.0.1"),
},
{
name: "Bind address IPv6 unicast address and no Node object",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "fd00:4321::2",
expectedIP: netutils.ParseIPSloppy("fd00:4321::2"),
},
{
name: "No Valid IP found",
nodeInfo: makeNodeWithAddresses("", "", ""),
hostname: "fakeHost",
bindAddress: "",
expectedIP: netutils.ParseIPSloppy("127.0.0.1"),
},
// Disabled because the GetNodeIP method has a backoff retry mechanism
// and the test takes more than 30 seconds
// ok k8s.io/kubernetes/cmd/kube-proxy/app 34.136s
// {
// name: "No Valid IP found and unspecified bind address",
// nodeInfo: makeNodeWithAddresses("", "", ""),
// hostname: "fakeHost",
// bindAddress: "0.0.0.0",
// expectedIP: net.IP{127,0,0,1),
// },
{
name: "Bind address 0.0.0.0 and node with IPv4 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("192.168.1.1"),
},
{
name: "Bind address :: and node with IPv4 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("192.168.1.1"),
},
{
name: "Bind address 0.0.0.0 and node with IPv6 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("fd00:1234::1"),
},
{
name: "Bind address :: and node with IPv6 InternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("fd00:1234::1"),
},
{
name: "Bind address 0.0.0.0 and node with only IPv4 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("90.90.90.90"),
},
{
name: "Bind address :: and node with only IPv4 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("90.90.90.90"),
},
{
name: "Bind address 0.0.0.0 and node with only IPv6 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "0.0.0.0",
expectedIP: netutils.ParseIPSloppy("2001:db8::2"),
},
{
name: "Bind address :: and node with only IPv6 ExternalIP set",
nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"),
hostname: "fakeHost",
bindAddress: "::",
expectedIP: netutils.ParseIPSloppy("2001:db8::2"),
},
}
for _, c := range cases {
client := clientsetfake.NewSimpleClientset(c.nodeInfo)
ip := detectNodeIP(client, c.hostname, c.bindAddress)
if !ip.Equal(c.expectedIP) {
t.Errorf("Case[%s] Expected IP %q got %q", c.name, c.expectedIP, ip)
}
}
}