Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not prepend garbage in the container.Exec response (#624)
* chore: add a test demonstrating the bug * fix: handle container exec attach using TTY * Revert "fix: handle container exec attach using TTY" This reverts commit 5733288. * chore: handle multiplexed output
- Loading branch information
1 parent
1f88e88
commit 4c4def7
Showing
8 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package testcontainers | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
tcexec "github.com/testcontainers/testcontainers-go/exec" | ||
) | ||
|
||
func TestExecWithMultiplexedResponse(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", "/usr/share/nginx"}, tcexec.Multiplexed()) | ||
require.NoError(t, err) | ||
require.Zero(t, code) | ||
require.NotNil(t, reader) | ||
|
||
b, err := io.ReadAll(reader) | ||
require.NoError(t, err) | ||
require.NotNil(t, b) | ||
|
||
str := string(b) | ||
require.Equal(t, "html\n", str) | ||
} | ||
|
||
func TestExecWithNonMultiplexedResponse(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", "/usr/share/nginx"}) | ||
require.NoError(t, err) | ||
require.Zero(t, code) | ||
require.NotNil(t, reader) | ||
|
||
b, err := io.ReadAll(reader) | ||
require.NoError(t, err) | ||
require.NotNil(t, b) | ||
|
||
str := string(b) | ||
require.True(t, strings.HasSuffix(str, "html\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package exec | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
|
||
"github.com/docker/docker/pkg/stdcopy" | ||
) | ||
|
||
// ProcessOptions defines options applicable to the reader processor | ||
type ProcessOptions struct { | ||
Reader io.Reader | ||
} | ||
|
||
// ProcessOption defines a common interface to modify the reader processor | ||
// These options can be passed to the Exec function in a variadic way to customize the returned Reader instance | ||
type ProcessOption interface { | ||
Apply(opts *ProcessOptions) | ||
} | ||
|
||
type ProcessOptionFunc func(opts *ProcessOptions) | ||
|
||
func (fn ProcessOptionFunc) Apply(opts *ProcessOptions) { | ||
fn(opts) | ||
} | ||
|
||
func Multiplexed() ProcessOption { | ||
return ProcessOptionFunc(func(opts *ProcessOptions) { | ||
done := make(chan struct{}) | ||
|
||
var outBuff bytes.Buffer | ||
var errBuff bytes.Buffer | ||
go func() { | ||
if _, err := stdcopy.StdCopy(&outBuff, &errBuff, opts.Reader); err != nil { | ||
return | ||
} | ||
close(done) | ||
}() | ||
|
||
<-done | ||
|
||
opts.Reader = &outBuff | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters