Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver/docker: collect tty container logs #5609

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions drivers/docker/docklog/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (c *dockerLoggerClient) Start(opts *StartOpts) error {
ContainerId: opts.ContainerID,
StdoutFifo: opts.Stdout,
StderrFifo: opts.Stderr,
Tty: opts.TTY,

TlsCert: opts.TLSCert,
TlsKey: opts.TLSKey,
Expand Down
2 changes: 2 additions & 0 deletions drivers/docker/docklog/docker_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type StartOpts struct {

// ContainerID of the container to monitor logs for
ContainerID string
TTY bool

// Stdout path to fifo
Stdout string
Expand Down Expand Up @@ -97,6 +98,7 @@ func (d *dockerLogger) Start(opts *StartOpts) error {
Follow: true,
Stdout: true,
Stderr: true,
RawTerminal: opts.TTY,
}

err := client.Logs(logOpts)
Expand Down
83 changes: 83 additions & 0 deletions drivers/docker/docklog/docker_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,89 @@ func TestDockerLogger_Success(t *testing.T) {
})
}

func TestDockerLogger_Success_TTY(t *testing.T) {
ctu.DockerCompatible(t)

t.Parallel()
require := require.New(t)

containerImage, containerImageName, containerImageTag := testContainerDetails()

client, err := docker.NewClientFromEnv()
if err != nil {
t.Skip("docker unavailable:", err)
}

if img, err := client.InspectImage(containerImage); err != nil || img == nil {
t.Log("image not found locally, downloading...")
err = client.PullImage(docker.PullImageOptions{
Repository: containerImageName,
Tag: containerImageTag,
}, docker.AuthConfiguration{})
require.NoError(err, "failed to pull image")
}

containerConf := docker.CreateContainerOptions{
Config: &docker.Config{
Cmd: []string{
"sh", "-c", "touch ~/docklog; tail -f ~/docklog",
},
Image: containerImage,
Tty: true,
},
Context: context.Background(),
}

container, err := client.CreateContainer(containerConf)
require.NoError(err)

defer client.RemoveContainer(docker.RemoveContainerOptions{
ID: container.ID,
Force: true,
})

err = client.StartContainer(container.ID, nil)
require.NoError(err)

testutil.WaitForResult(func() (bool, error) {
container, err = client.InspectContainer(container.ID)
if err != nil {
return false, err
}
if !container.State.Running {
return false, fmt.Errorf("container not running")
}
return true, nil
}, func(err error) {
require.NoError(err)
})

stdout := &noopCloser{bytes.NewBuffer(nil)}
stderr := &noopCloser{bytes.NewBuffer(nil)}

dl := NewDockerLogger(testlog.HCLogger(t)).(*dockerLogger)
dl.stdout = stdout
dl.stderr = stderr
require.NoError(dl.Start(&StartOpts{
ContainerID: container.ID,
TTY: true,
}))

echoToContainer(t, client, container.ID, "abc")
echoToContainer(t, client, container.ID, "123")

testutil.WaitForResult(func() (bool, error) {
act := stdout.String()
if "abc\r\n123\r\n" != act {
return false, fmt.Errorf("expected abc\\n123\\n for stdout but got %s", act)
}

return true, nil
}, func(err error) {
require.NoError(err)
})
}

func echoToContainer(t *testing.T, client *docker.Client, id string, line string) {
op := docker.CreateExecOptions{
Container: id,
Expand Down
69 changes: 39 additions & 30 deletions drivers/docker/docklog/proto/docker_logger.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions drivers/docker/docklog/proto/docker_logger.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ message StartRequest {
string tls_cert = 5;
string tls_key = 6;
string tls_ca = 7;
bool tty = 8;
}

message StartResponse {
Expand Down
1 change: 1 addition & 0 deletions drivers/docker/docklog/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (s *dockerLoggerServer) Start(ctx context.Context, req *proto.StartRequest)
ContainerID: req.ContainerId,
Stdout: req.StdoutFifo,
Stderr: req.StderrFifo,
TTY: req.Tty,

TLSCert: req.TlsCert,
TLSKey: req.TlsKey,
Expand Down
1 change: 1 addition & 0 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (d *Driver) setupNewDockerLogger(container *docker.Container, cfg *drivers.
if err := dlogger.Start(&docklog.StartOpts{
Endpoint: d.config.Endpoint,
ContainerID: container.ID,
TTY: container.Config.Tty,
Stdout: cfg.StdoutPath,
Stderr: cfg.StderrPath,
TLSCert: d.config.TLS.Cert,
Expand Down