2014-11-20 00:19:16 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-10 23:16:42 -04:00
|
|
|
"bufio"
|
2014-11-20 00:19:16 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-03-16 19:41:38 -04:00
|
|
|
"path"
|
2014-11-20 00:19:16 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-11-20 00:19:16 -05:00
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #6509
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
|
2014-11-20 00:19:16 -05:00
|
|
|
checkRedirect := func(command string) {
|
|
|
|
_, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Could not open pty: %v", err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
cmd := exec.Command("sh", "-c", command)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("start err: %v", err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
2015-04-27 13:29:48 -04:00
|
|
|
ch := make(chan error)
|
2014-11-20 00:19:16 -05:00
|
|
|
go func() {
|
2015-04-27 13:29:48 -04:00
|
|
|
ch <- cmd.Wait()
|
2014-11-20 00:19:16 -05:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("command timeout")
|
2015-04-27 13:29:48 -04:00
|
|
|
case err := <-ch:
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("wait err=%v", err)
|
|
|
|
}
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
|
|
|
|
checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test recursive bind mount works by default
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
|
2014-11-20 00:19:16 -05:00
|
|
|
tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
// Create a temporary tmpfs mount.
|
|
|
|
tmpfsDir := filepath.Join(tmpDir, "tmpfs")
|
|
|
|
if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile(tmpfsDir, "touch-me")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
|
|
|
|
out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil && exitCode != 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, stderr, err)
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
if !strings.Contains(out, filepath.Base(f.Name())) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Recursive bind mount test failed. Expected file not found")
|
2014-11-20 00:19:16 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-11 14:21:38 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-02-11 14:21:38 -05:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err, out)
|
2015-02-11 14:21:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ul := strings.TrimSpace(out)
|
|
|
|
if ul != "42" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
|
2015-02-11 14:21:38 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 19:41:38 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-03-16 19:41:38 -04:00
|
|
|
|
|
|
|
cgroupParent := "test"
|
|
|
|
data, err := ioutil.ReadFile("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to read '/proc/self/cgroup - %v", err)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
2015-03-20 18:32:40 -04:00
|
|
|
selfCgroupPaths := parseCgroupPaths(string(data))
|
2015-03-19 18:45:01 -04:00
|
|
|
selfCpuCgroup, found := selfCgroupPaths["memory"]
|
2015-03-16 19:41:38 -04:00
|
|
|
if !found {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
2015-03-20 18:32:40 -04:00
|
|
|
cgroupPaths := parseCgroupPaths(string(out))
|
2015-03-16 19:41:38 -04:00
|
|
|
if len(cgroupPaths) == 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected output - %q", string(out))
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
|
|
|
found = false
|
|
|
|
expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
|
|
|
|
for _, path := range cgroupPaths {
|
|
|
|
if strings.HasPrefix(path, expectedCgroupPrefix) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-03-16 19:41:38 -04:00
|
|
|
|
|
|
|
cgroupParent := "/cgroup-parent/test"
|
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
2015-03-20 18:32:40 -04:00
|
|
|
cgroupPaths := parseCgroupPaths(string(out))
|
2015-03-16 19:41:38 -04:00
|
|
|
if len(cgroupPaths) == 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected output - %q", string(out))
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, path := range cgroupPaths {
|
|
|
|
if strings.HasPrefix(path, cgroupParent) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
|
2015-03-16 19:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 13:51:51 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-02-12 13:51:51 -05:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
|
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err, out)
|
2015-02-12 13:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected output /dev/snd/timer, received %s", actual)
|
2015-02-12 13:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
|
|
|
|
|
|
|
|
out, _, err = runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err, out)
|
2015-02-12 13:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
|
2015-02-12 13:51:51 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 23:16:42 -04:00
|
|
|
|
|
|
|
// TestRunDetach checks attaching and detaching with the escape sequence.
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
|
2015-04-10 23:16:42 -04:00
|
|
|
name := "attach-detach"
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
cmd.Stdin = tty
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
if err := waitRun(name); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(name, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatal("expected container to still be running")
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-04-18 12:46:47 -04:00
|
|
|
exec.Command(dockerBinary, "kill", name).Run()
|
2015-04-10 23:16:42 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-10 23:16:42 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 14:22:21 -04:00
|
|
|
|
|
|
|
// "test" should be printed
|
|
|
|
func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
|
|
|
|
testRequires(c, CpuCfsQuota)
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if out != "test" {
|
|
|
|
c.Errorf("container should've printed 'test'")
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err = inspectField("test", "HostConfig.CpuQuota")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
if out != "8000" {
|
|
|
|
c.Fatalf("setting the CPU CFS quota failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
|
|
|
|
testRequires(c, CpuCfsPeriod)
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true")
|
|
|
|
if _, err := runCommand(runCmd); err != nil {
|
|
|
|
c.Fatalf("failed to run container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := inspectField("test", "HostConfig.CpuPeriod")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
if out != "50000" {
|
|
|
|
c.Fatalf("setting the CPU CFS period failed")
|
|
|
|
}
|
|
|
|
}
|