docker_api_containers_windows_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //go:build windows
  2. package main
  3. import (
  4. "context"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "strings"
  9. "testing"
  10. winio "github.com/Microsoft/go-winio"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/mount"
  14. "github.com/pkg/errors"
  15. "gotest.tools/v3/assert"
  16. is "gotest.tools/v3/assert/cmp"
  17. )
  18. func (s *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
  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 = io.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, 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. }
  67. func mountWrapper(device, target, mType, options string) error {
  68. // This should never be called.
  69. return errors.Errorf("there is no implementation of Mount on this platform")
  70. }