docker_api_containers_windows_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // +build windows
  2. package main
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "net/http"
  8. "strings"
  9. winio "github.com/Microsoft/go-winio"
  10. "github.com/docker/docker/integration-cli/checker"
  11. "github.com/docker/docker/integration-cli/request"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestContainersAPICreateMountsBindNamedPipe(c *check.C) {
  15. testRequires(c, SameHostDaemon, DaemonIsWindowsAtLeastBuild(16210)) // Named pipe support was added in RS3
  16. // Create a host pipe to map into the container
  17. hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
  18. pc := &winio.PipeConfig{
  19. SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
  20. }
  21. l, err := winio.ListenPipe(hostPipeName, pc)
  22. if err != nil {
  23. c.Fatal(err)
  24. }
  25. defer l.Close()
  26. // Asynchronously read data that the container writes to the mapped pipe.
  27. var b []byte
  28. ch := make(chan error)
  29. go func() {
  30. conn, err := l.Accept()
  31. if err == nil {
  32. b, err = ioutil.ReadAll(conn)
  33. conn.Close()
  34. }
  35. ch <- err
  36. }()
  37. containerPipeName := `\\.\pipe\docker-cli-test-pipe`
  38. text := "hello from a pipe"
  39. cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
  40. name := "test-bind-npipe"
  41. data := map[string]interface{}{
  42. "Image": testEnv.MinimalBaseImage(),
  43. "Cmd": []string{"cmd", "/c", cmd},
  44. "HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{{"Type": "npipe", "Source": hostPipeName, "Target": containerPipeName}}},
  45. }
  46. status, resp, err := request.SockRequest("POST", "/containers/create?name="+name, data, daemonHost())
  47. c.Assert(err, checker.IsNil, check.Commentf(string(resp)))
  48. c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp)))
  49. status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost())
  50. c.Assert(err, checker.IsNil)
  51. c.Assert(status, checker.Equals, http.StatusNoContent)
  52. err = <-ch
  53. if err != nil {
  54. c.Fatal(err)
  55. }
  56. result := strings.TrimSpace(string(b))
  57. if result != text {
  58. c.Errorf("expected pipe to contain %s, got %s", text, result)
  59. }
  60. }