docker_api_attach_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 error)
  36. go func() {
  37. _, err := ws.Read(actual)
  38. outChan <- err
  39. close(outChan)
  40. }()
  41. inChan := make(chan error)
  42. go func() {
  43. _, err := ws.Write(expected)
  44. inChan <- err
  45. close(inChan)
  46. }()
  47. select {
  48. case err := <-inChan:
  49. if err != nil {
  50. c.Fatal(err)
  51. }
  52. case <-time.After(5 * time.Second):
  53. c.Fatal("Timeout writing to ws")
  54. }
  55. select {
  56. case err := <-outChan:
  57. if err != nil {
  58. c.Fatal(err)
  59. }
  60. case <-time.After(5 * time.Second):
  61. c.Fatal("Timeout reading from ws")
  62. }
  63. if !bytes.Equal(expected, actual) {
  64. c.Fatal("Expected output on websocket to match input")
  65. }
  66. }