integration-cli/docker_cli_experimental_test.go
78578125
 // +build experimental
 
 package main
 
 import (
44e1023a
 	"fmt"
 	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
 	"strconv"
78578125
 	"strings"
 
ff91c8ac
 	"github.com/docker/docker/pkg/integration/checker"
44e1023a
 	"github.com/docker/docker/pkg/system"
78578125
 	"github.com/go-check/check"
 )
 
 func (s *DockerSuite) TestExperimentalVersion(c *check.C) {
668e2369
 	out, _ := dockerCmd(c, "version")
78578125
 	for _, line := range strings.Split(out, "\n") {
b372f9f2
 		if strings.HasPrefix(line, "Experimental (client):") || strings.HasPrefix(line, "Experimental (server):") {
ff91c8ac
 			c.Assert(line, checker.Matches, "*true")
78578125
 		}
 	}
b372f9f2
 
668e2369
 	out, _ = dockerCmd(c, "-v")
ff91c8ac
 	c.Assert(out, checker.Contains, ", experimental", check.Commentf("docker version did not contain experimental"))
78578125
 }
44e1023a
 
 // user namespaces test: run daemon with remapped root setting
 // 1. validate uid/gid maps are set properly
 // 2. verify that files created are owned by remapped root
 func (s *DockerDaemonSuite) TestDaemonUserNamespaceRootSetting(c *check.C) {
157b66ad
 	testRequires(c, DaemonIsLinux, SameHostDaemon)
44e1023a
 
ff91c8ac
 	c.Assert(s.d.StartWithBusybox("--userns-remap", "default"), checker.IsNil)
44e1023a
 
 	tmpDir, err := ioutil.TempDir("", "userns")
ff91c8ac
 	c.Assert(err, checker.IsNil)
44e1023a
 	defer os.RemoveAll(tmpDir)
 
 	// we need to find the uid and gid of the remapped root from the daemon's root dir info
 	uidgid := strings.Split(filepath.Base(s.d.root), ".")
ff91c8ac
 	c.Assert(uidgid, checker.HasLen, 2, check.Commentf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.root)))
44e1023a
 	uid, err := strconv.Atoi(uidgid[0])
ff91c8ac
 	c.Assert(err, checker.IsNil, check.Commentf("Can't parse uid"))
44e1023a
 	gid, err := strconv.Atoi(uidgid[1])
ff91c8ac
 	c.Assert(err, checker.IsNil, check.Commentf("Can't parse gid"))
44e1023a
 
 	//writeable by the remapped root UID/GID pair
ff91c8ac
 	c.Assert(os.Chown(tmpDir, uid, gid), checker.IsNil)
44e1023a
 
 	out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "busybox", "sh", "-c", "touch /goofy/testfile; top")
ff91c8ac
 	c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
44e1023a
 
 	pid, err := s.d.Cmd("inspect", "--format='{{.State.Pid}}'", "userns")
ff91c8ac
 	c.Assert(err, checker.IsNil, check.Commentf("Could not inspect running container: out: %q", pid))
44e1023a
 	// check the uid and gid maps for the PID to ensure root is remapped
 	// (cmd = cat /proc/<pid>/uid_map | grep -E '0\s+9999\s+1')
 	out, rc1, err := runCommandPipelineWithOutput(
 		exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"),
 		exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid)))
ff91c8ac
 	c.Assert(rc1, checker.Equals, 0, check.Commentf("Didn't match uid_map: output: %s", out))
44e1023a
 
 	out, rc2, err := runCommandPipelineWithOutput(
 		exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"),
 		exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid)))
ff91c8ac
 	c.Assert(rc2, checker.Equals, 0, check.Commentf("Didn't match gid_map: output: %s", out))
44e1023a
 
 	// check that the touched file is owned by remapped uid:gid
 	stat, err := system.Stat(filepath.Join(tmpDir, "testfile"))
ff91c8ac
 	c.Assert(err, checker.IsNil)
 	c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Touched file not owned by remapped root UID"))
 	c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Touched file not owned by remapped root GID"))
44e1023a
 }