gitee.com/bomy/docker.git@v1.13.1/pkg/integration/cmd/command_test.go (about) 1 package cmd 2 3 import ( 4 "runtime" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/docker/docker/pkg/testutil/assert" 10 ) 11 12 func TestRunCommand(t *testing.T) { 13 // TODO Windows: Port this test 14 if runtime.GOOS == "windows" { 15 t.Skip("Needs porting to Windows") 16 } 17 18 var cmd string 19 if runtime.GOOS == "solaris" { 20 cmd = "gls" 21 } else { 22 cmd = "ls" 23 } 24 result := RunCommand(cmd) 25 result.Assert(t, Expected{}) 26 27 result = RunCommand("doesnotexists") 28 expectedError := `exec: "doesnotexists": executable file not found` 29 result.Assert(t, Expected{ExitCode: 127, Error: expectedError}) 30 31 result = RunCommand(cmd, "-z") 32 result.Assert(t, Expected{ 33 ExitCode: 2, 34 Error: "exit status 2", 35 Err: "invalid option", 36 }) 37 assert.Contains(t, result.Combined(), "invalid option") 38 } 39 40 func TestRunCommandWithCombined(t *testing.T) { 41 // TODO Windows: Port this test 42 if runtime.GOOS == "windows" { 43 t.Skip("Needs porting to Windows") 44 } 45 46 result := RunCommand("ls", "-a") 47 result.Assert(t, Expected{}) 48 49 assert.Contains(t, result.Combined(), "..") 50 assert.Contains(t, result.Stdout(), "..") 51 } 52 53 func TestRunCommandWithTimeoutFinished(t *testing.T) { 54 // TODO Windows: Port this test 55 if runtime.GOOS == "windows" { 56 t.Skip("Needs porting to Windows") 57 } 58 59 result := RunCmd(Cmd{ 60 Command: []string{"ls", "-a"}, 61 Timeout: 50 * time.Millisecond, 62 }) 63 result.Assert(t, Expected{Out: ".."}) 64 } 65 66 func TestRunCommandWithTimeoutKilled(t *testing.T) { 67 // TODO Windows: Port this test 68 if runtime.GOOS == "windows" { 69 t.Skip("Needs porting to Windows") 70 } 71 72 command := []string{"sh", "-c", "while true ; do echo 1 ; sleep .5 ; done"} 73 result := RunCmd(Cmd{Command: command, Timeout: 1250 * time.Millisecond}) 74 result.Assert(t, Expected{Timeout: true}) 75 76 ones := strings.Split(result.Stdout(), "\n") 77 assert.Equal(t, len(ones), 4) 78 } 79 80 func TestRunCommandWithErrors(t *testing.T) { 81 result := RunCommand("/foobar") 82 result.Assert(t, Expected{Error: "foobar", ExitCode: 127}) 83 } 84 85 func TestRunCommandWithStdoutStderr(t *testing.T) { 86 result := RunCommand("echo", "hello", "world") 87 result.Assert(t, Expected{Out: "hello world\n", Err: None}) 88 } 89 90 func TestRunCommandWithStdoutStderrError(t *testing.T) { 91 result := RunCommand("doesnotexists") 92 93 expected := `exec: "doesnotexists": executable file not found` 94 result.Assert(t, Expected{Out: None, Err: None, ExitCode: 127, Error: expected}) 95 96 switch runtime.GOOS { 97 case "windows": 98 expected = "ls: unknown option" 99 case "solaris": 100 expected = "gls: invalid option" 101 default: 102 expected = "ls: invalid option" 103 } 104 105 var cmd string 106 if runtime.GOOS == "solaris" { 107 cmd = "gls" 108 } else { 109 cmd = "ls" 110 } 111 result = RunCommand(cmd, "-z") 112 result.Assert(t, Expected{ 113 Out: None, 114 Err: expected, 115 ExitCode: 2, 116 Error: "exit status 2", 117 }) 118 }