Skip to content

Commit

Permalink
add NewDirectIOWithTerminal; add test for pty
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Hazlett <[email protected]>
  • Loading branch information
ehazlett committed Apr 25, 2018
1 parent 209a7fc commit 6b4355d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
10 changes: 10 additions & 0 deletions cio/io_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,18 @@ func openFifos(ctx context.Context, fifos *FIFOSet) (pipes, error) {
// NewDirectIO returns an IO implementation that exposes the IO streams as io.ReadCloser
// and io.WriteCloser.
func NewDirectIO(ctx context.Context, fifos *FIFOSet) (*DirectIO, error) {
return newDirectIO(ctx, fifos, false)
}

// NewDirectIOWithTerminal returns an IO implementation that exposes the streams with terminal enabled
func NewDirectIOWithTerminal(ctx context.Context, fifos *FIFOSet) (*DirectIO, error) {
return newDirectIO(ctx, fifos, true)
}

func newDirectIO(ctx context.Context, fifos *FIFOSet, terminal bool) (*DirectIO, error) {
ctx, cancel := context.WithCancel(ctx)
pipes, err := openFifos(ctx, fifos)
fifos.Config.Terminal = terminal
return &DirectIO{
pipes: pipes,
cio: cio{
Expand Down
96 changes: 89 additions & 7 deletions container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,76 @@ func TestDaemonRestart(t *testing.T) {
<-statusC
}

func TestContainerPTY(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext()
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}

container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithTTY, withProcessArgs("echo", "hello")), WithNewSnapshot(id, image))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

direct, err := newDirectIOWithTerminal(ctx)
if err != nil {
t.Fatal(err)
}
defer direct.Delete()
var (
wg sync.WaitGroup
buf = bytes.NewBuffer(nil)
)
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(buf, direct.Stdout)
}()

task, err := container.NewTask(ctx, direct.IOCreate)
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)

status, err := task.Wait(ctx)
if err != nil {
t.Error(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}

<-status
wg.Wait()

if err := direct.Close(); err != nil {
t.Error(err)
}

out := buf.String()
if !strings.ContainsAny(fmt.Sprintf("%#q", out), `\x00`) {
t.Fatal(`expected \x00 in output`)
}
}

func TestContainerAttach(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -290,7 +360,7 @@ func TestContainerAttach(t *testing.T) {

expected := "hello" + newLine

direct, err := newDirectIO(ctx)
direct, err := newDirectIOStandard(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -359,12 +429,24 @@ func TestContainerAttach(t *testing.T) {
}
}

func newDirectIO(ctx context.Context) (*directIO, error) {
func newDirectIOStandard(ctx context.Context) (*directIO, error) {
return newDirectIO(ctx, false)
}

func newDirectIOWithTerminal(ctx context.Context) (*directIO, error) {
return newDirectIO(ctx, true)
}

func newDirectIO(ctx context.Context, terminal bool) (*directIO, error) {
fifos, err := cio.NewFIFOSetInDir("", "", false)
if err != nil {
return nil, err
}
dio, err := cio.NewDirectIO(ctx, fifos)
f := cio.NewDirectIO
if terminal {
f = cio.NewDirectIOWithTerminal
}
dio, err := f(ctx, fifos)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -426,7 +508,7 @@ func TestContainerUsername(t *testing.T) {
if err != nil {
t.Fatal(err)
}
direct, err := newDirectIO(ctx)
direct, err := newDirectIOStandard(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -501,7 +583,7 @@ func testContainerUser(t *testing.T, userstr, expectedOutput string) {
if err != nil {
t.Fatal(err)
}
direct, err := newDirectIO(ctx)
direct, err := newDirectIOStandard(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -586,7 +668,7 @@ func TestContainerAttachProcess(t *testing.T) {
expected := "hello" + newLine

// creating IO early for easy resource cleanup
direct, err := newDirectIO(ctx)
direct, err := newDirectIOStandard(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -693,7 +775,7 @@ func TestContainerUserID(t *testing.T) {
if err != nil {
t.Fatal(err)
}
direct, err := newDirectIO(ctx)
direct, err := newDirectIOStandard(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6b4355d

Please sign in to comment.