Skip to content

Commit ee0a070

Browse files
authored
Merge pull request kubernetes#110082 from twilight0620/addTest2
add test case TestValidateServiceNodePort for validateServiceNodePort method
2 parents 4a2391c + 62298c0 commit ee0a070

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

cmd/kube-apiserver/app/options/validation_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121
"testing"
2222

23+
utilnet "k8s.io/apimachinery/pkg/util/net"
2324
netutils "k8s.io/utils/net"
2425
)
2526

@@ -124,6 +125,65 @@ func getIPnetFromCIDR(cidr string) *net.IPNet {
124125
return ipnet
125126
}
126127

128+
func TestValidateServiceNodePort(t *testing.T) {
129+
testCases := []struct {
130+
name string
131+
options *ServerRunOptions
132+
expectErrors bool
133+
}{
134+
{
135+
name: "validate port less than 0",
136+
options: makeOptionsWithPort(-1, 30065, 1),
137+
expectErrors: true,
138+
},
139+
{
140+
name: "validate port more than 65535",
141+
options: makeOptionsWithPort(65536, 30065, 1),
142+
expectErrors: true,
143+
},
144+
{
145+
name: "validate port equal 0",
146+
options: makeOptionsWithPort(0, 0, 1),
147+
expectErrors: true,
148+
},
149+
{
150+
name: "validate port less than base",
151+
options: makeOptionsWithPort(30064, 30065, 1),
152+
expectErrors: true,
153+
},
154+
{
155+
name: "validate port minus base more than size",
156+
options: makeOptionsWithPort(30067, 30065, 1),
157+
expectErrors: true,
158+
},
159+
{
160+
name: "validate success",
161+
options: makeOptionsWithPort(30067, 30065, 5),
162+
expectErrors: false,
163+
},
164+
}
165+
166+
for _, tc := range testCases {
167+
t.Run(tc.name, func(t *testing.T) {
168+
err := validateServiceNodePort(tc.options)
169+
if err != nil && !tc.expectErrors {
170+
t.Errorf("expected no errors, error found %+v", err)
171+
}
172+
})
173+
}
174+
}
175+
176+
func makeOptionsWithPort(kubernetesServiceNodePort int, base int, size int) *ServerRunOptions {
177+
var portRange = utilnet.PortRange{
178+
Base: base,
179+
Size: size,
180+
}
181+
return &ServerRunOptions{
182+
ServiceNodePortRange: portRange,
183+
KubernetesServiceNodePort: kubernetesServiceNodePort,
184+
}
185+
}
186+
127187
func TestValidateMaxCIDRRange(t *testing.T) {
128188
testCases := []struct {
129189
// tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage

0 commit comments

Comments
 (0)