@@ -240,13 +240,13 @@ func TestDockerHostConfigPortBinding(t *testing.T) {
240
240
Ports : []apicontainer.PortBinding {
241
241
{
242
242
ContainerPort : 10 ,
243
- HostPort : 10 ,
243
+ HostPort : 20 ,
244
244
BindIP : "" ,
245
245
Protocol : apicontainer .TransportProtocolTCP ,
246
246
},
247
247
{
248
248
ContainerPort : 20 ,
249
- HostPort : 20 ,
249
+ HostPort : 30 ,
250
250
BindIP : "" ,
251
251
Protocol : apicontainer .TransportProtocolUDP ,
252
252
},
@@ -256,6 +256,31 @@ func TestDockerHostConfigPortBinding(t *testing.T) {
256
256
}
257
257
258
258
testTask2 := & Task {
259
+ Containers : []* apicontainer.Container {
260
+ {
261
+ Name : "c1" ,
262
+ Ports : []apicontainer.PortBinding {
263
+ {
264
+ ContainerPort : 10 ,
265
+ BindIP : "" ,
266
+ Protocol : apicontainer .TransportProtocolTCP ,
267
+ },
268
+ {
269
+ ContainerPort : 20 ,
270
+ BindIP : "" ,
271
+ Protocol : apicontainer .TransportProtocolUDP ,
272
+ },
273
+ {
274
+ ContainerPort : 30 ,
275
+ BindIP : "" ,
276
+ Protocol : apicontainer .TransportProtocolUDP ,
277
+ },
278
+ },
279
+ },
280
+ },
281
+ }
282
+
283
+ testTask3 := & Task {
259
284
Containers : []* apicontainer.Container {
260
285
{
261
286
Name : "c1" ,
@@ -278,17 +303,20 @@ func TestDockerHostConfigPortBinding(t *testing.T) {
278
303
testCases := []struct {
279
304
testName string
280
305
testTask * Task
281
- getHostPortRange func (numberOfPorts int , protocol string , dynamicHostPortRange string ) (string , error )
306
+ testDynamicHostPortRange string
307
+ testContainerPortRange string
282
308
expectedPortBinding nat.PortMap
283
309
expectedContainerPortSet map [int ]struct {}
284
310
expectedContainerPortRangeMap map [string ]string
311
+ expectedError bool
285
312
}{
286
313
{
287
- testName : "2 port bindings, each with singular container port - host port" ,
288
- testTask : testTask1 ,
314
+ testName : "user-specified container ports and host ports" ,
315
+ testTask : testTask1 ,
316
+ testDynamicHostPortRange : "40000-60000" ,
289
317
expectedPortBinding : nat.PortMap {
290
- nat .Port ("10/tcp" ): []nat.PortBinding {{HostPort : "10 " }},
291
- nat .Port ("20/udp" ): []nat.PortBinding {{HostPort : "20 " }},
318
+ nat .Port ("10/tcp" ): []nat.PortBinding {{HostPort : "20 " }},
319
+ nat .Port ("20/udp" ): []nat.PortBinding {{HostPort : "30 " }},
292
320
},
293
321
expectedContainerPortSet : map [int ]struct {}{
294
322
10 : {},
@@ -297,23 +325,37 @@ func TestDockerHostConfigPortBinding(t *testing.T) {
297
325
expectedContainerPortRangeMap : map [string ]string {},
298
326
},
299
327
{
300
- testName : "2 port bindings, one with container port range, other with singular container port" ,
301
- testTask : testTask2 ,
302
- getHostPortRange : func (numberOfPorts int , protocol string , dynamicHostPortRange string ) (string , error ) {
303
- return "155-157" , nil
304
- },
305
- expectedPortBinding : nat.PortMap {
306
- nat .Port ("55/udp" ): []nat.PortBinding {{HostPort : "155" }},
307
- nat .Port ("56/udp" ): []nat.PortBinding {{HostPort : "156" }},
308
- nat .Port ("57/udp" ): []nat.PortBinding {{HostPort : "157" }},
309
- nat .Port ("80/tcp" ): []nat.PortBinding {{HostPort : "0" }},
328
+ testName : "user-specified container ports with a ideal dynamicHostPortRange" ,
329
+ testTask : testTask2 ,
330
+ testDynamicHostPortRange : "40000-60000" ,
331
+ expectedContainerPortSet : map [int ]struct {}{
332
+ 10 : {},
333
+ 20 : {},
334
+ 30 : {},
310
335
},
336
+ expectedContainerPortRangeMap : map [string ]string {},
337
+ },
338
+ {
339
+ testName : "user-specified container ports with a bad dynamicHostPortRange" ,
340
+ testTask : testTask2 ,
341
+ testDynamicHostPortRange : "100-101" ,
342
+ expectedError : true ,
343
+ },
344
+ {
345
+ testName : "user-specified container port and container port range with a ideal dynamicHostPortRange" ,
346
+ testTask : testTask3 ,
347
+ testDynamicHostPortRange : "40000-60000" ,
348
+ testContainerPortRange : "55-57" ,
311
349
expectedContainerPortSet : map [int ]struct {}{
312
350
80 : {},
313
351
},
314
- expectedContainerPortRangeMap : map [string ]string {
315
- "55-57" : "155-157" ,
316
- },
352
+ },
353
+ {
354
+ testName : "user-specified container port and container port range with a bad user-specified dynamicHostPortRange" ,
355
+ testTask : testTask3 ,
356
+ testDynamicHostPortRange : "40000-40001" ,
357
+ testContainerPortRange : "55-57" ,
358
+ expectedError : true ,
317
359
},
318
360
}
319
361
@@ -322,22 +364,51 @@ func TestDockerHostConfigPortBinding(t *testing.T) {
322
364
defer func () {
323
365
getHostPortRange = utils .GetHostPortRange
324
366
}()
325
- getHostPortRange = tc .getHostPortRange
326
367
327
- config , err := tc .testTask .DockerHostConfig (tc .testTask .Containers [0 ], dockerMap (tc .testTask ), defaultDockerClientAPIVersion ,
328
- & config.Config {})
329
- assert .Nil (t , err )
368
+ // Get the Docker host config for the task container
369
+ config , err := tc .testTask .DockerHostConfig (tc .testTask .Containers [0 ], dockerMap (tc .testTask ),
370
+ defaultDockerClientAPIVersion , & config.Config {DynamicHostPortRange : tc .testDynamicHostPortRange })
371
+ if ! tc .expectedError {
372
+ assert .Nil (t , err )
330
373
331
- if ! reflect .DeepEqual (config .PortBindings , tc .expectedPortBinding ) {
332
- t .Error ("Expected port bindings to be resolved, was: " , config .PortBindings )
333
- }
374
+ // Verify PortBindings
375
+ if tc .expectedPortBinding != nil {
376
+ if ! reflect .DeepEqual (config .PortBindings , tc .expectedPortBinding ) {
377
+ t .Error ("Expected port bindings to be resolved, was: " , config .PortBindings )
378
+ }
379
+ } else {
380
+ // Verify ECS Agent assigned host ports are within the dynamic host port range
381
+ eStartPort , eEndPort , _ := nat .ParsePortRangeToInt (tc .testDynamicHostPortRange )
382
+ for _ , hostPortBinding := range config .PortBindings {
383
+ hostPort , _ := strconv .Atoi (hostPortBinding [0 ].HostPort )
384
+ result := utils .PortIsInRange (hostPort , eStartPort , eEndPort )
385
+ if ! result {
386
+ t .Error ("Actual host port is not in the dynamicHostPortRange: " , hostPort )
387
+ break
388
+ }
389
+ }
390
+ }
334
391
335
- if ! reflect .DeepEqual (tc .testTask .Containers [0 ].ContainerPortSet , tc .expectedContainerPortSet ) {
336
- t .Error ("Expected container port set to be resolved, was: " , tc .testTask .Containers [0 ].GetContainerPortSet ())
337
- }
392
+ // Verify ContainerPortSet
393
+ if ! reflect .DeepEqual (tc .testTask .Containers [0 ].ContainerPortSet , tc .expectedContainerPortSet ) {
394
+ t .Error ("Expected container port set to be resolved, was: " , tc .testTask .Containers [0 ].GetContainerPortSet ())
395
+ }
338
396
339
- if ! reflect .DeepEqual (tc .testTask .Containers [0 ].ContainerPortRangeMap , tc .expectedContainerPortRangeMap ) {
340
- t .Error ("Expected container port range map to be resolved, was: " , tc .testTask .Containers [0 ].GetContainerPortRangeMap ())
397
+ // Verify ContainerPortRangeMap
398
+ if tc .expectedContainerPortRangeMap != nil {
399
+ if ! reflect .DeepEqual (tc .testTask .Containers [0 ].ContainerPortRangeMap , tc .expectedContainerPortRangeMap ) {
400
+ t .Error ("Expected container port range map to be resolved, was: " , tc .testTask .Containers [0 ].GetContainerPortRangeMap ())
401
+ }
402
+ } else {
403
+ // Verify ECS Agent assigned host port range are within the dynamic host port range
404
+ hostPortRange := tc .testTask .Containers [0 ].ContainerPortRangeMap [tc .testContainerPortRange ]
405
+ result := utils .VerifyPortsWithinRange (hostPortRange , tc .testDynamicHostPortRange )
406
+ if ! result {
407
+ t .Error ("Expected host port range should be in the dynamicHostPortRange, but the actual host port range is: " , hostPortRange )
408
+ }
409
+ }
410
+ } else {
411
+ assert .NotNil (t , err )
341
412
}
342
413
})
343
414
}
0 commit comments