mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
update unit test for pkg/util/node
Remove duplicate testcases for func TestGetNodeHostIPs Signed-off-by: zhoumingcheng <zhoumingcheng@beyondcent.com>
This commit is contained in:
parent
540ec51196
commit
8dfb7af374
@ -151,17 +151,6 @@ func TestGetNodeHostIPs(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectIPs: []net.IP{netutils.ParseIPSloppy("1.2.3.4"), netutils.ParseIPSloppy("a:b::c:d")},
|
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",
|
name: "dual-stack node, different order",
|
||||||
addresses: []v1.NodeAddress{
|
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")},
|
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",
|
name: "dual-stack node, IPv6-first, no internal IPv4, dual-stack cluster",
|
||||||
addresses: []v1.NodeAddress{
|
addresses: []v1.NodeAddress{
|
||||||
@ -267,17 +235,20 @@ func TestGetHostname(t *testing.T) {
|
|||||||
func TestGetNodeIP(t *testing.T) {
|
func TestGetNodeIP(t *testing.T) {
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
Node *v1.Node
|
nodeName string
|
||||||
expect net.IP
|
node *v1.Node
|
||||||
|
expect net.IP
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "",
|
name: "failed to get nodeIP,can't find the node",
|
||||||
expect: nil,
|
nodeName: "",
|
||||||
|
expect: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "node1",
|
name: "failed to get nodeIP",
|
||||||
Node: &v1.Node{
|
nodeName: "node1",
|
||||||
|
node: &v1.Node{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "node1",
|
Name: "node1",
|
||||||
},
|
},
|
||||||
@ -287,8 +258,56 @@ func TestGetNodeIP(t *testing.T) {
|
|||||||
expect: nil,
|
expect: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "node1",
|
name: "IPv4-only, simple",
|
||||||
Node: &v1.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"},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
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{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "node1",
|
Name: "node1",
|
||||||
},
|
},
|
||||||
@ -301,27 +320,67 @@ func TestGetNodeIP(t *testing.T) {
|
|||||||
{Type: v1.NodeExternalIP, Address: "d:c::b:a"},
|
{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 {
|
for _, test := range testCases {
|
||||||
kubeClientSet := kubeclientfake.NewSimpleClientset()
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if test.Node != nil {
|
kubeClientSet := kubeclientfake.NewSimpleClientset()
|
||||||
if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.Node, metav1.CreateOptions{}); err != nil {
|
if test.node != nil {
|
||||||
t.Error("create node error")
|
if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.node, metav1.CreateOptions{}); err != nil {
|
||||||
|
t.Error("create node error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
result := GetNodeIP(kubeClientSet, test.nodeName)
|
||||||
result := GetNodeIP(kubeClientSet, test.name)
|
assert.Equal(t, test.expect, result)
|
||||||
assert.Equal(t, test.expect, result)
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsNodeReady(t *testing.T) {
|
func TestIsNodeReady(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
name string
|
||||||
Node *v1.Node
|
Node *v1.Node
|
||||||
expect bool
|
expect bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
name: "case that returns true",
|
||||||
Node: &v1.Node{
|
Node: &v1.Node{
|
||||||
Status: v1.NodeStatus{
|
Status: v1.NodeStatus{
|
||||||
Conditions: []v1.NodeCondition{
|
Conditions: []v1.NodeCondition{
|
||||||
@ -335,6 +394,7 @@ func TestIsNodeReady(t *testing.T) {
|
|||||||
expect: true,
|
expect: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "case that returns false",
|
||||||
Node: &v1.Node{
|
Node: &v1.Node{
|
||||||
Status: v1.NodeStatus{
|
Status: v1.NodeStatus{
|
||||||
Conditions: []v1.NodeCondition{
|
Conditions: []v1.NodeCondition{
|
||||||
@ -348,6 +408,7 @@ func TestIsNodeReady(t *testing.T) {
|
|||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "case that returns false",
|
||||||
Node: &v1.Node{
|
Node: &v1.Node{
|
||||||
Status: v1.NodeStatus{
|
Status: v1.NodeStatus{
|
||||||
Conditions: []v1.NodeCondition{
|
Conditions: []v1.NodeCondition{
|
||||||
@ -362,7 +423,9 @@ func TestIsNodeReady(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
result := IsNodeReady(test.Node)
|
t.Run(test.name, func(t *testing.T) {
|
||||||
assert.Equal(t, test.expect, result)
|
result := IsNodeReady(test.Node)
|
||||||
|
assert.Equal(t, test.expect, result)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user