Skip to content

Commit

Permalink
Merge pull request #21 from mbobrovskyi/master
Browse files Browse the repository at this point in the history
Add ExposePorts Option
  • Loading branch information
dhui authored Sep 8, 2024
2 parents 072d182 + 109fc3b commit 4619817
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
15 changes: 8 additions & 7 deletions dktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ func runImage(ctx context.Context, lgr Logger, dc client.ContainerAPIClient, img
opts Options) (ContainerInfo, error) {
c := ContainerInfo{Name: genContainerName(), ImageName: imgName}
createResp, err := dc.ContainerCreate(ctx, &container.Config{
Image: imgName,
Labels: map[string]string{label: "true"},
Env: opts.env(),
Entrypoint: opts.Entrypoint,
Cmd: opts.Cmd,
Volumes: opts.volumes(),
Hostname: opts.Hostname,
Image: imgName,
Labels: map[string]string{label: "true"},
Env: opts.env(),
Entrypoint: opts.Entrypoint,
Cmd: opts.Cmd,
Volumes: opts.volumes(),
Hostname: opts.Hostname,
ExposedPorts: opts.ExposedPorts,
}, &container.HostConfig{
PublishAllPorts: true,
PortBindings: opts.PortBindings,
Expand Down
17 changes: 17 additions & 0 deletions dktest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,20 @@ func TestRunWithNetworkPortBinding(t *testing.T) {
dktest.Run(t, testNetworkImage, dktest.Options{ReadyFunc: nginxReady, PortRequired: true,
PortBindings: nat.PortMap{port: []nat.PortBinding{{HostPort: "8181"}}}}, noop)
}

func TestRunWithExposesPorts(t *testing.T) {
port, err := nat.NewPort("tcp", "8080")
if err != nil {
t.Fatal("Invalid port:", err)
}

dktest.Run(t, testNetworkImage, dktest.Options{
ReadyFunc: nginxReady,
PortRequired: true,
ExposedPorts: nat.PortSet{port: struct{}{}},
}, func(t *testing.T, info dktest.ContainerInfo) {
if _, ok := info.Ports[port]; !ok {
t.Fatal("port not exposed")
}
})
}
3 changes: 2 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type Options struct {
Mounts []mount.Mount
Hostname string
// Platform specifies the platform of the docker image that is pulled.
Platform string
Platform string
ExposedPorts nat.PortSet
}

func (o *Options) init() {
Expand Down

0 comments on commit 4619817

Please sign in to comment.