mirror of
https://github.com/moby/moby.git
synced 2025-08-01 05:47:11 +03:00
Add -u|--user
flag to docker exec for running command as a different user
Signed-off-by: Lei Jitang <leijitang@huawei.com>
This commit is contained in:
@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user