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