From 44285a16ecc6e43c2121756bd8919ccf9d467396 Mon Sep 17 00:00:00 2001 From: ZhangYu Date: Wed, 16 Nov 2022 14:13:39 +0800 Subject: [PATCH] Add test for cmd/kubeadm/app/constants/constants.go Add test for cmd/kubeadm/app/constants/constants.go --- cmd/kubeadm/app/constants/constants_test.go | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/cmd/kubeadm/app/constants/constants_test.go b/cmd/kubeadm/app/constants/constants_test.go index ae991172e46..3b1dabb3912 100644 --- a/cmd/kubeadm/app/constants/constants_test.go +++ b/cmd/kubeadm/app/constants/constants_test.go @@ -350,3 +350,64 @@ func TestGetAPIServerVirtualIP(t *testing.T) { }) } } + +func TestGetDNSIP(t *testing.T) { + tests := []struct { + name string + svcSubnetList string + expected string + expectedError bool + }{ + { + name: "valid IPv4 range from single-stack", + svcSubnetList: "192.168.10.0/24", + expected: "192.168.10.10", + expectedError: false, + }, + { + name: "valid IPv6 range from single-stack", + svcSubnetList: "fd03::/112", + expected: "fd03::a", + expectedError: false, + }, + { + name: "valid ranges from dual-stack", + svcSubnetList: "192.168.10.0/24,fd03::/112", + expected: "192.168.10.10", + expectedError: false, + }, + { + name: "valid ranges from dual-stack", + svcSubnetList: "fd03::/112,192.168.10.0/24", + expected: "fd03::a", + expectedError: false, + }, + { + name: "invalid subnet range from dual-stack", + svcSubnetList: "192.168.10.0/24,fd03:x::/112", + expected: "", + expectedError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual, actualError := GetDNSIP(tt.svcSubnetList) + if tt.expectedError { + if actualError == nil { + t.Errorf("failed GetDNSIP:\n\texpected error, but got no error") + } + } else if !tt.expectedError && actualError != nil { + t.Errorf("failed GetDNSIP:\n\texpected no error, but got: %v", actualError) + } else { + if actual.String() != tt.expected { + t.Errorf( + "failed GetDNSIP:\n\texpected: %s\n\t actual: %s", + tt.expected, + actual.String(), + ) + } + } + }) + } +}