docker_api_containers_windows_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "github.com/pkg/errors"
  16. "gotest.tools/v3/assert"
  17. is "gotest.tools/v3/assert/cmp"
  18. )
  19. func (s *DockerSuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
  20. testRequires(c, testEnv.IsLocalDaemon, DaemonIsWindowsAtLeastBuild(osversion.RS3)) // Named pipe support was added in RS3
  21. // Create a host pipe to map into the container
  22. hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
  23. pc := &winio.PipeConfig{
  24. SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
  25. }
  26. l, err := winio.ListenPipe(hostPipeName, pc)
  27. if err != nil {
  28. c.Fatal(err)
  29. }
  30. defer l.Close()
  31. // Asynchronously read data that the container writes to the mapped pipe.
  32. var b []byte
  33. ch := make(chan error)
  34. go func() {
  35. conn, err := l.Accept()
  36. if err == nil {
  37. b, err = ioutil.ReadAll(conn)
  38. conn.Close()
  39. }
  40. ch <- err
  41. }()
  42. containerPipeName := `\\.\pipe\docker-cli-test-pipe`
  43. text := "hello from a pipe"
  44. cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
  45. name := "test-bind-npipe"
  46. ctx := context.Background()
  47. client := testEnv.APIClient()
  48. _, err = client.ContainerCreate(ctx,
  49. &container.Config{
  50. Image: testEnv.PlatformDefaults.BaseImage,
  51. Cmd: []string{"cmd", "/c", cmd},
  52. }, &container.HostConfig{
  53. Mounts: []mount.Mount{
  54. {
  55. Type: "npipe",
  56. Source: hostPipeName,
  57. Target: containerPipeName,
  58. },
  59. },
  60. },
  61. nil, nil, name)
  62. assert.NilError(c, err)
  63. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  64. assert.NilError(c, err)
  65. err = <-ch
  66. assert.NilError(c, err)
  67. assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
  68. }
  69. func mountWrapper(device, target, mType, options string) error {
  70. // This should never be called.
  71. return errors.Errorf("there is no implementation of Mount on this platform")
  72. }