@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ "net"
56 "regexp"
67 "sort"
78 "strings"
@@ -245,3 +246,49 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
245246 c .Errorf ("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s" , unpPort1 , expBnd2 , out )
246247 }
247248}
249+
250+ func (s * DockerSuite ) TestPortHostBinding (c * check.C ) {
251+ out , _ := dockerCmd (c , "run" , "-d" , "-p" , "9876:80" , "busybox" ,
252+ "nc" , "-l" , "-p" , "80" )
253+ firstID := strings .TrimSpace (out )
254+
255+ out , _ = dockerCmd (c , "port" , firstID , "80" )
256+
257+ if ! assertPortList (c , out , []string {"0.0.0.0:9876" }) {
258+ c .Error ("Port list is not correct" )
259+ }
260+
261+ dockerCmd (c , "run" , "--net=host" , "busybox" ,
262+ "nc" , "localhost" , "9876" )
263+
264+ dockerCmd (c , "rm" , "-f" , firstID )
265+
266+ if _ , _ , err := dockerCmdWithError ("run" , "--net=host" , "busybox" ,
267+ "nc" , "localhost" , "9876" ); err == nil {
268+ c .Error ("Port is still bound after the Container is removed" )
269+ }
270+ }
271+
272+ func (s * DockerSuite ) TestPortExposeHostBinding (c * check.C ) {
273+ out , _ := dockerCmd (c , "run" , "-d" , "-P" , "--expose" , "80" , "busybox" ,
274+ "nc" , "-l" , "-p" , "80" )
275+ firstID := strings .TrimSpace (out )
276+
277+ out , _ = dockerCmd (c , "port" , firstID , "80" )
278+
279+ _ , exposedPort , err := net .SplitHostPort (out )
280+
281+ if err != nil {
282+ c .Fatal (out , err )
283+ }
284+
285+ dockerCmd (c , "run" , "--net=host" , "busybox" ,
286+ "nc" , "localhost" , strings .TrimSpace (exposedPort ))
287+
288+ dockerCmd (c , "rm" , "-f" , firstID )
289+
290+ if _ , _ , err = dockerCmdWithError ("run" , "--net=host" , "busybox" ,
291+ "nc" , "localhost" , strings .TrimSpace (exposedPort )); err == nil {
292+ c .Error ("Port is still bound after the Container is removed" )
293+ }
294+ }
0 commit comments