@@ -19,6 +19,7 @@ limitations under the License.
19
19
package aws
20
20
21
21
import (
22
+ "k8s.io/apimachinery/pkg/types"
22
23
"testing"
23
24
24
25
"github.com/aws/aws-sdk-go/aws"
@@ -296,3 +297,126 @@ func TestElbListenersAreEqual(t *testing.T) {
296
297
})
297
298
}
298
299
}
300
+
301
+ func TestBuildTargetGroupName (t * testing.T ) {
302
+ type args struct {
303
+ serviceName types.NamespacedName
304
+ servicePort int64
305
+ nodePort int64
306
+ targetProtocol string
307
+ targetType string
308
+ }
309
+ tests := []struct {
310
+ name string
311
+ clusterID string
312
+ args args
313
+ want string
314
+ }{
315
+ {
316
+ name : "base case" ,
317
+ clusterID : "cluster-a" ,
318
+ args : args {
319
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
320
+ servicePort : 80 ,
321
+ nodePort : 8080 ,
322
+ targetProtocol : "TCP" ,
323
+ targetType : "instance" ,
324
+ },
325
+ want : "k8s-default-servicea-0aeb5b75af" ,
326
+ },
327
+ {
328
+ name : "base case & clusterID changed" ,
329
+ clusterID : "cluster-b" ,
330
+ args : args {
331
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
332
+ servicePort : 80 ,
333
+ nodePort : 8080 ,
334
+ targetProtocol : "TCP" ,
335
+ targetType : "instance" ,
336
+ },
337
+ want : "k8s-default-servicea-5d3a0a69a8" ,
338
+ },
339
+ {
340
+ name : "base case & serviceNamespace changed" ,
341
+ clusterID : "cluster-a" ,
342
+ args : args {
343
+ serviceName : types.NamespacedName {Namespace : "another" , Name : "service-a" },
344
+ servicePort : 80 ,
345
+ nodePort : 8080 ,
346
+ targetProtocol : "TCP" ,
347
+ targetType : "instance" ,
348
+ },
349
+ want : "k8s-another-servicea-f3a3263315" ,
350
+ },
351
+ {
352
+ name : "base case & serviceName changed" ,
353
+ clusterID : "cluster-a" ,
354
+ args : args {
355
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-b" },
356
+ servicePort : 80 ,
357
+ nodePort : 8080 ,
358
+ targetProtocol : "TCP" ,
359
+ targetType : "instance" ,
360
+ },
361
+ want : "k8s-default-serviceb-9a3c03b25e" ,
362
+ },
363
+ {
364
+ name : "base case & servicePort changed" ,
365
+ clusterID : "cluster-a" ,
366
+ args : args {
367
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
368
+ servicePort : 9090 ,
369
+ nodePort : 8080 ,
370
+ targetProtocol : "TCP" ,
371
+ targetType : "instance" ,
372
+ },
373
+ want : "k8s-default-servicea-6e07474ff4" ,
374
+ },
375
+ {
376
+ name : "base case & nodePort changed" ,
377
+ clusterID : "cluster-a" ,
378
+ args : args {
379
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
380
+ servicePort : 80 ,
381
+ nodePort : 9090 ,
382
+ targetProtocol : "TCP" ,
383
+ targetType : "instance" ,
384
+ },
385
+ want : "k8s-default-servicea-6cb2d0201c" ,
386
+ },
387
+ {
388
+ name : "base case & targetProtocol changed" ,
389
+ clusterID : "cluster-a" ,
390
+ args : args {
391
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
392
+ servicePort : 80 ,
393
+ nodePort : 8080 ,
394
+ targetProtocol : "UDP" ,
395
+ targetType : "instance" ,
396
+ },
397
+ want : "k8s-default-servicea-70495e628e" ,
398
+ },
399
+ {
400
+ name : "base case & targetType changed" ,
401
+ clusterID : "cluster-a" ,
402
+ args : args {
403
+ serviceName : types.NamespacedName {Namespace : "default" , Name : "service-a" },
404
+ servicePort : 80 ,
405
+ nodePort : 8080 ,
406
+ targetProtocol : "TCP" ,
407
+ targetType : "ip" ,
408
+ },
409
+ want : "k8s-default-servicea-fff6dd8028" ,
410
+ },
411
+ }
412
+ for _ , tt := range tests {
413
+ t .Run (tt .name , func (t * testing.T ) {
414
+ c := & Cloud {
415
+ tagging : awsTagging {ClusterID : tt .clusterID },
416
+ }
417
+ if got := c .buildTargetGroupName (tt .args .serviceName , tt .args .servicePort , tt .args .nodePort , tt .args .targetProtocol , tt .args .targetType ); got != tt .want {
418
+ assert .Equal (t , tt .want , got )
419
+ }
420
+ })
421
+ }
422
+ }
0 commit comments