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