From 540ec51196de676e72500e5dd0a67e44faa894e6 Mon Sep 17 00:00:00 2001 From: zhoumingcheng Date: Tue, 28 Jun 2022 16:07:30 +0800 Subject: [PATCH 1/3] add unit test coverage for pkg/util/node Signed-off-by: zhoumingcheng --- pkg/util/node/node_test.go | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/pkg/util/node/node_test.go b/pkg/util/node/node_test.go index 0abaee447f0..da7aa3dce06 100644 --- a/pkg/util/node/node_test.go +++ b/pkg/util/node/node_test.go @@ -17,12 +17,16 @@ limitations under the License. package node import ( + "context" "net" "reflect" "testing" + "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubeclientfake "k8s.io/client-go/kubernetes/fake" netutils "k8s.io/utils/net" ) @@ -259,3 +263,106 @@ func TestGetHostname(t *testing.T) { } } + +func TestGetNodeIP(t *testing.T) { + + testCases := []struct { + name string + Node *v1.Node + expect net.IP + }{ + { + name: "", + expect: nil, + }, + { + name: "node1", + Node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{}, + }, + expect: nil, + }, + { + name: "node1", + Node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, + {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, + }}, + }, + expect: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 1, 2, 3, 4}, + }, + } + for _, test := range testCases { + kubeClientSet := kubeclientfake.NewSimpleClientset() + if test.Node != nil { + if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.Node, metav1.CreateOptions{}); err != nil { + t.Error("create node error") + } + } + result := GetNodeIP(kubeClientSet, test.name) + assert.Equal(t, test.expect, result) + } +} + +func TestIsNodeReady(t *testing.T) { + testCases := []struct { + Node *v1.Node + expect bool + }{ + { + Node: &v1.Node{ + Status: v1.NodeStatus{ + Conditions: []v1.NodeCondition{ + { + Type: v1.NodeReady, + Status: v1.ConditionTrue, + }, + }, + }, + }, + expect: true, + }, + { + Node: &v1.Node{ + Status: v1.NodeStatus{ + Conditions: []v1.NodeCondition{ + { + Type: v1.NodeReady, + Status: v1.ConditionFalse, + }, + }, + }, + }, + expect: false, + }, + { + Node: &v1.Node{ + Status: v1.NodeStatus{ + Conditions: []v1.NodeCondition{ + { + Type: v1.NodeMemoryPressure, + Status: v1.ConditionFalse, + }, + }, + }, + }, + expect: false, + }, + } + for _, test := range testCases { + result := IsNodeReady(test.Node) + assert.Equal(t, test.expect, result) + } +} From 8dfb7af374e1b63789990dae211877e6f2001e67 Mon Sep 17 00:00:00 2001 From: zhoumingcheng Date: Tue, 28 Jun 2022 17:40:04 +0800 Subject: [PATCH 2/3] update unit test for pkg/util/node Remove duplicate testcases for func TestGetNodeHostIPs Signed-off-by: zhoumingcheng --- pkg/util/node/node_test.go | 165 +++++++++++++++++++++++++------------ 1 file changed, 114 insertions(+), 51 deletions(-) diff --git a/pkg/util/node/node_test.go b/pkg/util/node/node_test.go index da7aa3dce06..a030317c670 100644 --- a/pkg/util/node/node_test.go +++ b/pkg/util/node/node_test.go @@ -151,17 +151,6 @@ func TestGetNodeHostIPs(t *testing.T) { }, expectIPs: []net.IP{netutils.ParseIPSloppy("1.2.3.4"), netutils.ParseIPSloppy("a:b::c:d")}, }, - { - name: "dual-stack node", - addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - }, - expectIPs: []net.IP{netutils.ParseIPSloppy("1.2.3.4"), netutils.ParseIPSloppy("a:b::c:d")}, - }, { name: "dual-stack node, different order", addresses: []v1.NodeAddress{ @@ -173,27 +162,6 @@ func TestGetNodeHostIPs(t *testing.T) { }, expectIPs: []net.IP{netutils.ParseIPSloppy("1.2.3.4"), netutils.ParseIPSloppy("a:b::c:d")}, }, - { - name: "dual-stack node, different order", - addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - }, - expectIPs: []net.IP{netutils.ParseIPSloppy("1.2.3.4"), netutils.ParseIPSloppy("a:b::c:d")}, - }, - { - name: "dual-stack node, IPv6-first, no internal IPv4", - addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - }, - expectIPs: []net.IP{netutils.ParseIPSloppy("a:b::c:d"), netutils.ParseIPSloppy("4.3.2.1")}, - }, { name: "dual-stack node, IPv6-first, no internal IPv4, dual-stack cluster", addresses: []v1.NodeAddress{ @@ -267,17 +235,20 @@ func TestGetHostname(t *testing.T) { func TestGetNodeIP(t *testing.T) { testCases := []struct { - name string - Node *v1.Node - expect net.IP + name string + nodeName string + node *v1.Node + expect net.IP }{ { - name: "", - expect: nil, + name: "failed to get nodeIP,can't find the node", + nodeName: "", + expect: nil, }, { - name: "node1", - Node: &v1.Node{ + name: "failed to get nodeIP", + nodeName: "node1", + node: &v1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: "node1", }, @@ -287,8 +258,56 @@ func TestGetNodeIP(t *testing.T) { expect: nil, }, { - name: "node1", - Node: &v1.Node{ + name: "IPv4-only, simple", + nodeName: "node1", + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + }}, + }, + expect: netutils.ParseIPSloppy("1.2.3.4"), + }, + { + name: "IPv4-only, external-first", + nodeName: "node1", + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, + }}, + }, + expect: netutils.ParseIPSloppy("1.2.3.4"), + }, + { + name: "IPv4-only, no internal", + nodeName: "node1", + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + }}, + }, + expect: netutils.ParseIPSloppy("4.3.2.1"), + }, + { + name: "dual-stack node", + nodeName: "node1", + node: &v1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: "node1", }, @@ -301,27 +320,67 @@ func TestGetNodeIP(t *testing.T) { {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, }}, }, - expect: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 1, 2, 3, 4}, + expect: netutils.ParseIPSloppy("1.2.3.4"), + }, + { + name: "dual-stack node, different order", + nodeName: "node1", + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, + {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, + }}, + }, + expect: netutils.ParseIPSloppy("1.2.3.4"), + }, + { + name: "dual-stack node, IPv6-first, no internal IPv4", + nodeName: "node1", + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ + {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, + {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + }}, + }, + expect: netutils.ParseIPSloppy("a:b::c:d"), }, } for _, test := range testCases { - kubeClientSet := kubeclientfake.NewSimpleClientset() - if test.Node != nil { - if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.Node, metav1.CreateOptions{}); err != nil { - t.Error("create node error") + t.Run(test.name, func(t *testing.T) { + kubeClientSet := kubeclientfake.NewSimpleClientset() + if test.node != nil { + if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.node, metav1.CreateOptions{}); err != nil { + t.Error("create node error") + } } - } - result := GetNodeIP(kubeClientSet, test.name) - assert.Equal(t, test.expect, result) + result := GetNodeIP(kubeClientSet, test.nodeName) + assert.Equal(t, test.expect, result) + }) + } } func TestIsNodeReady(t *testing.T) { testCases := []struct { + name string Node *v1.Node expect bool }{ { + name: "case that returns true", Node: &v1.Node{ Status: v1.NodeStatus{ Conditions: []v1.NodeCondition{ @@ -335,6 +394,7 @@ func TestIsNodeReady(t *testing.T) { expect: true, }, { + name: "case that returns false", Node: &v1.Node{ Status: v1.NodeStatus{ Conditions: []v1.NodeCondition{ @@ -348,6 +408,7 @@ func TestIsNodeReady(t *testing.T) { expect: false, }, { + name: "case that returns false", Node: &v1.Node{ Status: v1.NodeStatus{ Conditions: []v1.NodeCondition{ @@ -362,7 +423,9 @@ func TestIsNodeReady(t *testing.T) { }, } for _, test := range testCases { - result := IsNodeReady(test.Node) - assert.Equal(t, test.expect, result) + t.Run(test.name, func(t *testing.T) { + result := IsNodeReady(test.Node) + assert.Equal(t, test.expect, result) + }) } } From 673930e3848b1814d82278a970af0b0164328950 Mon Sep 17 00:00:00 2001 From: zhoumingcheng Date: Thu, 7 Jul 2022 10:45:45 +0800 Subject: [PATCH 3/3] update test func for pkg/util/node Signed-off-by: zhoumingcheng --- pkg/util/node/node_test.go | 143 ------------------------------------- 1 file changed, 143 deletions(-) diff --git a/pkg/util/node/node_test.go b/pkg/util/node/node_test.go index a030317c670..2edf39c1c2d 100644 --- a/pkg/util/node/node_test.go +++ b/pkg/util/node/node_test.go @@ -17,7 +17,6 @@ limitations under the License. package node import ( - "context" "net" "reflect" "testing" @@ -26,7 +25,6 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kubeclientfake "k8s.io/client-go/kubernetes/fake" netutils "k8s.io/utils/net" ) @@ -232,147 +230,6 @@ func TestGetHostname(t *testing.T) { } } -func TestGetNodeIP(t *testing.T) { - - testCases := []struct { - name string - nodeName string - node *v1.Node - expect net.IP - }{ - { - name: "failed to get nodeIP,can't find the node", - nodeName: "", - expect: nil, - }, - { - name: "failed to get nodeIP", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{}, - }, - expect: nil, - }, - { - name: "IPv4-only, simple", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - }}, - }, - expect: netutils.ParseIPSloppy("1.2.3.4"), - }, - { - name: "IPv4-only, external-first", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - }}, - }, - expect: netutils.ParseIPSloppy("1.2.3.4"), - }, - { - name: "IPv4-only, no internal", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - }}, - }, - expect: netutils.ParseIPSloppy("4.3.2.1"), - }, - { - name: "dual-stack node", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - }}, - }, - expect: netutils.ParseIPSloppy("1.2.3.4"), - }, - { - name: "dual-stack node, different order", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "1.2.3.4"}, - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - }}, - }, - expect: netutils.ParseIPSloppy("1.2.3.4"), - }, - { - name: "dual-stack node, IPv6-first, no internal IPv4", - nodeName: "node1", - node: &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node1", - }, - Spec: v1.NodeSpec{}, - Status: v1.NodeStatus{Addresses: []v1.NodeAddress{ - {Type: v1.NodeInternalIP, Address: "a:b::c:d"}, - {Type: v1.NodeExternalIP, Address: "d:c::b:a"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, - {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, - }}, - }, - expect: netutils.ParseIPSloppy("a:b::c:d"), - }, - } - for _, test := range testCases { - t.Run(test.name, func(t *testing.T) { - kubeClientSet := kubeclientfake.NewSimpleClientset() - if test.node != nil { - if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.node, metav1.CreateOptions{}); err != nil { - t.Error("create node error") - } - } - result := GetNodeIP(kubeClientSet, test.nodeName) - assert.Equal(t, test.expect, result) - }) - - } -} - func TestIsNodeReady(t *testing.T) { testCases := []struct { name string