f3ed4228 |
// +build !windows
package main
import ( |
f7538c77 |
"bufio" |
f3ed4228 |
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath" |
ef1d410b |
"regexp" |
94464e3a |
"strconv" |
f3ed4228 |
"strings"
"time"
|
15aa2a66 |
"github.com/docker/docker/pkg/homedir" |
b89fdc12 |
"github.com/docker/docker/pkg/integration/checker" |
f3ed4228 |
"github.com/docker/docker/pkg/mount" |
94464e3a |
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/sysinfo" |
dc944ea7 |
"github.com/go-check/check" |
f3ed4228 |
"github.com/kr/pty"
)
// #6509 |
dc944ea7 |
func (s *DockerSuite) TestRunRedirectStdout(c *check.C) { |
f3ed4228 |
checkRedirect := func(command string) {
_, tty, err := pty.Open() |
7a82429b |
c.Assert(err, checker.IsNil, check.Commentf("Could not open pty")) |
f3ed4228 |
cmd := exec.Command("sh", "-c", command)
cmd.Stdin = tty
cmd.Stdout = tty
cmd.Stderr = tty |
7a82429b |
c.Assert(cmd.Start(), checker.IsNil) |
4203230c |
ch := make(chan error) |
f3ed4228 |
go func() { |
4203230c |
ch <- cmd.Wait() |
f3ed4228 |
close(ch)
}()
select {
case <-time.After(10 * time.Second): |
dc944ea7 |
c.Fatal("command timeout") |
4203230c |
case err := <-ch: |
7a82429b |
c.Assert(err, checker.IsNil, check.Commentf("wait err")) |
f3ed4228 |
}
}
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 |
dc944ea7 |
func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) { |
ea3afdad |
// /tmp gets permission denied
testRequires(c, NotUserNamespace) |
f3ed4228 |
tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test") |
7a82429b |
c.Assert(err, checker.IsNil) |
f3ed4228 |
defer os.RemoveAll(tmpDir)
// Create a temporary tmpfs mount.
tmpfsDir := filepath.Join(tmpDir, "tmpfs") |
7a82429b |
c.Assert(os.MkdirAll(tmpfsDir, 0777), checker.IsNil, check.Commentf("failed to mkdir at %s", tmpfsDir))
c.Assert(mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""), checker.IsNil, check.Commentf("failed to create a tmpfs mount at %s", tmpfsDir)) |
f3ed4228 |
f, err := ioutil.TempFile(tmpfsDir, "touch-me") |
7a82429b |
c.Assert(err, checker.IsNil) |
f3ed4228 |
defer f.Close()
runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs") |
7a82429b |
out, _, _, err := runCommandWithStdoutStderr(runCmd)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Contains, filepath.Base(f.Name()), check.Commentf("Recursive bind mount test failed. Expected file not found")) |
f3ed4228 |
} |
3f390506 |
|
dc944ea7 |
func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) { |
157b66ad |
testRequires(c, DaemonIsLinux, NotUserNamespace) |
88b89511 |
if _, err := os.Stat("/dev/snd"); err != nil {
c.Skip("Host does not have /dev/snd")
} |
664004ed |
|
71868228 |
out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/") |
7a82429b |
c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "timer", check.Commentf("expected output /dev/snd/timer")) |
664004ed |
|
71868228 |
out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/") |
7a82429b |
c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "seq", check.Commentf("expected output /dev/othersnd/seq")) |
664004ed |
} |
f7538c77 |
|
15aa2a66 |
// TestRunDetach checks attaching and detaching with the default escape sequence. |
dc944ea7 |
func (s *DockerSuite) TestRunAttachDetach(c *check.C) { |
f7538c77 |
name := "attach-detach" |
15aa2a66 |
dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
cmd := exec.Command(dockerBinary, "attach", name) |
f7538c77 |
stdout, err := cmd.StdoutPipe() |
7a82429b |
c.Assert(err, checker.IsNil) |
f7538c77 |
cpty, tty, err := pty.Open() |
7a82429b |
c.Assert(err, checker.IsNil) |
f7538c77 |
defer cpty.Close()
cmd.Stdin = tty |
7a82429b |
c.Assert(cmd.Start(), checker.IsNil) |
799d9605 |
c.Assert(waitRun(name), check.IsNil) |
f7538c77 |
|
7a82429b |
_, err = cpty.Write([]byte("hello\n"))
c.Assert(err, checker.IsNil) |
f7538c77 |
out, err := bufio.NewReader(stdout).ReadString('\n') |
7a82429b |
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Equals, "hello") |
f7538c77 |
// escape sequence |
7a82429b |
_, err = cpty.Write([]byte{16})
c.Assert(err, checker.IsNil) |
f7538c77 |
time.Sleep(100 * time.Millisecond) |
7a82429b |
_, err = cpty.Write([]byte{17})
c.Assert(err, checker.IsNil) |
f7538c77 |
ch := make(chan struct{})
go func() {
cmd.Wait()
ch <- struct{}{}
}()
|
15aa2a66 |
select {
case <-ch:
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for container to exit")
}
running, err := inspectField(name, "State.Running")
c.Assert(err, checker.IsNil)
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
}
// TestRunDetach checks attaching and detaching with the escape sequence specified via flags.
func (s *DockerSuite) TestRunAttachDetachFromFlag(c *check.C) {
name := "attach-detach"
keyCtrlA := []byte{1}
keyA := []byte{97}
dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
cmd := exec.Command(dockerBinary, "attach", "--detach-keys='ctrl-a,a'", name)
stdout, err := cmd.StdoutPipe()
if err != nil {
c.Fatal(err)
}
cpty, tty, err := pty.Open()
if err != nil {
c.Fatal(err)
}
defer cpty.Close()
cmd.Stdin = tty
if err := cmd.Start(); err != nil {
c.Fatal(err)
}
c.Assert(waitRun(name), check.IsNil)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
c.Fatal(err)
}
out, err := bufio.NewReader(stdout).ReadString('\n')
if err != nil {
c.Fatal(err)
}
if strings.TrimSpace(out) != "hello" {
c.Fatalf("expected 'hello', got %q", out)
}
// escape sequence
if _, err := cpty.Write(keyCtrlA); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write(keyA); err != nil {
c.Fatal(err)
}
ch := make(chan struct{})
go func() {
cmd.Wait()
ch <- struct{}{}
}()
select {
case <-ch:
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for container to exit")
}
running, err := inspectField(name, "State.Running")
c.Assert(err, checker.IsNil)
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
}
// TestRunDetach checks attaching and detaching with the escape sequence specified via config file.
func (s *DockerSuite) TestRunAttachDetachFromConfig(c *check.C) {
keyCtrlA := []byte{1}
keyA := []byte{97}
// Setup config
homeKey := homedir.Key()
homeVal := homedir.Get()
tmpDir, err := ioutil.TempDir("", "fake-home")
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpDir)
dotDocker := filepath.Join(tmpDir, ".docker")
os.Mkdir(dotDocker, 0600)
tmpCfg := filepath.Join(dotDocker, "config.json")
defer func() { os.Setenv(homeKey, homeVal) }()
os.Setenv(homeKey, tmpDir)
data := `{
"detachKeys": "ctrl-a,a"
}`
err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
c.Assert(err, checker.IsNil)
// Then do the work
name := "attach-detach"
dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
cmd := exec.Command(dockerBinary, "attach", name)
stdout, err := cmd.StdoutPipe()
if err != nil {
c.Fatal(err)
}
cpty, tty, err := pty.Open()
if err != nil {
c.Fatal(err)
}
defer cpty.Close()
cmd.Stdin = tty
if err := cmd.Start(); err != nil {
c.Fatal(err)
}
c.Assert(waitRun(name), check.IsNil)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
c.Fatal(err)
}
out, err := bufio.NewReader(stdout).ReadString('\n')
if err != nil {
c.Fatal(err)
}
if strings.TrimSpace(out) != "hello" {
c.Fatalf("expected 'hello', got %q", out)
}
// escape sequence
if _, err := cpty.Write(keyCtrlA); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write(keyA); err != nil {
c.Fatal(err)
}
ch := make(chan struct{})
go func() {
cmd.Wait()
ch <- struct{}{}
}()
select {
case <-ch:
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for container to exit")
}
|
f7538c77 |
running, err := inspectField(name, "State.Running") |
7a82429b |
c.Assert(err, checker.IsNil)
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running")) |
15aa2a66 |
}
// TestRunDetach checks attaching and detaching with the detach flags, making sure it overrides config file
func (s *DockerSuite) TestRunAttachDetachKeysOverrideConfig(c *check.C) {
keyCtrlA := []byte{1}
keyA := []byte{97}
// Setup config
homeKey := homedir.Key()
homeVal := homedir.Get()
tmpDir, err := ioutil.TempDir("", "fake-home")
c.Assert(err, checker.IsNil)
defer os.RemoveAll(tmpDir)
dotDocker := filepath.Join(tmpDir, ".docker")
os.Mkdir(dotDocker, 0600)
tmpCfg := filepath.Join(dotDocker, "config.json") |
f7538c77 |
|
15aa2a66 |
defer func() { os.Setenv(homeKey, homeVal) }()
os.Setenv(homeKey, tmpDir)
data := `{
"detachKeys": "ctrl-e,e"
}`
err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
c.Assert(err, checker.IsNil)
// Then do the work
name := "attach-detach"
dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
cmd := exec.Command(dockerBinary, "attach", "--detach-keys='ctrl-a,a'", name)
stdout, err := cmd.StdoutPipe()
if err != nil {
c.Fatal(err)
}
cpty, tty, err := pty.Open()
if err != nil {
c.Fatal(err)
}
defer cpty.Close()
cmd.Stdin = tty
if err := cmd.Start(); err != nil {
c.Fatal(err)
}
c.Assert(waitRun(name), check.IsNil)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
c.Fatal(err)
}
out, err := bufio.NewReader(stdout).ReadString('\n')
if err != nil {
c.Fatal(err)
}
if strings.TrimSpace(out) != "hello" {
c.Fatalf("expected 'hello', got %q", out)
}
// escape sequence
if _, err := cpty.Write(keyCtrlA); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write(keyA); err != nil {
c.Fatal(err)
}
ch := make(chan struct{}) |
f7538c77 |
go func() { |
15aa2a66 |
cmd.Wait()
ch <- struct{}{} |
f7538c77 |
}()
select {
case <-ch: |
15aa2a66 |
case <-time.After(10 * time.Second): |
dc944ea7 |
c.Fatal("timed out waiting for container to exit") |
f7538c77 |
} |
15aa2a66 |
running, err := inspectField(name, "State.Running")
c.Assert(err, checker.IsNil)
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running")) |
f7538c77 |
} |
a9141012 |
|
15aa2a66 |
// "test" should be printed |
0a426c47 |
func (s *DockerSuite) TestRunWithCPUQuota(c *check.C) { |
6b3c9281 |
testRequires(c, cpuCfsQuota) |
71868228 |
|
0a426c47 |
file := "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
out, _ := dockerCmd(c, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "8000") |
a9141012 |
|
0a426c47 |
out, err := inspectField("test", "HostConfig.CpuQuota") |
a9141012 |
c.Assert(err, check.IsNil) |
7a82429b |
c.Assert(out, checker.Equals, "8000", check.Commentf("setting the CPU CFS quota failed")) |
a9141012 |
}
func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) { |
6b3c9281 |
testRequires(c, cpuCfsPeriod) |
71868228 |
|
0a426c47 |
file := "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
out, _ := dockerCmd(c, "run", "--cpu-period", "50000", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "50000") |
a9141012 |
out, err := inspectField("test", "HostConfig.CpuPeriod")
c.Assert(err, check.IsNil) |
7a82429b |
c.Assert(out, checker.Equals, "50000", check.Commentf("setting the CPU CFS period failed")) |
a9141012 |
} |
c1b52448 |
|
b6f1b4ad |
func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) {
testRequires(c, kernelMemorySupport)
|
dd7b4fd6 |
file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes" |
0a426c47 |
stdout, _, _ := dockerCmdWithStdoutStderr(c, "run", "--kernel-memory", "50M", "--name", "test1", "busybox", "cat", file)
c.Assert(strings.TrimSpace(stdout), checker.Equals, "52428800") |
b6f1b4ad |
|
b89fdc12 |
out, err := inspectField("test1", "HostConfig.KernelMemory") |
b6f1b4ad |
c.Assert(err, check.IsNil) |
b89fdc12 |
c.Assert(out, check.Equals, "52428800") |
2347f980 |
}
func (s *DockerSuite) TestRunWithInvalidKernelMemory(c *check.C) {
testRequires(c, kernelMemorySupport)
out, _, err := dockerCmdWithError("run", "--kernel-memory", "2M", "busybox", "true")
c.Assert(err, check.NotNil)
expected := "Minimum kernel memory limit allowed is 4MB"
c.Assert(out, checker.Contains, expected) |
b89fdc12 |
out, _, err = dockerCmdWithError("run", "--kernel-memory", "-16m", "--name", "test2", "busybox", "echo", "test")
c.Assert(err, check.NotNil) |
2347f980 |
expected = "invalid size" |
b89fdc12 |
c.Assert(out, checker.Contains, expected) |
b6f1b4ad |
}
|
0a426c47 |
func (s *DockerSuite) TestRunWithCPUShares(c *check.C) { |
c340ca4f |
testRequires(c, cpuShare) |
0a426c47 |
file := "/sys/fs/cgroup/cpu/cpu.shares"
out, _ := dockerCmd(c, "run", "--cpu-shares", "1000", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "1000")
out, err := inspectField("test", "HostConfig.CPUShares")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "1000") |
c340ca4f |
}
// "test" should be printed
func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) {
testRequires(c, cpuShare)
testRequires(c, memoryLimitSupport) |
2046ba3e |
out, _, _ := dockerCmdWithStdoutStderr(c, "run", "--cpu-shares", "1000", "-m", "32m", "busybox", "echo", "test") |
7a82429b |
c.Assert(out, checker.Equals, "test\n", check.Commentf("container should've printed 'test'")) |
c340ca4f |
}
func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
testRequires(c, cgroupCpuset) |
0a426c47 |
file := "/sys/fs/cgroup/cpuset/cpuset.cpus"
out, _ := dockerCmd(c, "run", "--cpuset-cpus", "0", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
out, err := inspectField("test", "HostConfig.CpusetCpus")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "0") |
c340ca4f |
}
func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
testRequires(c, cgroupCpuset) |
0a426c47 |
file := "/sys/fs/cgroup/cpuset/cpuset.mems"
out, _ := dockerCmd(c, "run", "--cpuset-mems", "0", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
out, err := inspectField("test", "HostConfig.CpusetMems")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "0") |
c340ca4f |
}
func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
testRequires(c, blkioWeight) |
0a426c47 |
file := "/sys/fs/cgroup/blkio/blkio.weight"
out, _ := dockerCmd(c, "run", "--blkio-weight", "300", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "300")
out, err := inspectField("test", "HostConfig.BlkioWeight")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "300") |
c340ca4f |
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidBlkioWeight(c *check.C) { |
c340ca4f |
testRequires(c, blkioWeight) |
d550fbb8 |
out, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true")
c.Assert(err, check.NotNil, check.Commentf(out))
expected := "Range of blkio weight is from 10 to 1000"
c.Assert(out, checker.Contains, expected) |
c340ca4f |
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidPathforBlkioWeightDevice(c *check.C) { |
0fbfa144 |
testRequires(c, blkioWeight) |
673f2b86 |
out, _, err := dockerCmdWithError("run", "--blkio-weight-device", "/dev/sdX:100", "busybox", "true") |
0fbfa144 |
c.Assert(err, check.NotNil, check.Commentf(out))
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceReadBps(c *check.C) { |
3f15a055 |
testRequires(c, blkioWeight) |
2236ecdd |
out, _, err := dockerCmdWithError("run", "--device-read-bps", "/dev/sdX:500", "busybox", "true") |
3f15a055 |
c.Assert(err, check.NotNil, check.Commentf(out))
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceWriteBps(c *check.C) { |
3f15a055 |
testRequires(c, blkioWeight) |
2236ecdd |
out, _, err := dockerCmdWithError("run", "--device-write-bps", "/dev/sdX:500", "busybox", "true") |
3f15a055 |
c.Assert(err, check.NotNil, check.Commentf(out))
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceReadIOps(c *check.C) { |
843084b0 |
testRequires(c, blkioWeight)
out, _, err := dockerCmdWithError("run", "--device-read-iops", "/dev/sdX:500", "busybox", "true")
c.Assert(err, check.NotNil, check.Commentf(out))
}
|
673f2b86 |
func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceWriteIOps(c *check.C) { |
843084b0 |
testRequires(c, blkioWeight)
out, _, err := dockerCmdWithError("run", "--device-write-iops", "/dev/sdX:500", "busybox", "true")
c.Assert(err, check.NotNil, check.Commentf(out))
}
|
c1b52448 |
func (s *DockerSuite) TestRunOOMExitCode(c *check.C) { |
6b3c9281 |
testRequires(c, oomControl) |
c1b52448 |
errChan := make(chan error)
go func() {
defer close(errChan) |
2c53643b |
//changing memory to 40MB from 4MB due to an issue with GCCGO that test fails to start the container.
out, exitCode, _ := dockerCmdWithError("run", "-m", "40MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done") |
c1b52448 |
if expected := 137; exitCode != expected {
errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
}
}()
select {
case err := <-errChan:
c.Assert(err, check.IsNil) |
b1cc78b8 |
case <-time.After(600 * time.Second): |
c1b52448 |
c.Fatal("Timeout waiting for container to die on OOM")
}
}
|
0a426c47 |
func (s *DockerSuite) TestRunWithMemoryLimit(c *check.C) { |
c340ca4f |
testRequires(c, memoryLimitSupport)
|
0a426c47 |
file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
stdout, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "32M", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(stdout), checker.Equals, "33554432")
out, err := inspectField("test", "HostConfig.Memory")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "33554432") |
c340ca4f |
}
|
cf6a5364 |
// TestRunWithoutMemoryswapLimit sets memory limit and disables swap
// memory limit, this means the processes in the container can use
// 16M memory and as much swap memory as they need (if the host
// supports swap memory). |
c340ca4f |
func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) { |
157b66ad |
testRequires(c, DaemonIsLinux) |
c340ca4f |
testRequires(c, memoryLimitSupport)
testRequires(c, swapMemorySupport) |
b1cc78b8 |
dockerCmd(c, "run", "-m", "32m", "--memory-swap", "-1", "busybox", "true") |
c340ca4f |
}
func (s *DockerSuite) TestRunWithSwappiness(c *check.C) {
testRequires(c, memorySwappinessSupport) |
0a426c47 |
file := "/sys/fs/cgroup/memory/memory.swappiness"
out, _ := dockerCmd(c, "run", "--memory-swappiness", "0", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "0")
out, err := inspectField("test", "HostConfig.MemorySwappiness")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "0") |
c340ca4f |
}
func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) {
testRequires(c, memorySwappinessSupport)
out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true") |
3571814d |
c.Assert(err, check.NotNil)
expected := "Valid memory swappiness range is 0-100"
c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected))
out, _, err = dockerCmdWithError("run", "--memory-swappiness", "-10", "busybox", "true")
c.Assert(err, check.NotNil)
c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected)) |
c340ca4f |
} |
0e50d946 |
|
aa178099 |
func (s *DockerSuite) TestRunWithMemoryReservation(c *check.C) {
testRequires(c, memoryReservationSupport) |
0a426c47 |
file := "/sys/fs/cgroup/memory/memory.soft_limit_in_bytes"
out, _ := dockerCmd(c, "run", "--memory-reservation", "200M", "--name", "test", "busybox", "cat", file)
c.Assert(strings.TrimSpace(out), checker.Equals, "209715200")
out, err := inspectField("test", "HostConfig.MemoryReservation")
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "209715200") |
aa178099 |
}
func (s *DockerSuite) TestRunWithMemoryReservationInvalid(c *check.C) {
testRequires(c, memoryLimitSupport)
testRequires(c, memoryReservationSupport)
out, _, err := dockerCmdWithError("run", "-m", "500M", "--memory-reservation", "800M", "busybox", "true")
c.Assert(err, check.NotNil)
expected := "Minimum memory limit should be larger than memory reservation limit" |
7a82429b |
c.Assert(strings.TrimSpace(out), checker.Contains, expected, check.Commentf("run container should fail with invalid memory reservation")) |
aa178099 |
}
|
0e50d946 |
func (s *DockerSuite) TestStopContainerSignal(c *check.C) {
out, _ := dockerCmd(c, "run", "--stop-signal", "SIGUSR1", "-d", "busybox", "/bin/sh", "-c", `trap 'echo "exit trapped"; exit 0' USR1; while true; do sleep 1; done`)
containerID := strings.TrimSpace(out)
|
7a82429b |
c.Assert(waitRun(containerID), checker.IsNil) |
0e50d946 |
dockerCmd(c, "stop", containerID)
out, _ = dockerCmd(c, "logs", containerID)
|
7a82429b |
c.Assert(out, checker.Contains, "exit trapped", check.Commentf("Expected `exit trapped` in the log")) |
0e50d946 |
} |
ab39a4c9 |
func (s *DockerSuite) TestRunSwapLessThanMemoryLimit(c *check.C) {
testRequires(c, memoryLimitSupport)
testRequires(c, swapMemorySupport)
out, _, err := dockerCmdWithError("run", "-m", "16m", "--memory-swap", "15m", "busybox", "echo", "test")
expected := "Minimum memoryswap limit should be larger than memory limit"
c.Assert(err, check.NotNil)
|
7a82429b |
c.Assert(out, checker.Contains, expected) |
ab39a4c9 |
} |
94464e3a |
func (s *DockerSuite) TestRunInvalidCpusetCpusFlagValue(c *check.C) {
testRequires(c, cgroupCpuset)
sysInfo := sysinfo.New(true)
cpus, err := parsers.ParseUintList(sysInfo.Cpus)
c.Assert(err, check.IsNil)
var invalid int
for i := 0; i <= len(cpus)+1; i++ {
if !cpus[i] {
invalid = i
break
}
}
out, _, err := dockerCmdWithError("run", "--cpuset-cpus", strconv.Itoa(invalid), "busybox", "true")
c.Assert(err, check.NotNil) |
41de7a18 |
expected := fmt.Sprintf("Error response from daemon: Requested CPUs are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Cpus) |
7a82429b |
c.Assert(out, checker.Contains, expected) |
94464e3a |
}
func (s *DockerSuite) TestRunInvalidCpusetMemsFlagValue(c *check.C) {
testRequires(c, cgroupCpuset)
sysInfo := sysinfo.New(true)
mems, err := parsers.ParseUintList(sysInfo.Mems)
c.Assert(err, check.IsNil)
var invalid int
for i := 0; i <= len(mems)+1; i++ {
if !mems[i] {
invalid = i
break
}
}
out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
c.Assert(err, check.NotNil) |
41de7a18 |
expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Mems) |
7a82429b |
c.Assert(out, checker.Contains, expected) |
94464e3a |
} |
042d08a2 |
func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) { |
157b66ad |
testRequires(c, cpuShare, DaemonIsLinux) |
042d08a2 |
out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
c.Assert(err, check.NotNil, check.Commentf(out))
expected := "The minimum allowed cpu-shares is 2"
c.Assert(out, checker.Contains, expected)
out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
c.Assert(err, check.NotNil, check.Commentf(out))
expected = "shares: invalid argument"
c.Assert(out, checker.Contains, expected)
out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
c.Assert(err, check.NotNil, check.Commentf(out))
expected = "The maximum allowed cpu-shares is"
c.Assert(out, checker.Contains, expected)
} |
ef1d410b |
func (s *DockerSuite) TestRunWithDefaultShmSize(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "shm-default"
out, _ := dockerCmd(c, "run", "--name", name, "busybox", "mount")
shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
if !shmRegex.MatchString(out) {
c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
}
shmSize, err := inspectField(name, "HostConfig.ShmSize")
c.Assert(err, check.IsNil)
c.Assert(shmSize, check.Equals, "67108864")
}
func (s *DockerSuite) TestRunWithShmSize(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "shm"
out, _ := dockerCmd(c, "run", "--name", name, "--shm-size=1G", "busybox", "mount")
shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
if !shmRegex.MatchString(out) {
c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
}
shmSize, err := inspectField(name, "HostConfig.ShmSize")
c.Assert(err, check.IsNil)
c.Assert(shmSize, check.Equals, "1073741824")
} |
b3e527df |
func (s *DockerSuite) TestRunTmpfsMounts(c *check.C) {
// TODO Windows (Post TP4): This test cannot run on a Windows daemon as
// Windows does not support tmpfs mounts.
testRequires(c, DaemonIsLinux)
if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "busybox", "touch", "/run/somefile"); err != nil {
c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
} |
89a775d2 |
if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec", "busybox", "touch", "/run/somefile"); err != nil {
c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
} |
b3e527df |
if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec,nosuid,rw,size=5k,mode=700", "busybox", "touch", "/run/somefile"); err != nil {
c.Fatalf("/run failed to mount on tmpfs with valid options %q %s", err, out)
}
if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run:foobar", "busybox", "touch", "/run/somefile"); err == nil {
c.Fatalf("/run mounted on tmpfs when it should have vailed within invalid mount option")
}
if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "-v", "/run:/run", "busybox", "touch", "/run/somefile"); err == nil {
c.Fatalf("Should have generated an error saying Duplicate mount points")
}
} |
0433e389 |
|
626c9337 |
// TestRunSeccompProfileDenyUnshare checks that 'docker run --security-opt seccomp:/tmp/profile.json debian:jessie unshare' exits with operation not permitted. |
0433e389 |
func (s *DockerSuite) TestRunSeccompProfileDenyUnshare(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled)
jsonData := `{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"name": "unshare",
"action": "SCMP_ACT_ERRNO"
}
]
}`
tmpFile, err := ioutil.TempFile("", "profile.json")
defer tmpFile.Close()
if err != nil {
c.Fatal(err)
}
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
c.Fatal(err)
} |
a48fe623 |
runCmd := exec.Command(dockerBinary, "run", "--security-opt", "apparmor:unconfined", "--security-opt", "seccomp:"+tmpFile.Name(), "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc") |
0433e389 |
out, _, _ := runCommandWithOutput(runCmd)
if !strings.Contains(out, "Operation not permitted") {
c.Fatalf("expected unshare with seccomp profile denied to fail, got %s", out)
}
}
// TestRunSeccompProfileDenyChmod checks that 'docker run --security-opt seccomp:/tmp/profile.json busybox chmod 400 /etc/hostname' exits with operation not permitted.
func (s *DockerSuite) TestRunSeccompProfileDenyChmod(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled)
jsonData := `{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"name": "chmod",
"action": "SCMP_ACT_ERRNO"
}
]
}`
tmpFile, err := ioutil.TempFile("", "profile.json")
defer tmpFile.Close()
if err != nil {
c.Fatal(err)
}
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
c.Fatal(err)
}
runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:"+tmpFile.Name(), "busybox", "chmod", "400", "/etc/hostname")
out, _, _ := runCommandWithOutput(runCmd)
if !strings.Contains(out, "Operation not permitted") {
c.Fatalf("expected chmod with seccomp profile denied to fail, got %s", out)
}
} |
947293a2 |
|
626c9337 |
// TestRunSeccompProfileDenyUnshareUserns checks that 'docker run debian:jessie unshare --map-root-user --user sh -c whoami' with a specific profile to |
a48fe623 |
// deny unhare of a userns exits with operation not permitted.
func (s *DockerSuite) TestRunSeccompProfileDenyUnshareUserns(c *check.C) { |
947293a2 |
testRequires(c, SameHostDaemon, seccompEnabled)
// from sched.h
jsonData := fmt.Sprintf(`{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"name": "unshare",
"action": "SCMP_ACT_ERRNO",
"args": [
{
"index": 0,
"value": %d,
"op": "SCMP_CMP_EQ"
}
]
}
]
}`, uint64(0x10000000))
tmpFile, err := ioutil.TempFile("", "profile.json")
defer tmpFile.Close()
if err != nil {
c.Fatal(err)
}
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
c.Fatal(err)
} |
a48fe623 |
runCmd := exec.Command(dockerBinary, "run", "--security-opt", "apparmor:unconfined", "--security-opt", "seccomp:"+tmpFile.Name(), "debian:jessie", "unshare", "--map-root-user", "--user", "sh", "-c", "whoami") |
947293a2 |
out, _, _ := runCommandWithOutput(runCmd)
if !strings.Contains(out, "Operation not permitted") {
c.Fatalf("expected unshare userns with seccomp profile denied to fail, got %s", out)
}
} |
a48fe623 |
|
327421d1 |
// TestRunSeccompProfileDenyCloneUserns checks that 'docker run syscall-test' |
a48fe623 |
// with a the default seccomp profile exits with operation not permitted.
func (s *DockerSuite) TestRunSeccompProfileDenyCloneUserns(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled)
|
327421d1 |
runCmd := exec.Command(dockerBinary, "run", "syscall-test", "userns-test", "id") |
a48fe623 |
out, _, err := runCommandWithOutput(runCmd)
if err == nil || !strings.Contains(out, "clone failed: Operation not permitted") {
c.Fatalf("expected clone userns with default seccomp profile denied to fail, got %s: %v", out, err)
}
}
|
15674c5f |
// TestRunSeccompUnconfinedCloneUserns checks that |
327421d1 |
// 'docker run --security-opt seccomp:unconfined syscall-test' allows creating a userns. |
15674c5f |
func (s *DockerSuite) TestRunSeccompUnconfinedCloneUserns(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled, NotUserNamespace)
// make sure running w privileged is ok |
327421d1 |
runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "syscall-test", "userns-test", "id") |
15674c5f |
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
c.Fatalf("expected clone userns with --security-opt seccomp:unconfined to succeed, got %s: %v", out, err)
}
}
|
327421d1 |
// TestRunSeccompAllowPrivCloneUserns checks that 'docker run --privileged syscall-test' |
15674c5f |
// allows creating a userns. |
a48fe623 |
func (s *DockerSuite) TestRunSeccompAllowPrivCloneUserns(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled, NotUserNamespace)
// make sure running w privileged is ok |
327421d1 |
runCmd := exec.Command(dockerBinary, "run", "--privileged", "syscall-test", "userns-test", "id") |
a48fe623 |
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
c.Fatalf("expected clone userns with --privileged to succeed, got %s: %v", out, err)
}
}
// TestRunSeccompAllowAptKey checks that 'docker run debian:jessie apt-key' succeeds.
func (s *DockerSuite) TestRunSeccompAllowAptKey(c *check.C) { |
327421d1 |
testRequires(c, SameHostDaemon, seccompEnabled, Network) |
a48fe623 |
// apt-key uses setrlimit & getrlimit, so we want to make sure we don't break it
runCmd := exec.Command(dockerBinary, "run", "debian:jessie", "apt-key", "adv", "--keyserver", "hkp://p80.pool.sks-keyservers.net:80", "--recv-keys", "E871F18B51E0147C77796AC81196BA81F6B0FC61")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf("expected apt-key with seccomp to succeed, got %s: %v", out, err)
}
} |
327421d1 |
func (s *DockerSuite) TestRunSeccompDefaultProfile(c *check.C) {
testRequires(c, SameHostDaemon, seccompEnabled, NotUserNamespace)
out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "acct-test")
if err == nil || !strings.Contains(out, "Operation not permitted") {
c.Fatalf("expected Operation not permitted, got: %s", out)
}
out, _, err = dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "ns-test", "echo", "hello")
if err == nil || !strings.Contains(out, "Operation not permitted") {
c.Fatalf("expected Operation not permitted, got: %s", out)
}
out, _, err = dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp:unconfined", "syscall-test", "acct-test")
if err == nil || !strings.Contains(out, "No such file or directory") {
c.Fatalf("expected No such file or directory, got: %s", out)
}
out, _, err = dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp:unconfined", "syscall-test", "ns-test", "echo", "hello")
if err != nil || !strings.Contains(out, "hello") {
c.Fatalf("expected hello, got: %s, %v", out, err)
}
} |
2b4f64e5 |
func (s *DockerSuite) TestRunApparmorProcDirectory(c *check.C) {
testRequires(c, SameHostDaemon, Apparmor)
// running w seccomp unconfined tests the apparmor profile
runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "debian:jessie", "chmod", "777", "/proc/1/cgroup")
if out, _, err := runCommandWithOutput(runCmd); err == nil || !(strings.Contains(out, "Permission denied") || strings.Contains(out, "Operation not permitted")) {
c.Fatalf("expected chmod 777 /proc/1/cgroup to fail, got %s: %v", out, err)
}
runCmd = exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "debian:jessie", "chmod", "777", "/proc/1/attr/current")
if out, _, err := runCommandWithOutput(runCmd); err == nil || !(strings.Contains(out, "Permission denied") || strings.Contains(out, "Operation not permitted")) {
c.Fatalf("expected chmod 777 /proc/1/attr/current to fail, got %s: %v", out, err)
}
} |