github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/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  	result := RunCommand("ls")
    19  	result.Assert(t, Expected{})
    20  
    21  	result = RunCommand("doesnotexists")
    22  	expectedError := `exec: "doesnotexists": executable file not found`
    23  	result.Assert(t, Expected{ExitCode: 127, Error: expectedError})
    24  
    25  	result = RunCommand("ls", "-z")
    26  	result.Assert(t, Expected{
    27  		ExitCode: 2,
    28  		Error:    "exit status 2",
    29  		Err:      "invalid option",
    30  	})
    31  	assert.Contains(t, result.Combined(), "invalid option")
    32  }
    33  
    34  func TestRunCommandWithCombined(t *testing.T) {
    35  	// TODO Windows: Port this test
    36  	if runtime.GOOS == "windows" {
    37  		t.Skip("Needs porting to Windows")
    38  	}
    39  
    40  	result := RunCommand("ls", "-a")
    41  	result.Assert(t, Expected{})
    42  
    43  	assert.Contains(t, result.Combined(), "..")
    44  	assert.Contains(t, result.Stdout(), "..")
    45  }
    46  
    47  func TestRunCommandWithTimeoutFinished(t *testing.T) {
    48  	// TODO Windows: Port this test
    49  	if runtime.GOOS == "windows" {
    50  		t.Skip("Needs porting to Windows")
    51  	}
    52  
    53  	result := RunCmd(Cmd{
    54  		Command: []string{"ls", "-a"},
    55  		Timeout: 50 * time.Millisecond,
    56  	})
    57  	result.Assert(t, Expected{Out: ".."})
    58  }
    59  
    60  func TestRunCommandWithTimeoutKilled(t *testing.T) {
    61  	// TODO Windows: Port this test
    62  	if runtime.GOOS == "windows" {
    63  		t.Skip("Needs porting to Windows")
    64  	}
    65  
    66  	command := []string{"sh", "-c", "while true ; do echo 1 ; sleep .1 ; done"}
    67  	result := RunCmd(Cmd{Command: command, Timeout: 500 * time.Millisecond})
    68  	result.Assert(t, Expected{Timeout: true})
    69  
    70  	ones := strings.Split(result.Stdout(), "\n")
    71  	assert.Equal(t, len(ones), 6)
    72  }
    73  
    74  func TestRunCommandWithErrors(t *testing.T) {
    75  	result := RunCommand("/foobar")
    76  	result.Assert(t, Expected{Error: "foobar", ExitCode: 127})
    77  }
    78  
    79  func TestRunCommandWithStdoutStderr(t *testing.T) {
    80  	result := RunCommand("echo", "hello", "world")
    81  	result.Assert(t, Expected{Out: "hello world\n", Err: None})
    82  }
    83  
    84  func TestRunCommandWithStdoutStderrError(t *testing.T) {
    85  	result := RunCommand("doesnotexists")
    86  
    87  	expected := `exec: "doesnotexists": executable file not found`
    88  	result.Assert(t, Expected{Out: None, Err: None, ExitCode: 127, Error: expected})
    89  
    90  	switch runtime.GOOS {
    91  	case "windows":
    92  		expected = "ls: unknown option"
    93  	default:
    94  		expected = "ls: invalid option"
    95  	}
    96  
    97  	result = RunCommand("ls", "-z")
    98  	result.Assert(t, Expected{
    99  		Out:      None,
   100  		Err:      expected,
   101  		ExitCode: 2,
   102  		Error:    "exit status 2",
   103  	})
   104  }