command_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package cmd
  2. import (
  3. "runtime"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/pkg/testutil/assert"
  8. )
  9. func TestRunCommand(t *testing.T) {
  10. // TODO Windows: Port this test
  11. if runtime.GOOS == "windows" {
  12. t.Skip("Needs porting to Windows")
  13. }
  14. result := RunCommand("ls")
  15. result.Assert(t, Expected{})
  16. result = RunCommand("doesnotexists")
  17. expectedError := `exec: "doesnotexists": executable file not found`
  18. result.Assert(t, Expected{ExitCode: 127, Error: expectedError})
  19. result = RunCommand("ls", "-z")
  20. result.Assert(t, Expected{
  21. ExitCode: 2,
  22. Error: "exit status 2",
  23. Err: "invalid option",
  24. })
  25. assert.Contains(t, result.Combined(), "invalid option")
  26. }
  27. func TestRunCommandWithCombined(t *testing.T) {
  28. // TODO Windows: Port this test
  29. if runtime.GOOS == "windows" {
  30. t.Skip("Needs porting to Windows")
  31. }
  32. result := RunCommand("ls", "-a")
  33. result.Assert(t, Expected{})
  34. assert.Contains(t, result.Combined(), "..")
  35. assert.Contains(t, result.Stdout(), "..")
  36. }
  37. func TestRunCommandWithTimeoutFinished(t *testing.T) {
  38. // TODO Windows: Port this test
  39. if runtime.GOOS == "windows" {
  40. t.Skip("Needs porting to Windows")
  41. }
  42. result := RunCmd(Cmd{
  43. Command: []string{"ls", "-a"},
  44. Timeout: 50 * time.Millisecond,
  45. })
  46. result.Assert(t, Expected{Out: ".."})
  47. }
  48. func TestRunCommandWithTimeoutKilled(t *testing.T) {
  49. // TODO Windows: Port this test
  50. if runtime.GOOS == "windows" {
  51. t.Skip("Needs porting to Windows")
  52. }
  53. command := []string{"sh", "-c", "while true ; do echo 1 ; sleep .1 ; done"}
  54. result := RunCmd(Cmd{Command: command, Timeout: 500 * time.Millisecond})
  55. result.Assert(t, Expected{Timeout: true})
  56. ones := strings.Split(result.Stdout(), "\n")
  57. assert.Equal(t, len(ones), 6)
  58. }
  59. func TestRunCommandWithErrors(t *testing.T) {
  60. result := RunCommand("/foobar")
  61. result.Assert(t, Expected{Error: "foobar", ExitCode: 127})
  62. }
  63. func TestRunCommandWithStdoutStderr(t *testing.T) {
  64. result := RunCommand("echo", "hello", "world")
  65. result.Assert(t, Expected{Out: "hello world\n", Err: None})
  66. }
  67. func TestRunCommandWithStdoutStderrError(t *testing.T) {
  68. result := RunCommand("doesnotexists")
  69. expected := `exec: "doesnotexists": executable file not found`
  70. result.Assert(t, Expected{Out: None, Err: None, ExitCode: 127, Error: expected})
  71. switch runtime.GOOS {
  72. case "windows":
  73. expected = "ls: unknown option"
  74. default:
  75. expected = "ls: invalid option"
  76. }
  77. result = RunCommand("ls", "-z")
  78. result.Assert(t, Expected{
  79. Out: None,
  80. Err: expected,
  81. ExitCode: 2,
  82. Error: "exit status 2",
  83. })
  84. }