123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package docker
- import (
- "bufio"
- "fmt"
- "github.com/dotcloud/docker/utils"
- "io"
- "io/ioutil"
- "strings"
- "testing"
- "time"
- )
- func closeWrap(args ...io.Closer) error {
- e := false
- ret := fmt.Errorf("Error closing elements")
- for _, c := range args {
- if err := c.Close(); err != nil {
- e = true
- ret = fmt.Errorf("%s\n%s", ret, err)
- }
- }
- if e {
- return ret
- }
- return nil
- }
- func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
- c := make(chan bool)
- // Make sure we are not too long
- go func() {
- time.Sleep(d)
- c <- true
- }()
- go func() {
- f()
- c <- false
- }()
- if <-c && msg != "" {
- t.Fatal(msg)
- }
- }
- func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
- for i := 0; i < count; i++ {
- if _, err := w.Write([]byte(input)); err != nil {
- return err
- }
- o, err := bufio.NewReader(r).ReadString('\n')
- if err != nil {
- return err
- }
- if strings.Trim(o, " \r\n") != output {
- return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", output, o)
- }
- }
- return nil
- }
- // TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
- func TestRunHostname(t *testing.T) {
- stdout, stdoutPipe := io.Pipe()
- cli := NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr)
- defer cleanup(globalRuntime)
- c := make(chan struct{})
- go func() {
- defer close(c)
- if err := cli.CmdRun("-h", "foobar", unitTestImageID, "hostname"); err != nil {
- t.Fatal(err)
- }
- }()
- utils.Debugf("--")
- setTimeout(t, "Reading command output time out", 2*time.Second, func() {
- cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
- if err != nil {
- t.Fatal(err)
- }
- if cmdOutput != "foobar\n" {
- t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", cmdOutput)
- }
- })
- setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
- <-c
- })
- }
- // TestAttachStdin checks attaching to stdin without stdout and stderr.
- // 'docker run -i -a stdin' should sends the client's stdin to the command,
- // then detach from it and print the container id.
- func TestRunAttachStdin(t *testing.T) {
- stdin, stdinPipe := io.Pipe()
- stdout, stdoutPipe := io.Pipe()
- cli := NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr)
- defer cleanup(globalRuntime)
- ch := make(chan struct{})
- go func() {
- defer close(ch)
- cli.CmdRun("-i", "-a", "stdin", unitTestImageID, "sh", "-c", "echo hello && cat")
- }()
- // Send input to the command, close stdin
- setTimeout(t, "Write timed out", 10*time.Second, func() {
- if _, err := stdinPipe.Write([]byte("hi there\n")); err != nil {
- t.Fatal(err)
- }
- if err := stdinPipe.Close(); err != nil {
- t.Fatal(err)
- }
- })
- container := globalRuntime.List()[0]
- // Check output
- setTimeout(t, "Reading command output time out", 10*time.Second, func() {
- cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
- if err != nil {
- t.Fatal(err)
- }
- if cmdOutput != container.ShortID()+"\n" {
- t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortID()+"\n", cmdOutput)
- }
- })
- // wait for CmdRun to return
- setTimeout(t, "Waiting for CmdRun timed out", 5*time.Second, func() {
- // Unblock hijack end
- stdout.Read([]byte{})
- <-ch
- })
- setTimeout(t, "Waiting for command to exit timed out", 5*time.Second, func() {
- container.Wait()
- })
- // Check logs
- if cmdLogs, err := container.ReadLog("json"); err != nil {
- t.Fatal(err)
- } else {
- if output, err := ioutil.ReadAll(cmdLogs); err != nil {
- t.Fatal(err)
- } else {
- expectedLogs := []string{"{\"log\":\"hello\\n\",\"stream\":\"stdout\"", "{\"log\":\"hi there\\n\",\"stream\":\"stdout\""}
- for _, expectedLog := range expectedLogs {
- if !strings.Contains(string(output), expectedLog) {
- t.Fatalf("Unexpected logs: should contains '%s', it is not '%s'\n", expectedLog, output)
- }
- }
- }
- }
- }
|