88
99
1010class ContainerTest (unittest .TestCase ):
11+
12+
13+ def setUp (self ):
14+ self .container_dict = {
15+ "Id" : "abc" ,
16+ "Image" : "busybox:latest" ,
17+ "Command" : "sleep 300" ,
18+ "Created" : 1387384730 ,
19+ "Status" : "Up 8 seconds" ,
20+ "Ports" : None ,
21+ "SizeRw" : 0 ,
22+ "SizeRootFs" : 0 ,
23+ "Names" : ["/figtest_db_1" ],
24+ "NetworkSettings" : {
25+ "Ports" : {},
26+ },
27+ }
28+
1129 def test_from_ps (self ):
12- container = Container .from_ps (None , {
13- "Id" :"abc" ,
14- "Image" :"busybox:latest" ,
15- "Command" :"sleep 300" ,
16- "Created" :1387384730 ,
17- "Status" :"Up 8 seconds" ,
18- "Ports" :None ,
19- "SizeRw" :0 ,
20- "SizeRootFs" :0 ,
21- "Names" :["/figtest_db_1" ]
22- }, has_been_inspected = True )
30+ container = Container .from_ps (None ,
31+ self .container_dict ,
32+ has_been_inspected = True )
2333 self .assertEqual (container .dictionary , {
2434 "Id" : "abc" ,
2535 "Image" :"busybox:latest" ,
@@ -42,35 +52,21 @@ def test_environment(self):
4252 })
4353
4454 def test_number (self ):
45- container = Container .from_ps (None , {
46- "Id" :"abc" ,
47- "Image" :"busybox:latest" ,
48- "Command" :"sleep 300" ,
49- "Created" :1387384730 ,
50- "Status" :"Up 8 seconds" ,
51- "Ports" :None ,
52- "SizeRw" :0 ,
53- "SizeRootFs" :0 ,
54- "Names" :["/figtest_db_1" ]
55- }, has_been_inspected = True )
55+ container = Container .from_ps (None ,
56+ self .container_dict ,
57+ has_been_inspected = True )
5658 self .assertEqual (container .number , 1 )
5759
5860 def test_name (self ):
59- container = Container .from_ps (None , {
60- "Id" :"abc" ,
61- "Image" :"busybox:latest" ,
62- "Command" :"sleep 300" ,
63- "Names" :["/figtest_db_1" ]
64- }, has_been_inspected = True )
61+ container = Container .from_ps (None ,
62+ self .container_dict ,
63+ has_been_inspected = True )
6564 self .assertEqual (container .name , "figtest_db_1" )
6665
6766 def test_name_without_project (self ):
68- container = Container .from_ps (None , {
69- "Id" :"abc" ,
70- "Image" :"busybox:latest" ,
71- "Command" :"sleep 300" ,
72- "Names" :["/figtest_db_1" ]
73- }, has_been_inspected = True )
67+ container = Container .from_ps (None ,
68+ self .container_dict ,
69+ has_been_inspected = True )
7470 self .assertEqual (container .name_without_project , "db_1" )
7571
7672 def test_inspect_if_not_inspected (self ):
@@ -85,3 +81,27 @@ def test_inspect_if_not_inspected(self):
8581
8682 container .inspect_if_not_inspected ()
8783 self .assertEqual (mock_client .inspect_container .call_count , 1 )
84+
85+ def test_human_readable_ports_none (self ):
86+ container = Container (None , self .container_dict , has_been_inspected = True )
87+ self .assertEqual (container .human_readable_ports , '' )
88+
89+ def test_human_readable_ports_public_and_private (self ):
90+ self .container_dict ['NetworkSettings' ]['Ports' ].update ({
91+ "45454/tcp" : [ { "HostIp" : "0.0.0.0" , "HostPort" : "49197" } ],
92+ "45453/tcp" : [],
93+ })
94+ container = Container (None , self .container_dict , has_been_inspected = True )
95+
96+ expected = "45453/tcp, 0.0.0.0:49197->45454/tcp"
97+ self .assertEqual (container .human_readable_ports , expected )
98+
99+ def test_get_local_port (self ):
100+ self .container_dict ['NetworkSettings' ]['Ports' ].update ({
101+ "45454/tcp" : [ { "HostIp" : "0.0.0.0" , "HostPort" : "49197" } ],
102+ })
103+ container = Container (None , self .container_dict , has_been_inspected = True )
104+
105+ self .assertEqual (
106+ container .get_local_port (45454 , protocol = 'tcp' ),
107+ '0.0.0.0:49197' )
0 commit comments