Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/test/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ func OptEnv(name, value string) Opt {
}
}

// OptPorts exposes one or more container ports and waits for an exposed port.
// OptPorts exposes one or more container ports and waits for each requested port.
func OptPorts(ports ...string) Opt {
return func(o *opts) error {
o.req.ExposedPorts = ports
o.appendWaitStrategy(wait.ForExposedPort())
for _, port := range ports {
o.appendWaitStrategy(wait.ForListeningPort(nat.Port(port)))
}
return nil
Comment on lines +55 to 62
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptPorts now appends no wait strategy when called with zero ports. Given NewContainer falls back to wait.ForExit() when no strategies are present, an accidental OptPorts() call can cause long-running containers to hang. Consider returning an error when len(ports)==0 (or explicitly documenting/handling the empty case).

Copilot uses AI. Check for mistakes.
}
}
Expand Down
20 changes: 14 additions & 6 deletions pkg/test/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ func Test_Opt_001(t *testing.T) {
require.NoError(OptEntrypoint("/bin/sh", "-c")(&o))
require.NoError(OptCommand([]string{"echo", "hello"})(&o))
require.NoError(OptEnv("A", "1")(&o))
require.NoError(OptPorts("3389/tcp")(&o))
require.NoError(OptPorts("3389/tcp", "389/tcp")(&o))
require.NoError(OptFile(testcontainers.ContainerFile{
Reader: strings.NewReader("echo bootstrap\n"),
ContainerFilePath: "/bootstrap.sh",
FileMode: 0o755,
})(&o))
require.NoError(OptWait(wait.ForListeningPort(nat.Port("3389/tcp")))(&o))
require.NoError(OptWaitLog("server ready")(&o))

assert.Equal([]string{"/bin/sh", "-c"}, o.req.Entrypoint)
assert.Equal([]string{"echo", "hello"}, o.req.Cmd)
assert.Equal("1", o.req.Env["A"])
assert.Equal([]string{"3389/tcp"}, o.req.ExposedPorts)
assert.Equal([]string{"3389/tcp", "389/tcp"}, o.req.ExposedPorts)
if assert.Len(o.req.Files, 1) {
assert.Equal("/bootstrap.sh", o.req.Files[0].ContainerFilePath)
assert.Equal(int64(0o755), o.req.Files[0].FileMode)
Expand All @@ -42,9 +41,18 @@ func Test_Opt_001(t *testing.T) {
multi, ok := o.req.WaitingFor.(*wait.MultiStrategy)
require.True(ok)
assert.Len(multi.Strategies, 3)
assert.IsType(wait.ForExposedPort(), multi.Strategies[0])
assert.IsType(wait.ForListeningPort(nat.Port("3389/tcp")), multi.Strategies[1])
assert.IsType(wait.ForLog("server ready"), multi.Strategies[2])

strategy0, ok := multi.Strategies[0].(*wait.HostPortStrategy)
require.True(ok)
assert.Equal(nat.Port("3389/tcp"), strategy0.Port)

strategy1, ok := multi.Strategies[1].(*wait.HostPortStrategy)
require.True(ok)
assert.Equal(nat.Port("389/tcp"), strategy1.Port)

strategy2, ok := multi.Strategies[2].(*wait.LogStrategy)
require.True(ok)
assert.Equal("server ready", strategy2.Log)
}

func Test_Opt_002(t *testing.T) {
Expand Down
Loading