diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 48def34910..4ca2ac9870 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -5,6 +5,7 @@ import ( "os/exec" "regexp" "strings" + "sync" "testing" ) @@ -436,3 +437,81 @@ func TestExitCode(t *testing.T) { logDone("run - correct exit code") } + +func TestUserDefaultsToRoot(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "busybox", "id") + + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + if !strings.Contains(out, "uid=0(root) gid=0(root)") { + t.Fatalf("expected root user got %s", out) + } + deleteAllContainers() + + logDone("run - default user") +} + +func TestUserByName(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id") + + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + if !strings.Contains(out, "uid=0(root) gid=0(root)") { + t.Fatalf("expected root user got %s", out) + } + deleteAllContainers() + + logDone("run - user by name") +} + +func TestUserByID(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id") + + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") { + t.Fatalf("expected daemon user got %s", out) + } + deleteAllContainers() + + logDone("run - user by id") +} + +func TestUserNotFound(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id") + + _, err := runCommand(cmd) + if err == nil { + t.Fatal("unknown user should cause container to fail") + } + deleteAllContainers() + + logDone("run - user not found") +} + +func TestRunTwoConcurrentContainers(t *testing.T) { + group := sync.WaitGroup{} + group.Add(2) + + for i := 0; i < 2; i++ { + go func() { + defer group.Done() + cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2") + if _, err := runCommand(cmd); err != nil { + t.Fatal(err) + } + }() + } + + group.Wait() + + deleteAllContainers() + + logDone("run - two concurrent containers") +} diff --git a/integration/container_test.go b/integration/container_test.go index 38381d0c5f..fee4af09bf 100644 --- a/integration/container_test.go +++ b/integration/container_test.go @@ -216,190 +216,6 @@ func TestRestartStdin(t *testing.T) { } } -func TestUser(t *testing.T) { - daemon := mkDaemon(t) - defer nuke(daemon) - - // Default user must be root - container, _, err := daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err := container.Output() - if err != nil { - t.Fatal(err) - } - if !strings.Contains(string(output), "uid=0(root) gid=0(root)") { - t.Error(string(output)) - } - - // Set a username - container, _, err = daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - - User: "root", - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err = container.Output() - if code := container.State.GetExitCode(); err != nil || code != 0 { - t.Fatal(err) - } - if !strings.Contains(string(output), "uid=0(root) gid=0(root)") { - t.Error(string(output)) - } - - // Set a UID - container, _, err = daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - - User: "0", - }, - "", - ) - if code := container.State.GetExitCode(); err != nil || code != 0 { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err = container.Output() - if code := container.State.GetExitCode(); err != nil || code != 0 { - t.Fatal(err) - } - if !strings.Contains(string(output), "uid=0(root) gid=0(root)") { - t.Error(string(output)) - } - - // Set a different user by uid - container, _, err = daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - - User: "1", - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err = container.Output() - if err != nil { - t.Fatal(err) - } else if code := container.State.GetExitCode(); code != 0 { - t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", code, output) - } - if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") { - t.Error(string(output)) - } - - // Set a different user by username - container, _, err = daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - - User: "daemon", - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err = container.Output() - if code := container.State.GetExitCode(); err != nil || code != 0 { - t.Fatal(err) - } - if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") { - t.Error(string(output)) - } - - // Test an wrong username - container, _, err = daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"id"}, - - User: "unknownuser", - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err = container.Output() - if container.State.GetExitCode() == 0 { - t.Fatal("Starting container with wrong uid should fail but it passed.") - } -} - -func TestMultipleContainers(t *testing.T) { - daemon := mkDaemon(t) - defer nuke(daemon) - - container1, _, err := daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"sleep", "2"}, - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container1) - - container2, _, err := daemon.Create(&runconfig.Config{ - Image: GetTestImage(daemon).ID, - Cmd: []string{"sleep", "2"}, - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container2) - - // Start both containers - if err := container1.Start(); err != nil { - t.Fatal(err) - } - if err := container2.Start(); err != nil { - t.Fatal(err) - } - - // Make sure they are running before trying to kill them - container1.WaitTimeout(250 * time.Millisecond) - container2.WaitTimeout(250 * time.Millisecond) - - // If we are here, both containers should be running - if !container1.State.IsRunning() { - t.Fatal("Container not running") - } - if !container2.State.IsRunning() { - t.Fatal("Container not running") - } - - // Kill them - if err := container1.Kill(); err != nil { - t.Fatal(err) - } - - if err := container2.Kill(); err != nil { - t.Fatal(err) - } -} - func TestStdin(t *testing.T) { daemon := mkDaemon(t) defer nuke(daemon)