@@ -3,6 +3,7 @@ package dockertest
3
3
import (
4
4
"fmt"
5
5
"io/ioutil"
6
+ "net"
6
7
"os"
7
8
"path/filepath"
8
9
"runtime"
@@ -22,6 +23,7 @@ type Pool struct {
22
23
23
24
// Resource represents a docker container.
24
25
type Resource struct {
26
+ pool * Pool
25
27
Container * dc.Container
26
28
}
27
29
@@ -60,6 +62,32 @@ func (r *Resource) GetBoundIP(id string) string {
60
62
return m [0 ].HostIP
61
63
}
62
64
65
+ // GetHostPort returns a resource's published port with an address.
66
+ func (r * Resource ) GetHostPort (portID string ) string {
67
+ if r .Container == nil {
68
+ return ""
69
+ } else if r .Container .NetworkSettings == nil {
70
+ return ""
71
+ }
72
+
73
+ m , ok := r .Container .NetworkSettings .Ports [dc .Port (portID )]
74
+ if ! ok {
75
+ return ""
76
+ } else if len (m ) == 0 {
77
+ return ""
78
+ }
79
+ ip := m [0 ].HostIP
80
+ if ip == "0.0.0.0" {
81
+ ip = "localhost"
82
+ }
83
+ return net .JoinHostPort (ip , m [0 ].HostPort )
84
+ }
85
+
86
+ // Close removes a container and linked volumes from docker by calling pool.Purge.
87
+ func (r * Resource ) Close () error {
88
+ return r .pool .Purge (r )
89
+ }
90
+
63
91
// NewTLSPool creates a new pool given an endpoint and the certificate path. This is required for endpoints that
64
92
// require TLS communication.
65
93
func NewTLSPool (endpoint , certpath string ) (* Pool , error ) {
@@ -221,7 +249,7 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
221
249
Cmd : cmd ,
222
250
Mounts : mounts ,
223
251
ExposedPorts : exp ,
224
- WorkingDir : wd ,
252
+ WorkingDir : wd ,
225
253
},
226
254
HostConfig : & dc.HostConfig {
227
255
PublishAllPorts : true ,
@@ -245,6 +273,7 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
245
273
}
246
274
247
275
return & Resource {
276
+ pool : d ,
248
277
Container : c ,
249
278
}, nil
250
279
}
0 commit comments