docker_api_containers_windows_test.go 1.9 KB

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