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