2021-08-23 13:14:53 +00:00
|
|
|
//go:build windows
|
2017-07-31 21:23:52 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2017-07-31 21:23:52 +00:00
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2017-07-31 21:23:52 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2017-07-31 21:23:52 +00:00
|
|
|
|
|
|
|
winio "github.com/Microsoft/go-winio"
|
2017-09-19 20:12:29 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
2020-11-05 14:23:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-07-31 21:23:52 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T) {
|
2017-07-31 21:23:52 +00:00
|
|
|
// 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 {
|
2021-08-24 10:10:50 +00:00
|
|
|
b, err = io.ReadAll(conn)
|
2017-07-31 21:23:52 +00:00
|
|
|
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"
|
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-19 20:54:48 +00:00
|
|
|
nil, nil, name)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(c, err)
|
2017-07-31 21:23:52 +00:00
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(c, err)
|
2017-07-31 21:23:52 +00:00
|
|
|
|
|
|
|
err = <-ch
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
|
2017-07-31 21:23:52 +00:00
|
|
|
}
|
2020-11-05 14:23:49 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|