github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/integration-cli/docker_cli_cp_to_container_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/integration-cli/checker"
    13  	"github.com/docker/docker/pkg/system"
    14  	"github.com/go-check/check"
    15  )
    16  
    17  // Check ownership is root, both in non-userns and userns enabled modes
    18  func (s *DockerSuite) TestCpCheckDestOwnership(c *check.C) {
    19  	testRequires(c, DaemonIsLinux, SameHostDaemon)
    20  	tmpVolDir := getTestDir(c, "test-cp-tmpvol")
    21  	containerID := makeTestContainer(c,
    22  		testContainerOptions{volumes: []string{fmt.Sprintf("%s:/tmpvol", tmpVolDir)}})
    23  
    24  	tmpDir := getTestDir(c, "test-cp-to-check-ownership")
    25  	defer os.RemoveAll(tmpDir)
    26  
    27  	makeTestContentInDir(c, tmpDir)
    28  
    29  	srcPath := cpPath(tmpDir, "file1")
    30  	dstPath := containerCpPath(containerID, "/tmpvol", "file1")
    31  
    32  	err := runDockerCp(c, srcPath, dstPath)
    33  	c.Assert(err, checker.IsNil)
    34  
    35  	stat, err := system.Stat(filepath.Join(tmpVolDir, "file1"))
    36  	c.Assert(err, checker.IsNil)
    37  	uid, gid, err := getRootUIDGID()
    38  	c.Assert(err, checker.IsNil)
    39  	c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Copied file not owned by container root UID"))
    40  	c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Copied file not owned by container root GID"))
    41  }
    42  
    43  func getRootUIDGID() (int, int, error) {
    44  	uidgid := strings.Split(filepath.Base(testEnv.DockerBasePath()), ".")
    45  	if len(uidgid) == 1 {
    46  		//user namespace remapping is not turned on; return 0
    47  		return 0, 0, nil
    48  	}
    49  	uid, err := strconv.Atoi(uidgid[0])
    50  	if err != nil {
    51  		return 0, 0, err
    52  	}
    53  	gid, err := strconv.Atoi(uidgid[1])
    54  	if err != nil {
    55  		return 0, 0, err
    56  	}
    57  	return uid, gid, nil
    58  }