docker_api_containers_windows_test.go 1.9 KB

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