From 1d64fef1a13a810550b3bba08bfaa23c7c63522b Mon Sep 17 00:00:00 2001 From: Aidan Steele Date: Tue, 9 Apr 2019 20:45:58 +1000 Subject: [PATCH] =?UTF-8?q?Use=20docker=E2=80=99s=20stdcopy=20to=20ensure?= =?UTF-8?q?=20we=20don=E2=80=99t=20emit=20garbage=20bytes=20to=20stdout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- container/docker_common.go | 11 ++++---- container/docker_run_test.go | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 container/docker_run_test.go diff --git a/container/docker_common.go b/container/docker_common.go index 1a33eb0a42b..5d9352e995a 100644 --- a/container/docker_common.go +++ b/container/docker_common.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/docker/docker/pkg/stdcopy" "io" "os" @@ -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) } } diff --git a/container/docker_run_test.go b/container/docker_run_test.go new file mode 100644 index 00000000000..a1dfd9c4e3a --- /dev/null +++ b/container/docker_run_test.go @@ -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)]) +}