Add -u|--user flag to docker exec for running command as a different user

Signed-off-by: Lei Jitang <leijitang@huawei.com>
Upstream-commit: 2cce4791b0e75201cb65daad07d4203d1c4c2996
Component: engine
This commit is contained in:
Lei Jitang
2015-04-11 11:04:24 +08:00
parent cefc6ef8df
commit d1f2626097
7 changed files with 46 additions and 7 deletions
@@ -665,3 +665,32 @@ func TestRunMutableNetworkFiles(t *testing.T) {
}
logDone("run - mutable network files")
}
func TestExecWithUser(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
t.Fatal(out, err)
}
cmd := exec.Command(dockerBinary, "exec", "-u", "1", "parent", "id")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
t.Fatal(err, out)
}
if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
t.Fatalf("exec with user by id expected daemon user got %s", out)
}
cmd = exec.Command(dockerBinary, "exec", "-u", "root", "parent", "id")
out, _, err = runCommandWithOutput(cmd)
if err != nil {
t.Fatal(err, out)
}
if !strings.Contains(out, "uid=0(root) gid=0(root)") {
t.Fatalf("exec with user by root expected root user got %s", out)
}
logDone("exec - with user")
}