github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/engine/integration-cli/docker_api_containers_windows_test.go (about)

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