@@ -5,9 +5,11 @@ import (
55 "strings"
66 "testing"
77
8+ "github.com/cpuguy83/go-docker/container/containerapi"
89 "github.com/cpuguy83/go-docker/errdefs"
910 "github.com/cpuguy83/go-docker/testutils"
1011 "gotest.tools/v3/assert"
12+ "gotest.tools/v3/assert/cmp"
1113)
1214
1315func TestCreate (t * testing.T ) {
@@ -36,4 +38,56 @@ func TestCreate(t *testing.T) {
3638 inspect , err := c .Inspect (ctx )
3739 assert .NilError (t , err )
3840 assert .Equal (t , name , strings .TrimPrefix (inspect .Name , "/" ))
41+
42+ t .Run ("port bindings" , func (t * testing.T ) {
43+ t .Parallel ()
44+
45+ c , err := s .Create (ctx , "busybox:latest" ,
46+ WithCreateName (name ),
47+ WithCreatePortForwarding ("tcp" , 80 ),
48+ WithCreatePortForwarding ("udp" , 81 ),
49+ WithCreatePortForwarding ("udp" , 82 , containerapi.PortBinding {HostIP : "127.0.0.1" }),
50+ WithCreateCmd ("top" ),
51+ )
52+ assert .NilError (t , err )
53+ defer func () {
54+ assert .Check (t , s .Remove (ctx , c .ID (), WithRemoveForce ))
55+ }()
56+
57+ assert .Assert (t , c .ID () != "" )
58+ assert .NilError (t , c .Start (ctx ))
59+
60+ inspect , err := c .Inspect (ctx )
61+ assert .NilError (t , err )
62+ assert .Check (t , cmp .Equal (inspect .Config .ExposedPorts ["80/tcp" ], struct {}{}))
63+
64+ port80 , ok := inspect .NetworkSettings .Ports ["80/tcp" ]
65+ assert .Check (t , ok )
66+ port81 , ok := inspect .NetworkSettings .Ports ["81/udp" ]
67+ assert .Check (t , ok )
68+
69+ port82 , ok := inspect .NetworkSettings .Ports ["82/udp" ]
70+ assert .Check (t , ok )
71+ assert .Check (t , cmp .Equal (port82 [0 ].HostIP , "127.0.0.1" ))
72+
73+ // Depending on the version of docker (and ipv6 support), there maybe be one
74+ // or two bindings.
75+ var hostPort string
76+ for _ , bind := range port80 {
77+ if bind .HostPort != "" {
78+ hostPort = bind .HostPort
79+ break
80+ }
81+ }
82+ assert .Check (t , hostPort != "" , "expected a host port binding for 80/tcp" )
83+
84+ hostPort = ""
85+ for _ , bind := range port81 {
86+ if bind .HostPort != "" {
87+ hostPort = bind .HostPort
88+ break
89+ }
90+ }
91+ assert .Check (t , hostPort != "" , "expected a host port binding for 81/udp" )
92+ })
3993}
0 commit comments