From 7201237b1000a19be4175ca4abe32ed89506e84d Mon Sep 17 00:00:00 2001 From: zhangyue Date: Sun, 21 Oct 2018 17:44:26 +0800 Subject: [PATCH] bugfix: start cli setRawMode when in tty mode and Terminal Signed-off-by: zhangyue --- cli/start.go | 22 ++++++++++++++-------- test/cli_start_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cli/start.go b/cli/start.go index 13959d82a8..5ebf9095e9 100644 --- a/cli/start.go +++ b/cli/start.go @@ -60,7 +60,6 @@ func (s *StartCommand) addFlags() { func (s *StartCommand) runStart(args []string) error { ctx := context.Background() apiClient := s.cli.Client() - // attach to io. if s.attach || s.stdin { var wait chan struct{} @@ -69,17 +68,24 @@ func (s *StartCommand) runStart(args []string) error { return fmt.Errorf("cannot start and attach multiple containers at once") } - in, out, err := setRawMode(s.stdin, false) + container := args[0] + c, err := apiClient.ContainerGet(ctx, container) if err != nil { - return fmt.Errorf("failed to set raw mode") + return err } - defer func() { - if err := restoreMode(in, out); err != nil { - fmt.Fprintf(os.Stderr, "failed to restore term mode") + + if c.Config.Tty && terminal.IsTerminal(int(os.Stdout.Fd())) { + in, out, err := setRawMode(s.stdin, false) + if err != nil { + return fmt.Errorf("failed to set raw mode") } - }() + defer func() { + if err := restoreMode(in, out); err != nil { + fmt.Fprintf(os.Stderr, "failed to restore term mode") + } + }() + } - container := args[0] conn, br, err := apiClient.ContainerAttach(ctx, container, s.stdin) if err != nil { return fmt.Errorf("failed to attach container: %v", err) diff --git a/test/cli_start_test.go b/test/cli_start_test.go index 54a081cc66..77646b4a69 100644 --- a/test/cli_start_test.go +++ b/test/cli_start_test.go @@ -3,6 +3,7 @@ package main import ( "bufio" "encoding/json" + "io" "io/ioutil" "os" "os/exec" @@ -324,6 +325,31 @@ func (suite *PouchStartSuite) TestStartFromCheckpoint(c *check.C) { command.PouchRun("start", "--checkpoint-dir", tmpDir, "--checkpoint", checkpoint, restoredContainer).Assert(c, icmd.Success) } +// TestStartWithTty tests running container with -tty flag. +func (suite *PouchStartSuite) TestStartWithTty(c *check.C) { + name := "TestStartWithTty" + res := command.PouchRun("create", "-t", "--name", name, busyboxImage, "/bin/sh", "-c", "while true;do echo hello;done") + defer DelContainerForceMultyTime(c, name) + res.Assert(c, icmd.Success) + + cmd := exec.Command(environment.PouchBinary, "start", "-a", "-i", name) + out, err := cmd.StdoutPipe() + if err != nil { + c.Fatal(err) + } + defer out.Close() + if err := cmd.Start(); err != nil { + c.Fatal(err) + } + buf := make([]byte, 1024) + if _, err := out.Read(buf); err != nil && err != io.EOF { + c.Fatal(err) + } + if !strings.Contains(string(buf), "hello") { + c.Fatalf("unexpected output %s expected hello\n", string(buf)) + } +} + // TestStartMultiContainers tries to start more than one container. func (suite *PouchStartSuite) TestStartMultiContainers(c *check.C) { containernames := []string{"TestStartMultiContainer-1", "TestStartMultiContainer-2"}