Skip to content

Commit

Permalink
Merge pull request #87 from ClaytonNorthey92/feature/cmd-as-slice
Browse files Browse the repository at this point in the history
Using a slice of strings ([]string) for Docker Cmd.
  • Loading branch information
gianarb authored Sep 4, 2019
2 parents 44956d7 + 97f3a28 commit 733f65a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"},
}
```
5 changes: 2 additions & 3 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 733f65a

Please sign in to comment.