Skip to content

Commit

Permalink
bugfix: start cli setRawMode when in tty mode and Terminal
Browse files Browse the repository at this point in the history
Signed-off-by: zhangyue <[email protected]>
  • Loading branch information
zhangyue committed Oct 21, 2018
1 parent b2542f1 commit 7201237
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
22 changes: 14 additions & 8 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions test/cli_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit 7201237

Please sign in to comment.