summaryrefslogtreecommitdiff
path: root/integration-cli/docker_api_containers_windows_test.go
blob: 88c2303c625b26927aaaaa46ccc47f423ef556e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//go:build windows
// +build windows

package main

import (
	"context"
	"fmt"
	"io"
	"math/rand"
	"strings"
	"testing"

	winio "github.com/Microsoft/go-winio"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/mount"
	"github.com/pkg/errors"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func (s *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
	// Create a host pipe to map into the container
	hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
	pc := &winio.PipeConfig{
		SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
	}
	l, err := winio.ListenPipe(hostPipeName, pc)
	if err != nil {
		c.Fatal(err)
	}
	defer l.Close()

	// Asynchronously read data that the container writes to the mapped pipe.
	var b []byte
	ch := make(chan error)
	go func() {
		conn, err := l.Accept()
		if err == nil {
			b, err = io.ReadAll(conn)
			conn.Close()
		}
		ch <- err
	}()

	containerPipeName := `\\.\pipe\docker-cli-test-pipe`
	text := "hello from a pipe"
	cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
	name := "test-bind-npipe"

	ctx := context.Background()
	client := testEnv.APIClient()
	_, err = client.ContainerCreate(ctx,
		&container.Config{
			Image: testEnv.PlatformDefaults.BaseImage,
			Cmd:   []string{"cmd", "/c", cmd},
		}, &container.HostConfig{
			Mounts: []mount.Mount{
				{
					Type:   "npipe",
					Source: hostPipeName,
					Target: containerPipeName,
				},
			},
		},
		nil, nil, name)
	assert.NilError(c, err)

	err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
	assert.NilError(c, err)

	err = <-ch
	assert.NilError(c, err)
	assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
}

func mountWrapper(device, target, mType, options string) error {
	// This should never be called.
	return errors.Errorf("there is no implementation of Mount on this platform")
}