This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It returns stdin, stdout and stderr stream of the specified process in the container. Fixes: #258 Signed-off-by: Peng Tao <[email protected]>
- Loading branch information
Showing
12 changed files
with
361 additions
and
1 deletion.
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
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,91 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
type iostream struct { | ||
sandbox *Sandbox | ||
container *Container | ||
process string | ||
closed bool | ||
} | ||
|
||
// io.WriteCloser | ||
type stdinStream struct { | ||
*iostream | ||
} | ||
|
||
// io.Reader | ||
type stdoutStream struct { | ||
*iostream | ||
} | ||
|
||
// io.Reader | ||
type stderrStream struct { | ||
*iostream | ||
} | ||
|
||
func newIOStream(s *Sandbox, c *Container, proc string) *iostream { | ||
return &iostream{ | ||
sandbox: s, | ||
container: c, | ||
process: proc, | ||
closed: false, // needed to workaround buggy structcheck | ||
} | ||
} | ||
|
||
func (s *iostream) stdin() io.WriteCloser { | ||
return &stdinStream{s} | ||
} | ||
|
||
func (s *iostream) stdout() io.Reader { | ||
return &stdoutStream{s} | ||
} | ||
|
||
func (s *iostream) stderr() io.Reader { | ||
return &stderrStream{s} | ||
} | ||
|
||
func (s *stdinStream) Write(data []byte) (n int, err error) { | ||
if s.closed { | ||
return 0, errors.New("stream closed") | ||
} | ||
|
||
return s.sandbox.agent.writeProcessStdin(s.container, s.process, data) | ||
} | ||
|
||
func (s *stdinStream) Close() error { | ||
if s.closed { | ||
return errors.New("stream closed") | ||
} | ||
|
||
err := s.sandbox.agent.closeProcessStdin(s.container, s.process) | ||
if err == nil { | ||
s.closed = true | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (s *stdoutStream) Read(data []byte) (n int, err error) { | ||
if s.closed { | ||
return 0, errors.New("stream closed") | ||
} | ||
|
||
return s.sandbox.agent.readProcessStdout(s.container, s.process, data) | ||
} | ||
|
||
func (s *stderrStream) Read(data []byte) (n int, err error) { | ||
if s.closed { | ||
return 0, errors.New("stream closed") | ||
} | ||
|
||
return s.sandbox.agent.readProcessStderr(s.container, s.process, data) | ||
} |
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,59 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIOStream(t *testing.T) { | ||
hConfig := newHypervisorConfig(nil, nil) | ||
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, []ContainerConfig{}, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer cleanUp() | ||
|
||
contID := "foo" | ||
processID := "bar" | ||
config := newTestContainerConfigNoop(contID) | ||
c := &Container{ | ||
sandbox: s, | ||
config: &config, | ||
} | ||
|
||
stream := newIOStream(s, c, processID) | ||
stdin := stream.stdin() | ||
stdout := stream.stdout() | ||
stderr := stream.stderr() | ||
|
||
buffer := []byte("randombufferdata") | ||
_, err = stdin.Write(buffer) | ||
assert.Nil(t, err, "stdin write failed: %s", err) | ||
|
||
_, err = stdout.Read(buffer) | ||
assert.Nil(t, err, "stdout read failed: %s", err) | ||
|
||
_, err = stderr.Read(buffer) | ||
assert.Nil(t, err, "stderr read failed: %s", err) | ||
|
||
err = stdin.Close() | ||
assert.Nil(t, err, "stream close failed: %s", err) | ||
|
||
_, err = stdin.Write(buffer) | ||
assert.NotNil(t, err, "stdin write closed should fail") | ||
|
||
_, err = stdout.Read(buffer) | ||
assert.NotNil(t, err, "stdout read closed should fail") | ||
|
||
_, err = stderr.Read(buffer) | ||
assert.NotNil(t, err, "stderr read closed should fail") | ||
|
||
err = stdin.Close() | ||
assert.NotNil(t, err, "stdin close closed should fail") | ||
} |
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
Oops, something went wrong.