Skip to content

Commit de15774

Browse files
committed
kube-proxy unit test FilterIncorrectIPVersion
Add an unit test to the kube-proxy FilterIncorrectIPVersion function
1 parent 4a55a55 commit de15774

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/proxy/util/utils_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,66 @@ func TestShuffleStrings(t *testing.T) {
537537
}
538538
}
539539
}
540+
541+
func TestFilterIncorrectIPVersion(t *testing.T) {
542+
testCases := []struct {
543+
desc string
544+
ipString []string
545+
wantIPv6 bool
546+
expectCorrect []string
547+
expectIncorrect []string
548+
}{
549+
{
550+
desc: "want IPv6 and receive IPv4 and IPv6",
551+
ipString: []string{"192.168.200.2", "192.1.34.23", "fd00:20::1", "2001:db9::3"},
552+
wantIPv6: true,
553+
expectCorrect: []string{"fd00:20::1", "2001:db9::3"},
554+
expectIncorrect: []string{"192.168.200.2", "192.1.34.23"},
555+
},
556+
{
557+
desc: "want IPv4 and receive IPv4 and IPv6",
558+
ipString: []string{"192.168.200.2", "192.1.34.23", "fd00:20::1", "2001:db9::3"},
559+
wantIPv6: false,
560+
expectCorrect: []string{"192.168.200.2", "192.1.34.23"},
561+
expectIncorrect: []string{"fd00:20::1", "2001:db9::3"},
562+
},
563+
{
564+
desc: "want IPv4 and receive IPv4 only",
565+
ipString: []string{"192.168.200.2", "192.1.34.23"},
566+
wantIPv6: false,
567+
expectCorrect: []string{"192.168.200.2", "192.1.34.23"},
568+
expectIncorrect: nil,
569+
},
570+
{
571+
desc: "want IPv6 and receive IPv4 only",
572+
ipString: []string{"192.168.200.2", "192.1.34.23"},
573+
wantIPv6: true,
574+
expectCorrect: nil,
575+
expectIncorrect: []string{"192.168.200.2", "192.1.34.23"},
576+
},
577+
{
578+
desc: "want IPv4 and receive IPv6 only",
579+
ipString: []string{"fd00:20::1", "2001:db9::3"},
580+
wantIPv6: false,
581+
expectCorrect: nil,
582+
expectIncorrect: []string{"fd00:20::1", "2001:db9::3"},
583+
},
584+
{
585+
desc: "want IPv6 and receive IPv6 only",
586+
ipString: []string{"fd00:20::1", "2001:db9::3"},
587+
wantIPv6: true,
588+
expectCorrect: []string{"fd00:20::1", "2001:db9::3"},
589+
expectIncorrect: nil,
590+
},
591+
}
592+
593+
for i := range testCases {
594+
correct, incorrect := FilterIncorrectIPVersion(testCases[i].ipString, testCases[i].wantIPv6)
595+
if !reflect.DeepEqual(testCases[i].expectCorrect, correct) {
596+
t.Errorf("Test %v failed: expected %v, got %v", testCases[i].desc, testCases[i].expectCorrect, correct)
597+
}
598+
if !reflect.DeepEqual(testCases[i].expectIncorrect, incorrect) {
599+
t.Errorf("Test %v failed: expected %v, got %v", testCases[i].desc, testCases[i].expectIncorrect, incorrect)
600+
}
601+
}
602+
}

0 commit comments

Comments
 (0)