docker_cli_experimental_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build experimental
  2. package main
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/docker/docker/pkg/system"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerSuite) TestExperimentalVersion(c *check.C) {
  16. out, _ := dockerCmd(c, "version")
  17. for _, line := range strings.Split(out, "\n") {
  18. if strings.HasPrefix(line, "Experimental (client):") || strings.HasPrefix(line, "Experimental (server):") {
  19. c.Assert(line, checker.Matches, "*true")
  20. }
  21. }
  22. out, _ = dockerCmd(c, "-v")
  23. c.Assert(out, checker.Contains, ", experimental", check.Commentf("docker version did not contain experimental"))
  24. }
  25. // user namespaces test: run daemon with remapped root setting
  26. // 1. validate uid/gid maps are set properly
  27. // 2. verify that files created are owned by remapped root
  28. func (s *DockerDaemonSuite) TestDaemonUserNamespaceRootSetting(c *check.C) {
  29. testRequires(c, DaemonIsLinux, SameHostDaemon)
  30. c.Assert(s.d.StartWithBusybox("--userns-remap", "default"), checker.IsNil)
  31. tmpDir, err := ioutil.TempDir("", "userns")
  32. c.Assert(err, checker.IsNil)
  33. defer os.RemoveAll(tmpDir)
  34. // we need to find the uid and gid of the remapped root from the daemon's root dir info
  35. uidgid := strings.Split(filepath.Base(s.d.root), ".")
  36. c.Assert(uidgid, checker.HasLen, 2, check.Commentf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.root)))
  37. uid, err := strconv.Atoi(uidgid[0])
  38. c.Assert(err, checker.IsNil, check.Commentf("Can't parse uid"))
  39. gid, err := strconv.Atoi(uidgid[1])
  40. c.Assert(err, checker.IsNil, check.Commentf("Can't parse gid"))
  41. //writeable by the remapped root UID/GID pair
  42. c.Assert(os.Chown(tmpDir, uid, gid), checker.IsNil)
  43. out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "busybox", "sh", "-c", "touch /goofy/testfile; top")
  44. c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
  45. pid, err := s.d.Cmd("inspect", "--format='{{.State.Pid}}'", "userns")
  46. c.Assert(err, checker.IsNil, check.Commentf("Could not inspect running container: out: %q", pid))
  47. // check the uid and gid maps for the PID to ensure root is remapped
  48. // (cmd = cat /proc/<pid>/uid_map | grep -E '0\s+9999\s+1')
  49. out, rc1, err := runCommandPipelineWithOutput(
  50. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"),
  51. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid)))
  52. c.Assert(rc1, checker.Equals, 0, check.Commentf("Didn't match uid_map: output: %s", out))
  53. out, rc2, err := runCommandPipelineWithOutput(
  54. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"),
  55. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid)))
  56. c.Assert(rc2, checker.Equals, 0, check.Commentf("Didn't match gid_map: output: %s", out))
  57. // check that the touched file is owned by remapped uid:gid
  58. stat, err := system.Stat(filepath.Join(tmpDir, "testfile"))
  59. c.Assert(err, checker.IsNil)
  60. c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Touched file not owned by remapped root UID"))
  61. c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Touched file not owned by remapped root GID"))
  62. }