Skip to content

Commit

Permalink
Merge pull request #57 from aidansteele/fix-nontty
Browse files Browse the repository at this point in the history
Use docker’s stdcopy to ensure we don’t emit garbage bytes to stdout
  • Loading branch information
cplee committed May 23, 2019
2 parents f2cb9e3 + 1d64fef commit d85cabf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
11 changes: 5 additions & 6 deletions container/docker_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/docker/docker/pkg/stdcopy"
"io"
"os"

Expand All @@ -31,12 +32,10 @@ type dockerMessage struct {
}

func (i *DockerExecutorInput) logDockerOutput(dockerResponse io.Reader) {
scanner := bufio.NewScanner(dockerResponse)
if i.Logger == nil {
return
}
for scanner.Scan() {
i.Logger.Infof(scanner.Text())
w := i.Logger.Writer()
_, err := stdcopy.StdCopy(w, w, dockerResponse)
if err != nil {
i.Logger.Error(err)
}
}

Expand Down
55 changes: 55 additions & 0 deletions container/docker_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package container

import (
"bytes"
"context"
"github.com/nektos/act/common"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

type rawFormatter struct{}

func (f *rawFormatter) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(entry.Message), nil
}

func TestNewDockerRunExecutor(t *testing.T) {
if testing.Short() {
t.Skip("skipping slower test")
}

noopLogger := logrus.New()
noopLogger.SetOutput(ioutil.Discard)

buf := &bytes.Buffer{}
logger := logrus.New()
logger.SetOutput(buf)
logger.SetFormatter(&rawFormatter{})

runner := NewDockerRunExecutor(NewDockerRunExecutorInput{
DockerExecutorInput: DockerExecutorInput{
Ctx: context.TODO(),
Logger: logrus.NewEntry(logger),
},
Image: "hello-world",
})

puller := NewDockerPullExecutor(NewDockerPullExecutorInput{
DockerExecutorInput: DockerExecutorInput{
Ctx: context.TODO(),
Logger: logrus.NewEntry(noopLogger),
},
Image: "hello-world",
})

pipeline := common.NewPipelineExecutor(puller, runner)
err := pipeline()
assert.NoError(t, err)

expected := `docker run image=hello-world entrypoint=[] cmd=[]Hello from Docker!`
actual := buf.String()
assert.Equal(t, expected, actual[:len(expected)])
}

0 comments on commit d85cabf

Please sign in to comment.