@@ -18,6 +18,8 @@ package controller
18
18
19
19
import (
20
20
"fmt"
21
+ "reflect"
22
+ "sort"
21
23
"strings"
22
24
"testing"
23
25
@@ -203,3 +205,101 @@ func TestRepairWithExisting(t *testing.T) {
203
205
t .Errorf ("unexpected portallocator state: %d free" , free )
204
206
}
205
207
}
208
+
209
+ func TestCollectServiceNodePorts (t * testing.T ) {
210
+ tests := []struct {
211
+ name string
212
+ serviceSpec corev1.ServiceSpec
213
+ expected []int
214
+ }{
215
+ {
216
+ name : "no duplicated nodePorts" ,
217
+ serviceSpec : corev1.ServiceSpec {
218
+ Ports : []corev1.ServicePort {
219
+ {NodePort : 111 , Protocol : corev1 .ProtocolTCP },
220
+ {NodePort : 112 , Protocol : corev1 .ProtocolUDP },
221
+ {NodePort : 113 , Protocol : corev1 .ProtocolUDP },
222
+ },
223
+ },
224
+ expected : []int {111 , 112 , 113 },
225
+ },
226
+ {
227
+ name : "duplicated nodePort with TCP protocol" ,
228
+ serviceSpec : corev1.ServiceSpec {
229
+ Ports : []corev1.ServicePort {
230
+ {NodePort : 111 , Protocol : corev1 .ProtocolTCP },
231
+ {NodePort : 111 , Protocol : corev1 .ProtocolTCP },
232
+ {NodePort : 112 , Protocol : corev1 .ProtocolUDP },
233
+ },
234
+ },
235
+ expected : []int {111 , 111 , 112 },
236
+ },
237
+ {
238
+ name : "duplicated nodePort with UDP protocol" ,
239
+ serviceSpec : corev1.ServiceSpec {
240
+ Ports : []corev1.ServicePort {
241
+ {NodePort : 111 , Protocol : corev1 .ProtocolUDP },
242
+ {NodePort : 111 , Protocol : corev1 .ProtocolUDP },
243
+ {NodePort : 112 , Protocol : corev1 .ProtocolTCP },
244
+ },
245
+ },
246
+ expected : []int {111 , 111 , 112 },
247
+ },
248
+ {
249
+ name : "duplicated nodePort with different protocol" ,
250
+ serviceSpec : corev1.ServiceSpec {
251
+ Ports : []corev1.ServicePort {
252
+ {NodePort : 111 , Protocol : corev1 .ProtocolTCP },
253
+ {NodePort : 112 , Protocol : corev1 .ProtocolTCP },
254
+ {NodePort : 111 , Protocol : corev1 .ProtocolUDP },
255
+ },
256
+ },
257
+ expected : []int {111 , 112 },
258
+ },
259
+ {
260
+ name : "no duplicated port(with health check port)" ,
261
+ serviceSpec : corev1.ServiceSpec {
262
+ Ports : []corev1.ServicePort {
263
+ {NodePort : 111 , Protocol : corev1 .ProtocolTCP },
264
+ {NodePort : 112 , Protocol : corev1 .ProtocolUDP },
265
+ },
266
+ HealthCheckNodePort : 113 ,
267
+ },
268
+ expected : []int {111 , 112 , 113 },
269
+ },
270
+ {
271
+ name : "nodePort has different protocol with duplicated health check port" ,
272
+ serviceSpec : corev1.ServiceSpec {
273
+ Ports : []corev1.ServicePort {
274
+ {NodePort : 111 , Protocol : corev1 .ProtocolUDP },
275
+ {NodePort : 112 , Protocol : corev1 .ProtocolTCP },
276
+ },
277
+ HealthCheckNodePort : 111 ,
278
+ },
279
+ expected : []int {111 , 112 },
280
+ },
281
+ {
282
+ name : "nodePort has same protocol as duplicated health check port" ,
283
+ serviceSpec : corev1.ServiceSpec {
284
+ Ports : []corev1.ServicePort {
285
+ {NodePort : 111 , Protocol : corev1 .ProtocolUDP },
286
+ {NodePort : 112 , Protocol : corev1 .ProtocolTCP },
287
+ },
288
+ HealthCheckNodePort : 112 ,
289
+ },
290
+ expected : []int {111 , 112 , 112 },
291
+ },
292
+ }
293
+ for _ , tc := range tests {
294
+ t .Run (tc .name , func (t * testing.T ) {
295
+ ports := collectServiceNodePorts (& corev1.Service {
296
+ ObjectMeta : metav1.ObjectMeta {Namespace : "one" , Name : "one" },
297
+ Spec : tc .serviceSpec ,
298
+ })
299
+ sort .Ints (ports )
300
+ if ! reflect .DeepEqual (tc .expected , ports ) {
301
+ t .Fatalf ("Invalid result\n expected: %v\n got: %v" , tc .expected , ports )
302
+ }
303
+ })
304
+ }
305
+ }
0 commit comments