@@ -424,6 +424,14 @@ var (
424
424
defaultSCProtocol = "/tcp"
425
425
)
426
426
427
+ func getDefaultDynamicHostPortRange () (start int , end int ) {
428
+ startHostPortRange , endHostPortRange , err := utils .GetDynamicHostPortRange ()
429
+ if err != nil {
430
+ return utils .DefaultPortRangeStart , utils .DefaultPortRangeEnd
431
+ }
432
+ return startHostPortRange , endHostPortRange
433
+ }
434
+
427
435
func getTestTaskServiceConnectBridgeMode () * Task {
428
436
testTask := & Task {
429
437
NetworkMode : BridgeNetworkMode ,
@@ -479,61 +487,110 @@ func convertSCPort(port uint16) nat.Port {
479
487
}
480
488
481
489
// TestDockerHostConfigSCBridgeMode verifies port bindings and network mode overrides for each
482
- // container in an SC-enabled bridge mode task. The test task is consisted of the SC container, a regular container,
490
+ // container in an SC-enabled bridge mode task with default/user-specified dynamic host port range.
491
+ // The test task is consisted of the SC container, a regular container,
483
492
// and two pause containers associated with each.
484
493
func TestDockerHostConfigSCBridgeMode (t * testing.T ) {
485
494
testTask := getTestTaskServiceConnectBridgeMode ()
486
- // task container and SC container should both get empty port binding map and "container" network mode
487
- actualConfig , err := testTask .DockerHostConfig (testTask .Containers [0 ], dockerMap (testTask ), defaultDockerClientAPIVersion ,
488
- & config.Config {})
489
- assert .Nil (t , err )
490
- assert .NotNil (t , actualConfig )
491
- assert .Equal (t , dockercontainer .NetworkMode (fmt .Sprintf ("%s-%s" , // e.g. "container:dockerid-~internal~ecs~pause-C1"
492
- dockerMappingContainerPrefix + dockerIDPrefix + NetworkPauseContainerName , "C1" )), actualConfig .NetworkMode )
493
- assert .Empty (t , actualConfig .PortBindings , "Task container port binding should be empty" )
494
-
495
- actualConfig , err = testTask .DockerHostConfig (testTask .Containers [2 ], dockerMap (testTask ), defaultDockerClientAPIVersion ,
496
- & config.Config {})
497
- assert .Nil (t , err )
498
- assert .NotNil (t , actualConfig )
499
- assert .Equal (t , dockercontainer .NetworkMode (fmt .Sprintf ("%s-%s" , // e.g. "container:dockerid-~internal~ecs~pause-C1"
500
- dockerMappingContainerPrefix + dockerIDPrefix + NetworkPauseContainerName , serviceConnectContainerTestName )), actualConfig .NetworkMode )
501
- assert .Empty (t , actualConfig .PortBindings , "SC container port binding should be empty" )
495
+ testCases := []struct {
496
+ testStartHostPort int
497
+ testEndHostPort int
498
+ testName string
499
+ testError bool
500
+ }{
501
+ {
502
+ testStartHostPort : 0 ,
503
+ testEndHostPort : 0 ,
504
+ testName : "with default dynamic host port range" ,
505
+ },
506
+ {
507
+ testStartHostPort : 50000 ,
508
+ testEndHostPort : 60000 ,
509
+ testName : "with user-specified dynamic host port range" ,
510
+ },
511
+ {
512
+ testStartHostPort : 500 ,
513
+ testEndHostPort : 501 ,
514
+ testName : "with user-specified dynamic host port range but no available host port" ,
515
+ testError : true ,
516
+ },
517
+ }
502
518
503
- // task pause container should get port binding map of the task container
504
- actualConfig , err = testTask .DockerHostConfig (testTask .Containers [1 ], dockerMap (testTask ), defaultDockerClientAPIVersion ,
505
- & config.Config {})
506
- assert .Nil (t , err )
507
- assert .NotNil (t , actualConfig )
508
- assert .Equal (t , dockercontainer .NetworkMode (BridgeNetworkMode ), actualConfig .NetworkMode )
509
- bindings , ok := actualConfig .PortBindings [convertSCPort (SCTaskContainerPort1 )]
510
- assert .True (t , ok , "Could not get port bindings" )
511
- assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
512
- assert .Equal (t , "0" , bindings [0 ].HostPort , "Wrong hostport" )
513
- bindings , ok = actualConfig .PortBindings [convertSCPort (SCTaskContainerPort2 )]
514
- assert .True (t , ok , "Could not get port bindings" )
515
- assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
516
- assert .Equal (t , "0" , bindings [0 ].HostPort , "Wrong hostport" )
517
-
518
- // SC pause container should get port binding map of all ingress listeners
519
- actualConfig , err = testTask .DockerHostConfig (testTask .Containers [3 ], dockerMap (testTask ), defaultDockerClientAPIVersion ,
520
- & config.Config {})
521
- assert .Nil (t , err )
522
- assert .NotNil (t , actualConfig )
523
- assert .Equal (t , dockercontainer .NetworkMode (BridgeNetworkMode ), actualConfig .NetworkMode )
524
- // SC - ingress listener 1 - default experience
525
- bindings , ok = actualConfig .PortBindings [convertSCPort (SCIngressListener1ContainerPort )]
526
- assert .True (t , ok , "Could not get port bindings" )
527
- assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
528
- assert .Equal (t , "0" , bindings [0 ].HostPort , "Wrong hostport" )
529
- // SC - ingress listener 2 - non-default host port
530
- bindings , ok = actualConfig .PortBindings [convertSCPort (SCIngressListener2ContainerPort )]
531
- assert .True (t , ok , "Could not get port bindings" )
532
- assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
533
- assert .Equal (t , strconv .Itoa (int (SCIngressListener2HostPort )), bindings [0 ].HostPort , "Wrong hostport" )
534
- // SC - egress listener - should not have port binding
535
- bindings , ok = actualConfig .PortBindings [convertSCPort (SCEgressListenerContainerPort )]
536
- assert .False (t , ok , "egress listener has port binding but it shouldn't" )
519
+ for _ , tc := range testCases {
520
+ t .Run (tc .testName , func (t * testing.T ) {
521
+ // need to reset the tracker to avoid getting data from previous test cases
522
+ utils .ResetTracker ()
523
+ if tc .testStartHostPort == 0 && tc .testEndHostPort == 0 {
524
+ tc .testStartHostPort , tc .testEndHostPort = getDefaultDynamicHostPortRange ()
525
+ }
526
+ testDynamicHostPortRange := fmt .Sprintf ("%d-%d" , tc .testStartHostPort , tc .testEndHostPort )
527
+ testConfig := & config.Config {DynamicHostPortRange : testDynamicHostPortRange }
528
+
529
+ // task container and SC container should both get empty port binding map and "container" network mode
530
+
531
+ // the task container
532
+ actualConfig , err := testTask .DockerHostConfig (testTask .Containers [0 ], dockerMap (testTask ), defaultDockerClientAPIVersion , testConfig )
533
+ assert .Nil (t , err )
534
+ assert .NotNil (t , actualConfig )
535
+ assert .Equal (t , dockercontainer .NetworkMode (fmt .Sprintf ("%s-%s" , // e.g. "container:dockerid-~internal~ecs~pause-C1"
536
+ dockerMappingContainerPrefix + dockerIDPrefix + NetworkPauseContainerName , "C1" )), actualConfig .NetworkMode )
537
+ assert .Empty (t , actualConfig .PortBindings , "Task container port binding should be empty" )
538
+
539
+ // the service connect container
540
+ actualConfig , err = testTask .DockerHostConfig (testTask .Containers [2 ], dockerMap (testTask ), defaultDockerClientAPIVersion , testConfig )
541
+ assert .Nil (t , err )
542
+ assert .NotNil (t , actualConfig )
543
+ assert .Equal (t , dockercontainer .NetworkMode (fmt .Sprintf ("%s-%s" , // e.g. "container:dockerid-~internal~ecs~pause-C1"
544
+ dockerMappingContainerPrefix + dockerIDPrefix + NetworkPauseContainerName , serviceConnectContainerTestName )), actualConfig .NetworkMode )
545
+ assert .Empty (t , actualConfig .PortBindings , "SC container port binding should be empty" )
546
+
547
+ // task pause container should get port binding map of the task container
548
+ actualConfig , err = testTask .DockerHostConfig (testTask .Containers [1 ], dockerMap (testTask ), defaultDockerClientAPIVersion , testConfig )
549
+ if ! tc .testError {
550
+ assert .Nil (t , err )
551
+ assert .NotNil (t , actualConfig )
552
+ assert .Equal (t , dockercontainer .NetworkMode (BridgeNetworkMode ), actualConfig .NetworkMode )
553
+ bindings , ok := actualConfig .PortBindings [convertSCPort (SCTaskContainerPort1 )]
554
+ assert .True (t , ok , "Could not get port bindings" )
555
+ assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
556
+ hostPort , _ := strconv .Atoi (bindings [0 ].HostPort )
557
+ result := utils .PortIsInRange (hostPort , tc .testStartHostPort , tc .testEndHostPort )
558
+ assert .True (t , result , "hostport is not in the dynamic host port range" )
559
+
560
+ bindings , ok = actualConfig .PortBindings [convertSCPort (SCTaskContainerPort2 )]
561
+ assert .True (t , ok , "Could not get port bindings" )
562
+ assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
563
+ hostPort , _ = strconv .Atoi (bindings [0 ].HostPort )
564
+ result = utils .PortIsInRange (hostPort , tc .testStartHostPort , tc .testEndHostPort )
565
+ assert .True (t , result , "hostport is not in the dynamic host port range" )
566
+
567
+ // SC pause container should get port binding map of all ingress listeners
568
+ actualConfig , err = testTask .DockerHostConfig (testTask .Containers [3 ], dockerMap (testTask ), defaultDockerClientAPIVersion , testConfig )
569
+ assert .Nil (t , err )
570
+ assert .NotNil (t , actualConfig )
571
+ assert .Equal (t , dockercontainer .NetworkMode (BridgeNetworkMode ), actualConfig .NetworkMode )
572
+
573
+ // SC - ingress listener 1 - default experience
574
+ bindings , ok = actualConfig .PortBindings [convertSCPort (SCIngressListener1ContainerPort )]
575
+ assert .True (t , ok , "Could not get port bindings" )
576
+ hostPort , _ = strconv .Atoi (bindings [0 ].HostPort )
577
+ result = utils .PortIsInRange (hostPort , tc .testStartHostPort , tc .testEndHostPort )
578
+ assert .True (t , result , "hostport is not in the dynamic host port range" )
579
+
580
+ // SC - ingress listener 2 - non-default host port
581
+ bindings , ok = actualConfig .PortBindings [convertSCPort (SCIngressListener2ContainerPort )]
582
+ assert .True (t , ok , "Could not get port bindings" )
583
+ assert .Equal (t , 1 , len (bindings ), "Wrong number of bindings" )
584
+ assert .Equal (t , strconv .Itoa (int (SCIngressListener2HostPort )), bindings [0 ].HostPort , "Wrong hostport" )
585
+
586
+ // SC - egress listener - should not have port binding
587
+ bindings , ok = actualConfig .PortBindings [convertSCPort (SCEgressListenerContainerPort )]
588
+ assert .False (t , ok , "egress listener has port binding but it shouldn't" )
589
+ } else {
590
+ assert .NotNil (t , err )
591
+ }
592
+ })
593
+ }
537
594
}
538
595
539
596
// TestDockerHostConfigSCBridgeMode_getPortBindingFailure verifies that when we can't find the task
0 commit comments