diff --git a/README.md b/README.md index b00fc282bc..336b0d9f97 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,17 @@ can use it to make a GET: `resp, err := http.Get(fmt.Sprintf("http://%s", ip))` To clean your environment you can defer the container termination `defer nginxC.Terminate(ctx, t)`. `t` is `*testing.T` and it is used to notify is the `defer` failed marking the test as failed. + +## Sending a CMD to a Container + +If you would like to send a CMD (command) to a container, you can pass it in to the container request via the `Cmd` field... + +```go +req := ContainerRequest{ + Image: "alpine", + WaitingFor: wait.ForAll( + wait.ForLog("command override!"), + ), + Cmd: []string{"echo", "command override!"}, +} +``` diff --git a/container.go b/container.go index 71e8dc0b72..4e0e4eca6b 100644 --- a/container.go +++ b/container.go @@ -46,7 +46,7 @@ type ContainerRequest struct { Image string Env map[string]string ExposedPorts []string // allow specifying protocol info - Cmd string + Cmd []string Labels map[string]string BindMounts map[string]string RegistryCred string @@ -55,8 +55,7 @@ type ContainerRequest struct { Privileged bool // for starting privileged container Networks []string // for specifying network names NetworkAliases map[string][]string // for specifying network aliases - - SkipReaper bool // indicates whether we skip setting up a reaper for this + SkipReaper bool // indicates whether we skip setting up a reaper for this } // ProviderType is an enum for the possible providers diff --git a/docker.go b/docker.go index c05ed1540f..257715de2f 100644 --- a/docker.go +++ b/docker.go @@ -300,10 +300,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque Env: env, ExposedPorts: exposedPortSet, Labels: req.Labels, - } - - if req.Cmd != "" { - dockerInput.Cmd = strings.Split(req.Cmd, " ") + Cmd: req.Cmd, } _, _, err = p.client.ImageInspectWithRaw(ctx, req.Image) diff --git a/docker_test.go b/docker_test.go index 702bc9a3a8..d37409175a 100644 --- a/docker_test.go +++ b/docker_test.go @@ -705,3 +705,33 @@ func TestContainerCreationWaitsForLogAndPort(t *testing.T) { } } + +func TestCMD(t *testing.T) { + /* + echo a unique statement to ensure that we + can pass in a command to the ContainerRequest + and it will be run when we run the container + */ + + ctx := context.Background() + + req := ContainerRequest{ + Image: "alpine", + WaitingFor: wait.ForAll( + wait.ForLog("command override!"), + ), + Cmd: []string{"echo", "command override!"}, + } + + c, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + + if err != nil { + t.Fatal(err) + } + + // defer not needed, but keeping it in for consistency + defer c.Terminate(ctx) +}