docker_api_attach_test.go 1.2 KB

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