diff --git a/pkg/kubelet/kubelet_network_test.go b/pkg/kubelet/kubelet_network_test.go index edc91af2b23..d9c3453b1a8 100644 --- a/pkg/kubelet/kubelet_network_test.go +++ b/pkg/kubelet/kubelet_network_test.go @@ -17,8 +17,6 @@ limitations under the License. package kubelet import ( - "fmt" - "net" "testing" "github.com/stretchr/testify/assert" @@ -94,97 +92,6 @@ func TestNoOpHostSupportsLegacyFeatures(t *testing.T) { } } -func TestNodeIPParam(t *testing.T) { - type test struct { - nodeIP string - success bool - testName string - } - tests := []test{ - { - nodeIP: "", - success: false, - testName: "IP not set", - }, - { - nodeIP: "127.0.0.1", - success: false, - testName: "IPv4 loopback address", - }, - { - nodeIP: "::1", - success: false, - testName: "IPv6 loopback address", - }, - { - nodeIP: "224.0.0.1", - success: false, - testName: "multicast IPv4 address", - }, - { - nodeIP: "ff00::1", - success: false, - testName: "multicast IPv6 address", - }, - { - nodeIP: "169.254.0.1", - success: false, - testName: "IPv4 link-local unicast address", - }, - { - nodeIP: "fe80::0202:b3ff:fe1e:8329", - success: false, - testName: "IPv6 link-local unicast address", - }, - { - nodeIP: "0.0.0.0", - success: false, - testName: "Unspecified IPv4 address", - }, - { - nodeIP: "::", - success: false, - testName: "Unspecified IPv6 address", - }, - { - nodeIP: "1.2.3.4", - success: false, - testName: "IPv4 address that doesn't belong to host", - }, - } - addrs, err := net.InterfaceAddrs() - if err != nil { - assert.Error(t, err, fmt.Sprintf( - "Unable to obtain a list of the node's unicast interface addresses.")) - } - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - if ip.IsLoopback() || ip.IsLinkLocalUnicast() { - break - } - successTest := test{ - nodeIP: ip.String(), - success: true, - testName: fmt.Sprintf("Success test case for address %s", ip.String()), - } - tests = append(tests, successTest) - } - for _, test := range tests { - err := validateNodeIP(net.ParseIP(test.nodeIP)) - if test.success { - assert.NoError(t, err, "test %s", test.testName) - } else { - assert.Error(t, err, fmt.Sprintf("test %s", test.testName)) - } - } -} - func TestGetIPTablesMark(t *testing.T) { tests := []struct { bit int diff --git a/pkg/kubelet/kubelet_node_status_test.go b/pkg/kubelet/kubelet_node_status_test.go index af3dc0d6568..639a6036197 100644 --- a/pkg/kubelet/kubelet_node_status_test.go +++ b/pkg/kubelet/kubelet_node_status_test.go @@ -1416,3 +1416,94 @@ func TestUpdateDefaultLabels(t *testing.T) { assert.Equal(t, tc.finalLabels, tc.existingNode.Labels, tc.name) } } + +func TestValidateNodeIPParam(t *testing.T) { + type test struct { + nodeIP string + success bool + testName string + } + tests := []test{ + { + nodeIP: "", + success: false, + testName: "IP not set", + }, + { + nodeIP: "127.0.0.1", + success: false, + testName: "IPv4 loopback address", + }, + { + nodeIP: "::1", + success: false, + testName: "IPv6 loopback address", + }, + { + nodeIP: "224.0.0.1", + success: false, + testName: "multicast IPv4 address", + }, + { + nodeIP: "ff00::1", + success: false, + testName: "multicast IPv6 address", + }, + { + nodeIP: "169.254.0.1", + success: false, + testName: "IPv4 link-local unicast address", + }, + { + nodeIP: "fe80::0202:b3ff:fe1e:8329", + success: false, + testName: "IPv6 link-local unicast address", + }, + { + nodeIP: "0.0.0.0", + success: false, + testName: "Unspecified IPv4 address", + }, + { + nodeIP: "::", + success: false, + testName: "Unspecified IPv6 address", + }, + { + nodeIP: "1.2.3.4", + success: false, + testName: "IPv4 address that doesn't belong to host", + }, + } + addrs, err := net.InterfaceAddrs() + if err != nil { + assert.Error(t, err, fmt.Sprintf( + "Unable to obtain a list of the node's unicast interface addresses.")) + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if ip.IsLoopback() || ip.IsLinkLocalUnicast() { + break + } + successTest := test{ + nodeIP: ip.String(), + success: true, + testName: fmt.Sprintf("Success test case for address %s", ip.String()), + } + tests = append(tests, successTest) + } + for _, test := range tests { + err := validateNodeIP(net.ParseIP(test.nodeIP)) + if test.success { + assert.NoError(t, err, "test %s", test.testName) + } else { + assert.Error(t, err, fmt.Sprintf("test %s", test.testName)) + } + } +}