github.com/ld86/docker@v1.7.1-rc3/integration-cli/docker_api_attach_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"os/exec"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/go-check/check"
    11  
    12  	"code.google.com/p/go.net/websocket"
    13  )
    14  
    15  func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
    16  	runCmd := exec.Command(dockerBinary, "run", "-dit", "busybox", "cat")
    17  	out, _, err := runCommandWithOutput(runCmd)
    18  	if err != nil {
    19  		c.Fatalf(out, err)
    20  	}
    21  
    22  	rwc, err := sockConn(time.Duration(10 * time.Second))
    23  	if err != nil {
    24  		c.Fatal(err)
    25  	}
    26  
    27  	cleanedContainerID := strings.TrimSpace(out)
    28  	config, err := websocket.NewConfig(
    29  		"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
    30  		"http://localhost",
    31  	)
    32  	if err != nil {
    33  		c.Fatal(err)
    34  	}
    35  
    36  	ws, err := websocket.NewClient(config, rwc)
    37  	if err != nil {
    38  		c.Fatal(err)
    39  	}
    40  	defer ws.Close()
    41  
    42  	expected := []byte("hello")
    43  	actual := make([]byte, len(expected))
    44  
    45  	outChan := make(chan error)
    46  	go func() {
    47  		_, err := ws.Read(actual)
    48  		outChan <- err
    49  		close(outChan)
    50  	}()
    51  
    52  	inChan := make(chan error)
    53  	go func() {
    54  		_, err := ws.Write(expected)
    55  		inChan <- err
    56  		close(inChan)
    57  	}()
    58  
    59  	select {
    60  	case err := <-inChan:
    61  		if err != nil {
    62  			c.Fatal(err)
    63  		}
    64  	case <-time.After(5 * time.Second):
    65  		c.Fatal("Timeout writing to ws")
    66  	}
    67  
    68  	select {
    69  	case err := <-outChan:
    70  		if err != nil {
    71  			c.Fatal(err)
    72  		}
    73  	case <-time.After(5 * time.Second):
    74  		c.Fatal("Timeout reading from ws")
    75  	}
    76  
    77  	if !bytes.Equal(expected, actual) {
    78  		c.Fatal("Expected output on websocket to match input")
    79  	}
    80  }
    81  
    82  // regression gh14320
    83  func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
    84  	status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil)
    85  	c.Assert(status, check.Equals, http.StatusNotFound)
    86  	c.Assert(err, check.IsNil)
    87  	expected := "no such id: doesnotexist\n"
    88  	if !strings.Contains(string(body), expected) {
    89  		c.Fatalf("Expected response body to contain %q", expected)
    90  	}
    91  }
    92  
    93  func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
    94  	status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
    95  	c.Assert(status, check.Equals, http.StatusNotFound)
    96  	c.Assert(err, check.IsNil)
    97  	expected := "no such id: doesnotexist\n"
    98  	if !strings.Contains(string(body), expected) {
    99  		c.Fatalf("Expected response body to contain %q", expected)
   100  	}
   101  }