github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration-cli/docker_api_containers_windows_test.go (about)

     1  // +build windows
     2  
     3  package main
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"math/rand"
    10  	"strings"
    11  
    12  	winio "github.com/Microsoft/go-winio"
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/api/types/container"
    15  	"github.com/docker/docker/api/types/mount"
    16  	"github.com/go-check/check"
    17  	"gotest.tools/assert"
    18  	is "gotest.tools/assert/cmp"
    19  )
    20  
    21  func (s *DockerSuite) TestContainersAPICreateMountsBindNamedPipe(c *check.C) {
    22  	testRequires(c, testEnv.IsLocalDaemon, DaemonIsWindowsAtLeastBuild(16299)) // Named pipe support was added in RS3
    23  
    24  	// Create a host pipe to map into the container
    25  	hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
    26  	pc := &winio.PipeConfig{
    27  		SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
    28  	}
    29  	l, err := winio.ListenPipe(hostPipeName, pc)
    30  	if err != nil {
    31  		c.Fatal(err)
    32  	}
    33  	defer l.Close()
    34  
    35  	// Asynchronously read data that the container writes to the mapped pipe.
    36  	var b []byte
    37  	ch := make(chan error)
    38  	go func() {
    39  		conn, err := l.Accept()
    40  		if err == nil {
    41  			b, err = ioutil.ReadAll(conn)
    42  			conn.Close()
    43  		}
    44  		ch <- err
    45  	}()
    46  
    47  	containerPipeName := `\\.\pipe\docker-cli-test-pipe`
    48  	text := "hello from a pipe"
    49  	cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
    50  	name := "test-bind-npipe"
    51  
    52  	ctx := context.Background()
    53  	client := testEnv.APIClient()
    54  	_, err = client.ContainerCreate(ctx,
    55  		&container.Config{
    56  			Image: testEnv.PlatformDefaults.BaseImage,
    57  			Cmd:   []string{"cmd", "/c", cmd},
    58  		}, &container.HostConfig{
    59  			Mounts: []mount.Mount{
    60  				{
    61  					Type:   "npipe",
    62  					Source: hostPipeName,
    63  					Target: containerPipeName,
    64  				},
    65  			},
    66  		},
    67  		nil, name)
    68  	assert.NilError(c, err)
    69  
    70  	err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
    71  	assert.NilError(c, err)
    72  
    73  	err = <-ch
    74  	assert.NilError(c, err)
    75  	assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
    76  }