docker_cli_cp_to_container_unix_test.go 1.6 KB

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