Skip to content

Commit

Permalink
Merge pull request #110082 from twilight0620/addTest2
Browse files Browse the repository at this point in the history
add test case TestValidateServiceNodePort for validateServiceNodePort method
  • Loading branch information
k8s-ci-robot committed May 26, 2022
2 parents 4a2391c + 62298c0 commit ee0a070
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions cmd/kube-apiserver/app/options/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"testing"

utilnet "k8s.io/apimachinery/pkg/util/net"
netutils "k8s.io/utils/net"
)

Expand Down Expand Up @@ -124,6 +125,65 @@ func getIPnetFromCIDR(cidr string) *net.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) {
testCases := []struct {
// tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage
Expand Down

0 comments on commit ee0a070

Please sign in to comment.