docker_cli_experimental_test.go 3.1 KB

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