Skip to content

Commit

Permalink
feat: exec multiplexed to return stderr (#1933)
Browse files Browse the repository at this point in the history
Co-authored-by: Augusto Junior <augusto.junior@vm.co.mz>
  • Loading branch information
gutocondela and Augusto Junior committed Dec 1, 2023
1 parent 5046458 commit 927b11b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
28 changes: 28 additions & 0 deletions docker_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ func TestExecWithOptions(t *testing.T) {
}
}

func TestExecWithMultiplexedStderrResponse(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: nginxAlpineImage,
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, container)

code, reader, err := container.Exec(ctx, []string{"ls", "/non-existing-directory"}, tcexec.Multiplexed())
require.NoError(t, err)
require.NotZero(t, code)
require.NotNil(t, reader)

b, err := io.ReadAll(reader)
require.NoError(t, err)
require.NotNil(t, b)

str := string(b)
require.Contains(t, str, "No such file or directory")
}

func TestExecWithNonMultiplexedResponse(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Expand Down
6 changes: 5 additions & 1 deletion exec/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func Multiplexed() ProcessOption {

<-done

opts.Reader = &outBuff
if errBuff.Bytes() != nil {
opts.Reader = &errBuff
} else {
opts.Reader = &outBuff
}
})
}
2 changes: 1 addition & 1 deletion modules/compose/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ require (
github.com/secure-systems-lab/go-securesystemslib v0.4.0 // indirect
github.com/serialx/hashring v0.0.0-20190422032157-8b2912629002 // indirect
github.com/shibumi/go-pathspec v1.3.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.9 // indirect
github.com/shirou/gopsutil/v3 v3.23.10 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cobra v1.7.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions modules/compose/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ github.com/shibumi/go-pathspec v1.3.0 h1:QUyMZhFo0Md5B8zV8x2tesohbb5kfbpTi9rBnKh
github.com/shibumi/go-pathspec v1.3.0/go.mod h1:Xutfslp817l2I1cZvgcfeMQJG5QnU2lh5tVaaMCl3jE=
github.com/shirou/gopsutil/v3 v3.23.9 h1:ZI5bWVeu2ep4/DIxB4U9okeYJ7zp/QLTO4auRb/ty/E=
github.com/shirou/gopsutil/v3 v3.23.9/go.mod h1:x/NWSb71eMcjFIO0vhyGW5nZ7oSIgVjrCnADckb85GA=
github.com/shirou/gopsutil/v3 v3.23.10/go.mod h1:JIE26kpucQi+innVlAUnIEOSBhBUkirr5b44yr55+WE=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
Expand Down

0 comments on commit 927b11b

Please sign in to comment.