docker_cli_experimental_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, NativeExecDriver)
  30. testRequires(c, SameHostDaemon)
  31. c.Assert(s.d.StartWithBusybox("--userns-remap", "default"), checker.IsNil)
  32. tmpDir, err := ioutil.TempDir("", "userns")
  33. c.Assert(err, checker.IsNil)
  34. defer os.RemoveAll(tmpDir)
  35. // we need to find the uid and gid of the remapped root from the daemon's root dir info
  36. uidgid := strings.Split(filepath.Base(s.d.root), ".")
  37. c.Assert(uidgid, checker.HasLen, 2, check.Commentf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.root)))
  38. uid, err := strconv.Atoi(uidgid[0])
  39. c.Assert(err, checker.IsNil, check.Commentf("Can't parse uid"))
  40. gid, err := strconv.Atoi(uidgid[1])
  41. c.Assert(err, checker.IsNil, check.Commentf("Can't parse gid"))
  42. //writeable by the remapped root UID/GID pair
  43. c.Assert(os.Chown(tmpDir, uid, gid), checker.IsNil)
  44. out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "busybox", "sh", "-c", "touch /goofy/testfile; top")
  45. c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
  46. pid, err := s.d.Cmd("inspect", "--format='{{.State.Pid}}'", "userns")
  47. c.Assert(err, checker.IsNil, check.Commentf("Could not inspect running container: out: %q", pid))
  48. // check the uid and gid maps for the PID to ensure root is remapped
  49. // (cmd = cat /proc/<pid>/uid_map | grep -E '0\s+9999\s+1')
  50. out, rc1, err := runCommandPipelineWithOutput(
  51. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"),
  52. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid)))
  53. c.Assert(rc1, checker.Equals, 0, check.Commentf("Didn't match uid_map: output: %s", out))
  54. out, rc2, err := runCommandPipelineWithOutput(
  55. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"),
  56. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid)))
  57. c.Assert(rc2, checker.Equals, 0, check.Commentf("Didn't match gid_map: output: %s", out))
  58. // check that the touched file is owned by remapped uid:gid
  59. stat, err := system.Stat(filepath.Join(tmpDir, "testfile"))
  60. c.Assert(err, checker.IsNil)
  61. c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Touched file not owned by remapped root UID"))
  62. c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Touched file not owned by remapped root GID"))
  63. }