github.com/xdlianrong208/docker-ce-comments@v17.12.1-ce-rc2+incompatible/components/engine/integration-cli/docker_api_containers_windows_test.go (about)

     1  // +build windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"math/rand"
     9  	"net/http"
    10  	"strings"
    11  
    12  	winio "github.com/Microsoft/go-winio"
    13  	"github.com/docker/docker/integration-cli/checker"
    14  	"github.com/docker/docker/integration-cli/request"
    15  	"github.com/go-check/check"
    16  )
    17  
    18  func (s *DockerSuite) TestContainersAPICreateMountsBindNamedPipe(c *check.C) {
    19  	testRequires(c, SameHostDaemon, DaemonIsWindowsAtLeastBuild(16210)) // Named pipe support was added in RS3
    20  
    21  	// Create a host pipe to map into the container
    22  	hostPipeName := fmt.Sprintf(`\\.\pipe\docker-cli-test-pipe-%x`, rand.Uint64())
    23  	pc := &winio.PipeConfig{
    24  		SecurityDescriptor: "D:P(A;;GA;;;AU)", // Allow all users access to the pipe
    25  	}
    26  	l, err := winio.ListenPipe(hostPipeName, pc)
    27  	if err != nil {
    28  		c.Fatal(err)
    29  	}
    30  	defer l.Close()
    31  
    32  	// Asynchronously read data that the container writes to the mapped pipe.
    33  	var b []byte
    34  	ch := make(chan error)
    35  	go func() {
    36  		conn, err := l.Accept()
    37  		if err == nil {
    38  			b, err = ioutil.ReadAll(conn)
    39  			conn.Close()
    40  		}
    41  		ch <- err
    42  	}()
    43  
    44  	containerPipeName := `\\.\pipe\docker-cli-test-pipe`
    45  	text := "hello from a pipe"
    46  	cmd := fmt.Sprintf("echo %s > %s", text, containerPipeName)
    47  
    48  	name := "test-bind-npipe"
    49  	data := map[string]interface{}{
    50  		"Image":      testEnv.MinimalBaseImage(),
    51  		"Cmd":        []string{"cmd", "/c", cmd},
    52  		"HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{{"Type": "npipe", "Source": hostPipeName, "Target": containerPipeName}}},
    53  	}
    54  
    55  	status, resp, err := request.SockRequest("POST", "/containers/create?name="+name, data, daemonHost())
    56  	c.Assert(err, checker.IsNil, check.Commentf(string(resp)))
    57  	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp)))
    58  
    59  	status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost())
    60  	c.Assert(err, checker.IsNil)
    61  	c.Assert(status, checker.Equals, http.StatusNoContent)
    62  
    63  	err = <-ch
    64  	if err != nil {
    65  		c.Fatal(err)
    66  	}
    67  	result := strings.TrimSpace(string(b))
    68  	if result != text {
    69  		c.Errorf("expected pipe to contain %s, got %s", text, result)
    70  	}
    71  }