github.com/christopherobin/docker@v1.6.2/integration-cli/docker_api_attach_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"os/exec"
     6  	"testing"
     7  	"time"
     8  
     9  	"code.google.com/p/go.net/websocket"
    10  )
    11  
    12  func TestGetContainersAttachWebsocket(t *testing.T) {
    13  	runCmd := exec.Command(dockerBinary, "run", "-dit", "busybox", "cat")
    14  	out, _, err := runCommandWithOutput(runCmd)
    15  	if err != nil {
    16  		t.Fatalf(out, err)
    17  	}
    18  	defer deleteAllContainers()
    19  
    20  	rwc, err := sockConn(time.Duration(10 * time.Second))
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	cleanedContainerID := stripTrailingCharacters(out)
    26  	config, err := websocket.NewConfig(
    27  		"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
    28  		"http://localhost",
    29  	)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	ws, err := websocket.NewClient(config, rwc)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer ws.Close()
    39  
    40  	expected := []byte("hello")
    41  	actual := make([]byte, len(expected))
    42  	outChan := make(chan string)
    43  	go func() {
    44  		if _, err := ws.Read(actual); err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		outChan <- "done"
    48  	}()
    49  
    50  	inChan := make(chan string)
    51  	go func() {
    52  		if _, err := ws.Write(expected); err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		inChan <- "done"
    56  	}()
    57  
    58  	<-inChan
    59  	<-outChan
    60  
    61  	if !bytes.Equal(expected, actual) {
    62  		t.Fatal("Expected output on websocket to match input")
    63  	}
    64  
    65  	logDone("container attach websocket - can echo input via cat")
    66  }