diff --git a/container.go b/container.go index 0f0ca1686b..6f28134bc9 100644 --- a/container.go +++ b/container.go @@ -76,6 +76,7 @@ type ContainerRequest struct { NetworkAliases map[string][]string // for specifying network aliases SkipReaper bool // indicates whether we skip setting up a reaper for this ReaperImage string // alternative reaper image + AutoRemove bool // if set to true, the container will be removed from the host when stopped } // ProviderType is an enum for the possible providers diff --git a/container_test.go b/container_test.go index 52c33390fe..316cc4369f 100644 --- a/container_test.go +++ b/container_test.go @@ -5,6 +5,8 @@ import ( "bytes" "context" "io" + "io/ioutil" + "strings" "testing" "time" @@ -128,7 +130,7 @@ func Test_BuildImageWithContexts(t *testing.T) { Contents string }{ { - Name: "Dockerfile", + Name: "Dockerfile", Contents: `FROM alpine CMD ["echo", "this is from the archive"]`, }, @@ -172,11 +174,11 @@ func Test_BuildImageWithContexts(t *testing.T) { Contents string }{ { - Name: "say_hi.sh", + Name: "say_hi.sh", Contents: `echo hi this is from the say_hi.sh file!`, }, { - Name: "Dockerfile", + Name: "Dockerfile", Contents: `FROM alpine WORKDIR /app COPY . . @@ -265,3 +267,39 @@ func Test_BuildImageWithContexts(t *testing.T) { } } + +func Test_GetLogsFromFailedContainer(t *testing.T) { + ctx := context.Background() + req := ContainerRequest{ + Image: "alpine", + Cmd: []string{"echo", "-n", "I was not expecting this"}, + WaitingFor: wait.ForLog("I was expecting this").WithStartupTimeout(5 * time.Second), + } + + c, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + + if err != nil && err.Error() != "failed to start container: context deadline exceeded" { + t.Fatal(err) + } else if err == nil { + c.Terminate(ctx) + t.Fatal("was expecting error starting container") + } + + logs, logErr := c.Logs(ctx) + if logErr != nil { + t.Fatal(logErr) + } + + b, err := ioutil.ReadAll(logs) + if err != nil { + t.Fatal(err) + } + + log := string(b) + if strings.Contains(log, "I was not expecting this") == false { + t.Fatalf("could not find expected log in %s", log) + } +} diff --git a/docker.go b/docker.go index 12d1949885..918467b9b2 100644 --- a/docker.go +++ b/docker.go @@ -443,7 +443,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque hostConfig := &container.HostConfig{ PortBindings: exposedPortMap, Mounts: mounts, - AutoRemove: true, + AutoRemove: req.AutoRemove, Privileged: req.Privileged, } diff --git a/reaper.go b/reaper.go index ca465111b0..6caa3ba948 100644 --- a/reaper.go +++ b/reaper.go @@ -52,6 +52,7 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r BindMounts: map[string]string{ "/var/run/docker.sock": "/var/run/docker.sock", }, + AutoRemove: true, } c, err := provider.RunContainer(ctx, req)