Skip to content

Commit

Permalink
feat: allow viewing of logs from stopped container
Browse files Browse the repository at this point in the history
fixes #118

setting any container besides a reaper to not auto-remove when creating
  • Loading branch information
ClaytonNorthey92 committed Dec 25, 2019
1 parent 0c6fcb4 commit f79a5b2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 41 additions & 3 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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"]`,
},
Expand Down Expand Up @@ -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 . .
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f79a5b2

Please sign in to comment.