Skip to content

Commit

Permalink
Merge pull request kubernetes#110825 from zhoumingcheng/master-unit-v4
Browse files Browse the repository at this point in the history
add unit test coverage for pkg/util/node and Remove duplicate testcases for func TestGetNodeHostIPs
  • Loading branch information
k8s-ci-robot authored Jul 11, 2022
2 parents b0d5769 + 673930e commit a3f7776
Showing 1 changed file with 59 additions and 32 deletions.
91 changes: 59 additions & 32 deletions pkg/util/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
netutils "k8s.io/utils/net"
Expand Down Expand Up @@ -147,17 +149,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{
Expand All @@ -169,27 +160,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{
Expand Down Expand Up @@ -259,3 +229,60 @@ func TestGetHostname(t *testing.T) {

}
}

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{
{
Type: v1.NodeReady,
Status: v1.ConditionTrue,
},
},
},
},
expect: true,
},
{
name: "case that returns false",
Node: &v1.Node{
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionFalse,
},
},
},
},
expect: false,
},
{
name: "case that returns false",
Node: &v1.Node{
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeMemoryPressure,
Status: v1.ConditionFalse,
},
},
},
},
expect: false,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
result := IsNodeReady(test.Node)
assert.Equal(t, test.expect, result)
})
}
}

0 comments on commit a3f7776

Please sign in to comment.