From 64133240911848af17f4661d166abfd490e4e2cb Mon Sep 17 00:00:00 2001 From: Andy Walker Date: Sun, 16 Oct 2022 00:53:27 -0400 Subject: [PATCH 1/3] allow resource.Exec to wait for process completion - This bug was due to a bug in the vendored client library where StartExec would return early even if opts.Detatch was true, if stderr and stdout were not provided. This fix always attaches them and falls back to io.Discard if the user does not provide their own readers. - See: https://github.com/fsouza/go-dockerclient/issues/838 - Fixes #372 --- dockertest.go | 14 ++++++++++++-- dockertest_test.go | 28 +++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/dockertest.go b/dockertest.go index 396e6c79..7d64ac65 100644 --- a/dockertest.go +++ b/dockertest.go @@ -117,8 +117,8 @@ func (r *Resource) Exec(cmd []string, opts ExecOptions) (exitCode int, err error Container: r.Container.ID, Cmd: cmd, Env: opts.Env, - AttachStderr: opts.StdErr != nil, - AttachStdout: opts.StdOut != nil, + AttachStderr: true, + AttachStdout: true, AttachStdin: opts.StdIn != nil, Tty: opts.TTY, }) @@ -126,6 +126,16 @@ func (r *Resource) Exec(cmd []string, opts ExecOptions) (exitCode int, err error return -1, errors.Wrap(err, "Create exec failed") } + // Always attach stderr/stdout, even if not specified, to ensure that exec + // waits with opts.Detach as true (default) + // ref: https://github.com/fsouza/go-dockerclient/issues/838 + if opts.StdErr == nil { + opts.StdErr = io.Discard + } + if opts.StdOut == nil { + opts.StdOut = io.Discard + } + err = r.pool.Client.StartExec(exec.ID, dc.StartExecOptions{ InputStream: opts.StdIn, OutputStream: opts.StdOut, diff --git a/dockertest_test.go b/dockertest_test.go index dd4a8a36..f01597bd 100644 --- a/dockertest_test.go +++ b/dockertest_test.go @@ -20,8 +20,10 @@ import ( "github.com/stretchr/testify/require" ) -var docker = os.Getenv("DOCKER_URL") -var pool *Pool +var ( + docker = os.Getenv("DOCKER_URL") + pool *Pool +) func TestMain(m *testing.M) { var err error @@ -65,7 +67,6 @@ func TestMongo(t *testing.T) { err = pool.Retry(func() error { response, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s", port)) - if err != nil { return err } @@ -197,7 +198,7 @@ func TestBuildImage(t *testing.T) { dockerfilePath := dir + "/Dockerfile" ioutil.WriteFile(dockerfilePath, []byte("FROM postgres:9.5"), - 0644, + 0o644, ) resource, err := pool.BuildAndRun("postgres-test", dockerfilePath, nil) @@ -219,7 +220,7 @@ ARG foo RUN echo -n $foo > /build-time-value CMD sleep 10 `)), - 0644, + 0o644, ) resource, err := pool.BuildAndRunWithBuildOptions( @@ -467,3 +468,20 @@ func TestClientRaceCondition(t *testing.T) { }) } } + +func TestExecStatus(t *testing.T) { + resource, err := pool.RunWithOptions(&RunOptions{ + Repository: "alpine", + Tag: "3.16", + Cmd: []string{"tail", "-f", "/dev/null"}, + }) + //resource, err := pool.Run("alpine", "3.16", nil) + defer resource.Close() + require.Nil(t, err) + exitCode, err := resource.Exec([]string{"/bin/false"}, ExecOptions{}) + require.Nil(t, err) + require.Equal(t, 1, exitCode) + exitCode, err = resource.Exec([]string{"/bin/sh", "-c", "/bin/sleep 2 && exit 42"}, ExecOptions{}) + require.Nil(t, err) + require.Equal(t, 42, exitCode) +} From f197f0913b2a0221e9ba297a0613ced31dca7909 Mon Sep 17 00:00:00 2001 From: Andy Walker Date: Sun, 16 Oct 2022 01:00:07 -0400 Subject: [PATCH 2/3] comment typo - s/true/false/ --- dockertest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockertest.go b/dockertest.go index 7d64ac65..0c2714b6 100644 --- a/dockertest.go +++ b/dockertest.go @@ -127,7 +127,7 @@ func (r *Resource) Exec(cmd []string, opts ExecOptions) (exitCode int, err error } // Always attach stderr/stdout, even if not specified, to ensure that exec - // waits with opts.Detach as true (default) + // waits with opts.Detach as false (default) // ref: https://github.com/fsouza/go-dockerclient/issues/838 if opts.StdErr == nil { opts.StdErr = io.Discard From 7b4fdebb5d7b0b3d21eedde59c36228550f28ca9 Mon Sep 17 00:00:00 2001 From: Andy Walker Date: Sun, 16 Oct 2022 01:01:50 -0400 Subject: [PATCH 3/3] hanging comment --- dockertest_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/dockertest_test.go b/dockertest_test.go index f01597bd..0661b80e 100644 --- a/dockertest_test.go +++ b/dockertest_test.go @@ -475,7 +475,6 @@ func TestExecStatus(t *testing.T) { Tag: "3.16", Cmd: []string{"tail", "-f", "/dev/null"}, }) - //resource, err := pool.Run("alpine", "3.16", nil) defer resource.Close() require.Nil(t, err) exitCode, err := resource.Exec([]string{"/bin/false"}, ExecOptions{})