Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
51 changes: 50 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"archive/tar"
"bufio"
"bytes"
"context"
"encoding/binary"
Expand Down Expand Up @@ -260,11 +261,59 @@ func (c *DockerContainer) inspectContainer(ctx context.Context) (*types.Containe
// Logs will fetch both STDOUT and STDERR from the current container. Returns a
// ReadCloser and leaves it up to the caller to extract what it wants.
func (c *DockerContainer) Logs(ctx context.Context) (io.ReadCloser, error) {

const streamHeaderSize = 8

options := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
}
return c.provider.client.ContainerLogs(ctx, c.ID, options)

rc, err := c.provider.client.ContainerLogs(ctx, c.ID, options)
if err != nil {
return nil, err
}

pr, pw := io.Pipe()
r := bufio.NewReader(rc)

go func() {
var (
isPrefix = true
lineStarted = true
line []byte
)
for err == nil {
line, isPrefix, err = r.ReadLine()

if lineStarted && len(line) >= streamHeaderSize {
line = line[streamHeaderSize:] // trim stream header
lineStarted = false
}
if !isPrefix {
lineStarted = true
}

_, errW := pw.Write(line)
if errW != nil {
return
}

if !isPrefix {
_, errW := pw.Write([]byte("\n"))
if errW != nil {
return
}
}

if err != nil {
_ = pw.CloseWithError(err)
return
}
}
}()

return pr, nil
}

// FollowOutput adds a LogConsumer to be sent logs from the container's
Expand Down
31 changes: 30 additions & 1 deletion logconsumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -143,6 +145,7 @@ func Test_ShouldRecognizeLogTypes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer func() { _ = c.Terminate(ctx) }()

ep, err := c.Endpoint(ctx, "http")
if err != nil {
Expand Down Expand Up @@ -183,7 +186,6 @@ func Test_ShouldRecognizeLogTypes(t *testing.T) {
StdoutLog: "echo this-is-stdout\n",
StderrLog: "echo this-is-stderr\n",
}, g.LogTypes)
_ = c.Terminate(ctx)
}

func TestContainerLogWithErrClosed(t *testing.T) {
Expand Down Expand Up @@ -304,3 +306,30 @@ func TestContainerLogWithErrClosed(t *testing.T) {
)
}
}

func TestContainerLogsShouldBeWithoutStreamHeader(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: "alpine:latest",
Cmd: []string{"sh", "-c", "id -u"},
WaitingFor: wait.ForExit(),
}
container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatal(err)
}
defer container.Terminate(ctx)
r, err := container.Logs(ctx)
if err != nil {
t.Fatal(err)
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "0", strings.TrimSpace(string(b)))
}