@@ -346,6 +346,66 @@ def test_stats(self):
346
346
'memory_stats' , 'blkio_stats' ]:
347
347
assert key in stats
348
348
349
+ def test_ports_target_none (self ):
350
+ client = docker .from_env (version = TEST_API_VERSION )
351
+ ports = None
352
+ target_ports = {'2222/tcp' : ports }
353
+ container = client .containers .run (
354
+ "alpine" , "sleep 100" , detach = True ,
355
+ ports = target_ports
356
+ )
357
+ self .tmp_containers .append (container .id )
358
+ container .reload () # required to get auto-assigned ports
359
+ actual_ports = container .ports
360
+ assert sorted (target_ports .keys ()) == sorted (actual_ports .keys ())
361
+ for target_client , target_host in target_ports .items ():
362
+ for actual_port in actual_ports [target_client ]:
363
+ actual_keys = sorted (actual_port .keys ())
364
+ assert sorted (['HostIp' , 'HostPort' ]) == actual_keys
365
+ assert target_host is ports
366
+ assert int (actual_port ['HostPort' ]) > 0
367
+ client .close ()
368
+
369
+ def test_ports_target_tuple (self ):
370
+ client = docker .from_env (version = TEST_API_VERSION )
371
+ ports = ('127.0.0.1' , 1111 )
372
+ target_ports = {'2222/tcp' : ports }
373
+ container = client .containers .run (
374
+ "alpine" , "sleep 100" , detach = True ,
375
+ ports = target_ports
376
+ )
377
+ self .tmp_containers .append (container .id )
378
+ container .reload () # required to get auto-assigned ports
379
+ actual_ports = container .ports
380
+ assert sorted (target_ports .keys ()) == sorted (actual_ports .keys ())
381
+ for target_client , target_host in target_ports .items ():
382
+ for actual_port in actual_ports [target_client ]:
383
+ actual_keys = sorted (actual_port .keys ())
384
+ assert sorted (['HostIp' , 'HostPort' ]) == actual_keys
385
+ assert target_host == ports
386
+ assert int (actual_port ['HostPort' ]) > 0
387
+ client .close ()
388
+
389
+ def test_ports_target_list (self ):
390
+ client = docker .from_env (version = TEST_API_VERSION )
391
+ ports = [1234 , 4567 ]
392
+ target_ports = {'2222/tcp' : ports }
393
+ container = client .containers .run (
394
+ "alpine" , "sleep 100" , detach = True ,
395
+ ports = target_ports
396
+ )
397
+ self .tmp_containers .append (container .id )
398
+ container .reload () # required to get auto-assigned ports
399
+ actual_ports = container .ports
400
+ assert sorted (target_ports .keys ()) == sorted (actual_ports .keys ())
401
+ for target_client , target_host in target_ports .items ():
402
+ for actual_port in actual_ports [target_client ]:
403
+ actual_keys = sorted (actual_port .keys ())
404
+ assert sorted (['HostIp' , 'HostPort' ]) == actual_keys
405
+ assert target_host == ports
406
+ assert int (actual_port ['HostPort' ]) > 0
407
+ client .close ()
408
+
349
409
def test_stop (self ):
350
410
client = docker .from_env (version = TEST_API_VERSION )
351
411
container = client .containers .run ("alpine" , "top" , detach = True )
0 commit comments