docker_api_containers_windows_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build windows
  2. package main
  3. import (
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "strings"
  9. winio "github.com/Microsoft/go-winio"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/api/types/mount"
  13. "github.com/go-check/check"
  14. "gotest.tools/assert"
  15. is "gotest.tools/assert/cmp"
  16. )
  17. func (s *DockerSuite) TestContainersAPICreateMountsBindNamedPipe(c *check.C) {
  18. testRequires(c, testEnv.IsLocalDaemon, DaemonIsWindowsAtLeastBuild(16299)) // Named pipe support was added in RS3
  19. // Create a host pipe to map into the container
  20. hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
  21. pc := &winio.PipeConfig{
  22. SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
  23. }
  24. l, err := winio.ListenPipe(hostPipeName, pc)
  25. if err != nil {
  26. c.Fatal(err)
  27. }
  28. defer l.Close()
  29. // Asynchronously read data that the container writes to the mapped pipe.
  30. var b []byte
  31. ch := make(chan error)
  32. go func() {
  33. conn, err := l.Accept()
  34. if err == nil {
  35. b, err = ioutil.ReadAll(conn)
  36. conn.Close()
  37. }
  38. ch <- err
  39. }()
  40. containerPipeName := `\\.\pipe\docker-cli-test-pipe`
  41. text := "hello from a pipe"
  42. cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
  43. name := "test-bind-npipe"
  44. ctx := context.Background()
  45. client := testEnv.APIClient()
  46. _, err = client.ContainerCreate(ctx,
  47. &container.Config{
  48. Image: testEnv.PlatformDefaults.BaseImage,
  49. Cmd: []string{"cmd", "/c", cmd},
  50. }, &container.HostConfig{
  51. Mounts: []mount.Mount{
  52. {
  53. Type: "npipe",
  54. Source: hostPipeName,
  55. Target: containerPipeName,
  56. },
  57. },
  58. },
  59. nil, name)
  60. assert.NilError(c, err)
  61. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  62. assert.NilError(c, err)
  63. err = <-ch
  64. assert.NilError(c, err)
  65. assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
  66. }