github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/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/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 *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
    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 = io.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, 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  }
    77  
    78  func mountWrapper(device, target, mType, options string) error {
    79  	// This should never be called.
    80  	return errors.Errorf("there is no implementation of Mount on this platform")
    81  }