docker_api_containers_windows_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //go:build windows
  2. // +build windows
  3. package main
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "math/rand"
  9. "strings"
  10. "testing"
  11. winio "github.com/Microsoft/go-winio"
  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 *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
  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 = io.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. }
  68. func mountWrapper(device, target, mType, options string) error {
  69. // This should never be called.
  70. return errors.Errorf("there is no implementation of Mount on this platform")
  71. }