From 109fc3b414e5a62ca67beefe8c8fe7e6aa8ad46c Mon Sep 17 00:00:00 2001 From: Mykhailo Bobrovskyi Date: Mon, 13 Feb 2023 10:26:22 +0200 Subject: [PATCH] Add ExposedPorts option. --- dktest.go | 15 ++++++++------- dktest_test.go | 17 +++++++++++++++++ options.go | 3 ++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/dktest.go b/dktest.go index e30e4e9..751ed60 100644 --- a/dktest.go +++ b/dktest.go @@ -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, diff --git a/dktest_test.go b/dktest_test.go index 436312e..4eefd98 100644 --- a/dktest_test.go +++ b/dktest_test.go @@ -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") + } + }) +} diff --git a/options.go b/options.go index 52c5ba3..09f2910 100644 --- a/options.go +++ b/options.go @@ -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() {