@@ -20,6 +20,7 @@ import (
20
20
"net"
21
21
"testing"
22
22
23
+ utilnet "k8s.io/apimachinery/pkg/util/net"
23
24
netutils "k8s.io/utils/net"
24
25
)
25
26
@@ -124,6 +125,65 @@ func getIPnetFromCIDR(cidr string) *net.IPNet {
124
125
return ipnet
125
126
}
126
127
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
+
127
187
func TestValidateMaxCIDRRange (t * testing.T ) {
128
188
testCases := []struct {
129
189
// tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage
0 commit comments