add test case TestValidateServiceNodePort for validateServiceNodePort method

This commit is contained in:
twilight0620 2022-05-17 14:32:06 +08:00
parent b74d023e70
commit 62298c0493

View File

@ -20,6 +20,7 @@ import (
"net" "net"
"testing" "testing"
utilnet "k8s.io/apimachinery/pkg/util/net"
netutils "k8s.io/utils/net" netutils "k8s.io/utils/net"
) )
@ -124,6 +125,65 @@ func getIPnetFromCIDR(cidr string) *net.IPNet {
return ipnet return ipnet
} }
func TestValidateServiceNodePort(t *testing.T) {
testCases := []struct {
name string
options *ServerRunOptions
expectErrors bool
}{
{
name: "validate port less than 0",
options: makeOptionsWithPort(-1, 30065, 1),
expectErrors: true,
},
{
name: "validate port more than 65535",
options: makeOptionsWithPort(65536, 30065, 1),
expectErrors: true,
},
{
name: "validate port equal 0",
options: makeOptionsWithPort(0, 0, 1),
expectErrors: true,
},
{
name: "validate port less than base",
options: makeOptionsWithPort(30064, 30065, 1),
expectErrors: true,
},
{
name: "validate port minus base more than size",
options: makeOptionsWithPort(30067, 30065, 1),
expectErrors: true,
},
{
name: "validate success",
options: makeOptionsWithPort(30067, 30065, 5),
expectErrors: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := validateServiceNodePort(tc.options)
if err != nil && !tc.expectErrors {
t.Errorf("expected no errors, error found %+v", err)
}
})
}
}
func makeOptionsWithPort(kubernetesServiceNodePort int, base int, size int) *ServerRunOptions {
var portRange = utilnet.PortRange{
Base: base,
Size: size,
}
return &ServerRunOptions{
ServiceNodePortRange: portRange,
KubernetesServiceNodePort: kubernetesServiceNodePort,
}
}
func TestValidateMaxCIDRRange(t *testing.T) { func TestValidateMaxCIDRRange(t *testing.T) {
testCases := []struct { testCases := []struct {
// tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage // tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage