@@ -2,70 +2,151 @@ package main
22
33import (
44 "fmt"
5+ "regexp"
56 "sort"
67 "strings"
8+ "time"
79
810 "github.com/docker/docker/pkg/integration/checker"
911 "github.com/go-check/check"
1012)
1113
1214func (s * DockerSuite ) TestPortList (c * check.C ) {
15+ printTestCaseName ()
16+ defer printTestDuration (time .Now ())
17+ pullImageIfNotExist ("busybox" )
1318 testRequires (c , DaemonIsLinux )
14-
1519 // one port
16- // _, errCode := dockerCmd(c, "pull", singlePortImage)
17- // c.Assert(errCode, checker.Equals, 0)
18-
19- out , _ := dockerCmd (c , "run" , "-d" , singlePortImage , "top" )
20+ out , _ := dockerCmd (c , "run" , "-d" , "-p" , "9876:80" , "busybox" , "top" )
2021 firstID := strings .TrimSpace (out )
2122
2223 out , _ = dockerCmd (c , "port" , firstID , "80" )
2324
24- err := assertPortList (c , out , []string {"0.0.0.0:80 " })
25+ err := assertPortList (c , out , []string {"0.0.0.0:9876 " })
2526 // Port list is not correct
2627 c .Assert (err , checker .IsNil )
2728
2829 out , _ = dockerCmd (c , "port" , firstID )
2930
30- err = assertPortList (c , out , []string {"80/tcp -> 0.0.0.0:80 " })
31+ err = assertPortList (c , out , []string {"80/tcp -> 0.0.0.0:9876 " })
3132 // Port list is not correct
3233 c .Assert (err , checker .IsNil )
3334
3435 dockerCmd (c , "rm" , "-f" , firstID )
3536
36- // four port
37+ // three port
3738 out , _ = dockerCmd (c , "run" , "-d" ,
38- multiPortImage , "top" )
39+ "-p" , "9876:80" ,
40+ "-p" , "9877:81" ,
41+ "-p" , "9878:82" ,
42+ "busybox" , "top" )
3943 ID := strings .TrimSpace (out )
4044
4145 out , _ = dockerCmd (c , "port" , ID , "80" )
4246
43- err = assertPortList (c , out , []string {"0.0.0.0:80 " })
47+ err = assertPortList (c , out , []string {"0.0.0.0:9876 " })
4448 // Port list is not correct
4549 c .Assert (err , checker .IsNil )
4650
4751 out , _ = dockerCmd (c , "port" , ID )
4852
4953 err = assertPortList (c , out , []string {
50- "80/tcp -> 0.0.0.0:80" ,
51- "82/tcp -> 0.0.0.0:82" ,
52- "84/tcp -> 0.0.0.0:84" ,
53- "86/tcp -> 0.0.0.0:86" })
54+ "80/tcp -> 0.0.0.0:9876" ,
55+ "81/tcp -> 0.0.0.0:9877" ,
56+ "82/tcp -> 0.0.0.0:9878" })
57+ // Port list is not correct
58+ c .Assert (err , checker .IsNil )
59+
60+ dockerCmd (c , "rm" , "-f" , ID )
61+
62+ // more and one port mapped to the same container port
63+ out , _ = dockerCmd (c , "run" , "-d" ,
64+ "-p" , "9876:80" ,
65+ "-p" , "9999:80" ,
66+ "-p" , "9877:81" ,
67+ "-p" , "9878:82" ,
68+ "busybox" , "top" )
69+ ID = strings .TrimSpace (out )
70+
71+ out , _ = dockerCmd (c , "port" , ID , "80" )
72+
73+ err = assertPortList (c , out , []string {"0.0.0.0:9876" , "0.0.0.0:9999" })
5474 // Port list is not correct
5575 c .Assert (err , checker .IsNil )
5676
77+ out , _ = dockerCmd (c , "port" , ID )
78+
79+ err = assertPortList (c , out , []string {
80+ "80/tcp -> 0.0.0.0:9876" ,
81+ "80/tcp -> 0.0.0.0:9999" ,
82+ "81/tcp -> 0.0.0.0:9877" ,
83+ "82/tcp -> 0.0.0.0:9878" })
84+ // Port list is not correct
85+ c .Assert (err , checker .IsNil )
86+ dockerCmd (c , "rm" , "-f" , ID )
87+
88+ testRange := func () {
89+ // host port ranges used
90+ IDs := make ([]string , 3 )
91+ for i := 0 ; i < 3 ; i ++ {
92+ out , _ = dockerCmd (c , "run" , "-d" ,
93+ "-p" , "9090-9092:80" ,
94+ "busybox" , "top" )
95+ IDs [i ] = strings .TrimSpace (out )
96+
97+ out , _ = dockerCmd (c , "port" , IDs [i ])
98+
99+ err = assertPortList (c , out , []string {fmt .Sprintf ("80/tcp -> 0.0.0.0:%d" , 9090 )})
100+ // Port list is not correct
101+ c .Assert (err , checker .IsNil )
102+ }
103+
104+ for i := 0 ; i < 3 ; i ++ {
105+ dockerCmd (c , "rm" , "-f" , IDs [i ])
106+ }
107+ }
108+ testRange ()
109+ // Verify we ran re-use port ranges after they are no longer in use.
110+ testRange ()
111+
112+ // test invalid port ranges
113+ for _ , invalidRange := range []string {"9090-9089:80" , "9090-:80" , "-9090:80" } {
114+ out , _ , err = dockerCmdWithError ("run" , "-d" ,
115+ "-p" , invalidRange ,
116+ "busybox" , "top" )
117+ // Port range should have returned an error
118+ c .Assert (err , checker .NotNil , check .Commentf ("out: %s" , out ))
119+ }
120+
121+ // test host range:container range spec.
122+ out , _ = dockerCmd (c , "run" , "-d" ,
123+ "-p" , "9800-9803:80-83" ,
124+ "busybox" , "top" )
125+ ID = strings .TrimSpace (out )
126+
127+ out , _ = dockerCmd (c , "port" , ID )
128+
129+ err = assertPortList (c , out , []string {
130+ "80/tcp -> 0.0.0.0:9800" ,
131+ "81/tcp -> 0.0.0.0:9801" ,
132+ "82/tcp -> 0.0.0.0:9802" ,
133+ "83/tcp -> 0.0.0.0:9803" })
134+ // Port list is not correct
135+ c .Assert (err , checker .IsNil )
57136 dockerCmd (c , "rm" , "-f" , ID )
58137
59138 // test mixing protocols in same port range
60139 out , _ = dockerCmd (c , "run" , "-d" ,
61- mixPortocalPortImage , "top" )
140+ "-p" , "8000-8080:80" ,
141+ "-p" , "8000-8080:80/udp" ,
142+ "busybox" , "top" )
62143 ID = strings .TrimSpace (out )
63144
64145 out , _ = dockerCmd (c , "port" , ID )
65146
66147 err = assertPortList (c , out , []string {
67- "80/tcp -> 0.0.0.0:80 " ,
68- "81 /udp -> 0.0.0.0:81 " })
148+ "80/tcp -> 0.0.0.0:8000 " ,
149+ "80 /udp -> 0.0.0.0:8000 " })
69150 // Port list is not correct
70151 c .Assert (err , checker .IsNil )
71152 dockerCmd (c , "rm" , "-f" , ID )
@@ -88,22 +169,90 @@ func assertPortList(c *check.C, out string, expected []string) error {
88169 return nil
89170}
90171
172+ func stopRemoveContainer (id string , c * check.C ) {
173+ dockerCmd (c , "rm" , "-f" , id )
174+ }
175+
91176func (s * DockerSuite ) TestUnpublishedPortsInPsOutput (c * check.C ) {
177+ printTestCaseName ()
178+ defer printTestDuration (time .Now ())
179+ pullImageIfNotExist ("busybox" )
92180 testRequires (c , DaemonIsLinux )
181+ // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
93182 port1 := 80
94- port2 := 82
183+ port2 := 443
184+ expose1 := fmt .Sprintf ("--expose=%d" , port1 )
185+ expose2 := fmt .Sprintf ("--expose=%d" , port2 )
186+ dockerCmd (c , "run" , "-d" , expose1 , expose2 , "busybox" , "sleep" , "5" )
187+
95188 unpPort1 := fmt .Sprintf ("%d/tcp" , port1 )
96189 unpPort2 := fmt .Sprintf ("%d/tcp" , port2 )
97190
98- // Run the container auto publish the exposed ports
99- dockerCmd (c , "run" , "-d" , multiPortImage , " sleep" , "35 " )
191+ // Run the container forcing to publish the exposed ports
192+ dockerCmd (c , "run" , "-d" , "-P" , expose1 , expose2 , "busybox" , " sleep" , "5 " )
100193
101194 // Check docker ps o/p for last created container reports the exposed ports in the port bindings
102- expBnd1 := fmt . Sprintf ( " 0.0.0.0:%d->%s" , port1 , unpPort1 )
103- expBnd2 := fmt . Sprintf ( " 0.0.0.0:%d->%s" , port2 , unpPort2 )
195+ expBndRegx1 := regexp . MustCompile ( ` 0.0.0.0:\d+->` + unpPort1 )
196+ expBndRegx2 := regexp . MustCompile ( ` 0.0.0.0:\d+->` + unpPort2 )
104197 out , _ := dockerCmd (c , "ps" , "-n=1" )
105198 // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output
106- c .Assert (strings . Contains (out , expBnd1 ), checker .Equals , true , check .Commentf ("out: %s; unpPort1: %s" , out , unpPort1 ))
199+ c .Assert (expBndRegx1 . MatchString (out ), checker .Equals , true , check .Commentf ("out: %s; unpPort1: %s" , out , unpPort1 ))
107200 // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output
108- c .Assert (strings .Contains (out , expBnd2 ), checker .Equals , true , check .Commentf ("out: %s; unpPort2: %s" , out , unpPort2 ))
201+ c .Assert (expBndRegx2 .MatchString (out ), checker .Equals , true , check .Commentf ("out: %s; unpPort2: %s" , out , unpPort2 ))
202+
203+ // Run the container specifying explicit port bindings for the exposed ports
204+ offset := 10000
205+ pFlag1 := fmt .Sprintf ("%d:%d" , offset + port1 , port1 )
206+ pFlag2 := fmt .Sprintf ("%d:%d" , offset + port2 , port2 )
207+ out , _ = dockerCmd (c , "run" , "-d" , "-p" , pFlag1 , "-p" , pFlag2 , expose1 , expose2 , "busybox" , "sleep" , "5" )
208+ id := strings .TrimSpace (out )
209+
210+ // Check docker ps o/p for last created container reports the specified port mappings
211+ expBnd1 := fmt .Sprintf ("0.0.0.0:%d->%s" , offset + port1 , unpPort1 )
212+ expBnd2 := fmt .Sprintf ("0.0.0.0:%d->%s" , offset + port2 , unpPort2 )
213+ out , _ = dockerCmd (c , "ps" , "-n=1" )
214+ // Cannot find expected port binding (expBnd1) in docker ps output
215+ c .Assert (out , checker .Contains , expBnd1 )
216+ // Cannot find expected port binding (expBnd2) in docker ps output
217+ c .Assert (out , checker .Contains , expBnd2 )
218+
219+ // Remove container now otherwise it will interfere with next test
220+ stopRemoveContainer (id , c )
221+
222+ // Run the container with explicit port bindings and no exposed ports
223+ out , _ = dockerCmd (c , "run" , "-d" , "-p" , pFlag1 , "-p" , pFlag2 , "busybox" , "sleep" , "5" )
224+ id = strings .TrimSpace (out )
225+
226+ // Check docker ps o/p for last created container reports the specified port mappings
227+ out , _ = dockerCmd (c , "ps" , "-n=1" )
228+ // Cannot find expected port binding (expBnd1) in docker ps output
229+ c .Assert (out , checker .Contains , expBnd1 )
230+ // Cannot find expected port binding (expBnd2) in docker ps output
231+ c .Assert (out , checker .Contains , expBnd2 )
232+ // Remove container now otherwise it will interfere with next test
233+ stopRemoveContainer (id , c )
234+
235+ // Run the container with one unpublished exposed port and one explicit port binding
236+ dockerCmd (c , "run" , "-d" , expose1 , "-p" , pFlag2 , "busybox" , "sleep" , "5" )
237+
238+ // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
239+ out , _ = dockerCmd (c , "ps" , "-n=1" )
240+ // Missing port binding (expBnd2) in docker ps output
241+ c .Assert (out , checker .Contains , expBnd2 )
242+ }
243+
244+ func (s * DockerSuite ) TestPortHostBinding (c * check.C ) {
245+ printTestCaseName ()
246+ defer printTestDuration (time .Now ())
247+ pullImageIfNotExist ("busybox" )
248+ testRequires (c , DaemonIsLinux , NotUserNamespace )
249+ out , _ := dockerCmd (c , "run" , "-d" , "-p" , "9876:80" , "busybox" ,
250+ "nc" , "-l" , "-p" , "80" )
251+ firstID := strings .TrimSpace (out )
252+
253+ out , _ = dockerCmd (c , "port" , firstID , "80" )
254+
255+ err := assertPortList (c , out , []string {"0.0.0.0:9876" })
256+ // Port list is not correct
257+ c .Assert (err , checker .IsNil )
109258}
0 commit comments