docker_api_attach_test.go 1.3 KB

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