2014-02-25 16:17:48 +00:00
package main
import (
2014-08-12 09:36:49 +00:00
"bufio"
2014-10-17 18:06:05 +00:00
"bytes"
2014-02-25 16:17:48 +00:00
"fmt"
2014-06-26 11:03:23 +00:00
"io/ioutil"
2014-09-06 01:59:31 +00:00
"net"
2014-04-18 02:53:08 +00:00
"os"
2014-02-25 16:17:48 +00:00
"os/exec"
2014-08-28 00:25:10 +00:00
"path"
2014-07-30 02:22:50 +00:00
"path/filepath"
2014-02-17 23:14:30 +00:00
"reflect"
2014-04-18 01:58:20 +00:00
"regexp"
2016-05-25 17:19:17 +00:00
"runtime"
2014-04-18 02:53:08 +00:00
"sort"
2014-09-17 01:08:30 +00:00
"strconv"
2014-02-25 16:17:48 +00:00
"strings"
2014-04-18 02:46:03 +00:00
"sync"
2014-08-12 09:36:49 +00:00
"time"
2014-06-26 11:03:23 +00:00
2015-09-27 11:09:27 +00:00
"github.com/docker/docker/pkg/integration/checker"
2015-12-14 15:39:53 +00:00
"github.com/docker/docker/pkg/mount"
2016-04-08 22:25:07 +00:00
"github.com/docker/docker/pkg/stringid"
2016-02-28 10:47:37 +00:00
"github.com/docker/docker/pkg/stringutils"
2015-09-27 11:09:27 +00:00
"github.com/docker/docker/runconfig"
2015-12-18 17:58:48 +00:00
"github.com/docker/go-connections/nat"
2015-05-06 22:39:29 +00:00
"github.com/docker/libnetwork/resolvconf"
2016-05-08 07:33:16 +00:00
"github.com/docker/libnetwork/types"
2015-04-18 16:46:47 +00:00
"github.com/go-check/check"
2016-03-30 21:25:51 +00:00
libcontainerUser "github.com/opencontainers/runc/libcontainer/user"
2014-02-25 16:17:48 +00:00
)
// "test123" should be printed by docker run
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEchoStdout ( c * check . C ) {
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "echo" , "test123" )
2014-02-25 16:17:48 +00:00
if out != "test123\n" {
2015-09-01 21:37:04 +00:00
c . Fatalf ( "container should've printed 'test123', got '%s'" , out )
2014-02-25 16:17:48 +00:00
}
}
// "test" should be printed
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEchoNamedContainer ( c * check . C ) {
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--name" , "testfoonamedcontainer" , "busybox" , "echo" , "test" )
2014-02-25 16:17:48 +00:00
if out != "test\n" {
2015-04-18 16:46:47 +00:00
c . Errorf ( "container should've printed 'test'" )
2014-02-25 16:17:48 +00:00
}
}
2015-09-01 21:37:04 +00:00
// docker run should not leak file descriptors. This test relies on Unix
// specific functionality and cannot run on Windows.
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunLeakyFileDescriptors ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "ls" , "-C" , "/proc/self/fd" )
2014-04-29 05:22:54 +00:00
// normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
if out != "0 1 2 3\n" {
2015-04-18 16:46:47 +00:00
c . Errorf ( "container should've printed '0 1 2 3', not: %s" , out )
2014-04-29 05:22:54 +00:00
}
}
2015-03-11 05:09:09 +00:00
// it should be possible to lookup Google DNS
2014-02-25 16:17:48 +00:00
// this will fail when Internet access is unavailable
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunLookupGoogleDns ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , Network , NotArm )
2015-09-01 21:37:04 +00:00
image := DefaultImage
if daemonPlatform == "windows" {
// nslookup isn't present in Windows busybox. Is built-in.
image = WindowsBaseImage
}
dockerCmd ( c , "run" , image , "nslookup" , "google.com" )
2014-02-25 16:17:48 +00:00
}
// the exit code should be 0
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunExitCodeZero ( c * check . C ) {
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "busybox" , "true" )
2014-02-25 16:17:48 +00:00
}
// the exit code should be 1
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunExitCodeOne ( c * check . C ) {
2015-07-27 18:13:25 +00:00
_ , exitCode , err := dockerCmdWithError ( "run" , "busybox" , "false" )
2016-03-24 16:43:04 +00:00
c . Assert ( err , checker . NotNil )
c . Assert ( exitCode , checker . Equals , 1 )
2014-02-25 16:17:48 +00:00
}
// it should be possible to pipe in data via stdin to a process running in a container
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunStdinPipe ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This needs some work to make compatible.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-05-16 18:04:25 +00:00
runCmd := exec . Command ( dockerBinary , "run" , "-i" , "-a" , "stdin" , "busybox" , "cat" )
runCmd . Stdin = strings . NewReader ( "blahblah" )
2014-02-25 16:17:48 +00:00
out , _ , _ , err := runCommandWithStdoutStderr ( runCmd )
2014-09-12 13:51:21 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "failed to run container: %v, output: %q" , err , out )
2014-09-12 13:51:21 +00:00
}
2014-02-25 16:17:48 +00:00
2015-04-06 13:21:18 +00:00
out = strings . TrimSpace ( out )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "wait" , out )
2014-02-25 16:17:48 +00:00
2015-07-19 20:47:47 +00:00
logsOut , _ := dockerCmd ( c , "logs" , out )
2014-02-25 16:17:48 +00:00
2015-04-06 13:21:18 +00:00
containerLogs := strings . TrimSpace ( logsOut )
2014-02-25 16:17:48 +00:00
if containerLogs != "blahblah" {
2015-04-18 16:46:47 +00:00
c . Errorf ( "logs didn't print the container's logs %s" , containerLogs )
2014-02-25 16:17:48 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "rm" , out )
2014-02-25 16:17:48 +00:00
}
// the container's ID should be printed when starting a container in detached mode
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDetachedContainerIDPrinting ( c * check . C ) {
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "true" )
2014-02-25 16:17:48 +00:00
2015-04-06 13:21:18 +00:00
out = strings . TrimSpace ( out )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "wait" , out )
2014-02-25 16:17:48 +00:00
2015-07-19 20:47:47 +00:00
rmOut , _ := dockerCmd ( c , "rm" , out )
2014-02-25 16:17:48 +00:00
2015-04-06 13:21:18 +00:00
rmOut = strings . TrimSpace ( rmOut )
2014-02-25 16:17:48 +00:00
if rmOut != out {
2015-04-18 16:46:47 +00:00
c . Errorf ( "rm didn't print the container ID %s %s" , out , rmOut )
2014-02-25 16:17:48 +00:00
}
}
// the working directory should be set correctly
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWorkingDirectory ( c * check . C ) {
2015-09-01 21:37:04 +00:00
dir := "/root"
image := "busybox"
if daemonPlatform == "windows" {
2016-05-24 22:13:06 +00:00
dir = ` C:/Windows `
2015-09-01 21:37:04 +00:00
}
2014-02-25 16:17:48 +00:00
2015-09-01 21:37:04 +00:00
// First with -w
out , _ := dockerCmd ( c , "run" , "-w" , dir , image , "pwd" )
2015-04-06 13:21:18 +00:00
out = strings . TrimSpace ( out )
2015-09-01 21:37:04 +00:00
if out != dir {
2015-04-18 16:46:47 +00:00
c . Errorf ( "-w failed to set working directory" )
2014-02-25 16:17:48 +00:00
}
2015-09-01 21:37:04 +00:00
// Then with --workdir
out , _ = dockerCmd ( c , "run" , "--workdir" , dir , image , "pwd" )
2015-04-06 13:21:18 +00:00
out = strings . TrimSpace ( out )
2015-09-01 21:37:04 +00:00
if out != dir {
2015-04-18 16:46:47 +00:00
c . Errorf ( "--workdir failed to set working directory" )
2014-02-25 16:17:48 +00:00
}
}
// pinging Google's DNS resolver should fail when we disable the networking
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWithoutNetworking ( c * check . C ) {
2015-09-01 21:37:04 +00:00
count := "-c"
image := "busybox"
if daemonPlatform == "windows" {
count = "-n"
image = WindowsBaseImage
}
// First using the long form --net
out , exitCode , err := dockerCmdWithError ( "run" , "--net=none" , image , "ping" , count , "1" , "8.8.8.8" )
2014-02-25 16:17:48 +00:00
if err != nil && exitCode != 1 {
2015-04-18 16:46:47 +00:00
c . Fatal ( out , err )
2014-02-25 16:17:48 +00:00
}
if exitCode != 1 {
2015-04-18 16:46:47 +00:00
c . Errorf ( "--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8" )
2014-02-25 16:17:48 +00:00
}
}
2014-03-31 17:41:40 +00:00
2015-01-14 07:25:58 +00:00
//test --link use container name to link target
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunLinksContainerWithContainerName ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as the networking
// settings are not populated back yet on inspect.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-i" , "-t" , "-d" , "--name" , "parent" , "busybox" )
2016-01-28 14:19:25 +00:00
ip := inspectField ( c , "parent" , "NetworkSettings.Networks.bridge.IPAddress" )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--link" , "parent:test" , "busybox" , "/bin/cat" , "/etc/hosts" )
2015-01-14 07:25:58 +00:00
if ! strings . Contains ( out , ip + " test" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "use a container name to link target failed" )
2015-01-14 07:25:58 +00:00
}
}
//test --link use container id to link target
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunLinksContainerWithContainerId ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as the networking
// settings are not populated back yet on inspect.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
cID , _ := dockerCmd ( c , "run" , "-i" , "-t" , "-d" , "busybox" )
2015-01-14 07:25:58 +00:00
cID = strings . TrimSpace ( cID )
2016-01-28 14:19:25 +00:00
ip := inspectField ( c , cID , "NetworkSettings.Networks.bridge.IPAddress" )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--link" , cID + ":test" , "busybox" , "/bin/cat" , "/etc/hosts" )
2015-01-14 07:25:58 +00:00
if ! strings . Contains ( out , ip + " test" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "use a container id to link target failed" )
2015-01-14 07:25:58 +00:00
}
}
2016-01-05 19:20:47 +00:00
func ( s * DockerSuite ) TestUserDefinedNetworkLinks ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2016-01-05 19:20:47 +00:00
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "udlinkNet" )
dockerCmd ( c , "run" , "-d" , "--net=udlinkNet" , "--name=first" , "busybox" , "top" )
c . Assert ( waitRun ( "first" ) , check . IsNil )
// run a container in user-defined network udlinkNet with a link for an existing container
2016-02-11 23:21:52 +00:00
// and a link for a container that doesn't exist
2016-01-05 19:20:47 +00:00
dockerCmd ( c , "run" , "-d" , "--net=udlinkNet" , "--name=second" , "--link=first:foo" ,
"--link=third:bar" , "busybox" , "top" )
c . Assert ( waitRun ( "second" ) , check . IsNil )
// ping to first and its alias foo must succeed
_ , _ , err := dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo" )
c . Assert ( err , check . IsNil )
// ping to third and its alias must fail
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "third" )
c . Assert ( err , check . NotNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "bar" )
c . Assert ( err , check . NotNil )
// start third container now
dockerCmd ( c , "run" , "-d" , "--net=udlinkNet" , "--name=third" , "busybox" , "top" )
c . Assert ( waitRun ( "third" ) , check . IsNil )
// ping to third and its alias must succeed now
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "third" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "bar" )
c . Assert ( err , check . IsNil )
}
func ( s * DockerSuite ) TestUserDefinedNetworkLinksWithRestart ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2016-01-05 19:20:47 +00:00
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "udlinkNet" )
dockerCmd ( c , "run" , "-d" , "--net=udlinkNet" , "--name=first" , "busybox" , "top" )
c . Assert ( waitRun ( "first" ) , check . IsNil )
dockerCmd ( c , "run" , "-d" , "--net=udlinkNet" , "--name=second" , "--link=first:foo" ,
"busybox" , "top" )
c . Assert ( waitRun ( "second" ) , check . IsNil )
// ping to first and its alias foo must succeed
_ , _ , err := dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo" )
c . Assert ( err , check . IsNil )
// Restart first container
dockerCmd ( c , "restart" , "first" )
c . Assert ( waitRun ( "first" ) , check . IsNil )
// ping to first and its alias foo must still succeed
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo" )
c . Assert ( err , check . IsNil )
// Restart second container
dockerCmd ( c , "restart" , "second" )
c . Assert ( waitRun ( "second" ) , check . IsNil )
// ping to first and its alias foo must still succeed
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo" )
c . Assert ( err , check . IsNil )
}
2016-03-02 08:52:41 +00:00
func ( s * DockerSuite ) TestRunWithNetAliasOnDefaultNetworks ( c * check . C ) {
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
defaults := [ ] string { "bridge" , "host" , "none" }
for _ , net := range defaults {
out , _ , err := dockerCmdWithError ( "run" , "-d" , "--net" , net , "--net-alias" , "alias_" + net , "busybox" , "top" )
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , runconfig . ErrUnsupportedNetworkAndAlias . Error ( ) )
}
}
2016-01-08 13:45:56 +00:00
func ( s * DockerSuite ) TestUserDefinedNetworkAlias ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2016-01-08 13:45:56 +00:00
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "net1" )
2016-04-08 22:25:07 +00:00
cid1 , _ := dockerCmd ( c , "run" , "-d" , "--net=net1" , "--name=first" , "--net-alias=foo1" , "--net-alias=foo2" , "busybox" , "top" )
2016-01-08 13:45:56 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2016-04-08 22:25:07 +00:00
// Check if default short-id alias is added automatically
id := strings . TrimSpace ( cid1 )
aliases := inspectField ( c , id , "NetworkSettings.Networks.net1.Aliases" )
c . Assert ( aliases , checker . Contains , stringid . TruncateID ( id ) )
cid2 , _ := dockerCmd ( c , "run" , "-d" , "--net=net1" , "--name=second" , "busybox" , "top" )
2016-01-08 13:45:56 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2016-04-08 22:25:07 +00:00
// Check if default short-id alias is added automatically
id = strings . TrimSpace ( cid2 )
aliases = inspectField ( c , id , "NetworkSettings.Networks.net1.Aliases" )
c . Assert ( aliases , checker . Contains , stringid . TruncateID ( id ) )
2016-01-08 13:45:56 +00:00
// ping to first and its network-scoped aliases
_ , _ , err := dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo1" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo2" )
c . Assert ( err , check . IsNil )
2016-04-08 22:25:07 +00:00
// ping first container's short-id alias
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , stringid . TruncateID ( cid1 ) )
c . Assert ( err , check . IsNil )
2016-01-08 13:45:56 +00:00
// Restart first container
dockerCmd ( c , "restart" , "first" )
c . Assert ( waitRun ( "first" ) , check . IsNil )
// ping to first and its network-scoped aliases must succeed
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "first" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo1" )
c . Assert ( err , check . IsNil )
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , "foo2" )
c . Assert ( err , check . IsNil )
2016-04-08 22:25:07 +00:00
// ping first container's short-id alias
_ , _ , err = dockerCmdWithError ( "exec" , "second" , "ping" , "-c" , "1" , stringid . TruncateID ( cid1 ) )
c . Assert ( err , check . IsNil )
2016-01-08 13:45:56 +00:00
}
2015-05-01 14:00:43 +00:00
// Issue 9677.
func ( s * DockerSuite ) TestRunWithDaemonFlags ( c * check . C ) {
2015-11-09 14:37:24 +00:00
out , _ , err := dockerCmdWithError ( "--exec-opt" , "foo=bar" , "run" , "-i" , "busybox" , "true" )
2015-05-01 14:00:43 +00:00
if err != nil {
2015-11-09 14:37:24 +00:00
if ! strings . Contains ( out , "flag provided but not defined: --exec-opt" ) { // no daemon (client-only)
2015-05-01 14:00:43 +00:00
c . Fatal ( err , out )
}
}
}
2014-04-03 14:27:07 +00:00
// Regression test for #4979
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWithVolumesFromExited ( c * check . C ) {
2015-09-23 23:04:51 +00:00
var (
out string
exitCode int
)
// Create a file in a volume
if daemonPlatform == "windows" {
2016-03-29 00:26:34 +00:00
out , exitCode = dockerCmd ( c , "run" , "--name" , "test-data" , "--volume" , ` c:\some\dir ` , WindowsBaseImage , "cmd" , "/c" , ` echo hello > c:\some\dir\file ` )
2015-09-23 23:04:51 +00:00
} else {
out , exitCode = dockerCmd ( c , "run" , "--name" , "test-data" , "--volume" , "/some/dir" , "busybox" , "touch" , "/some/dir/file" )
}
2015-07-19 20:47:47 +00:00
if exitCode != 0 {
c . Fatal ( "1" , out , exitCode )
2014-04-03 14:27:07 +00:00
}
2015-09-23 23:04:51 +00:00
// Read the file from another container using --volumes-from to access the volume in the second container
if daemonPlatform == "windows" {
2016-03-29 00:26:34 +00:00
out , exitCode = dockerCmd ( c , "run" , "--volumes-from" , "test-data" , WindowsBaseImage , "cmd" , "/c" , ` type c:\some\dir\file ` )
2015-09-23 23:04:51 +00:00
} else {
out , exitCode = dockerCmd ( c , "run" , "--volumes-from" , "test-data" , "busybox" , "cat" , "/some/dir/file" )
}
2015-07-19 20:47:47 +00:00
if exitCode != 0 {
c . Fatal ( "2" , out , exitCode )
2014-04-03 14:27:07 +00:00
}
}
2015-03-30 18:35:37 +00:00
// Volume path is a symlink which also exists on the host, and the host side is a file not a dir
// But the volume call is just a normal volume, not a bind mount
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCreateVolumesInSymlinkDir ( c * check . C ) {
2015-09-23 23:04:51 +00:00
var (
dockerFile string
containerPath string
cmd string
)
2016-04-05 20:21:20 +00:00
// TODO Windows (Post TP5): This test cannot run on a Windows daemon as
// Windows does not support symlinks inside a volume path
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-03-30 18:35:37 +00:00
name := "test-volume-symlink"
dir , err := ioutil . TempDir ( "" , name )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-03-29 05:35:36 +00:00
}
2015-03-30 18:35:37 +00:00
defer os . RemoveAll ( dir )
2016-01-28 22:52:34 +00:00
// In the case of Windows to Windows CI, if the machine is setup so that
// the temp directory is not the C: drive, this test is invalid and will
// not work.
if daemonPlatform == "windows" && strings . ToLower ( dir [ : 1 ] ) != "c" {
c . Skip ( "Requires TEMP to point to C: drive" )
}
2015-03-30 18:35:37 +00:00
f , err := os . OpenFile ( filepath . Join ( dir , "test" ) , os . O_CREATE , 0700 )
2015-03-29 05:35:36 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-03-30 18:35:37 +00:00
}
f . Close ( )
2015-09-23 23:04:51 +00:00
if daemonPlatform == "windows" {
dockerFile = fmt . Sprintf ( "FROM %s\nRUN mkdir %s\nRUN mklink /D c:\\test %s" , WindowsBaseImage , dir , dir )
containerPath = ` c:\test\test `
cmd = "tasklist"
} else {
dockerFile = fmt . Sprintf ( "FROM busybox\nRUN mkdir -p %s\nRUN ln -s %s /test" , dir , dir )
containerPath = "/test/test"
cmd = "true"
}
2015-03-30 18:35:37 +00:00
if _ , err := buildImage ( name , dockerFile , false ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-03-30 18:35:37 +00:00
}
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "-v" , containerPath , name , cmd )
2015-03-29 05:35:36 +00:00
}
2016-01-28 22:52:34 +00:00
// Volume path is a symlink in the container
func ( s * DockerSuite ) TestRunCreateVolumesInSymlinkDir2 ( c * check . C ) {
var (
dockerFile string
containerPath string
cmd string
)
2016-04-05 20:21:20 +00:00
// TODO Windows (Post TP5): This test cannot run on a Windows daemon as
// Windows does not support symlinks inside a volume path
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2016-01-28 22:52:34 +00:00
name := "test-volume-symlink2"
if daemonPlatform == "windows" {
dockerFile = fmt . Sprintf ( "FROM %s\nRUN mkdir c:\\%s\nRUN mklink /D c:\\test c:\\%s" , WindowsBaseImage , name , name )
containerPath = ` c:\test\test `
cmd = "tasklist"
} else {
dockerFile = fmt . Sprintf ( "FROM busybox\nRUN mkdir -p /%s\nRUN ln -s /%s /test" , name , name )
containerPath = "/test/test"
cmd = "true"
}
if _ , err := buildImage ( name , dockerFile , false ) ; err != nil {
c . Fatal ( err )
}
dockerCmd ( c , "run" , "-v" , containerPath , name , cmd )
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVolumesMountedAsReadonly ( c * check . C ) {
2016-05-25 20:56:36 +00:00
// TODO Windows: Temporary check - remove once TP5 support is dropped
if daemonPlatform == "windows" && windowsDaemonKV < 14350 {
c . Skip ( "Needs later Windows build for RO volumes" )
}
2015-07-27 18:13:25 +00:00
if _ , code , err := dockerCmdWithError ( "run" , "-v" , "/test:/test:ro" , "busybox" , "touch" , "/test/somefile" ) ; err == nil || code == 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "run should fail because volume is ro: exit code %d" , code )
2014-04-08 01:23:22 +00:00
}
}
2014-04-08 09:26:09 +00:00
2015-09-23 23:04:51 +00:00
func ( s * DockerSuite ) TestRunVolumesFromInReadonlyModeFails ( c * check . C ) {
2016-05-25 20:56:36 +00:00
// TODO Windows: Temporary check - remove once TP5 support is dropped
if daemonPlatform == "windows" && windowsDaemonKV < 14350 {
c . Skip ( "Needs later Windows build for RO volumes" )
}
2015-09-23 23:04:51 +00:00
var (
volumeDir string
fileInVol string
)
if daemonPlatform == "windows" {
volumeDir = ` c:/test ` // Forward-slash as using busybox
fileInVol = ` c:/test/file `
} else {
testRequires ( c , DaemonIsLinux )
volumeDir = "/test"
fileInVol = ` /test/file `
}
dockerCmd ( c , "run" , "--name" , "parent" , "-v" , volumeDir , "busybox" , "true" )
2014-04-08 09:26:09 +00:00
2015-09-23 23:04:51 +00:00
if _ , code , err := dockerCmdWithError ( "run" , "--volumes-from" , "parent:ro" , "busybox" , "touch" , fileInVol ) ; err == nil || code == 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "run should fail because volume is ro: exit code %d" , code )
2014-04-08 09:26:09 +00:00
}
}
// Regression test for #1201
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVolumesFromInReadWriteMode ( c * check . C ) {
2015-09-23 23:04:51 +00:00
var (
volumeDir string
fileInVol string
)
if daemonPlatform == "windows" {
volumeDir = ` c:/test ` // Forward-slash as using busybox
fileInVol = ` c:/test/file `
} else {
volumeDir = "/test"
fileInVol = "/test/file"
}
dockerCmd ( c , "run" , "--name" , "parent" , "-v" , volumeDir , "busybox" , "true" )
dockerCmd ( c , "run" , "--volumes-from" , "parent:rw" , "busybox" , "touch" , fileInVol )
2014-10-03 22:33:11 +00:00
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
if out , _ , err := dockerCmdWithError ( "run" , "--volumes-from" , "parent:bar" , "busybox" , "touch" , fileInVol ) ; err == nil || ! strings . Contains ( out , ` invalid mode: bar ` ) {
2015-10-26 21:19:44 +00:00
c . Fatalf ( "running --volumes-from parent:bar should have failed with invalid mode: %q" , out )
2014-10-03 22:33:11 +00:00
}
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "--volumes-from" , "parent" , "busybox" , "touch" , fileInVol )
2014-04-08 09:26:09 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestVolumesFromGetsProperMode ( c * check . C ) {
2016-06-17 22:46:45 +00:00
testRequires ( c , SameHostDaemon )
2016-05-25 20:56:36 +00:00
prefix , slash := getPrefixAndSlashFromDaemonPlatform ( )
2016-06-17 22:46:45 +00:00
hostpath := randomTmpDirPath ( "test" , daemonPlatform )
if err := os . MkdirAll ( hostpath , 0755 ) ; err != nil {
c . Fatalf ( "Failed to create %s: %q" , hostpath , err )
}
defer os . RemoveAll ( hostpath )
2016-05-25 20:56:36 +00:00
// TODO Windows: Temporary check - remove once TP5 support is dropped
if daemonPlatform == "windows" && windowsDaemonKV < 14350 {
c . Skip ( "Needs later Windows build for RO volumes" )
}
2016-06-17 22:46:45 +00:00
dockerCmd ( c , "run" , "--name" , "parent" , "-v" , hostpath + ":" + prefix + slash + "test:ro" , "busybox" , "true" )
2015-07-19 20:47:47 +00:00
2015-04-20 17:54:47 +00:00
// Expect this "rw" mode to be be ignored since the inherited volume is "ro"
2016-05-25 20:56:36 +00:00
if _ , _ , err := dockerCmdWithError ( "run" , "--volumes-from" , "parent:rw" , "busybox" , "touch" , prefix + slash + "test" + slash + "file" ) ; err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected volumes-from to inherit read-only volume even when passing in `rw`" )
2014-08-28 14:18:08 +00:00
}
2016-06-17 22:46:45 +00:00
dockerCmd ( c , "run" , "--name" , "parent2" , "-v" , hostpath + ":" + prefix + slash + "test:ro" , "busybox" , "true" )
2015-07-19 20:47:47 +00:00
2014-08-28 14:18:08 +00:00
// Expect this to be read-only since both are "ro"
2016-05-25 20:56:36 +00:00
if _ , _ , err := dockerCmdWithError ( "run" , "--volumes-from" , "parent2:ro" , "busybox" , "touch" , prefix + slash + "test" + slash + "file" ) ; err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected volumes-from to inherit read-only volume even when passing in `ro`" )
2014-08-21 21:57:46 +00:00
}
}
2015-02-06 22:00:53 +00:00
// Test for GH#10618
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNoDupVolumes ( c * check . C ) {
2015-09-23 23:04:51 +00:00
path1 := randomTmpDirPath ( "test1" , daemonPlatform )
path2 := randomTmpDirPath ( "test2" , daemonPlatform )
someplace := ":/someplace"
if daemonPlatform == "windows" {
// Windows requires that the source directory exists before calling HCS
2015-11-13 21:02:30 +00:00
testRequires ( c , SameHostDaemon )
2015-09-23 23:04:51 +00:00
someplace = ` :c:\someplace `
if err := os . MkdirAll ( path1 , 0755 ) ; err != nil {
c . Fatalf ( "Failed to create %s: %q" , path1 , err )
}
defer os . RemoveAll ( path1 )
if err := os . MkdirAll ( path2 , 0755 ) ; err != nil {
c . Fatalf ( "Failed to create %s: %q" , path1 , err )
}
defer os . RemoveAll ( path2 )
}
mountstr1 := path1 + someplace
mountstr2 := path2 + someplace
2015-02-09 16:17:41 +00:00
2015-07-27 18:13:25 +00:00
if out , _ , err := dockerCmdWithError ( "run" , "-v" , mountstr1 , "-v" , mountstr2 , "busybox" , "true" ) ; err == nil {
2015-12-01 18:39:34 +00:00
c . Fatal ( "Expected error about duplicate mount definitions" )
2015-02-06 22:00:53 +00:00
} else {
2015-12-01 18:39:34 +00:00
if ! strings . Contains ( out , "Duplicate mount point" ) {
c . Fatalf ( "Expected 'duplicate mount point' error, got %v" , out )
2015-02-06 22:00:53 +00:00
}
}
2016-05-07 02:48:02 +00:00
// Test for https://github.com/docker/docker/issues/22093
volumename1 := "test1"
volumename2 := "test2"
volume1 := volumename1 + someplace
volume2 := volumename2 + someplace
if out , _ , err := dockerCmdWithError ( "run" , "-v" , volume1 , "-v" , volume2 , "busybox" , "true" ) ; err == nil {
c . Fatal ( "Expected error about duplicate mount definitions" )
} else {
if ! strings . Contains ( out , "Duplicate mount point" ) {
c . Fatalf ( "Expected 'duplicate mount point' error, got %v" , out )
}
}
// create failed should have create volume volumename1 or volumename2
// we should remove volumename2 or volumename2 successfully
out , _ := dockerCmd ( c , "volume" , "ls" )
if strings . Contains ( out , volumename1 ) {
dockerCmd ( c , "volume" , "rm" , volumename1 )
} else {
dockerCmd ( c , "volume" , "rm" , volumename2 )
}
2015-02-06 22:00:53 +00:00
}
2014-04-08 09:26:09 +00:00
// Test for #1351
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunApplyVolumesFromBeforeVolumes ( c * check . C ) {
2015-09-23 23:04:51 +00:00
prefix := ""
if daemonPlatform == "windows" {
prefix = ` c: `
}
dockerCmd ( c , "run" , "--name" , "parent" , "-v" , prefix + "/test" , "busybox" , "touch" , prefix + "/test/foo" )
dockerCmd ( c , "run" , "--volumes-from" , "parent" , "-v" , prefix + "/test" , "busybox" , "cat" , prefix + "/test/foo" )
2014-04-08 09:26:09 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunMultipleVolumesFrom ( c * check . C ) {
2015-09-23 23:04:51 +00:00
prefix := ""
if daemonPlatform == "windows" {
prefix = ` c: `
}
dockerCmd ( c , "run" , "--name" , "parent1" , "-v" , prefix + "/test" , "busybox" , "touch" , prefix + "/test/foo" )
dockerCmd ( c , "run" , "--name" , "parent2" , "-v" , prefix + "/other" , "busybox" , "touch" , prefix + "/other/bar" )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "--volumes-from" , "parent1" , "--volumes-from" , "parent2" , "busybox" , "sh" , "-c" , "cat /test/foo && cat /other/bar" )
2014-04-08 09:26:09 +00:00
}
2014-04-18 01:58:20 +00:00
2014-04-18 21:35:16 +00:00
// this tests verifies the ID format for the container
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVerifyContainerID ( c * check . C ) {
2015-07-27 18:13:25 +00:00
out , exit , err := dockerCmdWithError ( "run" , "-d" , "busybox" , "true" )
2014-04-18 21:35:16 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-04-18 21:35:16 +00:00
}
if exit != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected exit code 0 received %d" , exit )
2014-04-18 21:35:16 +00:00
}
2015-07-19 20:47:47 +00:00
2014-04-18 21:35:16 +00:00
match , err := regexp . MatchString ( "^[0-9a-f]{64}$" , strings . TrimSuffix ( out , "\n" ) )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-04-18 21:35:16 +00:00
}
if ! match {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Invalid container ID: %s" , out )
2014-04-18 21:35:16 +00:00
}
}
// Test that creating a container with a volume doesn't crash. Regression test for #995.
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCreateVolume ( c * check . C ) {
2015-09-23 23:04:51 +00:00
prefix := ""
if daemonPlatform == "windows" {
prefix = ` c: `
}
dockerCmd ( c , "run" , "-v" , prefix + "/var/lib/data" , "busybox" , "true" )
2014-04-18 21:35:16 +00:00
}
2014-04-25 01:22:22 +00:00
// Test that creating a volume with a symlink in its path works correctly. Test for #5152.
// Note that this bug happens only with symlinks with a target that starts with '/'.
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCreateVolumeWithSymlink ( c * check . C ) {
2015-09-23 23:04:51 +00:00
// Cannot run on Windows as relies on Linux-specific functionality (sh -c mount...)
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-02-20 06:56:02 +00:00
image := "docker-test-createvolumewithsymlink"
buildCmd := exec . Command ( dockerBinary , "build" , "-t" , image , "-" )
2014-05-27 21:54:38 +00:00
buildCmd . Stdin = strings . NewReader ( ` FROM busybox
2015-01-23 12:17:05 +00:00
RUN ln - s home / bar ` )
2014-05-27 21:54:38 +00:00
buildCmd . Dir = workingDirectory
2014-04-25 01:22:22 +00:00
err := buildCmd . Run ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "could not build '%s': %v" , image , err )
2014-04-25 01:22:22 +00:00
}
2015-07-27 18:13:25 +00:00
_ , exitCode , err := dockerCmdWithError ( "run" , "-v" , "/bar/foo" , "--name" , "test-createvolumewithsymlink" , image , "sh" , "-c" , "mount | grep -q /home/foo" )
2014-04-25 01:22:22 +00:00
if err != nil || exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "[run] err: %v, exitcode: %d" , err , exitCode )
2014-04-25 01:22:22 +00:00
}
2015-07-23 22:11:06 +00:00
volPath , err := inspectMountSourceField ( "test-createvolumewithsymlink" , "/bar/foo" )
2016-01-28 14:19:25 +00:00
c . Assert ( err , checker . IsNil )
2014-04-25 01:22:22 +00:00
2015-07-27 18:13:25 +00:00
_ , exitCode , err = dockerCmdWithError ( "rm" , "-v" , "test-createvolumewithsymlink" )
2014-04-25 01:22:22 +00:00
if err != nil || exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "[rm] err: %v, exitcode: %d" , err , exitCode )
2014-04-25 01:22:22 +00:00
}
2015-07-23 22:11:06 +00:00
_ , err = os . Stat ( volPath )
2014-04-25 01:22:22 +00:00
if ! os . IsNotExist ( err ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "[open] (expecting 'file does not exist' error) err: %v, volPath: %s" , err , volPath )
2014-04-25 01:22:22 +00:00
}
}
2014-05-27 21:49:43 +00:00
// Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVolumesFromSymlinkPath ( c * check . C ) {
2016-04-05 20:21:20 +00:00
// TODO Windows (Post TP5): This test cannot run on a Windows daemon as
// Windows does not support symlinks inside a volume path
testRequires ( c , DaemonIsLinux )
2014-11-17 16:05:49 +00:00
name := "docker-test-volumesfromsymlinkpath"
2015-09-23 23:04:51 +00:00
prefix := ""
dfContents := ` FROM busybox
RUN ln - s home / foo
VOLUME [ "/foo/bar" ] `
if daemonPlatform == "windows" {
prefix = ` c: `
dfContents = ` FROM ` + WindowsBaseImage + `
RUN mkdir c : \ home
2015-10-29 15:55:48 +00:00
RUN mklink / D c : \ foo c : \ home
2015-09-23 23:04:51 +00:00
VOLUME [ "c:/foo/bar" ]
ENTRYPOINT c : \ windows \ system32 \ cmd . exe `
}
2015-02-20 06:56:02 +00:00
2014-11-17 16:05:49 +00:00
buildCmd := exec . Command ( dockerBinary , "build" , "-t" , name , "-" )
2015-09-23 23:04:51 +00:00
buildCmd . Stdin = strings . NewReader ( dfContents )
2014-05-27 21:49:43 +00:00
buildCmd . Dir = workingDirectory
err := buildCmd . Run ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "could not build 'docker-test-volumesfromsymlinkpath': %v" , err )
2014-05-27 21:49:43 +00:00
}
2015-09-23 23:04:51 +00:00
out , exitCode , err := dockerCmdWithError ( "run" , "--name" , "test-volumesfromsymlinkpath" , name )
2014-05-27 21:49:43 +00:00
if err != nil || exitCode != 0 {
2015-09-23 23:04:51 +00:00
c . Fatalf ( "[run] (volume) err: %v, exitcode: %d, out: %s" , err , exitCode , out )
2014-05-27 21:49:43 +00:00
}
2015-09-23 23:04:51 +00:00
_ , exitCode , err = dockerCmdWithError ( "run" , "--volumes-from" , "test-volumesfromsymlinkpath" , "busybox" , "sh" , "-c" , "ls " + prefix + "/foo | grep -q bar" )
2014-05-27 21:49:43 +00:00
if err != nil || exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "[run] err: %v, exitcode: %d" , err , exitCode )
2014-05-27 21:49:43 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunExitCode ( c * check . C ) {
2015-09-01 21:37:04 +00:00
var (
exit int
err error
)
2015-09-23 22:12:23 +00:00
_ , exit , err = dockerCmdWithError ( "run" , "busybox" , "/bin/sh" , "-c" , "exit 72" )
2014-04-18 21:35:16 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "should not have a non nil error" )
2014-04-18 21:35:16 +00:00
}
if exit != 72 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected exit code 72 received %d" , exit )
2014-04-18 21:35:16 +00:00
}
}
2015-09-01 21:37:04 +00:00
func ( s * DockerSuite ) TestRunUserDefaults ( c * check . C ) {
expected := "uid=0(root) gid=0(root)"
if daemonPlatform == "windows" {
2016-04-06 21:01:12 +00:00
expected = "uid=1000(ContainerAdministrator) gid=1000(ContainerAdministrator)"
2015-09-01 21:37:04 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "id" )
2015-09-01 21:37:04 +00:00
if ! strings . Contains ( out , expected ) {
c . Fatalf ( "expected '%s' got %s" , expected , out )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserByName ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-u" , "root" , "busybox" , "id" )
2014-04-18 21:35:16 +00:00
if ! strings . Contains ( out , "uid=0(root) gid=0(root)" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected root user got %s" , out )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserByID ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-u" , "1" , "busybox" , "id" )
2014-04-18 21:35:16 +00:00
if ! strings . Contains ( out , "uid=1(daemon) gid=1(daemon)" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected daemon user got %s" , out )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserByIDBig ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotArm )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-u" , "2147483648" , "busybox" , "id" )
2014-05-17 18:43:31 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "No error, but must be." , out )
2014-05-17 18:43:31 +00:00
}
2016-06-21 08:35:50 +00:00
if ! strings . Contains ( strings . ToUpper ( out ) , strings . ToUpper ( libcontainerUser . ErrRange . Error ( ) ) ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected error about uids range, got %s" , out )
2014-05-17 18:43:31 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserByIDNegative ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-u" , "-1" , "busybox" , "id" )
2014-05-17 18:43:31 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "No error, but must be." , out )
2014-05-17 18:43:31 +00:00
}
2016-06-21 08:35:50 +00:00
if ! strings . Contains ( strings . ToUpper ( out ) , strings . ToUpper ( libcontainerUser . ErrRange . Error ( ) ) ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected error about uids range, got %s" , out )
2014-05-17 18:43:31 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserByIDZero ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-u" , "0" , "busybox" , "id" )
2014-05-17 18:43:31 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-05-17 18:43:31 +00:00
}
if ! strings . Contains ( out , "uid=0(root) gid=0(root) groups=10(wheel)" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected daemon user got %s" , out )
2014-05-17 18:43:31 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUserNotFound ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This test cannot run on a Windows daemon as Windows does
// not support the use of -u
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
_ , _ , err := dockerCmdWithError ( "run" , "-u" , "notme" , "busybox" , "id" )
2014-04-18 21:35:16 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "unknown user should cause container to fail" )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunTwoConcurrentContainers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
sleepTime := "2"
2014-04-18 21:35:16 +00:00
group := sync . WaitGroup { }
group . Add ( 2 )
2015-04-27 17:29:48 +00:00
errChan := make ( chan error , 2 )
2014-04-18 21:35:16 +00:00
for i := 0 ; i < 2 ; i ++ {
go func ( ) {
defer group . Done ( )
2015-09-01 21:37:04 +00:00
_ , _ , err := dockerCmdWithError ( "run" , "busybox" , "sleep" , sleepTime )
2015-04-27 17:29:48 +00:00
errChan <- err
2014-04-18 21:35:16 +00:00
} ( )
}
group . Wait ( )
2015-04-27 17:29:48 +00:00
close ( errChan )
for err := range errChan {
c . Assert ( err , check . IsNil )
}
2014-04-18 21:35:16 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEnvironment ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: Environment handling is different between Linux and
2015-11-04 19:39:12 +00:00
// Windows and this test relies currently on unix functionality.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-31 18:45:32 +00:00
cmd := exec . Command ( dockerBinary , "run" , "-h" , "testing" , "-e=FALSE=true" , "-e=TRUE" , "-e=TRICKY" , "-e=HOME=" , "busybox" , "env" )
2014-04-18 21:35:16 +00:00
cmd . Env = append ( os . Environ ( ) ,
"TRUE=false" ,
"TRICKY=tri\ncky\n" ,
)
out , _ , err := runCommandWithOutput ( cmd )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-04-18 21:35:16 +00:00
}
2015-11-04 19:39:12 +00:00
actualEnv := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2014-04-18 21:35:16 +00:00
sort . Strings ( actualEnv )
goodEnv := [ ] string {
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ,
"HOSTNAME=testing" ,
"FALSE=true" ,
"TRUE=false" ,
"TRICKY=tri" ,
"cky" ,
"" ,
2014-02-23 03:06:37 +00:00
"HOME=/root" ,
2014-04-18 21:35:16 +00:00
}
sort . Strings ( goodEnv )
if len ( goodEnv ) != len ( actualEnv ) {
2016-03-31 10:36:59 +00:00
c . Fatalf ( "Wrong environment: should be %d variables, not %d: %q" , len ( goodEnv ) , len ( actualEnv ) , strings . Join ( actualEnv , ", " ) )
2014-04-18 21:35:16 +00:00
}
for i := range goodEnv {
if actualEnv [ i ] != goodEnv [ i ] {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Wrong environment variable: should be %s, not %s" , goodEnv [ i ] , actualEnv [ i ] )
2014-04-18 21:35:16 +00:00
}
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEnvironmentErase ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: Environment handling is different between Linux and
2015-11-04 19:39:12 +00:00
// Windows and this test relies currently on unix functionality.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 21:37:04 +00:00
2015-01-16 20:57:08 +00:00
// Test to make sure that when we use -e on env vars that are
// not set in our local env that they're removed (if present) in
// the container
2015-02-20 06:56:02 +00:00
2015-01-16 20:57:08 +00:00
cmd := exec . Command ( dockerBinary , "run" , "-e" , "FOO" , "-e" , "HOSTNAME" , "busybox" , "env" )
2016-02-24 22:59:11 +00:00
cmd . Env = appendBaseEnv ( true )
2015-02-12 19:20:48 +00:00
2015-01-16 20:57:08 +00:00
out , _ , err := runCommandWithOutput ( cmd )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2015-01-16 20:57:08 +00:00
}
2015-11-04 19:39:12 +00:00
actualEnv := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2015-01-16 20:57:08 +00:00
sort . Strings ( actualEnv )
goodEnv := [ ] string {
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ,
"HOME=/root" ,
}
sort . Strings ( goodEnv )
if len ( goodEnv ) != len ( actualEnv ) {
2016-03-31 10:36:59 +00:00
c . Fatalf ( "Wrong environment: should be %d variables, not %d: %q" , len ( goodEnv ) , len ( actualEnv ) , strings . Join ( actualEnv , ", " ) )
2015-01-16 20:57:08 +00:00
}
for i := range goodEnv {
if actualEnv [ i ] != goodEnv [ i ] {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Wrong environment variable: should be %s, not %s" , goodEnv [ i ] , actualEnv [ i ] )
2015-01-16 20:57:08 +00:00
}
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEnvironmentOverride ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: Environment handling is different between Linux and
2015-11-04 19:39:12 +00:00
// Windows and this test relies currently on unix functionality.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 21:37:04 +00:00
2015-01-16 20:57:08 +00:00
// Test to make sure that when we use -e on env vars that are
// already in the env that we're overriding them
2015-02-20 06:56:02 +00:00
2015-01-16 20:57:08 +00:00
cmd := exec . Command ( dockerBinary , "run" , "-e" , "HOSTNAME" , "-e" , "HOME=/root2" , "busybox" , "env" )
2016-02-24 22:59:11 +00:00
cmd . Env = appendBaseEnv ( true , "HOSTNAME=bar" )
2015-02-12 19:20:48 +00:00
2015-01-16 20:57:08 +00:00
out , _ , err := runCommandWithOutput ( cmd )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2015-01-16 20:57:08 +00:00
}
2015-11-04 19:39:12 +00:00
actualEnv := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2015-01-16 20:57:08 +00:00
sort . Strings ( actualEnv )
goodEnv := [ ] string {
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ,
"HOME=/root2" ,
"HOSTNAME=bar" ,
}
sort . Strings ( goodEnv )
if len ( goodEnv ) != len ( actualEnv ) {
2016-03-31 10:36:59 +00:00
c . Fatalf ( "Wrong environment: should be %d variables, not %d: %q" , len ( goodEnv ) , len ( actualEnv ) , strings . Join ( actualEnv , ", " ) )
2015-01-16 20:57:08 +00:00
}
for i := range goodEnv {
if actualEnv [ i ] != goodEnv [ i ] {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Wrong environment variable: should be %s, not %s" , goodEnv [ i ] , actualEnv [ i ] )
2015-01-16 20:57:08 +00:00
}
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunContainerNetwork ( c * check . C ) {
2015-09-01 21:37:04 +00:00
if daemonPlatform == "windows" {
// Windows busybox does not have ping. Use built in ping instead.
dockerCmd ( c , "run" , WindowsBaseImage , "ping" , "-n" , "1" , "127.0.0.1" )
} else {
dockerCmd ( c , "run" , "busybox" , "ping" , "-c" , "1" , "127.0.0.1" )
}
2014-04-18 21:35:16 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNetHostNotAllowedWithLinks ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This is Linux specific as --link is not supported and
2015-12-13 16:00:39 +00:00
// this will be deprecated in favor of container networking model.
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "--name" , "linked" , "busybox" , "true" )
2015-07-27 18:13:25 +00:00
_ , _ , err := dockerCmdWithError ( "run" , "--net=host" , "--link" , "linked:linked" , "busybox" , "true" )
2014-07-16 21:09:30 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected error" )
2014-07-16 21:09:30 +00:00
}
}
2014-09-24 15:00:01 +00:00
// #7851 hostname outside container shows FQDN, inside only shortname
// For testing purposes it is not required to set host's hostname directly
// and use "--net=host" (as the original issue submitter did), as the same
// codepath is executed with "docker run -h <hostname>". Both were manually
// tested, but this testcase takes the simpler path of using "run -h .."
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunFullHostnameSet ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: -h is not yet functional.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-h" , "foo.bar.baz" , "busybox" , "hostname" )
2014-09-24 15:00:01 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "foo.bar.baz" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected hostname 'foo.bar.baz', received %s" , actual )
2014-09-24 15:00:01 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunPrivilegedCanMknod ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as Windows daemon does not support
// the concept of --privileged, and mknod is a Unix concept.
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--privileged" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2014-04-18 21:35:16 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-04-18 21:35:16 +00:00
}
}
2015-05-13 12:24:54 +00:00
func ( s * DockerSuite ) TestRunUnprivilegedCanMknod ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as Windows daemon does not support
// the concept of --privileged, and mknod is a Unix concept.
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2014-04-18 21:35:16 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapDropInvalid ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-drop
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-drop=CHPASS" , "busybox" , "ls" )
2014-07-10 23:38:11 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-10 23:38:11 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapDropCannotMknod ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-drop or mknod
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-drop=MKNOD" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2015-07-19 20:47:47 +00:00
2014-07-10 21:51:15 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-10 21:51:15 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual == "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output not ok received %s" , actual )
2014-07-10 21:51:15 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapDropCannotMknodLowerCase ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-drop or mknod
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-drop=mknod" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2015-07-19 20:47:47 +00:00
2014-07-16 18:47:55 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-16 18:47:55 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual == "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output not ok received %s" , actual )
2014-07-16 18:47:55 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapDropALLCannotMknod ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-drop or mknod
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-drop=ALL" , "--cap-add=SETGID" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2014-07-10 22:31:01 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-10 22:31:01 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual == "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output not ok received %s" , actual )
2014-07-10 22:31:01 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapDropALLAddMknodCanMknod ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-drop or mknod
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--cap-drop=ALL" , "--cap-add=MKNOD" , "--cap-add=SETGID" , "busybox" , "sh" , "-c" , "mknod /tmp/sda b 8 0 && echo ok" )
2014-07-10 23:02:39 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-07-10 23:02:39 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapAddInvalid ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-add
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-add=CHPASS" , "busybox" , "ls" )
2014-07-10 23:38:11 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-10 23:38:11 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapAddCanDownInterface ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-add
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--cap-add=NET_ADMIN" , "busybox" , "sh" , "-c" , "ip link set eth0 down && echo ok" )
2014-07-10 21:51:15 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-07-10 21:51:15 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapAddALLCanDownInterface ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-add
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--cap-add=ALL" , "busybox" , "sh" , "-c" , "ip link set eth0 down && echo ok" )
2014-07-10 22:31:01 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-07-10 22:31:01 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCapAddALLDropNetAdminCanDownInterface ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --cap-add
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--cap-add=ALL" , "--cap-drop=NET_ADMIN" , "busybox" , "sh" , "-c" , "ip link set eth0 down && echo ok" )
2014-07-10 23:02:39 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-07-10 23:02:39 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual == "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output not ok received %s" , actual )
2014-07-10 23:02:39 +00:00
}
}
2015-06-17 18:39:59 +00:00
func ( s * DockerSuite ) TestRunGroupAdd ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --group-add
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-10-13 16:39:05 +00:00
out , _ := dockerCmd ( c , "run" , "--group-add=audio" , "--group-add=staff" , "--group-add=777" , "busybox" , "sh" , "-c" , "id" )
2015-06-17 18:39:59 +00:00
2015-10-13 16:39:05 +00:00
groupsList := "uid=0(root) gid=0(root) groups=10(wheel),29(audio),50(staff),777"
2015-06-17 18:39:59 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != groupsList {
c . Fatalf ( "expected output %s received %s" , groupsList , actual )
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunPrivilegedCanMount ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --privileged
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--privileged" , "busybox" , "sh" , "-c" , "mount -t tmpfs none /tmp && echo ok" )
2014-04-18 21:35:16 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output ok received %s" , actual )
2014-04-18 21:35:16 +00:00
}
}
2015-05-13 12:24:54 +00:00
func ( s * DockerSuite ) TestRunUnprivilegedCannotMount ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of unprivileged
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "busybox" , "sh" , "-c" , "mount -t tmpfs none /tmp && echo ok" )
2015-07-19 20:47:47 +00:00
2014-04-18 21:35:16 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-04-18 21:35:16 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual == "ok" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output not ok received %s" , actual )
2014-04-18 21:35:16 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunSysNotWritableInNonPrivilegedContainers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of unprivileged
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotArm )
2015-07-27 18:13:25 +00:00
if _ , code , err := dockerCmdWithError ( "run" , "busybox" , "touch" , "/sys/kernel/profiling" ) ; err == nil || code == 0 {
2015-04-18 16:46:47 +00:00
c . Fatal ( "sys should not be writable in a non privileged container" )
2014-04-18 03:20:17 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunSysWritableInPrivilegedContainers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of unprivileged
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2015-07-27 18:13:25 +00:00
if _ , code , err := dockerCmdWithError ( "run" , "--privileged" , "busybox" , "touch" , "/sys/kernel/profiling" ) ; err != nil || code != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "sys should be writable in privileged container" )
2014-04-18 03:20:17 +00:00
}
2014-05-01 01:00:42 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunProcNotWritableInNonPrivilegedContainers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of unprivileged
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
if _ , code , err := dockerCmdWithError ( "run" , "busybox" , "touch" , "/proc/sysrq-trigger" ) ; err == nil || code == 0 {
2015-04-18 16:46:47 +00:00
c . Fatal ( "proc should not be writable in a non privileged container" )
2014-05-01 01:00:42 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunProcWritableInPrivilegedContainers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable for Windows as there is no concept of --privileged
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2016-04-04 21:27:44 +00:00
if _ , code := dockerCmd ( c , "run" , "--privileged" , "busybox" , "sh" , "-c" , "touch /proc/sysrq-trigger" ) ; code != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "proc should be writable in privileged container" )
2014-05-01 01:00:42 +00:00
}
2015-04-08 08:58:59 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDeviceNumbers ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as /dev/ is a Unix specific concept
2015-09-18 17:41:12 +00:00
// TODO: NotUserNamespace could be removed here if "root" "root" is replaced w user
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "sh" , "-c" , "ls -l /dev/null" )
2014-02-17 23:14:30 +00:00
deviceLineFields := strings . Fields ( out )
deviceLineFields [ 6 ] = ""
deviceLineFields [ 7 ] = ""
deviceLineFields [ 8 ] = ""
expected := [ ] string { "crw-rw-rw-" , "1" , "root" , "root" , "1," , "3" , "" , "" , "" , "/dev/null" }
if ! ( reflect . DeepEqual ( deviceLineFields , expected ) ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n" , out )
2014-02-17 23:14:30 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunThatCharacterDevicesActLikeCharacterDevices ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as /dev/ is a Unix specific concept
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "busybox" , "sh" , "-c" , "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero" )
2014-02-17 23:14:30 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual [ 0 ] == '0' {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s" , actual )
2014-02-17 23:14:30 +00:00
}
}
2014-06-03 01:23:47 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUnprivilegedWithChroot ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as it does not support chroot
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "busybox" , "chroot" , "/" , "true" )
2014-06-03 01:23:47 +00:00
}
2014-06-12 19:11:51 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAddingOptionalDevices ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support --device
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--device" , "/dev/zero:/dev/nulo" , "busybox" , "sh" , "-c" , "ls /dev/nulo" )
2014-05-31 04:00:47 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "/dev/nulo" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected output /dev/nulo, received %s" , actual )
2014-05-31 04:00:47 +00:00
}
}
2015-08-24 09:57:12 +00:00
func ( s * DockerSuite ) TestRunAddingOptionalDevicesNoSrc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support --device
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-08-24 09:57:12 +00:00
out , _ := dockerCmd ( c , "run" , "--device" , "/dev/zero:rw" , "busybox" , "sh" , "-c" , "ls /dev/zero" )
if actual := strings . Trim ( out , "\r\n" ) ; actual != "/dev/zero" {
c . Fatalf ( "expected output /dev/zero, received %s" , actual )
}
}
2015-08-31 22:28:40 +00:00
func ( s * DockerSuite ) TestRunAddingOptionalDevicesInvalidMode ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support --device
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-08-24 09:57:12 +00:00
_ , _ , err := dockerCmdWithError ( "run" , "--device" , "/dev/zero:ro" , "busybox" , "sh" , "-c" , "ls /dev/zero" )
if err == nil {
c . Fatalf ( "run container with device mode ro should fail" )
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunModeHostname ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support -h
2015-09-18 17:41:12 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2014-06-12 19:11:51 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-h=testhostname" , "busybox" , "cat" , "/etc/hostname" )
2014-06-12 19:11:51 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "testhostname" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected 'testhostname', but says: %q" , actual )
2014-06-12 19:11:51 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "--net=host" , "busybox" , "cat" , "/etc/hostname" )
2014-06-12 19:11:51 +00:00
hostname , err := os . Hostname ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-06-12 19:11:51 +00:00
}
if actual := strings . Trim ( out , "\r\n" ) ; actual != hostname {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected %q, but says: %q" , hostname , actual )
2014-06-12 19:11:51 +00:00
}
2014-06-24 16:53:53 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunRootWorkdir ( c * check . C ) {
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--workdir" , "/" , "busybox" , "pwd" )
2015-09-01 21:37:04 +00:00
expected := "/\n"
if daemonPlatform == "windows" {
expected = "C:" + expected
}
if out != expected {
c . Fatalf ( "pwd returned %q (expected %s)" , s , expected )
2014-06-24 16:53:53 +00:00
}
}
2014-06-26 17:50:18 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAllowBindMountingRoot ( c * check . C ) {
2015-09-23 23:04:51 +00:00
if daemonPlatform == "windows" {
// Windows busybox will fail with Permission Denied on items such as pagefile.sys
dockerCmd ( c , "run" , "-v" , ` c:\:c:\host ` , WindowsBaseImage , "cmd" , "-c" , "dir" , ` c:\host ` )
} else {
dockerCmd ( c , "run" , "-v" , "/:/host" , "busybox" , "ls" , "/host" )
}
2014-06-26 17:50:18 +00:00
}
2014-06-27 16:51:24 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDisallowBindMountingRootToRoot ( c * check . C ) {
2015-09-23 23:04:51 +00:00
mount := "/:/"
targetDir := "/host"
if daemonPlatform == "windows" {
mount = ` c:\:c\ `
targetDir = "c:/host" // Forward slash as using busybox
}
out , _ , err := dockerCmdWithError ( "run" , "-v" , mount , "busybox" , "ls" , targetDir )
2014-06-27 16:51:24 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( out , err )
2014-06-27 16:51:24 +00:00
}
}
2014-06-26 11:03:23 +00:00
2015-01-09 17:06:48 +00:00
// Verify that a container gets default DNS when only localhost resolvers exist
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDnsDefaultOptions ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as this is testing Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-01-09 17:06:48 +00:00
// preserve original resolv.conf for restoring after test
2014-10-08 00:58:39 +00:00
origResolvConf , err := ioutil . ReadFile ( "/etc/resolv.conf" )
if os . IsNotExist ( err ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "/etc/resolv.conf does not exist" )
2014-10-08 00:58:39 +00:00
}
2015-01-09 17:06:48 +00:00
// defer restored original conf
2014-10-08 00:58:39 +00:00
defer func ( ) {
if err := ioutil . WriteFile ( "/etc/resolv.conf" , origResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-08 00:58:39 +00:00
}
} ( )
2015-01-09 17:06:48 +00:00
// test 3 cases: standard IPv4 localhost, commented out localhost, and IPv6 localhost
// 2 are removed from the file at container start, and the 3rd (commented out) one is ignored by
// GetNameservers(), leading to a replacement of nameservers with the default set
tmpResolvConf := [ ] byte ( "nameserver 127.0.0.1\n#nameserver 127.0.2.1\nnameserver ::1" )
if err := ioutil . WriteFile ( "/etc/resolv.conf" , tmpResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-01-09 17:06:48 +00:00
}
2015-07-19 20:47:47 +00:00
actual , _ := dockerCmd ( c , "run" , "busybox" , "cat" , "/etc/resolv.conf" )
2015-01-09 17:06:48 +00:00
// check that the actual defaults are appended to the commented out
// localhost resolver (which should be preserved)
// NOTE: if we ever change the defaults from google dns, this will break
2015-09-02 23:43:28 +00:00
expected := "#nameserver 127.0.2.1\n\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n"
2014-10-08 00:58:39 +00:00
if actual != expected {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected resolv.conf be: %q, but was: %q" , expected , actual )
2014-06-26 11:03:23 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDnsOptions ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support --dns*, or
// the Unix-specific functionality of resolv.conf.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-08-31 18:47:25 +00:00
out , stderr , _ := dockerCmdWithStdoutStderr ( c , "run" , "--dns=127.0.0.1" , "--dns-search=mydomain" , "--dns-opt=ndots:9" , "busybox" , "cat" , "/etc/resolv.conf" )
2014-06-26 11:03:23 +00:00
2015-03-10 14:22:29 +00:00
// The client will get a warning on stderr when setting DNS to a localhost address; verify this:
if ! strings . Contains ( stderr , "Localhost DNS setting" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Expected warning on stderr about localhost resolver, but got %q" , stderr )
2015-03-10 14:22:29 +00:00
}
2014-06-26 11:03:23 +00:00
actual := strings . Replace ( strings . Trim ( out , "\r\n" ) , "\n" , " " , - 1 )
2015-08-31 18:47:25 +00:00
if actual != "search mydomain nameserver 127.0.0.1 options ndots:9" {
c . Fatalf ( "expected 'search mydomain nameserver 127.0.0.1 options ndots:9', but says: %q" , actual )
2014-06-26 11:03:23 +00:00
}
2015-08-31 18:47:25 +00:00
out , stderr , _ = dockerCmdWithStdoutStderr ( c , "run" , "--dns=127.0.0.1" , "--dns-search=." , "--dns-opt=ndots:3" , "busybox" , "cat" , "/etc/resolv.conf" )
2014-06-26 11:03:23 +00:00
actual = strings . Replace ( strings . Trim ( strings . Trim ( out , "\r\n" ) , " " ) , "\n" , " " , - 1 )
2015-08-31 18:47:25 +00:00
if actual != "nameserver 127.0.0.1 options ndots:3" {
c . Fatalf ( "expected 'nameserver 127.0.0.1 options ndots:3', but says: %q" , actual )
}
}
func ( s * DockerSuite ) TestRunDnsRepeatOptions ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
out , _ , _ := dockerCmdWithStdoutStderr ( c , "run" , "--dns=1.1.1.1" , "--dns=2.2.2.2" , "--dns-search=mydomain" , "--dns-search=mydomain2" , "--dns-opt=ndots:9" , "--dns-opt=timeout:3" , "busybox" , "cat" , "/etc/resolv.conf" )
actual := strings . Replace ( strings . Trim ( out , "\r\n" ) , "\n" , " " , - 1 )
if actual != "search mydomain mydomain2 nameserver 1.1.1.1 nameserver 2.2.2.2 options ndots:9 timeout:3" {
c . Fatalf ( "expected 'search mydomain mydomain2 nameserver 1.1.1.1 nameserver 2.2.2.2 options ndots:9 timeout:3', but says: %q" , actual )
2014-06-26 11:03:23 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDnsOptionsBasedOnHostResolvConf ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as testing Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2014-10-07 22:35:32 +00:00
origResolvConf , err := ioutil . ReadFile ( "/etc/resolv.conf" )
2014-06-26 11:03:23 +00:00
if os . IsNotExist ( err ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "/etc/resolv.conf does not exist" )
2014-06-26 11:03:23 +00:00
}
2016-05-18 13:26:16 +00:00
hostNameservers := resolvconf . GetNameservers ( origResolvConf , types . IP )
2014-10-07 22:35:32 +00:00
hostSearch := resolvconf . GetSearchDomains ( origResolvConf )
2014-06-26 11:03:23 +00:00
2015-02-19 04:06:23 +00:00
var out string
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "--dns=127.0.0.1" , "busybox" , "cat" , "/etc/resolv.conf" )
2014-06-26 11:03:23 +00:00
2016-05-08 07:33:16 +00:00
if actualNameservers := resolvconf . GetNameservers ( [ ] byte ( out ) , types . IP ) ; string ( actualNameservers [ 0 ] ) != "127.0.0.1" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected '127.0.0.1', but says: %q" , string ( actualNameservers [ 0 ] ) )
2014-06-26 11:03:23 +00:00
}
actualSearch := resolvconf . GetSearchDomains ( [ ] byte ( out ) )
if len ( actualSearch ) != len ( hostSearch ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected %q search domain(s), but it has: %q" , len ( hostSearch ) , len ( actualSearch ) )
2014-06-26 11:03:23 +00:00
}
for i := range actualSearch {
if actualSearch [ i ] != hostSearch [ i ] {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected %q domain, but says: %q" , actualSearch [ i ] , hostSearch [ i ] )
2014-06-26 11:03:23 +00:00
}
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "--dns-search=mydomain" , "busybox" , "cat" , "/etc/resolv.conf" )
2014-06-26 11:03:23 +00:00
2016-05-08 07:33:16 +00:00
actualNameservers := resolvconf . GetNameservers ( [ ] byte ( out ) , types . IP )
2016-05-18 13:26:16 +00:00
if len ( actualNameservers ) != len ( hostNameservers ) {
c . Fatalf ( "expected %q nameserver(s), but it has: %q" , len ( hostNameservers ) , len ( actualNameservers ) )
2014-06-26 11:03:23 +00:00
}
for i := range actualNameservers {
2016-05-18 13:26:16 +00:00
if actualNameservers [ i ] != hostNameservers [ i ] {
c . Fatalf ( "expected %q nameserver, but says: %q" , actualNameservers [ i ] , hostNameservers [ i ] )
2014-06-26 11:03:23 +00:00
}
}
if actualSearch = resolvconf . GetSearchDomains ( [ ] byte ( out ) ) ; string ( actualSearch [ 0 ] ) != "mydomain" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected 'mydomain', but says: %q" , string ( actualSearch [ 0 ] ) )
2014-10-07 22:35:32 +00:00
}
// test with file
tmpResolvConf := [ ] byte ( "search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1" )
if err := ioutil . WriteFile ( "/etc/resolv.conf" , tmpResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-07 22:35:32 +00:00
}
// put the old resolvconf back
defer func ( ) {
if err := ioutil . WriteFile ( "/etc/resolv.conf" , origResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-07 22:35:32 +00:00
}
} ( )
resolvConf , err := ioutil . ReadFile ( "/etc/resolv.conf" )
if os . IsNotExist ( err ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "/etc/resolv.conf does not exist" )
2014-10-07 22:35:32 +00:00
}
2016-05-18 13:26:16 +00:00
hostNameservers = resolvconf . GetNameservers ( resolvConf , types . IP )
2014-10-07 22:35:32 +00:00
hostSearch = resolvconf . GetSearchDomains ( resolvConf )
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "busybox" , "cat" , "/etc/resolv.conf" )
2016-05-08 07:33:16 +00:00
if actualNameservers = resolvconf . GetNameservers ( [ ] byte ( out ) , types . IP ) ; string ( actualNameservers [ 0 ] ) != "12.34.56.78" || len ( actualNameservers ) != 1 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected '12.34.56.78', but has: %v" , actualNameservers )
2014-10-07 22:35:32 +00:00
}
actualSearch = resolvconf . GetSearchDomains ( [ ] byte ( out ) )
if len ( actualSearch ) != len ( hostSearch ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected %q search domain(s), but it has: %q" , len ( hostSearch ) , len ( actualSearch ) )
2014-10-07 22:35:32 +00:00
}
for i := range actualSearch {
if actualSearch [ i ] != hostSearch [ i ] {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected %q domain, but says: %q" , actualSearch [ i ] , hostSearch [ i ] )
2014-10-07 22:35:32 +00:00
}
2014-06-26 11:03:23 +00:00
}
}
2014-07-16 17:47:12 +00:00
2015-10-13 16:39:05 +00:00
// Test to see if a non-root user can resolve a DNS name. Also
2015-08-07 22:24:18 +00:00
// check if the container resolv.conf file has at least 0644 perm.
2015-05-20 06:06:44 +00:00
func ( s * DockerSuite ) TestRunNonRootUserResolvName ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support --user
2015-11-25 23:38:33 +00:00
testRequires ( c , SameHostDaemon , Network , DaemonIsLinux , NotArm )
2015-05-20 06:06:44 +00:00
2015-10-13 16:39:05 +00:00
dockerCmd ( c , "run" , "--name=testperm" , "--user=nobody" , "busybox" , "nslookup" , "apt.dockerproject.org" )
2015-05-20 06:06:44 +00:00
cID , err := getIDByName ( "testperm" )
if err != nil {
c . Fatal ( err )
}
fmode := ( os . FileMode ) ( 0644 )
finfo , err := os . Stat ( containerStorageFile ( cID , "resolv.conf" ) )
if err != nil {
c . Fatal ( err )
}
if ( finfo . Mode ( ) & fmode ) != fmode {
2015-08-07 22:24:18 +00:00
c . Fatalf ( "Expected container resolv.conf mode to be at least %s, instead got %s" , fmode . String ( ) , finfo . Mode ( ) . String ( ) )
2015-05-20 06:06:44 +00:00
}
}
2015-05-06 22:39:29 +00:00
// Test if container resolv.conf gets updated the next time it restarts
// if host /etc/resolv.conf has changed. This only applies if the container
// uses the host's /etc/resolv.conf and does not have any dns options provided.
func ( s * DockerSuite ) TestRunResolvconfUpdate ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as testing unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2016-06-14 04:27:26 +00:00
c . Skip ( "Unstable test, to be re-activated once #19937 is resolved" )
2014-12-10 05:55:09 +00:00
2015-09-02 23:43:28 +00:00
tmpResolvConf := [ ] byte ( "search pommesfrites.fr\nnameserver 12.34.56.78\n" )
2014-12-10 05:55:09 +00:00
tmpLocalhostResolvConf := [ ] byte ( "nameserver 127.0.0.1" )
//take a copy of resolv.conf for restoring after test completes
resolvConfSystem , err := ioutil . ReadFile ( "/etc/resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-02-11 20:01:30 +00:00
// This test case is meant to test monitoring resolv.conf when it is
2015-04-18 16:46:47 +00:00
// a regular file not a bind mounc. So we unmount resolv.conf and replace
2015-02-11 20:01:30 +00:00
// it with a file containing the original settings.
2016-06-21 08:38:45 +00:00
mounted , err := mount . Mounted ( "/etc/resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
2016-06-21 08:38:45 +00:00
if mounted {
cmd := exec . Command ( "umount" , "/etc/resolv.conf" )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
}
2015-02-11 20:01:30 +00:00
2014-12-10 05:55:09 +00:00
//cleanup
defer func ( ) {
if err := ioutil . WriteFile ( "/etc/resolv.conf" , resolvConfSystem , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
} ( )
2015-05-06 22:39:29 +00:00
//1. test that a restarting container gets an updated resolv.conf
2016-06-01 05:19:13 +00:00
dockerCmd ( c , "run" , "--name=first" , "busybox" , "true" )
2014-12-10 05:55:09 +00:00
containerID1 , err := getIDByName ( "first" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
// replace resolv.conf with our temporary copy
bytesResolvConf := [ ] byte ( tmpResolvConf )
if err := ioutil . WriteFile ( "/etc/resolv.conf" , bytesResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
// start the container again to pickup changes
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "start" , "first" )
2015-05-06 22:39:29 +00:00
2014-12-10 05:55:09 +00:00
// check for update in container
containerResolv , err := readContainerFile ( containerID1 , "resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
if ! bytes . Equal ( containerResolv , bytesResolvConf ) {
2015-05-06 22:39:29 +00:00
c . Fatalf ( "Restarted container does not have updated resolv.conf; expected %q, got %q" , tmpResolvConf , string ( containerResolv ) )
2014-12-10 05:55:09 +00:00
}
2015-06-24 17:23:00 +00:00
/ * //make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
if err := ioutil . WriteFile ( "/etc/resolv.conf" , resolvConfSystem , 0644 ) ; err != nil {
2015-07-19 20:47:47 +00:00
c . Fatal ( err )
} * /
2015-05-06 22:39:29 +00:00
//2. test that a restarting container does not receive resolv.conf updates
2014-12-10 05:55:09 +00:00
// if it modified the container copy of the starting point resolv.conf
2016-06-01 05:19:13 +00:00
dockerCmd ( c , "run" , "--name=second" , "busybox" , "sh" , "-c" , "echo 'search mylittlepony.com' >>/etc/resolv.conf" )
2014-12-10 05:55:09 +00:00
containerID2 , err := getIDByName ( "second" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
//make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
if err := ioutil . WriteFile ( "/etc/resolv.conf" , resolvConfSystem , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
// start the container again
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "start" , "second" )
2015-05-06 22:39:29 +00:00
// check for update in container
containerResolv , err = readContainerFile ( containerID2 , "resolv.conf" )
2014-12-10 05:55:09 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
if bytes . Equal ( containerResolv , resolvConfSystem ) {
2015-11-20 04:44:42 +00:00
c . Fatalf ( "Container's resolv.conf should not have been updated with host resolv.conf: %q" , string ( containerResolv ) )
2014-12-10 05:55:09 +00:00
}
//3. test that a running container's resolv.conf is not modified while running
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
2014-12-10 05:55:09 +00:00
runningContainerID := strings . TrimSpace ( out )
// replace resolv.conf
if err := ioutil . WriteFile ( "/etc/resolv.conf" , bytesResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
// check for update in container
containerResolv , err = readContainerFile ( runningContainerID , "resolv.conf" )
2014-12-10 05:55:09 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
if bytes . Equal ( containerResolv , bytesResolvConf ) {
c . Fatalf ( "Running container should not have updated resolv.conf; expected %q, got %q" , string ( resolvConfSystem ) , string ( containerResolv ) )
2014-12-10 05:55:09 +00:00
}
//4. test that a running container's resolv.conf is updated upon restart
// (the above container is still running..)
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "restart" , runningContainerID )
2014-12-10 05:55:09 +00:00
// check for update in container
containerResolv , err = readContainerFile ( runningContainerID , "resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
if ! bytes . Equal ( containerResolv , bytesResolvConf ) {
2015-05-06 22:39:29 +00:00
c . Fatalf ( "Restarted container should have updated resolv.conf; expected %q, got %q" , string ( bytesResolvConf ) , string ( containerResolv ) )
2014-12-10 05:55:09 +00:00
}
//5. test that additions of a localhost resolver are cleaned from
// host resolv.conf before updating container's resolv.conf copies
// replace resolv.conf with a localhost-only nameserver copy
bytesResolvConf = [ ] byte ( tmpLocalhostResolvConf )
if err = ioutil . WriteFile ( "/etc/resolv.conf" , bytesResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-05-06 22:39:29 +00:00
// start the container again to pickup changes
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "start" , "first" )
2015-05-06 22:39:29 +00:00
2014-12-10 05:55:09 +00:00
// our first exited container ID should have been updated, but with default DNS
// after the cleanup of resolv.conf found only a localhost nameserver:
containerResolv , err = readContainerFile ( containerID1 , "resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-10 05:55:09 +00:00
}
2015-09-02 23:43:28 +00:00
expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n"
2014-12-10 05:55:09 +00:00
if ! bytes . Equal ( containerResolv , [ ] byte ( expected ) ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container does not have cleaned/replaced DNS in resolv.conf; expected %q, got %q" , expected , string ( containerResolv ) )
2014-12-10 05:55:09 +00:00
}
2015-02-11 20:01:30 +00:00
//6. Test that replacing (as opposed to modifying) resolv.conf triggers an update
// of containers' resolv.conf.
// Restore the original resolv.conf
if err := ioutil . WriteFile ( "/etc/resolv.conf" , resolvConfSystem , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
// Run the container so it picks up the old settings
2016-06-01 05:19:13 +00:00
dockerCmd ( c , "run" , "--name=third" , "busybox" , "true" )
2015-02-11 20:01:30 +00:00
containerID3 , err := getIDByName ( "third" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
// Create a modified resolv.conf.aside and override resolv.conf with it
bytesResolvConf = [ ] byte ( tmpResolvConf )
if err := ioutil . WriteFile ( "/etc/resolv.conf.aside" , bytesResolvConf , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
err = os . Rename ( "/etc/resolv.conf.aside" , "/etc/resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
2015-05-06 22:39:29 +00:00
// start the container again to pickup changes
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "start" , "third" )
2015-05-06 22:39:29 +00:00
2015-02-11 20:01:30 +00:00
// check for update in container
containerResolv , err = readContainerFile ( containerID3 , "resolv.conf" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-02-11 20:01:30 +00:00
}
if ! bytes . Equal ( containerResolv , bytesResolvConf ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Stopped container does not have updated resolv.conf; expected\n%q\n got\n%q" , tmpResolvConf , string ( containerResolv ) )
2015-02-11 20:01:30 +00:00
}
2014-12-10 05:55:09 +00:00
//cleanup, restore original resolv.conf happens in defer func()
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAddHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as it does not support --add-host
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--add-host=extra:86.75.30.9" , "busybox" , "grep" , "extra" , "/etc/hosts" )
2014-09-13 04:35:59 +00:00
actual := strings . Trim ( out , "\r\n" )
if actual != "86.75.30.9\textra" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected '86.75.30.9\textra', but says: %q" , actual )
2014-09-13 04:35:59 +00:00
}
}
2014-07-16 17:47:12 +00:00
// Regression test for #6983
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAttachStdErrOnlyTTYMode ( c * check . C ) {
2015-07-19 20:47:47 +00:00
_ , exitCode := dockerCmd ( c , "run" , "-t" , "-a" , "stderr" , "busybox" , "true" )
if exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container should have exited with error code 0" )
2014-07-16 17:47:12 +00:00
}
}
// Regression test for #6983
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAttachStdOutOnlyTTYMode ( c * check . C ) {
2015-07-19 20:47:47 +00:00
_ , exitCode := dockerCmd ( c , "run" , "-t" , "-a" , "stdout" , "busybox" , "true" )
if exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container should have exited with error code 0" )
2014-07-16 17:47:12 +00:00
}
}
// Regression test for #6983
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAttachStdOutAndErrTTYMode ( c * check . C ) {
2015-07-19 20:47:47 +00:00
_ , exitCode := dockerCmd ( c , "run" , "-t" , "-a" , "stdout" , "-a" , "stderr" , "busybox" , "true" )
if exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container should have exited with error code 0" )
2014-07-16 17:47:12 +00:00
}
}
2014-07-17 06:15:23 +00:00
2015-01-28 21:03:21 +00:00
// Test for #10388 - this will run the same test as TestRunAttachStdOutAndErrTTYMode
// but using --attach instead of -a to make sure we read the flag correctly
2015-05-13 09:49:19 +00:00
func ( s * DockerSuite ) TestRunAttachWithDetach ( c * check . C ) {
2015-01-28 21:03:21 +00:00
cmd := exec . Command ( dockerBinary , "run" , "-d" , "--attach" , "stdout" , "busybox" , "true" )
_ , stderr , _ , err := runCommandWithStdoutStderr ( cmd )
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container should have exited with error code different than 0" )
2015-01-28 21:03:21 +00:00
} else if ! strings . Contains ( stderr , "Conflicting options: -a and -d" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Should have been returned an error with conflicting options -a and -d" )
2015-01-28 21:03:21 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunState ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This needs some rework as Windows busybox does not support top
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
2014-07-17 06:15:23 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
state := inspectField ( c , id , "State.Running" )
2014-07-17 06:15:23 +00:00
if state != "true" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container state is 'not running'" )
2014-07-17 06:15:23 +00:00
}
2016-01-28 14:19:25 +00:00
pid1 := inspectField ( c , id , "State.Pid" )
2014-07-17 06:15:23 +00:00
if pid1 == "0" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container state Pid 0" )
2014-07-17 06:15:23 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "stop" , id )
2016-01-28 14:19:25 +00:00
state = inspectField ( c , id , "State.Running" )
2014-07-17 06:15:23 +00:00
if state != "false" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container state is 'running'" )
2014-07-17 06:15:23 +00:00
}
2016-01-28 14:19:25 +00:00
pid2 := inspectField ( c , id , "State.Pid" )
2014-07-17 06:15:23 +00:00
if pid2 == pid1 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container state Pid %s, but expected %s" , pid2 , pid1 )
2014-07-17 06:15:23 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "start" , id )
2016-01-28 14:19:25 +00:00
state = inspectField ( c , id , "State.Running" )
2014-07-17 06:15:23 +00:00
if state != "true" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container state is 'not running'" )
2014-07-17 06:15:23 +00:00
}
2016-01-28 14:19:25 +00:00
pid3 := inspectField ( c , id , "State.Pid" )
2014-07-17 06:15:23 +00:00
if pid3 == pid1 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container state Pid %s, but expected %s" , pid2 , pid1 )
2014-07-17 06:15:23 +00:00
}
}
2014-07-16 19:40:04 +00:00
// Test for #1737
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCopyVolumeUidGid ( c * check . C ) {
2015-09-23 23:04:51 +00:00
// Not applicable on Windows as it does not support uid or gid in this way
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-16 19:40:04 +00:00
name := "testrunvolumesuidgid"
_ , err := buildImage ( name ,
` FROM busybox
RUN echo ' dockerio : x : 1001 : 1001 : : / bin : / bin / false ' >> / etc / passwd
RUN echo ' dockerio : x : 1001 : ' >> / etc / group
RUN mkdir - p / hello && touch / hello / test && chown dockerio . dockerio / hello ` ,
true )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-07-16 19:40:04 +00:00
}
// Test that the uid and gid is copied from the image to the volume
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--rm" , "-v" , "/hello" , name , "sh" , "-c" , "ls -l / | grep hello | awk '{print $3\":\"$4}'" )
2014-07-16 19:40:04 +00:00
out = strings . TrimSpace ( out )
if out != "dockerio:dockerio" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Wrong /hello ownership: %s, expected dockerio:dockerio" , out )
2014-07-16 19:40:04 +00:00
}
}
2014-07-17 17:36:45 +00:00
// Test for #1582
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCopyVolumeContent ( c * check . C ) {
2016-04-06 21:01:12 +00:00
// TODO Windows, post TP5. Windows does not yet support volume functionality
2015-09-23 23:04:51 +00:00
// that copies from the image to the volume.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-17 17:36:45 +00:00
name := "testruncopyvolumecontent"
_ , err := buildImage ( name ,
` FROM busybox
RUN mkdir - p / hello / local && echo hello > / hello / local / world ` ,
true )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-07-17 17:36:45 +00:00
}
// Test that the content is copied from the image to the volume
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--rm" , "-v" , "/hello" , name , "find" , "/hello" )
2014-07-17 17:36:45 +00:00
if ! ( strings . Contains ( out , "/hello/local/world" ) && strings . Contains ( out , "/hello/local" ) ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container failed to transfer content to volume" )
2014-07-17 17:36:45 +00:00
}
}
2014-07-10 05:46:11 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCleanupCmdOnEntrypoint ( c * check . C ) {
2014-07-10 05:46:11 +00:00
name := "testrunmdcleanuponentrypoint"
if _ , err := buildImage ( name ,
` FROM busybox
ENTRYPOINT [ "echo" ]
2015-07-19 20:47:47 +00:00
CMD [ "testingpoint" ] ` ,
2014-07-10 05:46:11 +00:00
true ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-07-10 05:46:11 +00:00
}
2015-07-19 20:47:47 +00:00
out , exit := dockerCmd ( c , "run" , "--entrypoint" , "whoami" , name )
2014-07-10 05:46:11 +00:00
if exit != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "expected exit code 0 received %d, out: %q" , exit , out )
2014-07-10 05:46:11 +00:00
}
out = strings . TrimSpace ( out )
2015-09-01 21:37:04 +00:00
expected := "root"
if daemonPlatform == "windows" {
2016-04-06 21:01:12 +00:00
expected = ` user manager\containeradministrator `
2015-09-01 21:37:04 +00:00
}
if out != expected {
c . Fatalf ( "Expected output %s, got %q" , expected , out )
2014-07-10 05:46:11 +00:00
}
}
2014-08-12 07:48:36 +00:00
// TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWorkdirExistsAndIsFile ( c * check . C ) {
2015-09-01 21:37:04 +00:00
existingFile := "/bin/cat"
2016-03-08 15:40:30 +00:00
expected := "not a directory"
2015-09-01 21:37:04 +00:00
if daemonPlatform == "windows" {
existingFile = ` \windows\system32\ntdll.dll `
2016-01-27 21:03:09 +00:00
expected = ` Cannot mkdir: \windows\system32\ntdll.dll is not a directory. `
2015-09-01 21:37:04 +00:00
}
2015-07-29 12:21:16 +00:00
out , exitCode , err := dockerCmdWithError ( "run" , "-w" , existingFile , "busybox" )
if ! ( err != nil && exitCode == 125 && strings . Contains ( out , expected ) ) {
2016-03-08 15:40:30 +00:00
c . Fatalf ( "Existing binary as a directory should error out with exitCode 125; we got: %s, exitCode: %d" , out , exitCode )
2014-08-12 07:48:36 +00:00
}
}
2014-08-12 09:36:49 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunExitOnStdinClose ( c * check . C ) {
2014-08-12 09:36:49 +00:00
name := "testrunexitonstdinclose"
2015-09-01 21:37:04 +00:00
meow := "/bin/cat"
2016-04-14 19:16:43 +00:00
delay := 60
2015-09-01 21:37:04 +00:00
if daemonPlatform == "windows" {
meow = "cat"
}
runCmd := exec . Command ( dockerBinary , "run" , "--name" , name , "-i" , "busybox" , meow )
2014-08-12 09:36:49 +00:00
stdin , err := runCmd . StdinPipe ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
stdout , err := runCmd . StdoutPipe ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
if err := runCmd . Start ( ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
if _ , err := stdin . Write ( [ ] byte ( "hello\n" ) ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
r := bufio . NewReader ( stdout )
line , err := r . ReadString ( '\n' )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
line = strings . TrimSpace ( line )
if line != "hello" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Output should be 'hello', got '%q'" , line )
2014-08-12 09:36:49 +00:00
}
if err := stdin . Close ( ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-12 09:36:49 +00:00
}
2015-04-27 17:29:48 +00:00
finish := make ( chan error )
2014-08-12 09:36:49 +00:00
go func ( ) {
2015-04-27 17:29:48 +00:00
finish <- runCmd . Wait ( )
2014-08-12 09:36:49 +00:00
close ( finish )
} ( )
select {
2015-04-27 17:29:48 +00:00
case err := <- finish :
c . Assert ( err , check . IsNil )
2015-09-01 21:37:04 +00:00
case <- time . After ( time . Duration ( delay ) * time . Second ) :
2015-04-18 16:46:47 +00:00
c . Fatal ( "docker run failed to exit on stdin close" )
2014-08-12 09:36:49 +00:00
}
2016-01-28 14:19:25 +00:00
state := inspectField ( c , name , "State.Running" )
2015-04-27 17:29:48 +00:00
2014-08-12 09:36:49 +00:00
if state != "false" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container must be stopped after stdin closing" )
2014-08-12 09:36:49 +00:00
}
}
2014-07-22 18:16:15 +00:00
2016-05-17 02:30:06 +00:00
// Test run -i --restart xxx doesn't hang
func ( s * DockerSuite ) TestRunInteractiveWithRestartPolicy ( c * check . C ) {
name := "test-inter-restart"
runCmd := exec . Command ( dockerBinary , "run" , "-i" , "--name" , name , "--restart=always" , "busybox" , "sh" )
stdin , err := runCmd . StdinPipe ( )
c . Assert ( err , checker . IsNil )
err = runCmd . Start ( )
c . Assert ( err , checker . IsNil )
c . Assert ( waitRun ( name ) , check . IsNil )
_ , err = stdin . Write ( [ ] byte ( "exit 11\n" ) )
c . Assert ( err , checker . IsNil )
finish := make ( chan error )
go func ( ) {
finish <- runCmd . Wait ( )
close ( finish )
} ( )
delay := 10 * time . Second
select {
case <- finish :
case <- time . After ( delay ) :
c . Fatal ( "run -i --restart hangs" )
}
c . Assert ( waitRun ( name ) , check . IsNil )
dockerCmd ( c , "stop" , name )
}
2014-07-22 18:16:15 +00:00
// Test for #2267
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWriteHostsFileAndNotCommit ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Cannot run on Windows as Windows does not support diff.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-22 18:16:15 +00:00
name := "writehosts"
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--name" , name , "busybox" , "sh" , "-c" , "echo test2267 >> /etc/hosts && cat /etc/hosts" )
2014-07-22 18:16:15 +00:00
if ! strings . Contains ( out , "test2267" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc/hosts should contain 'test2267'" )
2014-07-22 18:16:15 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "diff" , name )
2015-04-18 16:46:47 +00:00
if len ( strings . Trim ( out , "\r\n" ) ) != 0 && ! eqToBaseDiff ( out , c ) {
c . Fatal ( "diff should be empty" )
2014-07-22 18:16:15 +00:00
}
}
2015-04-18 16:46:47 +00:00
func eqToBaseDiff ( out string , c * check . C ) bool {
2016-02-28 10:47:37 +00:00
name := "eqToBaseDiff" + stringutils . GenerateRandomAlphaOnlyString ( 32 )
dockerCmd ( c , "run" , "--name" , name , "busybox" , "echo" , "hello" )
cID , err := getIDByName ( name )
c . Assert ( err , check . IsNil )
2015-07-19 20:47:47 +00:00
baseDiff , _ := dockerCmd ( c , "diff" , cID )
2015-03-26 01:40:23 +00:00
baseArr := strings . Split ( baseDiff , "\n" )
sort . Strings ( baseArr )
outArr := strings . Split ( out , "\n" )
sort . Strings ( outArr )
return sliceEq ( baseArr , outArr )
2015-01-20 00:32:29 +00:00
}
func sliceEq ( a , b [ ] string ) bool {
if len ( a ) != len ( b ) {
return false
}
for i := range a {
if a [ i ] != b [ i ] {
return false
}
}
return true
}
2014-07-22 18:16:15 +00:00
// Test for #2267
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWriteHostnameFileAndNotCommit ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Cannot run on Windows as Windows does not support diff.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-22 18:16:15 +00:00
name := "writehostname"
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--name" , name , "busybox" , "sh" , "-c" , "echo test2267 >> /etc/hostname && cat /etc/hostname" )
2014-07-22 18:16:15 +00:00
if ! strings . Contains ( out , "test2267" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc/hostname should contain 'test2267'" )
2014-07-22 18:16:15 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "diff" , name )
2015-04-18 16:46:47 +00:00
if len ( strings . Trim ( out , "\r\n" ) ) != 0 && ! eqToBaseDiff ( out , c ) {
c . Fatal ( "diff should be empty" )
2014-07-22 18:16:15 +00:00
}
}
// Test for #2267
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWriteResolvFileAndNotCommit ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Cannot run on Windows as Windows does not support diff.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-07-22 18:16:15 +00:00
name := "writeresolv"
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--name" , name , "busybox" , "sh" , "-c" , "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf" )
2014-07-22 18:16:15 +00:00
if ! strings . Contains ( out , "test2267" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc/resolv.conf should contain 'test2267'" )
2014-07-22 18:16:15 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "diff" , name )
2015-04-18 16:46:47 +00:00
if len ( strings . Trim ( out , "\r\n" ) ) != 0 && ! eqToBaseDiff ( out , c ) {
c . Fatal ( "diff should be empty" )
2014-07-22 18:16:15 +00:00
}
}
2014-08-24 10:09:30 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWithBadDevice ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Cannot run on Windows as Windows does not support --device
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-08-24 10:09:30 +00:00
name := "baddevice"
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--device" , "/etc" , "busybox" , "true" )
2015-07-19 20:47:47 +00:00
2014-08-24 10:09:30 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Run should fail with bad device" )
2014-08-24 10:09:30 +00:00
}
2015-05-09 18:33:06 +00:00
expected := ` "/etc": not a device node `
2014-08-24 10:09:30 +00:00
if ! strings . Contains ( out , expected ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Output should contain %q, actual out: %q" , expected , out )
2014-08-24 10:09:30 +00:00
}
}
2014-08-28 00:25:10 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunEntrypoint ( c * check . C ) {
2014-08-28 00:25:10 +00:00
name := "entrypoint"
2015-07-19 20:47:47 +00:00
2016-03-29 00:26:34 +00:00
out , _ := dockerCmd ( c , "run" , "--name" , name , "--entrypoint" , "echo" , "busybox" , "-n" , "foobar" )
expected := "foobar"
2015-09-01 21:37:04 +00:00
2014-08-28 00:25:10 +00:00
if out != expected {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Output should be %q, actual out: %q" , expected , out )
2014-08-28 00:25:10 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunBindMounts ( c * check . C ) {
2015-09-23 23:04:51 +00:00
testRequires ( c , SameHostDaemon )
2015-11-13 21:02:30 +00:00
if daemonPlatform == "linux" {
2015-09-23 23:04:51 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
}
2014-11-17 16:05:49 +00:00
2016-05-25 20:56:36 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2014-08-28 00:25:10 +00:00
tmpDir , err := ioutil . TempDir ( "" , "docker-test-container" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-08-28 00:25:10 +00:00
}
defer os . RemoveAll ( tmpDir )
2015-04-18 16:46:47 +00:00
writeFile ( path . Join ( tmpDir , "touch-me" ) , "" , c )
2014-08-28 00:25:10 +00:00
2016-05-25 20:56:36 +00:00
// TODO Windows: Temporary check - remove once TP5 support is dropped
if daemonPlatform != "windows" || windowsDaemonKV >= 14350 {
2015-09-23 23:04:51 +00:00
// Test reading from a read-only bind mount
2016-05-25 20:56:36 +00:00
out , _ := dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( "%s:%s/tmp:ro" , tmpDir , prefix ) , "busybox" , "ls" , prefix + "/tmp" )
2015-09-23 23:04:51 +00:00
if ! strings . Contains ( out , "touch-me" ) {
c . Fatal ( "Container failed to read from bind mount" )
}
2014-08-28 00:25:10 +00:00
}
// test writing to bind mount
2015-09-23 23:04:51 +00:00
if daemonPlatform == "windows" {
dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( ` %s:c:\tmp:rw ` , tmpDir ) , "busybox" , "touch" , "c:/tmp/holla" )
} else {
dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( "%s:/tmp:rw" , tmpDir ) , "busybox" , "touch" , "/tmp/holla" )
}
2015-07-19 20:47:47 +00:00
2015-04-18 16:46:47 +00:00
readFile ( path . Join ( tmpDir , "holla" ) , c ) // Will fail if the file doesn't exist
2014-08-28 00:25:10 +00:00
// test mounting to an illegal destination directory
2015-07-27 18:13:25 +00:00
_ , _ , err = dockerCmdWithError ( "run" , "-v" , fmt . Sprintf ( "%s:." , tmpDir ) , "busybox" , "ls" , "." )
2014-08-28 00:25:10 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container bind mounted illegal directory" )
2014-08-28 00:25:10 +00:00
}
2015-09-23 23:04:51 +00:00
// Windows does not (and likely never will) support mounting a single file
if daemonPlatform != "windows" {
// test mount a file
dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( "%s/holla:/tmp/holla:rw" , tmpDir ) , "busybox" , "sh" , "-c" , "echo -n 'yotta' > /tmp/holla" )
content := readFile ( path . Join ( tmpDir , "holla" ) , c ) // Will fail if the file doesn't exist
expected := "yotta"
if content != expected {
c . Fatalf ( "Output should be %q, actual out: %q" , expected , content )
}
2014-08-28 00:25:10 +00:00
}
}
2014-07-14 23:19:37 +00:00
2014-09-01 15:55:39 +00:00
// Ensure that CIDFile gets deleted if it's empty
// Perform this test by making `docker run` fail
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCidFileCleanupIfEmpty ( c * check . C ) {
2016-06-28 20:04:42 +00:00
// Windows Server 2016 RS1 builds load the windowsservercore image from a tar rather than
// a .WIM file, and the tar layer has the default CMD set (same as the Linux ubuntu image),
// where-as the TP5 .WIM had a blank CMD. Hence this test is not applicable on RS1 or later
// builds as the command won't fail as it's not blank
if daemonPlatform == "windows" && windowsDaemonKV >= 14375 {
c . Skip ( "Not applicable on Windows RS1 or later builds" )
}
2014-09-01 15:55:39 +00:00
tmpDir , err := ioutil . TempDir ( "" , "TestRunCidFile" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-01 15:55:39 +00:00
}
defer os . RemoveAll ( tmpDir )
tmpCidFile := path . Join ( tmpDir , "cid" )
2015-07-19 20:47:47 +00:00
2015-09-01 21:37:04 +00:00
image := "emptyfs"
if daemonPlatform == "windows" {
// Windows can't support an emptyfs image. Just use the regular Windows image
image = WindowsBaseImage
}
out , _ , err := dockerCmdWithError ( "run" , "--cidfile" , tmpCidFile , image )
2014-09-01 15:55:39 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Run without command must fail. out=%s" , out )
2015-03-06 06:01:49 +00:00
} else if ! strings . Contains ( out , "No command specified" ) {
2015-04-27 20:33:30 +00:00
c . Fatalf ( "Run without command failed with wrong output. out=%s\nerr=%v" , out , err )
2014-09-01 15:55:39 +00:00
}
if _ , err := os . Stat ( tmpCidFile ) ; err == nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "empty CIDFile %q should've been deleted" , tmpCidFile )
2014-09-01 15:55:39 +00:00
}
}
2014-09-01 16:15:20 +00:00
// #2098 - Docker cidFiles only contain short version of the containerId
2015-04-18 16:46:47 +00:00
//sudo docker run --cidfile /tmp/docker_tesc.cid ubuntu echo "test"
2014-09-01 16:15:20 +00:00
// TestRunCidFile tests that run --cidfile returns the longid
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCidFileCheckIDLength ( c * check . C ) {
2014-09-01 16:15:20 +00:00
tmpDir , err := ioutil . TempDir ( "" , "TestRunCidFile" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-01 16:15:20 +00:00
}
tmpCidFile := path . Join ( tmpDir , "cid" )
defer os . RemoveAll ( tmpDir )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--cidfile" , tmpCidFile , "busybox" , "true" )
2014-09-01 16:15:20 +00:00
id := strings . TrimSpace ( out )
buffer , err := ioutil . ReadFile ( tmpCidFile )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-01 16:15:20 +00:00
}
cid := string ( buffer )
if len ( cid ) != 64 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "--cidfile should be a long id, not %q" , id )
2014-09-01 16:15:20 +00:00
}
if cid != id {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "cid must be equal to %s, got %s" , id , cid )
2014-09-01 16:15:20 +00:00
}
}
2014-09-04 05:50:58 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunSetMacAddress ( c * check . C ) {
2014-10-03 21:02:17 +00:00
mac := "12:34:56:78:9a:bc"
2015-09-01 21:37:04 +00:00
var out string
if daemonPlatform == "windows" {
out , _ = dockerCmd ( c , "run" , "-i" , "--rm" , fmt . Sprintf ( "--mac-address=%s" , mac ) , "busybox" , "sh" , "-c" , "ipconfig /all | grep 'Physical Address' | awk '{print $12}'" )
2015-09-23 23:04:51 +00:00
mac = strings . Replace ( strings . ToUpper ( mac ) , ":" , "-" , - 1 ) // To Windows-style MACs
2015-09-01 21:37:04 +00:00
} else {
out , _ = dockerCmd ( c , "run" , "-i" , "--rm" , fmt . Sprintf ( "--mac-address=%s" , mac ) , "busybox" , "/bin/sh" , "-c" , "ip link show eth0 | tail -1 | awk '{print $2}'" )
}
2015-07-19 20:47:47 +00:00
2014-10-03 21:02:17 +00:00
actualMac := strings . TrimSpace ( out )
if actualMac != mac {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Set MAC address with --mac-address failed. The container has an incorrect MAC address: %q, expected: %q" , actualMac , mac )
2014-10-03 21:02:17 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunInspectMacAddress ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. Network settings are not propagated back to inspect.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-03 21:02:17 +00:00
mac := "12:34:56:78:9a:bc"
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--mac-address=" + mac , "busybox" , "top" )
2014-10-03 21:02:17 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
inspectedMac := inspectField ( c , id , "NetworkSettings.Networks.bridge.MacAddress" )
2014-10-03 21:02:17 +00:00
if inspectedMac != mac {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "docker inspect outputs wrong MAC address: %q, should be: %q" , inspectedMac , mac )
2014-10-03 21:02:17 +00:00
}
}
2016-02-22 19:22:20 +00:00
// test docker run use an invalid mac address
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunWithInvalidMacAddress ( c * check . C ) {
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--mac-address" , "92:d0:c6:0a:29" , "busybox" )
2016-02-22 19:22:20 +00:00
//use an invalid mac address should with an error out
2015-02-27 15:27:12 +00:00
if err == nil || ! strings . Contains ( out , "is not a valid mac address" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "run with an invalid --mac-address should with error out" )
2015-02-27 15:27:12 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunDeallocatePortOnMissingIptablesRule ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. Network settings are not propagated back to inspect.
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-02-20 06:56:02 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "-p" , "23:23" , "busybox" , "top" )
2014-09-09 12:47:25 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
ip := inspectField ( c , id , "NetworkSettings.Networks.bridge.IPAddress" )
2014-11-15 01:36:38 +00:00
iptCmd := exec . Command ( "iptables" , "-D" , "DOCKER" , "-d" , fmt . Sprintf ( "%s/32" , ip ) ,
2014-09-09 12:47:25 +00:00
"!" , "-i" , "docker0" , "-o" , "docker0" , "-p" , "tcp" , "-m" , "tcp" , "--dport" , "23" , "-j" , "ACCEPT" )
2016-01-28 14:19:25 +00:00
out , _ , err := runCommandWithOutput ( iptCmd )
2014-09-09 12:47:25 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err , out )
2014-09-09 12:47:25 +00:00
}
if err := deleteContainer ( id ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-09 12:47:25 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "-p" , "23:23" , "busybox" , "top" )
2014-09-09 12:47:25 +00:00
}
2014-09-06 01:59:31 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunPortInUse ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. The duplicate NAT message returned by Windows will be
// changing as is currently completely undecipherable. Does need modifying
// to run sh rather than top though as top isn't in Windows busybox.
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-02-20 06:56:02 +00:00
2014-09-06 01:59:31 +00:00
port := "1234"
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "-p" , port + ":80" , "busybox" , "top" )
2014-10-08 12:41:26 +00:00
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-d" , "-p" , port + ":80" , "busybox" , "top" )
2014-11-11 00:19:16 +00:00
if err == nil {
c . Fatalf ( "Binding on used port must fail" )
2014-10-08 12:41:26 +00:00
}
2014-11-11 00:19:16 +00:00
if ! strings . Contains ( out , "port is already allocated" ) {
c . Fatalf ( "Out must be about \"port is already allocated\", got %s" , out )
2014-10-08 12:41:26 +00:00
}
}
2015-04-08 05:31:47 +00:00
// https://github.com/docker/docker/issues/12148
func ( s * DockerSuite ) TestRunAllocatePortInReservedRange ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. -P is not yet supported
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-04-08 05:31:47 +00:00
// allocate a dynamic port to get the most recent
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "-P" , "-p" , "80" , "busybox" , "top" )
2015-04-08 05:31:47 +00:00
id := strings . TrimSpace ( out )
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "port" , id , "80" )
2015-04-08 05:31:47 +00:00
strPort := strings . Split ( strings . TrimSpace ( out ) , ":" ) [ 1 ]
port , err := strconv . ParseInt ( strPort , 10 , 64 )
if err != nil {
c . Fatalf ( "invalid port, got: %s, error: %s" , strPort , err )
}
// allocate a static port and a dynamic port together, with static port
// takes the next recent port in dynamic port range.
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "-P" , "-p" , "80" , "-p" , fmt . Sprintf ( "%d:8080" , port + 1 ) , "busybox" , "top" )
2015-04-08 05:31:47 +00:00
}
2014-09-16 00:51:06 +00:00
// Regression test for #7792
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunMountOrdering ( c * check . C ) {
2016-04-06 21:01:12 +00:00
// TODO Windows: Post TP5. Updated, but Windows does not support nested mounts currently.
2015-09-23 23:04:51 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2015-02-20 06:56:02 +00:00
2014-09-16 00:51:06 +00:00
tmpDir , err := ioutil . TempDir ( "" , "docker_nested_mount_test" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-16 00:51:06 +00:00
}
defer os . RemoveAll ( tmpDir )
tmpDir2 , err := ioutil . TempDir ( "" , "docker_nested_mount_test2" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-16 00:51:06 +00:00
}
defer os . RemoveAll ( tmpDir2 )
2015-04-18 16:46:47 +00:00
// Create a temporary tmpfs mounc.
2014-09-16 00:51:06 +00:00
fooDir := filepath . Join ( tmpDir , "foo" )
if err := os . MkdirAll ( filepath . Join ( tmpDir , "foo" ) , 0755 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "failed to mkdir at %s - %s" , fooDir , err )
2014-09-16 00:51:06 +00:00
}
if err := ioutil . WriteFile ( fmt . Sprintf ( "%s/touch-me" , fooDir ) , [ ] byte { } , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-16 00:51:06 +00:00
}
if err := ioutil . WriteFile ( fmt . Sprintf ( "%s/touch-me" , tmpDir ) , [ ] byte { } , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-16 00:51:06 +00:00
}
if err := ioutil . WriteFile ( fmt . Sprintf ( "%s/touch-me" , tmpDir2 ) , [ ] byte { } , 0644 ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-16 00:51:06 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" ,
2015-09-23 23:04:51 +00:00
"-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp" , tmpDir ) ,
"-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp/foo" , fooDir ) ,
"-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp/tmp2" , tmpDir2 ) ,
"-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp/tmp2/foo" , fooDir ) ,
2015-05-19 20:05:25 +00:00
"busybox:latest" , "sh" , "-c" ,
2015-09-23 23:04:51 +00:00
"ls " + prefix + "/tmp/touch-me && ls " + prefix + "/tmp/foo/touch-me && ls " + prefix + "/tmp/tmp2/touch-me && ls " + prefix + "/tmp/tmp2/foo/touch-me" )
2014-09-16 00:51:06 +00:00
}
2014-09-10 16:43:16 +00:00
2014-09-26 18:28:21 +00:00
// Regression test for https://github.com/docker/docker/issues/8259
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunReuseBindVolumeThatIsSymlink ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as Windows does not support volumes
2015-09-23 23:04:51 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2015-02-20 06:56:02 +00:00
2014-09-26 18:28:21 +00:00
tmpDir , err := ioutil . TempDir ( os . TempDir ( ) , "testlink" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-26 18:28:21 +00:00
}
defer os . RemoveAll ( tmpDir )
linkPath := os . TempDir ( ) + "/testlink2"
if err := os . Symlink ( tmpDir , linkPath ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-09-26 18:28:21 +00:00
}
defer os . RemoveAll ( linkPath )
// Create first container
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp/test" , linkPath ) , "busybox" , "ls" , prefix + "/tmp/test" )
2014-09-26 18:28:21 +00:00
// Create second container with same symlinked path
// This will fail if the referenced issue is hit with a "Volume exists" error
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "-v" , fmt . Sprintf ( "%s:" + prefix + "/tmp/test" , linkPath ) , "busybox" , "ls" , prefix + "/tmp/test" )
2014-09-26 18:28:21 +00:00
}
2014-10-06 17:30:42 +00:00
2015-03-05 14:41:56 +00:00
//GH#10604: Test an "/etc" volume doesn't overlay special bind mounts in container
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunCreateVolumeEtc ( c * check . C ) {
2015-09-23 23:04:51 +00:00
// While Windows supports volumes, it does not support --add-host hence
// this test is not applicable on Windows.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--dns=127.0.0.1" , "-v" , "/etc" , "busybox" , "cat" , "/etc/resolv.conf" )
2015-02-12 02:39:07 +00:00
if ! strings . Contains ( out , "nameserver 127.0.0.1" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc volume mount hides /etc/resolv.conf" )
2015-02-12 02:39:07 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "-h=test123" , "-v" , "/etc" , "busybox" , "cat" , "/etc/hostname" )
2015-02-12 02:39:07 +00:00
if ! strings . Contains ( out , "test123" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc volume mount hides /etc/hostname" )
2015-02-12 02:39:07 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "--add-host=test:192.168.0.1" , "-v" , "/etc" , "busybox" , "cat" , "/etc/hosts" )
2015-02-12 02:39:07 +00:00
out = strings . Replace ( out , "\n" , " " , - 1 )
2015-03-05 14:41:56 +00:00
if ! strings . Contains ( out , "192.168.0.1\ttest" ) || ! strings . Contains ( out , "127.0.0.1\tlocalhost" ) {
2015-04-18 16:46:47 +00:00
c . Fatal ( "/etc volume mount hides /etc/hosts" )
2015-02-12 02:39:07 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestVolumesNoCopyData ( c * check . C ) {
2016-04-06 21:01:12 +00:00
// TODO Windows (Post TP5). Windows does not support volumes which
2015-09-23 23:04:51 +00:00
// are pre-populated such as is built in the dockerfile used in this test.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-06 17:30:42 +00:00
if _ , err := buildImage ( "dataimage" ,
` FROM busybox
2015-07-19 20:47:47 +00:00
RUN mkdir - p / foo
RUN touch / foo / bar ` ,
2014-10-06 17:30:42 +00:00
true ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-06 17:30:42 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "--name" , "test" , "-v" , "/foo" , "busybox" )
2014-10-06 17:30:42 +00:00
2015-07-27 18:13:25 +00:00
if out , _ , err := dockerCmdWithError ( "run" , "--volumes-from" , "test" , "dataimage" , "ls" , "-lh" , "/foo/bar" ) ; err == nil || ! strings . Contains ( out , "No such file or directory" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Data was copied on volumes-from but shouldn't be:\n%q" , out )
2014-10-06 17:30:42 +00:00
}
2015-09-24 17:53:47 +00:00
tmpDir := randomTmpDirPath ( "docker_test_bind_mount_copy_data" , daemonPlatform )
2015-07-27 18:13:25 +00:00
if out , _ , err := dockerCmdWithError ( "run" , "-v" , tmpDir + ":/foo" , "dataimage" , "ls" , "-lh" , "/foo/bar" ) ; err == nil || ! strings . Contains ( out , "No such file or directory" ) {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Data was copied on bind-mount but shouldn't be:\n%q" , out )
2014-10-06 17:30:42 +00:00
}
}
2014-10-08 19:01:25 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNoOutputFromPullInStdout ( c * check . C ) {
2014-10-17 18:06:05 +00:00
// just run with unknown image
cmd := exec . Command ( dockerBinary , "run" , "asdfsg" )
stdout := bytes . NewBuffer ( nil )
cmd . Stdout = stdout
if err := cmd . Run ( ) ; err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Run with unknown image should fail" )
2014-10-17 18:06:05 +00:00
}
if stdout . Len ( ) != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Stdout contains output from pull: %s" , stdout )
2014-10-17 18:06:05 +00:00
}
}
2014-10-20 19:27:26 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVolumesCleanPaths ( c * check . C ) {
2015-10-29 15:55:48 +00:00
testRequires ( c , SameHostDaemon )
2016-02-03 14:16:00 +00:00
prefix , slash := getPrefixAndSlashFromDaemonPlatform ( )
2014-10-20 19:27:26 +00:00
if _ , err := buildImage ( "run_volumes_clean_paths" ,
` FROM busybox
2015-09-23 23:04:51 +00:00
VOLUME ` +prefix+ ` / foo / ` ,
2014-10-20 19:27:26 +00:00
true ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-20 19:27:26 +00:00
}
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "-v" , prefix + "/foo" , "-v" , prefix + "/bar/" , "--name" , "dark_helmet" , "run_volumes_clean_paths" )
2014-10-20 19:27:26 +00:00
2015-09-23 23:04:51 +00:00
out , err := inspectMountSourceField ( "dark_helmet" , prefix + slash + "foo" + slash )
2015-07-22 12:59:24 +00:00
if err != errMountNotFound {
2015-09-23 23:04:51 +00:00
c . Fatalf ( "Found unexpected volume entry for '%s/foo/' in volumes\n%q" , prefix , out )
2014-10-20 19:27:26 +00:00
}
2015-09-23 23:04:51 +00:00
out , err = inspectMountSourceField ( "dark_helmet" , prefix + slash + ` foo ` )
2015-05-18 02:06:13 +00:00
c . Assert ( err , check . IsNil )
2015-09-23 23:04:51 +00:00
if ! strings . Contains ( strings . ToLower ( out ) , strings . ToLower ( volumesConfigPath ) ) {
c . Fatalf ( "Volume was not defined for %s/foo\n%q" , prefix , out )
2014-10-20 19:27:26 +00:00
}
2015-09-23 23:04:51 +00:00
out , err = inspectMountSourceField ( "dark_helmet" , prefix + slash + "bar" + slash )
2015-07-22 12:59:24 +00:00
if err != errMountNotFound {
2015-09-23 23:04:51 +00:00
c . Fatalf ( "Found unexpected volume entry for '%s/bar/' in volumes\n%q" , prefix , out )
2014-10-20 19:27:26 +00:00
}
2015-06-03 19:21:38 +00:00
2015-09-23 23:04:51 +00:00
out , err = inspectMountSourceField ( "dark_helmet" , prefix + slash + "bar" )
2015-05-18 02:06:13 +00:00
c . Assert ( err , check . IsNil )
2015-09-23 23:04:51 +00:00
if ! strings . Contains ( strings . ToLower ( out ) , strings . ToLower ( volumesConfigPath ) ) {
c . Fatalf ( "Volume was not defined for %s/bar\n%q" , prefix , out )
2014-10-20 19:27:26 +00:00
}
}
2014-10-03 17:38:44 +00:00
// Regression test for #3631
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunSlowStdoutConsumer ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This should be able to run on Windows if can find an
// alternate to /dev/zero and /dev/stdout.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-04-18 16:46:47 +00:00
cont := exec . Command ( dockerBinary , "run" , "--rm" , "busybox" , "/bin/sh" , "-c" , "dd if=/dev/zero of=/dev/stdout bs=1024 count=2000 | catv" )
2014-10-03 17:38:44 +00:00
2015-04-18 16:46:47 +00:00
stdout , err := cont . StdoutPipe ( )
2014-10-03 17:38:44 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-03 17:38:44 +00:00
}
2015-04-18 16:46:47 +00:00
if err := cont . Start ( ) ; err != nil {
c . Fatal ( err )
2014-10-03 17:38:44 +00:00
}
2014-10-30 19:10:38 +00:00
n , err := consumeWithSpeed ( stdout , 10000 , 5 * time . Millisecond , nil )
2014-10-03 17:38:44 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-10-03 17:38:44 +00:00
}
expected := 2 * 1024 * 2000
if n != expected {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Expected %d, got %d" , expected , n )
2014-10-03 17:38:44 +00:00
}
}
2014-09-17 01:08:30 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAllowPortRangeThroughExpose ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: -P is not currently supported. Also network
// settings are not propagated back.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--expose" , "3000-3003" , "-P" , "busybox" , "top" )
2014-09-17 01:08:30 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
portstr := inspectFieldJSON ( c , id , "NetworkSettings.Ports" )
2014-09-17 01:08:30 +00:00
var ports nat . PortMap
2016-01-28 14:19:25 +00:00
if err := unmarshalJSON ( [ ] byte ( portstr ) , & ports ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-03-26 03:18:42 +00:00
}
2014-09-17 01:08:30 +00:00
for port , binding := range ports {
portnum , _ := strconv . Atoi ( strings . Split ( string ( port ) , "/" ) [ 0 ] )
if portnum < 3000 || portnum > 3003 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Port %d is out of range " , portnum )
2014-09-17 01:08:30 +00:00
}
if binding == nil || len ( binding ) != 1 || len ( binding [ 0 ] . HostPort ) == 0 {
2015-08-19 08:47:33 +00:00
c . Fatalf ( "Port is not mapped for the port %s" , port )
2014-09-17 01:08:30 +00:00
}
}
}
2014-11-06 02:23:42 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunExposePort ( c * check . C ) {
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--expose" , "80000" , "busybox" )
2016-02-18 08:26:47 +00:00
c . Assert ( err , checker . NotNil , check . Commentf ( "--expose with an invalid port should error out" ) )
c . Assert ( out , checker . Contains , "invalid range format for --expose" )
2015-02-19 23:18:13 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunUnknownCommand ( c * check . C ) {
2015-07-20 18:20:50 +00:00
out , _ , _ := dockerCmdWithStdoutStderr ( c , "create" , "busybox" , "/bin/nada" )
2014-11-06 02:23:42 +00:00
2015-07-20 18:20:50 +00:00
cID := strings . TrimSpace ( out )
2015-07-27 18:13:25 +00:00
_ , _ , err := dockerCmdWithError ( "start" , cID )
2015-09-01 21:37:04 +00:00
// Windows and Linux are different here by architectural design. Linux will
// fail to start the container, so an error is expected. Windows will
// successfully start the container, and once started attempt to execute
// the command which will fail.
if daemonPlatform == "windows" {
// Wait for it to exit.
2015-09-28 20:40:17 +00:00
waitExited ( cID , 30 * time . Second )
2015-09-01 21:37:04 +00:00
c . Assert ( err , check . IsNil )
} else {
c . Assert ( err , check . NotNil )
}
2014-11-06 02:23:42 +00:00
2016-01-28 14:19:25 +00:00
rc := inspectField ( c , cID , "State.ExitCode" )
2015-01-20 00:32:29 +00:00
if rc == "0" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "ExitCode(%v) cannot be 0" , rc )
2014-11-06 02:23:42 +00:00
}
}
2014-11-10 21:14:17 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunModeIpcHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2015-09-18 17:41:12 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-02-20 06:56:02 +00:00
2014-11-10 21:14:17 +00:00
hostIpc , err := os . Readlink ( "/proc/1/ns/ipc" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-11-10 21:14:17 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--ipc=host" , "busybox" , "readlink" , "/proc/self/ns/ipc" )
out = strings . Trim ( out , "\n" )
if hostIpc != out {
c . Fatalf ( "IPC different with --ipc=host %s != %s\n" , hostIpc , out )
2014-11-10 21:14:17 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "busybox" , "readlink" , "/proc/self/ns/ipc" )
out = strings . Trim ( out , "\n" )
if hostIpc == out {
c . Fatalf ( "IPC should be different without --ipc=host %s == %s\n" , hostIpc , out )
2014-11-10 21:14:17 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunModeIpcContainer ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2016-03-23 13:37:15 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-02-20 06:56:02 +00:00
2016-02-09 00:51:45 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "sh" , "-c" , "echo -n test > /dev/shm/test && touch /dev/mqueue/toto && top" )
2015-07-19 20:47:47 +00:00
2014-11-10 21:14:17 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
state := inspectField ( c , id , "State.Running" )
2014-11-10 21:14:17 +00:00
if state != "true" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Container state is 'not running'" )
2014-11-10 21:14:17 +00:00
}
2016-01-28 14:19:25 +00:00
pid1 := inspectField ( c , id , "State.Pid" )
2014-11-10 21:14:17 +00:00
parentContainerIpc , err := os . Readlink ( fmt . Sprintf ( "/proc/%s/ns/ipc" , pid1 ) )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-11-10 21:14:17 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , fmt . Sprintf ( "--ipc=container:%s" , id ) , "busybox" , "readlink" , "/proc/self/ns/ipc" )
out = strings . Trim ( out , "\n" )
if parentContainerIpc != out {
c . Fatalf ( "IPC different with --ipc=container:%s %s != %s\n" , id , parentContainerIpc , out )
2014-11-10 21:14:17 +00:00
}
2015-08-13 22:35:18 +00:00
catOutput , _ := dockerCmd ( c , "run" , fmt . Sprintf ( "--ipc=container:%s" , id ) , "busybox" , "cat" , "/dev/shm/test" )
if catOutput != "test" {
c . Fatalf ( "Output of /dev/shm/test expected test but found: %s" , catOutput )
}
2016-02-09 00:51:45 +00:00
// check that /dev/mqueue is actually of mqueue type
grepOutput , _ := dockerCmd ( c , "run" , fmt . Sprintf ( "--ipc=container:%s" , id ) , "busybox" , "grep" , "/dev/mqueue" , "/proc/mounts" )
if ! strings . HasPrefix ( grepOutput , "mqueue /dev/mqueue mqueue rw" ) {
c . Fatalf ( "Output of 'grep /proc/mounts' expected 'mqueue /dev/mqueue mqueue rw' but found: %s" , grepOutput )
}
lsOutput , _ := dockerCmd ( c , "run" , fmt . Sprintf ( "--ipc=container:%s" , id ) , "busybox" , "ls" , "/dev/mqueue" )
lsOutput = strings . Trim ( lsOutput , "\n" )
if lsOutput != "toto" {
c . Fatalf ( "Output of 'ls /dev/mqueue' expected 'toto' but found: %s" , lsOutput )
}
2014-11-10 21:14:17 +00:00
}
2014-11-18 23:10:07 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunModeIpcContainerNotExists ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-d" , "--ipc" , "container:abcd1234" , "busybox" , "top" )
2015-03-28 01:07:20 +00:00
if ! strings . Contains ( out , "abcd1234" ) || err == nil {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "run IPC from a non exists container should with correct error out" )
2015-03-28 01:07:20 +00:00
}
}
2015-05-22 23:15:14 +00:00
func ( s * DockerSuite ) TestRunModeIpcContainerNotRunning ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2016-03-23 13:37:15 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-05-22 23:15:14 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "create" , "busybox" )
2015-05-22 23:15:14 +00:00
2015-07-19 20:47:47 +00:00
id := strings . TrimSpace ( out )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , fmt . Sprintf ( "--ipc=container:%s" , id ) , "busybox" )
2015-05-22 23:15:14 +00:00
if err == nil {
c . Fatalf ( "Run container with ipc mode container should fail with non running container: %s\n%s" , out , err )
}
}
2016-05-06 18:56:03 +00:00
func ( s * DockerSuite ) TestRunModePidContainer ( c * check . C ) {
// Not applicable on Windows as uses Unix-specific capabilities
testRequires ( c , SameHostDaemon , DaemonIsLinux )
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "sh" , "-c" , "top" )
id := strings . TrimSpace ( out )
state := inspectField ( c , id , "State.Running" )
if state != "true" {
c . Fatal ( "Container state is 'not running'" )
}
pid1 := inspectField ( c , id , "State.Pid" )
parentContainerPid , err := os . Readlink ( fmt . Sprintf ( "/proc/%s/ns/pid" , pid1 ) )
if err != nil {
c . Fatal ( err )
}
out , _ = dockerCmd ( c , "run" , fmt . Sprintf ( "--pid=container:%s" , id ) , "busybox" , "readlink" , "/proc/self/ns/pid" )
out = strings . Trim ( out , "\n" )
if parentContainerPid != out {
c . Fatalf ( "PID different with --pid=container:%s %s != %s\n" , id , parentContainerPid , out )
}
}
func ( s * DockerSuite ) TestRunModePidContainerNotExists ( c * check . C ) {
// Not applicable on Windows as uses Unix-specific capabilities
testRequires ( c , DaemonIsLinux )
out , _ , err := dockerCmdWithError ( "run" , "-d" , "--pid" , "container:abcd1234" , "busybox" , "top" )
if ! strings . Contains ( out , "abcd1234" ) || err == nil {
c . Fatalf ( "run PID from a non exists container should with correct error out" )
}
}
func ( s * DockerSuite ) TestRunModePidContainerNotRunning ( c * check . C ) {
// Not applicable on Windows as uses Unix-specific capabilities
testRequires ( c , SameHostDaemon , DaemonIsLinux )
out , _ := dockerCmd ( c , "create" , "busybox" )
id := strings . TrimSpace ( out )
out , _ , err := dockerCmdWithError ( "run" , fmt . Sprintf ( "--pid=container:%s" , id ) , "busybox" )
if err == nil {
c . Fatalf ( "Run container with pid mode container should fail with non running container: %s\n%s" , out , err )
}
}
2015-10-02 03:11:55 +00:00
func ( s * DockerSuite ) TestRunMountShmMqueueFromHost ( c * check . C ) {
// Not applicable on Windows as uses Unix-specific capabilities
2016-02-09 19:19:19 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-10-02 03:11:55 +00:00
2016-02-09 00:51:45 +00:00
dockerCmd ( c , "run" , "-d" , "--name" , "shmfromhost" , "-v" , "/dev/shm:/dev/shm" , "-v" , "/dev/mqueue:/dev/mqueue" , "busybox" , "sh" , "-c" , "echo -n test > /dev/shm/test && touch /dev/mqueue/toto && top" )
defer os . Remove ( "/dev/mqueue/toto" )
defer os . Remove ( "/dev/shm/test" )
2015-10-02 03:11:55 +00:00
volPath , err := inspectMountSourceField ( "shmfromhost" , "/dev/shm" )
2016-01-28 14:19:25 +00:00
c . Assert ( err , checker . IsNil )
2015-10-02 03:11:55 +00:00
if volPath != "/dev/shm" {
c . Fatalf ( "volumePath should have been /dev/shm, was %s" , volPath )
}
out , _ := dockerCmd ( c , "run" , "--name" , "ipchost" , "--ipc" , "host" , "busybox" , "cat" , "/dev/shm/test" )
if out != "test" {
c . Fatalf ( "Output of /dev/shm/test expected test but found: %s" , out )
}
2016-02-09 00:51:45 +00:00
// Check that the mq was created
if _ , err := os . Stat ( "/dev/mqueue/toto" ) ; err != nil {
c . Fatalf ( "Failed to confirm '/dev/mqueue/toto' presence on host: %s" , err . Error ( ) )
}
2015-10-02 03:11:55 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestContainerNetworkMode ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2016-03-23 13:37:15 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-02-20 06:56:02 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
2014-11-18 23:10:07 +00:00
id := strings . TrimSpace ( out )
2015-08-11 07:41:11 +00:00
c . Assert ( waitRun ( id ) , check . IsNil )
2016-01-28 14:19:25 +00:00
pid1 := inspectField ( c , id , "State.Pid" )
2014-11-18 23:10:07 +00:00
parentContainerNet , err := os . Readlink ( fmt . Sprintf ( "/proc/%s/ns/net" , pid1 ) )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-11-18 23:10:07 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , fmt . Sprintf ( "--net=container:%s" , id ) , "busybox" , "readlink" , "/proc/self/ns/net" )
out = strings . Trim ( out , "\n" )
if parentContainerNet != out {
c . Fatalf ( "NET different with --net=container:%s %s != %s\n" , id , parentContainerNet , out )
2014-11-18 23:10:07 +00:00
}
}
2014-11-20 15:29:04 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunModePidHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2015-11-05 00:40:58 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-02-20 06:56:02 +00:00
2015-01-14 00:39:08 +00:00
hostPid , err := os . Readlink ( "/proc/1/ns/pid" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-01-14 00:39:08 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--pid=host" , "busybox" , "readlink" , "/proc/self/ns/pid" )
out = strings . Trim ( out , "\n" )
if hostPid != out {
c . Fatalf ( "PID different with --pid=host %s != %s\n" , hostPid , out )
2015-01-14 00:39:08 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "busybox" , "readlink" , "/proc/self/ns/pid" )
out = strings . Trim ( out , "\n" )
if hostPid == out {
c . Fatalf ( "PID should be different without --pid=host %s == %s\n" , hostPid , out )
2015-01-14 00:39:08 +00:00
}
}
2015-05-05 22:32:36 +00:00
func ( s * DockerSuite ) TestRunModeUTSHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2015-11-05 00:40:58 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-05-05 22:32:36 +00:00
hostUTS , err := os . Readlink ( "/proc/1/ns/uts" )
if err != nil {
c . Fatal ( err )
}
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--uts=host" , "busybox" , "readlink" , "/proc/self/ns/uts" )
out = strings . Trim ( out , "\n" )
if hostUTS != out {
c . Fatalf ( "UTS different with --uts=host %s != %s\n" , hostUTS , out )
2015-05-05 22:32:36 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "busybox" , "readlink" , "/proc/self/ns/uts" )
out = strings . Trim ( out , "\n" )
if hostUTS == out {
c . Fatalf ( "UTS should be different without --uts=host %s == %s\n" , hostUTS , out )
2015-05-05 22:32:36 +00:00
}
2016-03-10 01:40:12 +00:00
out , _ = dockerCmdWithFail ( c , "run" , "-h=name" , "--uts=host" , "busybox" , "ps" )
c . Assert ( out , checker . Contains , runconfig . ErrConflictUTSHostname . Error ( ) )
2015-05-05 22:32:36 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunTLSverify ( c * check . C ) {
2016-02-24 22:59:11 +00:00
// Remote daemons use TLS and this test is not applicable when TLS is required.
testRequires ( c , SameHostDaemon )
2015-07-27 18:13:25 +00:00
if out , code , err := dockerCmdWithError ( "ps" ) ; err != nil || code != 0 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Should have worked: %v:\n%v" , err , out )
2014-11-20 15:29:04 +00:00
}
// Regardless of whether we specify true or false we need to
// test to make sure tls is turned on if --tlsverify is specified at all
2015-07-27 18:13:25 +00:00
out , code , err := dockerCmdWithError ( "--tlsverify=false" , "ps" )
2015-07-19 20:47:47 +00:00
if err == nil || code == 0 || ! strings . Contains ( out , "trying to connect" ) {
c . Fatalf ( "Should have failed: \net:%v\nout:%v\nerr:%v" , code , out , err )
2014-11-20 15:29:04 +00:00
}
2015-07-27 18:13:25 +00:00
out , code , err = dockerCmdWithError ( "--tlsverify=true" , "ps" )
2015-07-19 20:47:47 +00:00
if err == nil || code == 0 || ! strings . Contains ( out , "cert" ) {
c . Fatalf ( "Should have failed: \net:%v\nout:%v\nerr:%v" , code , out , err )
2014-11-20 15:29:04 +00:00
}
}
2014-12-02 00:24:43 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunPortFromDockerRangeInUse ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. Once moved to libnetwork/CNM, this may be able to be
// re-instated.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-12-02 00:24:43 +00:00
// first find allocator current position
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "-p" , ":80" , "busybox" , "top" )
2014-12-02 00:24:43 +00:00
id := strings . TrimSpace ( out )
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "port" , id )
2015-02-17 10:15:51 +00:00
2015-07-19 20:47:47 +00:00
out = strings . TrimSpace ( out )
2015-02-17 10:15:51 +00:00
if out == "" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "docker port command output is empty" )
2015-02-17 10:15:51 +00:00
}
2014-12-02 00:24:43 +00:00
out = strings . Split ( out , ":" ) [ 1 ]
lastPort , err := strconv . Atoi ( out )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-02 00:24:43 +00:00
}
port := lastPort + 1
l , err := net . Listen ( "tcp" , ":" + strconv . Itoa ( port ) )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-02 00:24:43 +00:00
}
defer l . Close ( )
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "-d" , "-p" , ":80" , "busybox" , "top" )
2014-12-02 00:24:43 +00:00
id = strings . TrimSpace ( out )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "port" , id )
2014-12-02 00:24:43 +00:00
}
2014-12-06 00:50:56 +00:00
2016-01-24 08:32:52 +00:00
func ( s * DockerSuite ) TestRunTTYWithPipe ( c * check . C ) {
2015-04-27 17:29:48 +00:00
errChan := make ( chan error )
2014-12-06 00:50:56 +00:00
go func ( ) {
2015-04-27 17:29:48 +00:00
defer close ( errChan )
2014-12-06 00:50:56 +00:00
cmd := exec . Command ( dockerBinary , "run" , "-ti" , "busybox" , "true" )
if _ , err := cmd . StdinPipe ( ) ; err != nil {
2015-04-27 17:29:48 +00:00
errChan <- err
return
2014-12-06 00:50:56 +00:00
}
2016-05-25 17:19:17 +00:00
expected := "the input device is not a TTY"
if runtime . GOOS == "windows" {
expected += ". If you are using mintty, try prefixing the command with 'winpty'"
}
2014-12-06 00:50:56 +00:00
if out , _ , err := runCommandWithOutput ( cmd ) ; err == nil {
2015-04-27 17:29:48 +00:00
errChan <- fmt . Errorf ( "run should have failed" )
return
2014-12-06 00:50:56 +00:00
} else if ! strings . Contains ( out , expected ) {
2015-04-27 17:29:48 +00:00
errChan <- fmt . Errorf ( "run failed with error %q: expected %q" , out , expected )
return
2014-12-06 00:50:56 +00:00
}
} ( )
select {
2015-04-27 17:29:48 +00:00
case err := <- errChan :
c . Assert ( err , check . IsNil )
2016-05-25 17:19:17 +00:00
case <- time . After ( 30 * time . Second ) :
2015-04-18 16:46:47 +00:00
c . Fatal ( "container is running but should have failed" )
2014-12-06 00:50:56 +00:00
}
}
2014-12-22 22:54:55 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNonLocalMacAddress ( c * check . C ) {
2014-12-22 22:54:55 +00:00
addr := "00:16:3E:08:00:50"
2016-03-29 00:26:34 +00:00
args := [ ] string { "run" , "--mac-address" , addr }
2015-09-01 21:37:04 +00:00
expected := addr
2016-03-29 00:26:34 +00:00
if daemonPlatform != "windows" {
args = append ( args , "busybox" , "ifconfig" )
} else {
args = append ( args , WindowsBaseImage , "ipconfig" , "/all" )
2015-09-23 23:04:51 +00:00
expected = strings . Replace ( strings . ToUpper ( addr ) , ":" , "-" , - 1 )
2015-09-01 21:37:04 +00:00
}
2016-03-29 00:26:34 +00:00
if out , _ := dockerCmd ( c , args ... ) ; ! strings . Contains ( out , expected ) {
2015-09-01 21:37:04 +00:00
c . Fatalf ( "Output should have contained %q: %s" , expected , out )
2014-12-22 22:54:55 +00:00
}
}
2014-12-25 04:40:41 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNetHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2015-09-18 17:41:12 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-02-26 04:16:44 +00:00
2014-12-29 20:56:07 +00:00
hostNet , err := os . Readlink ( "/proc/1/ns/net" )
2014-12-25 04:40:41 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2014-12-25 04:40:41 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--net=host" , "busybox" , "readlink" , "/proc/self/ns/net" )
out = strings . Trim ( out , "\n" )
if hostNet != out {
c . Fatalf ( "Net namespace different with --net=host %s != %s\n" , hostNet , out )
2014-12-29 20:56:07 +00:00
}
2015-07-19 20:47:47 +00:00
out , _ = dockerCmd ( c , "run" , "busybox" , "readlink" , "/proc/self/ns/net" )
out = strings . Trim ( out , "\n" )
if hostNet == out {
c . Fatalf ( "Net namespace should be different without --net=host %s == %s\n" , hostNet , out )
2014-12-29 20:56:07 +00:00
}
2014-12-25 04:40:41 +00:00
}
2014-11-03 18:15:55 +00:00
2015-06-24 17:23:00 +00:00
func ( s * DockerSuite ) TestRunNetHostTwiceSameName ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. As Windows networking evolves and converges towards
// CNM, this test may be possible to enable on Windows.
2015-09-18 17:41:12 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-06-24 17:23:00 +00:00
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "--rm" , "--name=thost" , "--net=host" , "busybox" , "true" )
dockerCmd ( c , "run" , "--rm" , "--name=thost" , "--net=host" , "busybox" , "true" )
2015-06-24 17:23:00 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunNetContainerWhichHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix-specific capabilities
2015-09-18 17:41:12 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-04-07 22:20:39 +00:00
hostNet , err := os . Readlink ( "/proc/1/ns/net" )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-04-07 22:20:39 +00:00
}
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "--net=host" , "--name=test" , "busybox" , "top" )
2015-04-07 22:20:39 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--net=container:test" , "busybox" , "readlink" , "/proc/self/ns/net" )
2015-04-07 22:20:39 +00:00
out = strings . Trim ( out , "\n" )
if hostNet != out {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container should have host network namespace" )
2015-04-07 22:20:39 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunAllowPortRangeThroughPublish ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. This may be possible to enable in the future. However,
// Windows does not currently support --expose, or populate the network
// settings seen through inspect.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--expose" , "3000-3003" , "-p" , "3000-3003" , "busybox" , "top" )
2014-11-03 18:15:55 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
portstr := inspectFieldJSON ( c , id , "NetworkSettings.Ports" )
2015-07-19 20:47:47 +00:00
2014-11-03 18:15:55 +00:00
var ports nat . PortMap
2016-01-28 14:19:25 +00:00
err := unmarshalJSON ( [ ] byte ( portstr ) , & ports )
c . Assert ( err , checker . IsNil , check . Commentf ( "failed to unmarshal: %v" , portstr ) )
2014-11-03 18:15:55 +00:00
for port , binding := range ports {
portnum , _ := strconv . Atoi ( strings . Split ( string ( port ) , "/" ) [ 0 ] )
if portnum < 3000 || portnum > 3003 {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Port %d is out of range " , portnum )
2014-11-03 18:15:55 +00:00
}
if binding == nil || len ( binding ) != 1 || len ( binding [ 0 ] . HostPort ) == 0 {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Port is not mapped for the port " + port , out )
2014-11-03 18:15:55 +00:00
}
}
}
2015-01-09 23:42:55 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunSetDefaultRestartPolicy ( c * check . C ) {
2016-05-03 17:52:09 +00:00
runSleepingContainer ( c , "--name=testrunsetdefaultrestartpolicy" )
out := inspectField ( c , "testrunsetdefaultrestartpolicy" , "HostConfig.RestartPolicy.Name" )
2015-03-10 02:09:12 +00:00
if out != "no" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Set default restart policy failed" )
2015-03-10 02:09:12 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunRestartMaxRetries ( c * check . C ) {
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--restart=on-failure:3" , "busybox" , "false" )
2015-09-01 21:37:04 +00:00
timeout := 10 * time . Second
if daemonPlatform == "windows" {
2016-02-04 19:59:13 +00:00
timeout = 120 * time . Second
2015-09-01 21:37:04 +00:00
}
2015-07-19 20:47:47 +00:00
2015-01-13 23:19:12 +00:00
id := strings . TrimSpace ( string ( out ) )
2015-09-01 21:37:04 +00:00
if err := waitInspect ( id , "{{ .State.Restarting }} {{ .State.Running }}" , "false false" , timeout ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( err )
2015-01-13 23:19:12 +00:00
}
2015-07-19 20:47:47 +00:00
2016-01-28 14:19:25 +00:00
count := inspectField ( c , id , "RestartCount" )
2015-01-13 23:19:12 +00:00
if count != "3" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container was restarted %s times, expected %d" , count , 3 )
2015-01-13 23:19:12 +00:00
}
2015-07-19 20:47:47 +00:00
2016-01-28 14:19:25 +00:00
MaximumRetryCount := inspectField ( c , id , "HostConfig.RestartPolicy.MaximumRetryCount" )
2015-03-17 05:19:59 +00:00
if MaximumRetryCount != "3" {
2015-04-18 16:46:47 +00:00
c . Fatalf ( "Container Maximum Retry Count is %s, expected %s" , MaximumRetryCount , "3" )
2015-03-17 05:19:59 +00:00
}
2015-01-13 23:19:12 +00:00
}
2015-01-13 21:52:51 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunContainerWithWritableRootfs ( c * check . C ) {
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "--rm" , "busybox" , "touch" , "/file" )
2015-01-13 21:52:51 +00:00
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunContainerWithReadonlyRootfs ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --read-only
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-02-26 04:16:44 +00:00
2016-03-12 10:58:03 +00:00
testReadOnlyFile ( c , "/file" , "/etc/hosts" , "/etc/resolv.conf" , "/etc/hostname" , "/sys/kernel" , "/dev/.dont.touch.me" )
2015-05-02 15:29:00 +00:00
}
2015-06-02 16:16:43 +00:00
func ( s * DockerSuite ) TestPermissionsPtsReadonlyRootfs ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows due to use of Unix specific functionality, plus
// the use of --read-only which is not supported.
2015-09-18 17:41:12 +00:00
// --read-only + userns has remount issues
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-06-02 16:16:43 +00:00
// Ensure we have not broken writing /dev/pts
out , status := dockerCmd ( c , "run" , "--read-only" , "--rm" , "busybox" , "mount" )
if status != 0 {
c . Fatal ( "Could not obtain mounts when checking /dev/pts mntpnt." )
}
expected := "type devpts (rw,"
if ! strings . Contains ( string ( out ) , expected ) {
c . Fatalf ( "expected output to contain %s but contains %s" , expected , out )
}
}
2016-03-12 10:58:03 +00:00
func testReadOnlyFile ( c * check . C , filenames ... string ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --read-only
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2016-03-12 10:58:03 +00:00
touch := "touch " + strings . Join ( filenames , " " )
out , _ , err := dockerCmdWithError ( "run" , "--read-only" , "--rm" , "busybox" , "sh" , "-c" , touch )
c . Assert ( err , checker . NotNil )
2015-05-02 15:29:00 +00:00
2016-03-12 10:58:03 +00:00
for _ , f := range filenames {
expected := "touch: " + f + ": Read-only file system"
c . Assert ( out , checker . Contains , expected )
2015-01-13 21:52:51 +00:00
}
2015-06-02 16:16:43 +00:00
2016-03-12 10:58:03 +00:00
out , _ , err = dockerCmdWithError ( "run" , "--read-only" , "--privileged" , "--rm" , "busybox" , "sh" , "-c" , touch )
c . Assert ( err , checker . NotNil )
for _ , f := range filenames {
expected := "touch: " + f + ": Read-only file system"
c . Assert ( out , checker . Contains , expected )
2015-01-13 21:52:51 +00:00
}
}
2014-12-18 14:57:36 +00:00
2015-05-02 15:29:00 +00:00
func ( s * DockerSuite ) TestRunContainerWithReadonlyEtcHostsAndLinkedContainer ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --link
2015-09-18 17:41:12 +00:00
// --read-only + userns has remount issues
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-05-02 15:29:00 +00:00
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "--name" , "test-etc-hosts-ro-linked" , "busybox" , "top" )
2015-05-02 15:29:00 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--read-only" , "--link" , "test-etc-hosts-ro-linked:testlinked" , "busybox" , "cat" , "/etc/hosts" )
2015-05-02 15:29:00 +00:00
if ! strings . Contains ( string ( out ) , "testlinked" ) {
c . Fatal ( "Expected /etc/hosts to be updated even if --read-only enabled" )
}
}
func ( s * DockerSuite ) TestRunContainerWithReadonlyRootfsWithDnsFlag ( c * check . C ) {
2015-09-18 17:41:12 +00:00
// Not applicable on Windows which does not support either --read-only or --dns.
// --read-only + userns has remount issues
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-05-02 15:29:00 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--read-only" , "--dns" , "1.1.1.1" , "busybox" , "/bin/cat" , "/etc/resolv.conf" )
2015-05-02 15:29:00 +00:00
if ! strings . Contains ( string ( out ) , "1.1.1.1" ) {
c . Fatal ( "Expected /etc/resolv.conf to be updated even if --read-only enabled and --dns flag used" )
}
}
func ( s * DockerSuite ) TestRunContainerWithReadonlyRootfsWithAddHostFlag ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --read-only
2015-09-18 17:41:12 +00:00
// --read-only + userns has remount issues
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-05-02 15:29:00 +00:00
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--read-only" , "--add-host" , "testreadonly:127.0.0.1" , "busybox" , "/bin/cat" , "/etc/hosts" )
2015-05-02 15:29:00 +00:00
if ! strings . Contains ( string ( out ) , "testreadonly" ) {
c . Fatal ( "Expected /etc/hosts to be updated even if --read-only enabled and --add-host flag used" )
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunVolumesFromRestartAfterRemoved ( c * check . C ) {
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2016-05-03 17:52:09 +00:00
runSleepingContainer ( c , "--name=voltest" , "-v" , prefix + "/foo" )
runSleepingContainer ( c , "--name=restarter" , "--volumes-from" , "voltest" )
2014-12-18 14:57:36 +00:00
// Remove the main volume container and restart the consuming container
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "rm" , "-f" , "voltest" )
2014-12-18 14:57:36 +00:00
// This should not fail since the volumes-from were already applied
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "restart" , "restarter" )
2014-12-18 14:57:36 +00:00
}
2015-03-22 18:09:12 +00:00
// run container with --rm should remove container if exit code != 0
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunContainerWithRmFlagExitCodeNotEqualToZero ( c * check . C ) {
2015-04-08 23:20:42 +00:00
name := "flowers"
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--rm" , "busybox" , "ls" , "/notexists" )
2015-03-22 18:09:12 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected docker run to fail" , out , err )
2015-03-22 18:09:12 +00:00
}
out , err = getAllContainers ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( out , err )
2015-03-22 18:09:12 +00:00
}
if out != "" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected not to have containers" , out )
2015-03-22 18:09:12 +00:00
}
}
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunContainerWithRmFlagCannotStartContainer ( c * check . C ) {
2015-04-08 23:20:42 +00:00
name := "sparkles"
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--rm" , "busybox" , "commandNotFound" )
2015-03-22 18:09:12 +00:00
if err == nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected docker run to fail" , out , err )
2015-03-22 18:09:12 +00:00
}
out , err = getAllContainers ( )
if err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( out , err )
2015-03-22 18:09:12 +00:00
}
if out != "" {
2015-04-18 16:46:47 +00:00
c . Fatal ( "Expected not to have containers" , out )
2015-03-22 18:09:12 +00:00
}
}
2015-04-10 18:04:30 +00:00
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestRunPidHostWithChildIsKillable ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-04-10 18:04:30 +00:00
name := "ibuildthecloud"
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "--pid=host" , "--name" , name , "busybox" , "sh" , "-c" , "sleep 30; echo hi" )
2015-08-11 07:41:11 +00:00
c . Assert ( waitRun ( name ) , check . IsNil )
2015-04-10 18:04:30 +00:00
errchan := make ( chan error )
go func ( ) {
2015-07-27 18:13:25 +00:00
if out , _ , err := dockerCmdWithError ( "kill" , name ) ; err != nil {
2015-04-10 18:04:30 +00:00
errchan <- fmt . Errorf ( "%v:\n%s" , err , out )
}
close ( errchan )
} ( )
select {
case err := <- errchan :
2015-04-27 17:29:48 +00:00
c . Assert ( err , check . IsNil )
2015-04-10 18:04:30 +00:00
case <- time . After ( 5 * time . Second ) :
2015-04-18 16:46:47 +00:00
c . Fatal ( "Kill container timed out" )
2015-04-10 18:04:30 +00:00
}
}
2015-04-20 18:58:24 +00:00
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestRunWithTooSmallMemoryLimit ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. This may be possible to enable once Windows supports
// memory limits on containers
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-04-20 18:58:24 +00:00
// this memory limit is 1 byte less than the min, which is 4MB
// https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-m" , "4194303" , "busybox" )
2015-04-20 18:58:24 +00:00
if err == nil || ! strings . Contains ( out , "Minimum memory limit allowed is 4MB" ) {
2015-05-07 22:02:08 +00:00
c . Fatalf ( "expected run to fail when using too low a memory limit: %q" , out )
2015-04-20 18:58:24 +00:00
}
}
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestRunWriteToProcAsound ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
_ , code , err := dockerCmdWithError ( "run" , "busybox" , "sh" , "-c" , "echo 111 >> /proc/asound/version" )
2015-04-20 18:58:24 +00:00
if err == nil || code == 0 {
2015-05-07 22:02:08 +00:00
c . Fatal ( "standard container should not be able to write to /proc/asound" )
2015-04-20 18:58:24 +00:00
}
}
2015-04-20 20:54:49 +00:00
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestRunReadProcTimer ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
out , code , err := dockerCmdWithError ( "run" , "busybox" , "cat" , "/proc/timer_stats" )
2015-07-28 18:48:18 +00:00
if code != 0 {
return
}
if err != nil {
2015-05-07 22:02:08 +00:00
c . Fatal ( err )
2015-04-29 18:20:31 +00:00
}
if strings . Trim ( out , "\n " ) != "" {
2015-05-07 22:02:08 +00:00
c . Fatalf ( "expected to receive no output from /proc/timer_stats but received %q" , out )
2015-04-20 20:54:49 +00:00
}
}
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestRunReadProcLatency ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-04-29 18:20:31 +00:00
// some kernels don't have this configured so skip the test if this file is not found
// on the host running the tests.
if _ , err := os . Stat ( "/proc/latency_stats" ) ; err != nil {
2016-02-22 19:22:20 +00:00
c . Skip ( "kernel doesn't have latency_stats configured" )
2015-04-29 18:20:31 +00:00
return
}
2015-07-27 18:13:25 +00:00
out , code , err := dockerCmdWithError ( "run" , "busybox" , "cat" , "/proc/latency_stats" )
2015-07-28 18:48:18 +00:00
if code != 0 {
return
}
if err != nil {
2015-05-07 22:02:08 +00:00
c . Fatal ( err )
2015-04-29 18:20:31 +00:00
}
if strings . Trim ( out , "\n " ) != "" {
2015-05-07 22:02:08 +00:00
c . Fatalf ( "expected to receive no output from /proc/latency_stats but received %q" , out )
2015-04-20 20:54:49 +00:00
}
}
2015-04-22 00:31:05 +00:00
2015-07-28 18:48:18 +00:00
func ( s * DockerSuite ) TestRunReadFilteredProc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-09-18 17:41:12 +00:00
testRequires ( c , Apparmor , DaemonIsLinux , NotUserNamespace )
2015-07-28 18:48:18 +00:00
testReadPaths := [ ] string {
"/proc/latency_stats" ,
"/proc/timer_stats" ,
"/proc/kcore" ,
}
for i , filePath := range testReadPaths {
name := fmt . Sprintf ( "procsieve-%d" , i )
shellCmd := fmt . Sprintf ( "exec 3<%s" , filePath )
2016-03-15 22:34:29 +00:00
out , exitCode , err := dockerCmdWithError ( "run" , "--privileged" , "--security-opt" , "apparmor=docker-default" , "--name" , name , "busybox" , "sh" , "-c" , shellCmd )
2015-07-29 20:57:14 +00:00
if exitCode != 0 {
return
}
if err != nil {
2015-07-28 18:48:18 +00:00
c . Fatalf ( "Open FD for read should have failed with permission denied, got: %s, %v" , out , err )
}
}
}
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestMountIntoProc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-27 18:13:25 +00:00
_ , code , err := dockerCmdWithError ( "run" , "-v" , "/proc//sys" , "busybox" , "true" )
2015-04-22 00:31:05 +00:00
if err == nil || code == 0 {
2015-05-07 22:02:08 +00:00
c . Fatal ( "container should not be able to mount into /proc" )
2015-04-22 00:31:05 +00:00
}
}
2015-05-07 22:02:08 +00:00
func ( s * DockerSuite ) TestMountIntoSys ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux )
2015-11-05 00:40:58 +00:00
testRequires ( c , NotUserNamespace )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-v" , "/sys/fs/cgroup" , "busybox" , "true" )
2015-04-22 00:31:05 +00:00
}
2015-05-20 16:35:24 +00:00
2015-05-04 20:20:01 +00:00
func ( s * DockerSuite ) TestRunUnshareProc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-17 19:49:31 +00:00
testRequires ( c , Apparmor , DaemonIsLinux , NotUserNamespace )
2015-05-04 20:20:01 +00:00
2016-03-04 05:15:41 +00:00
// In this test goroutines are used to run test cases in parallel to prevent the test from taking a long time to run.
errChan := make ( chan error )
2015-05-04 20:20:01 +00:00
2016-03-04 05:15:41 +00:00
go func ( ) {
name := "acidburn"
2016-03-15 22:34:29 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--security-opt" , "seccomp=unconfined" , "debian:jessie" , "unshare" , "-p" , "-m" , "-f" , "-r" , "--mount-proc=/proc" , "mount" )
2016-03-04 05:15:41 +00:00
if err == nil ||
! ( strings . Contains ( strings . ToLower ( out ) , "permission denied" ) ||
strings . Contains ( strings . ToLower ( out ) , "operation not permitted" ) ) {
errChan <- fmt . Errorf ( "unshare with --mount-proc should have failed with 'permission denied' or 'operation not permitted', got: %s, %v" , out , err )
} else {
errChan <- nil
}
} ( )
go func ( ) {
name := "cereal"
2016-03-15 22:34:29 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--security-opt" , "seccomp=unconfined" , "debian:jessie" , "unshare" , "-p" , "-m" , "-f" , "-r" , "mount" , "-t" , "proc" , "none" , "/proc" )
2016-03-04 05:15:41 +00:00
if err == nil ||
! ( strings . Contains ( strings . ToLower ( out ) , "mount: cannot mount none" ) ||
2016-03-18 18:50:19 +00:00
strings . Contains ( strings . ToLower ( out ) , "permission denied" ) ||
strings . Contains ( strings . ToLower ( out ) , "operation not permitted" ) ) {
2016-03-04 05:15:41 +00:00
errChan <- fmt . Errorf ( "unshare and mount of /proc should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v" , out , err )
} else {
errChan <- nil
}
} ( )
2015-05-28 18:55:22 +00:00
/* Ensure still fails if running privileged with the default policy */
2016-03-04 05:15:41 +00:00
go func ( ) {
name := "crashoverride"
2016-03-15 22:34:29 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--privileged" , "--security-opt" , "seccomp=unconfined" , "--security-opt" , "apparmor=docker-default" , "--name" , name , "debian:jessie" , "unshare" , "-p" , "-m" , "-f" , "-r" , "mount" , "-t" , "proc" , "none" , "/proc" )
2016-03-04 05:15:41 +00:00
if err == nil ||
! ( strings . Contains ( strings . ToLower ( out ) , "mount: cannot mount none" ) ||
2016-03-18 18:50:19 +00:00
strings . Contains ( strings . ToLower ( out ) , "permission denied" ) ||
strings . Contains ( strings . ToLower ( out ) , "operation not permitted" ) ) {
2016-03-04 05:15:41 +00:00
errChan <- fmt . Errorf ( "privileged unshare with apparmor should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v" , out , err )
} else {
errChan <- nil
}
} ( )
for i := 0 ; i < 3 ; i ++ {
err := <- errChan
if err != nil {
c . Fatal ( err )
}
2015-05-04 20:20:01 +00:00
}
}
2015-05-17 18:57:17 +00:00
func ( s * DockerSuite ) TestRunPublishPort ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This may be possible once Windows moves to libnetwork and CNM
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
dockerCmd ( c , "run" , "-d" , "--name" , "test" , "--expose" , "8080" , "busybox" , "top" )
out , _ := dockerCmd ( c , "port" , "test" )
2015-05-17 18:57:17 +00:00
out = strings . Trim ( out , "\r\n" )
if out != "" {
c . Fatalf ( "run without --publish-all should not publish port, out should be nil, but got: %s" , out )
}
}
2015-05-29 15:29:56 +00:00
// Issue #10184.
func ( s * DockerSuite ) TestDevicePermissions ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-05-29 15:29:56 +00:00
const permissions = "crw-rw-rw-"
out , status := dockerCmd ( c , "run" , "--device" , "/dev/fuse:/dev/fuse:mrw" , "busybox:latest" , "ls" , "-l" , "/dev/fuse" )
if status != 0 {
c . Fatalf ( "expected status 0, got %d" , status )
}
if ! strings . HasPrefix ( out , permissions ) {
c . Fatalf ( "output should begin with %q, got %q" , permissions , out )
}
}
2015-05-27 07:31:06 +00:00
2015-07-03 15:25:52 +00:00
func ( s * DockerSuite ) TestRunCapAddCHOWN ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-19 20:47:47 +00:00
out , _ := dockerCmd ( c , "run" , "--cap-drop=ALL" , "--cap-add=CHOWN" , "busybox" , "sh" , "-c" , "adduser -D -H newuser && chown newuser /home && echo ok" )
2015-07-03 15:25:52 +00:00
if actual := strings . Trim ( out , "\r\n" ) ; actual != "ok" {
c . Fatalf ( "expected output ok received %s" , actual )
}
}
2015-07-09 14:40:39 +00:00
// https://github.com/docker/docker/pull/14498
func ( s * DockerSuite ) TestVolumeFromMixedRWOptions ( c * check . C ) {
2016-02-03 14:16:00 +00:00
prefix , slash := getPrefixAndSlashFromDaemonPlatform ( )
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "--name" , "parent" , "-v" , prefix + "/test" , "busybox" , "true" )
2016-05-25 20:56:36 +00:00
// TODO Windows: Temporary check - remove once TP5 support is dropped
if daemonPlatform != "windows" || windowsDaemonKV >= 14350 {
2015-09-23 23:04:51 +00:00
dockerCmd ( c , "run" , "--volumes-from" , "parent:ro" , "--name" , "test-volumes-1" , "busybox" , "true" )
}
2015-07-09 14:40:39 +00:00
dockerCmd ( c , "run" , "--volumes-from" , "parent:rw" , "--name" , "test-volumes-2" , "busybox" , "true" )
2015-09-23 23:04:51 +00:00
if daemonPlatform != "windows" {
mRO , err := inspectMountPoint ( "test-volumes-1" , prefix + slash + "test" )
2016-01-28 14:19:25 +00:00
c . Assert ( err , checker . IsNil , check . Commentf ( "failed to inspect mount point" ) )
2015-09-23 23:04:51 +00:00
if mRO . RW {
c . Fatalf ( "Expected RO volume was RW" )
}
2015-07-09 14:40:39 +00:00
}
2015-06-03 19:21:38 +00:00
2015-09-23 23:04:51 +00:00
mRW , err := inspectMountPoint ( "test-volumes-2" , prefix + slash + "test" )
2016-01-28 14:19:25 +00:00
c . Assert ( err , checker . IsNil , check . Commentf ( "failed to inspect mount point" ) )
2015-06-03 19:21:38 +00:00
if ! mRW . RW {
2015-07-09 14:40:39 +00:00
c . Fatalf ( "Expected RW volume was RO" )
}
}
2015-05-12 03:00:05 +00:00
func ( s * DockerSuite ) TestRunWriteFilteredProc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , Apparmor , DaemonIsLinux , NotUserNamespace )
2015-05-12 03:00:05 +00:00
testWritePaths := [ ] string {
/ * modprobe and core_pattern should both be denied by generic
* policy of denials for / proc / sys / kernel . These files have been
* picked to be checked as they are particularly sensitive to writes * /
"/proc/sys/kernel/modprobe" ,
"/proc/sys/kernel/core_pattern" ,
"/proc/sysrq-trigger" ,
2015-07-28 18:48:18 +00:00
"/proc/kcore" ,
2015-05-12 03:00:05 +00:00
}
for i , filePath := range testWritePaths {
name := fmt . Sprintf ( "writeprocsieve-%d" , i )
shellCmd := fmt . Sprintf ( "exec 3>%s" , filePath )
2016-03-15 22:34:29 +00:00
out , code , err := dockerCmdWithError ( "run" , "--privileged" , "--security-opt" , "apparmor=docker-default" , "--name" , name , "busybox" , "sh" , "-c" , shellCmd )
2015-07-29 20:57:14 +00:00
if code != 0 {
return
}
if err != nil {
2015-05-12 03:00:05 +00:00
c . Fatalf ( "Open FD for write should have failed with permission denied, got: %s, %v" , out , err )
}
}
}
2015-07-14 00:54:00 +00:00
func ( s * DockerSuite ) TestRunNetworkFilesBindMount ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-07-14 00:54:00 +00:00
2015-07-24 18:23:20 +00:00
expected := "test123"
2015-07-14 00:54:00 +00:00
2015-07-24 18:23:20 +00:00
filename := createTmpFile ( c , expected )
2015-07-14 00:54:00 +00:00
defer os . Remove ( filename )
2015-07-24 18:23:20 +00:00
nwfiles := [ ] string { "/etc/resolv.conf" , "/etc/hosts" , "/etc/hostname" }
2015-07-14 00:54:00 +00:00
2015-07-24 18:23:20 +00:00
for i := range nwfiles {
actual , _ := dockerCmd ( c , "run" , "-v" , filename + ":" + nwfiles [ i ] , "busybox" , "cat" , nwfiles [ i ] )
if actual != expected {
c . Fatalf ( "expected %s be: %q, but was: %q" , nwfiles [ i ] , expected , actual )
}
}
}
2015-07-14 00:54:00 +00:00
2015-07-24 18:23:20 +00:00
func ( s * DockerSuite ) TestRunNetworkFilesBindMountRO ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , SameHostDaemon , DaemonIsLinux )
2015-07-24 18:23:20 +00:00
filename := createTmpFile ( c , "test123" )
defer os . Remove ( filename )
nwfiles := [ ] string { "/etc/resolv.conf" , "/etc/hosts" , "/etc/hostname" }
for i := range nwfiles {
_ , exitCode , err := dockerCmdWithError ( "run" , "-v" , filename + ":" + nwfiles [ i ] + ":ro" , "busybox" , "touch" , nwfiles [ i ] )
if err == nil || exitCode == 0 {
c . Fatalf ( "run should fail because bind mount of %s is ro: exit code %d" , nwfiles [ i ] , exitCode )
}
}
}
func ( s * DockerSuite ) TestRunNetworkFilesBindMountROFilesystem ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-09-18 17:41:12 +00:00
// --read-only + userns has remount issues
testRequires ( c , SameHostDaemon , DaemonIsLinux , NotUserNamespace )
2015-07-24 18:23:20 +00:00
filename := createTmpFile ( c , "test123" )
defer os . Remove ( filename )
nwfiles := [ ] string { "/etc/resolv.conf" , "/etc/hosts" , "/etc/hostname" }
for i := range nwfiles {
_ , exitCode := dockerCmd ( c , "run" , "-v" , filename + ":" + nwfiles [ i ] , "--read-only" , "busybox" , "touch" , nwfiles [ i ] )
if exitCode != 0 {
c . Fatalf ( "run should not fail because %s is mounted writable on read-only root filesystem: exit code %d" , nwfiles [ i ] , exitCode )
}
}
for i := range nwfiles {
_ , exitCode , err := dockerCmdWithError ( "run" , "-v" , filename + ":" + nwfiles [ i ] + ":ro" , "--read-only" , "busybox" , "touch" , nwfiles [ i ] )
if err == nil || exitCode == 0 {
c . Fatalf ( "run should fail because %s is mounted read-only on read-only root filesystem: exit code %d" , nwfiles [ i ] , exitCode )
}
2015-07-14 00:54:00 +00:00
}
}
2015-07-22 16:14:48 +00:00
func ( s * DockerTrustSuite ) TestTrustedRun ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Windows does not support this functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-23 12:12:36 +00:00
repoName := s . setupTrustedImage ( c , "trusted-run" )
2015-07-22 16:14:48 +00:00
// Try run
runCmd := exec . Command ( dockerBinary , "run" , repoName )
s . trustedCmd ( runCmd )
2015-07-23 12:12:36 +00:00
out , _ , err := runCommandWithOutput ( runCmd )
2015-07-22 16:14:48 +00:00
if err != nil {
c . Fatalf ( "Error running trusted run: %s\n%s\n" , err , out )
}
if ! strings . Contains ( string ( out ) , "Tagging" ) {
c . Fatalf ( "Missing expected output on trusted push:\n%s" , out )
}
dockerCmd ( c , "rmi" , repoName )
// Try untrusted run to ensure we pushed the tag to the registry
2015-07-24 08:59:42 +00:00
runCmd = exec . Command ( dockerBinary , "run" , "--disable-content-trust=true" , repoName )
2015-07-22 16:14:48 +00:00
s . trustedCmd ( runCmd )
out , _ , err = runCommandWithOutput ( runCmd )
if err != nil {
c . Fatalf ( "Error running trusted run: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Status: Downloaded" ) {
2015-07-24 08:59:42 +00:00
c . Fatalf ( "Missing expected output on trusted run with --disable-content-trust:\n%s" , out )
2015-07-22 16:14:48 +00:00
}
}
func ( s * DockerTrustSuite ) TestUntrustedRun ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Windows does not support this functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2016-01-27 01:52:36 +00:00
repoName := fmt . Sprintf ( "%v/dockercliuntrusted/runtest:latest" , privateRegistryURL )
2015-07-22 16:14:48 +00:00
// tag the image and upload it to the private registry
dockerCmd ( c , "tag" , "busybox" , repoName )
dockerCmd ( c , "push" , repoName )
dockerCmd ( c , "rmi" , repoName )
// Try trusted run on untrusted tag
runCmd := exec . Command ( dockerBinary , "run" , repoName )
s . trustedCmd ( runCmd )
out , _ , err := runCommandWithOutput ( runCmd )
if err == nil {
c . Fatalf ( "Error expected when running trusted run with:\n%s" , out )
}
2016-01-08 02:43:01 +00:00
if ! strings . Contains ( string ( out ) , "does not have trust data for" ) {
2015-07-22 16:14:48 +00:00
c . Fatalf ( "Missing expected output on trusted run:\n%s" , out )
}
}
func ( s * DockerTrustSuite ) TestRunWhenCertExpired ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Windows does not support this functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-29 19:09:40 +00:00
c . Skip ( "Currently changes system time, causing instability" )
2015-07-23 12:12:36 +00:00
repoName := s . setupTrustedImage ( c , "trusted-run-expired" )
2015-07-22 16:14:48 +00:00
// Certificates have 10 years of expiration
elevenYearsFromNow := time . Now ( ) . Add ( time . Hour * 24 * 365 * 11 )
runAtDifferentDate ( elevenYearsFromNow , func ( ) {
// Try run
runCmd := exec . Command ( dockerBinary , "run" , repoName )
s . trustedCmd ( runCmd )
2015-07-23 12:12:36 +00:00
out , _ , err := runCommandWithOutput ( runCmd )
2015-07-22 16:14:48 +00:00
if err == nil {
c . Fatalf ( "Error running trusted run in the distant future: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "could not validate the path to a trusted root" ) {
c . Fatalf ( "Missing expected output on trusted run in the distant future:\n%s" , out )
}
} )
runAtDifferentDate ( elevenYearsFromNow , func ( ) {
// Try run
2015-07-24 08:59:42 +00:00
runCmd := exec . Command ( dockerBinary , "run" , "--disable-content-trust" , repoName )
2015-07-22 16:14:48 +00:00
s . trustedCmd ( runCmd )
2015-07-23 12:12:36 +00:00
out , _ , err := runCommandWithOutput ( runCmd )
2015-07-22 16:14:48 +00:00
if err != nil {
c . Fatalf ( "Error running untrusted run in the distant future: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Status: Downloaded" ) {
c . Fatalf ( "Missing expected output on untrusted run in the distant future:\n%s" , out )
}
} )
}
2015-07-22 23:10:25 +00:00
func ( s * DockerTrustSuite ) TestTrustedRunFromBadTrustServer ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Windows does not support this functionality
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-07-22 23:10:25 +00:00
repoName := fmt . Sprintf ( "%v/dockerclievilrun/trusted:latest" , privateRegistryURL )
2016-02-25 21:40:00 +00:00
evilLocalConfigDir , err := ioutil . TempDir ( "" , "evilrun-local-config-dir" )
2015-07-22 23:10:25 +00:00
if err != nil {
c . Fatalf ( "Failed to create local temp dir" )
}
// tag the image and upload it to the private registry
dockerCmd ( c , "tag" , "busybox" , repoName )
pushCmd := exec . Command ( dockerBinary , "push" , repoName )
s . trustedCmd ( pushCmd )
out , _ , err := runCommandWithOutput ( pushCmd )
if err != nil {
c . Fatalf ( "Error running trusted push: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Signing and pushing trust metadata" ) {
c . Fatalf ( "Missing expected output on trusted push:\n%s" , out )
}
dockerCmd ( c , "rmi" , repoName )
// Try run
runCmd := exec . Command ( dockerBinary , "run" , repoName )
s . trustedCmd ( runCmd )
out , _ , err = runCommandWithOutput ( runCmd )
if err != nil {
c . Fatalf ( "Error running trusted run: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Tagging" ) {
c . Fatalf ( "Missing expected output on trusted push:\n%s" , out )
}
dockerCmd ( c , "rmi" , repoName )
// Kill the notary server, start a new "evil" one.
s . not . Close ( )
s . not , err = newTestNotary ( c )
if err != nil {
c . Fatalf ( "Restarting notary server failed." )
}
// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
// tag an image and upload it to the private registry
dockerCmd ( c , "--config" , evilLocalConfigDir , "tag" , "busybox" , repoName )
// Push up to the new server
pushCmd = exec . Command ( dockerBinary , "--config" , evilLocalConfigDir , "push" , repoName )
s . trustedCmd ( pushCmd )
out , _ , err = runCommandWithOutput ( pushCmd )
if err != nil {
c . Fatalf ( "Error running trusted push: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Signing and pushing trust metadata" ) {
c . Fatalf ( "Missing expected output on trusted push:\n%s" , out )
}
2016-02-25 21:40:00 +00:00
// Now, try running with the original client from this new trust server. This should fallback to our cached timestamp and metadata.
2015-07-22 23:10:25 +00:00
runCmd = exec . Command ( dockerBinary , "run" , repoName )
s . trustedCmd ( runCmd )
out , _ , err = runCommandWithOutput ( runCmd )
2016-02-25 21:40:00 +00:00
if err != nil {
c . Fatalf ( "Error falling back to cached trust data: %s\n%s" , err , out )
}
if ! strings . Contains ( string ( out ) , "Error while downloading remote metadata, using cached timestamp" ) {
2015-07-22 23:10:25 +00:00
c . Fatalf ( "Missing expected output on trusted push:\n%s" , out )
}
}
2015-07-30 16:54:21 +00:00
func ( s * DockerSuite ) TestPtraceContainerProcsFromHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
testRequires ( c , DaemonIsLinux , SameHostDaemon )
2015-07-30 16:54:21 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
id := strings . TrimSpace ( out )
2015-08-11 07:41:11 +00:00
c . Assert ( waitRun ( id ) , check . IsNil )
2016-01-28 14:19:25 +00:00
pid1 := inspectField ( c , id , "State.Pid" )
2015-07-30 16:54:21 +00:00
2016-01-28 14:19:25 +00:00
_ , err := os . Readlink ( fmt . Sprintf ( "/proc/%s/ns/net" , pid1 ) )
2015-07-30 16:54:21 +00:00
if err != nil {
c . Fatal ( err )
}
}
func ( s * DockerSuite ) TestAppArmorDeniesPtrace ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , SameHostDaemon , Apparmor , DaemonIsLinux , NotGCCGO )
2015-07-30 16:54:21 +00:00
// Run through 'sh' so we are NOT pid 1. Pid 1 may be able to trace
// itself, but pid>1 should not be able to trace pid1.
2015-10-10 04:35:09 +00:00
_ , exitCode , _ := dockerCmdWithError ( "run" , "busybox" , "sh" , "-c" , "sh -c readlink /proc/1/ns/net" )
2015-07-30 16:54:21 +00:00
if exitCode == 0 {
c . Fatal ( "ptrace was not successfully restricted by AppArmor" )
}
}
func ( s * DockerSuite ) TestAppArmorTraceSelf ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
testRequires ( c , DaemonIsLinux , SameHostDaemon , Apparmor )
2015-07-30 16:54:21 +00:00
_ , exitCode , _ := dockerCmdWithError ( "run" , "busybox" , "readlink" , "/proc/1/ns/net" )
if exitCode != 0 {
c . Fatal ( "ptrace of self failed." )
}
}
2015-07-31 04:53:46 +00:00
2015-08-13 16:35:03 +00:00
func ( s * DockerSuite ) TestAppArmorDeniesChmodProc ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-17 19:49:31 +00:00
testRequires ( c , SameHostDaemon , Apparmor , DaemonIsLinux , NotUserNamespace )
2015-08-13 16:35:03 +00:00
_ , exitCode , _ := dockerCmdWithError ( "run" , "busybox" , "chmod" , "744" , "/proc/cpuinfo" )
if exitCode == 0 {
// If our test failed, attempt to repair the host system...
_ , exitCode , _ := dockerCmdWithError ( "run" , "busybox" , "chmod" , "444" , "/proc/cpuinfo" )
if exitCode == 0 {
c . Fatal ( "AppArmor was unsuccessful in prohibiting chmod of /proc/* files." )
}
}
}
2015-07-31 04:53:46 +00:00
func ( s * DockerSuite ) TestRunCapAddSYSTIME ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-08-01 02:07:40 +00:00
2015-07-31 04:53:46 +00:00
dockerCmd ( c , "run" , "--cap-drop=ALL" , "--cap-add=SYS_TIME" , "busybox" , "sh" , "-c" , "grep ^CapEff /proc/self/status | sed 's/^CapEff:\t//' | grep ^0000000002000000$" )
}
2015-08-11 14:21:52 +00:00
// run create container failed should clean up the container
func ( s * DockerSuite ) TestRunCreateContainerFailedCleanUp ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows. This may be possible to enable once link is supported
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-08-11 14:21:52 +00:00
name := "unique_name"
_ , _ , err := dockerCmdWithError ( "run" , "--name" , name , "--link" , "nothing:nothing" , "busybox" )
2015-09-25 10:19:17 +00:00
c . Assert ( err , check . NotNil , check . Commentf ( "Expected docker run to fail!" ) )
2015-08-11 14:21:52 +00:00
2016-01-28 14:19:25 +00:00
containerID , err := inspectFieldWithError ( name , "Id" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Expected not to have this container: %s!" , containerID ) )
2015-08-11 14:21:52 +00:00
c . Assert ( containerID , check . Equals , "" , check . Commentf ( "Expected not to have this container: %s!" , containerID ) )
}
2015-06-12 13:25:32 +00:00
func ( s * DockerSuite ) TestRunNamedVolume ( c * check . C ) {
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2016-02-01 00:30:15 +00:00
dockerCmd ( c , "run" , "--name=test" , "-v" , "testing:" + prefix + "/foo" , "busybox" , "sh" , "-c" , "echo hello > " + prefix + "/foo/bar" )
2015-06-12 13:25:32 +00:00
2015-09-23 23:04:51 +00:00
out , _ := dockerCmd ( c , "run" , "--volumes-from" , "test" , "busybox" , "sh" , "-c" , "cat " + prefix + "/foo/bar" )
2015-06-12 13:25:32 +00:00
c . Assert ( strings . TrimSpace ( out ) , check . Equals , "hello" )
2016-02-01 00:30:15 +00:00
out , _ = dockerCmd ( c , "run" , "-v" , "testing:" + prefix + "/foo" , "busybox" , "sh" , "-c" , "cat " + prefix + "/foo/bar" )
2015-06-12 13:25:32 +00:00
c . Assert ( strings . TrimSpace ( out ) , check . Equals , "hello" )
}
2015-09-01 16:42:19 +00:00
func ( s * DockerSuite ) TestRunWithUlimits ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
out , _ := dockerCmd ( c , "run" , "--name=testulimits" , "--ulimit" , "nofile=42" , "busybox" , "/bin/sh" , "-c" , "ulimit -n" )
ul := strings . TrimSpace ( out )
if ul != "42" {
c . Fatalf ( "expected `ulimit -n` to be 42, got %s" , ul )
}
}
func ( s * DockerSuite ) TestRunContainerWithCgroupParent ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
cgroupParent := "test"
name := "cgroup-test"
out , _ , err := dockerCmdWithError ( "run" , "--cgroup-parent" , cgroupParent , "--name" , name , "busybox" , "cat" , "/proc/self/cgroup" )
if err != nil {
c . Fatalf ( "unexpected failure when running container with --cgroup-parent option - %s\n%v" , string ( out ) , err )
}
cgroupPaths := parseCgroupPaths ( string ( out ) )
if len ( cgroupPaths ) == 0 {
c . Fatalf ( "unexpected output - %q" , string ( out ) )
}
id , err := getIDByName ( name )
c . Assert ( err , check . IsNil )
expectedCgroup := path . Join ( cgroupParent , id )
found := false
for _ , path := range cgroupPaths {
if strings . HasSuffix ( path , expectedCgroup ) {
found = true
break
}
}
if ! found {
c . Fatalf ( "unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v" , expectedCgroup , cgroupPaths )
}
}
func ( s * DockerSuite ) TestRunContainerWithCgroupParentAbsPath ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
cgroupParent := "/cgroup-parent/test"
name := "cgroup-test"
out , _ , err := dockerCmdWithError ( "run" , "--cgroup-parent" , cgroupParent , "--name" , name , "busybox" , "cat" , "/proc/self/cgroup" )
if err != nil {
c . Fatalf ( "unexpected failure when running container with --cgroup-parent option - %s\n%v" , string ( out ) , err )
}
cgroupPaths := parseCgroupPaths ( string ( out ) )
if len ( cgroupPaths ) == 0 {
c . Fatalf ( "unexpected output - %q" , string ( out ) )
}
id , err := getIDByName ( name )
c . Assert ( err , check . IsNil )
expectedCgroup := path . Join ( cgroupParent , id )
found := false
for _ , path := range cgroupPaths {
if strings . HasSuffix ( path , expectedCgroup ) {
found = true
break
}
}
if ! found {
c . Fatalf ( "unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v" , expectedCgroup , cgroupPaths )
2016-01-04 12:29:32 +00:00
}
}
// TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent doesn't cause Docker to crash or start modifying /.
func ( s * DockerSuite ) TestRunInvalidCgroupParent ( c * check . C ) {
// Not applicable on Windows as uses Unix specific functionality
testRequires ( c , DaemonIsLinux )
cgroupParent := "../../../../../../../../SHOULD_NOT_EXIST"
cleanCgroupParent := "SHOULD_NOT_EXIST"
name := "cgroup-invalid-test"
out , _ , err := dockerCmdWithError ( "run" , "--cgroup-parent" , cgroupParent , "--name" , name , "busybox" , "cat" , "/proc/self/cgroup" )
if err != nil {
// XXX: This may include a daemon crash.
c . Fatalf ( "unexpected failure when running container with --cgroup-parent option - %s\n%v" , string ( out ) , err )
}
// We expect "/SHOULD_NOT_EXIST" to not exist. If not, we have a security issue.
if _ , err := os . Stat ( "/SHOULD_NOT_EXIST" ) ; err == nil || ! os . IsNotExist ( err ) {
c . Fatalf ( "SECURITY: --cgroup-parent with ../../ relative paths cause files to be created in the host (this is bad) !!" )
}
cgroupPaths := parseCgroupPaths ( string ( out ) )
if len ( cgroupPaths ) == 0 {
c . Fatalf ( "unexpected output - %q" , string ( out ) )
}
id , err := getIDByName ( name )
c . Assert ( err , check . IsNil )
expectedCgroup := path . Join ( cleanCgroupParent , id )
found := false
for _ , path := range cgroupPaths {
if strings . HasSuffix ( path , expectedCgroup ) {
found = true
break
}
}
if ! found {
c . Fatalf ( "unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v" , expectedCgroup , cgroupPaths )
}
}
// TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent doesn't cause Docker to crash or start modifying /.
func ( s * DockerSuite ) TestRunAbsoluteInvalidCgroupParent ( c * check . C ) {
// Not applicable on Windows as uses Unix specific functionality
testRequires ( c , DaemonIsLinux )
cgroupParent := "/../../../../../../../../SHOULD_NOT_EXIST"
cleanCgroupParent := "/SHOULD_NOT_EXIST"
name := "cgroup-absolute-invalid-test"
out , _ , err := dockerCmdWithError ( "run" , "--cgroup-parent" , cgroupParent , "--name" , name , "busybox" , "cat" , "/proc/self/cgroup" )
if err != nil {
// XXX: This may include a daemon crash.
c . Fatalf ( "unexpected failure when running container with --cgroup-parent option - %s\n%v" , string ( out ) , err )
}
// We expect "/SHOULD_NOT_EXIST" to not exist. If not, we have a security issue.
if _ , err := os . Stat ( "/SHOULD_NOT_EXIST" ) ; err == nil || ! os . IsNotExist ( err ) {
c . Fatalf ( "SECURITY: --cgroup-parent with /../../ garbage paths cause files to be created in the host (this is bad) !!" )
}
cgroupPaths := parseCgroupPaths ( string ( out ) )
if len ( cgroupPaths ) == 0 {
c . Fatalf ( "unexpected output - %q" , string ( out ) )
}
id , err := getIDByName ( name )
c . Assert ( err , check . IsNil )
expectedCgroup := path . Join ( cleanCgroupParent , id )
found := false
for _ , path := range cgroupPaths {
if strings . HasSuffix ( path , expectedCgroup ) {
found = true
break
}
}
if ! found {
c . Fatalf ( "unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v" , expectedCgroup , cgroupPaths )
2015-09-01 16:42:19 +00:00
}
}
func ( s * DockerSuite ) TestRunContainerWithCgroupMountRO ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows as uses Unix specific functionality
2015-09-18 17:41:12 +00:00
// --read-only + userns has remount issues
2015-11-05 00:40:58 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-09-01 16:42:19 +00:00
filename := "/sys/fs/cgroup/devices/test123"
out , _ , err := dockerCmdWithError ( "run" , "busybox" , "touch" , filename )
if err == nil {
c . Fatal ( "expected cgroup mount point to be read-only, touch file should fail" )
}
expected := "Read-only file system"
if ! strings . Contains ( out , expected ) {
c . Fatalf ( "expected output from failure to contain %s but contains %s" , expected , out )
}
}
func ( s * DockerSuite ) TestRunContainerNetworkModeToSelf ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --net=container
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name=me" , "--net=container:me" , "busybox" , "true" )
if err == nil || ! strings . Contains ( out , "cannot join own network" ) {
2015-09-16 18:56:26 +00:00
c . Fatalf ( "using container net mode to self should result in an error\nerr: %q\nout: %s" , err , out )
2015-09-01 16:42:19 +00:00
}
}
func ( s * DockerSuite ) TestRunContainerNetModeWithDnsMacHosts ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --net=container
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
out , _ , err := dockerCmdWithError ( "run" , "-d" , "--name" , "parent" , "busybox" , "top" )
if err != nil {
c . Fatalf ( "failed to run container: %v, output: %q" , err , out )
}
out , _ , err = dockerCmdWithError ( "run" , "--dns" , "1.2.3.4" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictNetworkAndDNS . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with --dns should error out" )
}
out , _ , err = dockerCmdWithError ( "run" , "--mac-address" , "92:d0:c6:0a:29:33" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictContainerNetworkAndMac . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with --mac-address should error out" )
}
out , _ , err = dockerCmdWithError ( "run" , "--add-host" , "test:192.168.2.109" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictNetworkHosts . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with --add-host should error out" )
}
}
func ( s * DockerSuite ) TestRunContainerNetModeWithExposePort ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --net=container
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
dockerCmd ( c , "run" , "-d" , "--name" , "parent" , "busybox" , "top" )
out , _ , err := dockerCmdWithError ( "run" , "-p" , "5000:5000" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictNetworkPublishPorts . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with -p should error out" )
}
out , _ , err = dockerCmdWithError ( "run" , "-P" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictNetworkPublishPorts . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with -P should error out" )
}
out , _ , err = dockerCmdWithError ( "run" , "--expose" , "5000" , "--net=container:parent" , "busybox" )
2015-12-01 22:01:51 +00:00
if err == nil || ! strings . Contains ( out , runconfig . ErrConflictNetworkExposePorts . Error ( ) ) {
2015-09-01 16:42:19 +00:00
c . Fatalf ( "run --net=container with --expose should error out" )
}
}
func ( s * DockerSuite ) TestRunLinkToContainerNetMode ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable on Windows which does not support --net=container or --link
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
dockerCmd ( c , "run" , "--name" , "test" , "-d" , "busybox" , "top" )
dockerCmd ( c , "run" , "--name" , "parent" , "-d" , "--net=container:test" , "busybox" , "top" )
dockerCmd ( c , "run" , "-d" , "--link=parent:parent" , "busybox" , "top" )
dockerCmd ( c , "run" , "--name" , "child" , "-d" , "--net=container:parent" , "busybox" , "top" )
dockerCmd ( c , "run" , "-d" , "--link=child:child" , "busybox" , "top" )
}
func ( s * DockerSuite ) TestRunLoopbackOnlyExistsWhenNetworkingDisabled ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: This may be possible to convert.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-01 16:42:19 +00:00
out , _ := dockerCmd ( c , "run" , "--net=none" , "busybox" , "ip" , "-o" , "-4" , "a" , "show" , "up" )
var (
count = 0
parts = strings . Split ( out , "\n" )
)
for _ , l := range parts {
if l != "" {
count ++
}
}
if count != 1 {
c . Fatalf ( "Wrong interface count in container %d" , count )
}
if ! strings . HasPrefix ( out , "1: lo" ) {
c . Fatalf ( "Wrong interface in test container: expected [1: lo], got %s" , out )
}
}
// Issue #4681
func ( s * DockerSuite ) TestRunLoopbackWhenNetworkDisabled ( c * check . C ) {
2015-09-01 21:37:04 +00:00
if daemonPlatform == "windows" {
dockerCmd ( c , "run" , "--net=none" , WindowsBaseImage , "ping" , "-n" , "1" , "127.0.0.1" )
} else {
dockerCmd ( c , "run" , "--net=none" , "busybox" , "ping" , "-c" , "1" , "127.0.0.1" )
}
2015-09-01 16:42:19 +00:00
}
func ( s * DockerSuite ) TestRunModeNetContainerHostname ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Windows does not support --net=container
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux , ExecSupport )
2015-09-01 16:42:19 +00:00
dockerCmd ( c , "run" , "-i" , "-d" , "--name" , "parent" , "busybox" , "top" )
out , _ := dockerCmd ( c , "exec" , "parent" , "cat" , "/etc/hostname" )
out1 , _ := dockerCmd ( c , "run" , "--net=container:parent" , "busybox" , "cat" , "/etc/hostname" )
if out1 != out {
c . Fatal ( "containers with shared net namespace should have same hostname" )
}
}
func ( s * DockerSuite ) TestRunNetworkNotInitializedNoneMode ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// TODO Windows: Network settings are not currently propagated. This may
// be resolved in the future with the move to libnetwork and CNM.
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-08 21:00:25 +00:00
out , _ := dockerCmd ( c , "run" , "-d" , "--net=none" , "busybox" , "top" )
2015-09-01 16:42:19 +00:00
id := strings . TrimSpace ( out )
2016-01-28 14:19:25 +00:00
res := inspectField ( c , id , "NetworkSettings.Networks.none.IPAddress" )
2015-09-01 16:42:19 +00:00
if res != "" {
c . Fatalf ( "For 'none' mode network must not be initialized, but container got IP: %s" , res )
}
}
func ( s * DockerSuite ) TestTwoContainersInNetHost ( c * check . C ) {
2015-09-01 21:37:04 +00:00
// Not applicable as Windows does not support --net=host
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotUserNamespace )
2015-09-01 16:42:19 +00:00
dockerCmd ( c , "run" , "-d" , "--net=host" , "--name=first" , "busybox" , "top" )
dockerCmd ( c , "run" , "-d" , "--net=host" , "--name=second" , "busybox" , "top" )
dockerCmd ( c , "stop" , "first" )
dockerCmd ( c , "stop" , "second" )
}
2015-09-14 20:34:23 +00:00
2015-09-25 10:19:17 +00:00
func ( s * DockerSuite ) TestContainersInUserDefinedNetwork ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork" )
dockerCmd ( c , "run" , "-d" , "--net=testnetwork" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-t" , "--net=testnetwork" , "--name=second" , "busybox" , "ping" , "-c" , "1" , "first" )
}
func ( s * DockerSuite ) TestContainersInMultipleNetworks ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2015-09-25 10:19:17 +00:00
// Create 2 networks using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork2" )
// Run and connect containers to testnetwork1
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=second" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Check connectivity between containers in testnetwork2
dockerCmd ( c , "exec" , "first" , "ping" , "-c" , "1" , "second.testnetwork1" )
// Connect containers to testnetwork2
dockerCmd ( c , "network" , "connect" , "testnetwork2" , "first" )
dockerCmd ( c , "network" , "connect" , "testnetwork2" , "second" )
// Check connectivity between containers
dockerCmd ( c , "exec" , "second" , "ping" , "-c" , "1" , "first.testnetwork2" )
}
func ( s * DockerSuite ) TestContainersNetworkIsolation ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2015-09-25 10:19:17 +00:00
// Create 2 networks using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork2" )
2015-09-27 11:09:27 +00:00
// Run 1 container in testnetwork1 and another in testnetwork2
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--net=testnetwork2" , "--name=second" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Check Isolation between containers : ping must fail
_ , _ , err := dockerCmdWithError ( "exec" , "first" , "ping" , "-c" , "1" , "second" )
c . Assert ( err , check . NotNil )
// Connect first container to testnetwork2
dockerCmd ( c , "network" , "connect" , "testnetwork2" , "first" )
// ping must succeed now
_ , _ , err = dockerCmdWithError ( "exec" , "first" , "ping" , "-c" , "1" , "second" )
c . Assert ( err , check . IsNil )
// Disconnect first container from testnetwork2
dockerCmd ( c , "network" , "disconnect" , "testnetwork2" , "first" )
// ping must fail again
_ , _ , err = dockerCmdWithError ( "exec" , "first" , "ping" , "-c" , "1" , "second" )
c . Assert ( err , check . NotNil )
}
func ( s * DockerSuite ) TestNetworkRmWithActiveContainers ( c * check . C ) {
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-09-25 10:19:17 +00:00
// Create 2 networks using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
// Run and connect containers to testnetwork1
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=second" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Network delete with active containers must fail
_ , _ , err := dockerCmdWithError ( "network" , "rm" , "testnetwork1" )
c . Assert ( err , check . NotNil )
dockerCmd ( c , "stop" , "first" )
_ , _ , err = dockerCmdWithError ( "network" , "rm" , "testnetwork1" )
c . Assert ( err , check . NotNil )
}
func ( s * DockerSuite ) TestContainerRestartInMultipleNetworks ( c * check . C ) {
2015-11-25 23:38:33 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace , NotArm )
2015-09-25 10:19:17 +00:00
// Create 2 networks using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork2" )
2015-10-13 21:18:05 +00:00
2015-09-25 10:19:17 +00:00
// Run and connect containers to testnetwork1
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=second" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Check connectivity between containers in testnetwork2
dockerCmd ( c , "exec" , "first" , "ping" , "-c" , "1" , "second.testnetwork1" )
// Connect containers to testnetwork2
dockerCmd ( c , "network" , "connect" , "testnetwork2" , "first" )
dockerCmd ( c , "network" , "connect" , "testnetwork2" , "second" )
// Check connectivity between containers
dockerCmd ( c , "exec" , "second" , "ping" , "-c" , "1" , "first.testnetwork2" )
// Stop second container and test ping failures on both networks
dockerCmd ( c , "stop" , "second" )
_ , _ , err := dockerCmdWithError ( "exec" , "first" , "ping" , "-c" , "1" , "second.testnetwork1" )
c . Assert ( err , check . NotNil )
_ , _ , err = dockerCmdWithError ( "exec" , "first" , "ping" , "-c" , "1" , "second.testnetwork2" )
c . Assert ( err , check . NotNil )
// Start second container and connectivity must be restored on both networks
dockerCmd ( c , "start" , "second" )
dockerCmd ( c , "exec" , "first" , "ping" , "-c" , "1" , "second.testnetwork1" )
dockerCmd ( c , "exec" , "second" , "ping" , "-c" , "1" , "first.testnetwork2" )
}
func ( s * DockerSuite ) TestContainerWithConflictingHostNetworks ( c * check . C ) {
2015-09-18 17:41:12 +00:00
testRequires ( c , DaemonIsLinux , NotUserNamespace )
2015-09-25 10:19:17 +00:00
// Run a container with --net=host
dockerCmd ( c , "run" , "-d" , "--net=host" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Create a network using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
// Connecting to the user defined network must fail
_ , _ , err := dockerCmdWithError ( "network" , "connect" , "testnetwork1" , "first" )
c . Assert ( err , check . NotNil )
}
func ( s * DockerSuite ) TestContainerWithConflictingSharedNetwork ( c * check . C ) {
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-25 10:19:17 +00:00
dockerCmd ( c , "run" , "-d" , "--name=first" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "first" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Run second container in first container's network namespace
dockerCmd ( c , "run" , "-d" , "--net=container:first" , "--name=second" , "busybox" , "top" )
2015-09-27 11:09:27 +00:00
c . Assert ( waitRun ( "second" ) , check . IsNil )
2015-09-25 10:19:17 +00:00
// Create a network using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
// Connecting to the user defined network must fail
2015-09-27 11:09:27 +00:00
out , _ , err := dockerCmdWithError ( "network" , "connect" , "testnetwork1" , "second" )
c . Assert ( err , check . NotNil )
c . Assert ( out , checker . Contains , runconfig . ErrConflictSharedNetwork . Error ( ) )
}
func ( s * DockerSuite ) TestContainerWithConflictingNoneNetwork ( c * check . C ) {
2016-03-23 13:37:15 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-27 11:09:27 +00:00
dockerCmd ( c , "run" , "-d" , "--net=none" , "--name=first" , "busybox" , "top" )
c . Assert ( waitRun ( "first" ) , check . IsNil )
// Create a network using bridge driver
dockerCmd ( c , "network" , "create" , "-d" , "bridge" , "testnetwork1" )
// Connecting to the user defined network must fail
out , _ , err := dockerCmdWithError ( "network" , "connect" , "testnetwork1" , "first" )
c . Assert ( err , check . NotNil )
c . Assert ( out , checker . Contains , runconfig . ErrConflictNoNetwork . Error ( ) )
// create a container connected to testnetwork1
dockerCmd ( c , "run" , "-d" , "--net=testnetwork1" , "--name=second" , "busybox" , "top" )
c . Assert ( waitRun ( "second" ) , check . IsNil )
// Connect second container to none network. it must fail as well
_ , _ , err = dockerCmdWithError ( "network" , "connect" , "none" , "second" )
2015-09-25 10:19:17 +00:00
c . Assert ( err , check . NotNil )
}
2015-09-14 20:34:23 +00:00
// #11957 - stdin with no tty does not exit if stdin is not closed even though container exited
func ( s * DockerSuite ) TestRunStdinBlockedAfterContainerExit ( c * check . C ) {
cmd := exec . Command ( dockerBinary , "run" , "-i" , "--name=test" , "busybox" , "true" )
in , err := cmd . StdinPipe ( )
c . Assert ( err , check . IsNil )
defer in . Close ( )
2016-03-24 16:43:04 +00:00
stdout := bytes . NewBuffer ( nil )
cmd . Stdout = stdout
cmd . Stderr = stdout
2015-09-14 20:34:23 +00:00
c . Assert ( cmd . Start ( ) , check . IsNil )
waitChan := make ( chan error )
go func ( ) {
waitChan <- cmd . Wait ( )
} ( )
select {
case err := <- waitChan :
2016-03-24 16:43:04 +00:00
c . Assert ( err , check . IsNil , check . Commentf ( stdout . String ( ) ) )
2015-09-28 21:08:26 +00:00
case <- time . After ( 30 * time . Second ) :
2015-09-14 20:34:23 +00:00
c . Fatal ( "timeout waiting for command to exit" )
}
}
2015-09-08 18:40:55 +00:00
func ( s * DockerSuite ) TestRunWrongCpusetCpusFlagValue ( c * check . C ) {
2015-09-23 23:04:51 +00:00
// TODO Windows: This needs validation (error out) in the daemon.
testRequires ( c , DaemonIsLinux )
2015-07-29 12:21:16 +00:00
out , exitCode , err := dockerCmdWithError ( "run" , "--cpuset-cpus" , "1-10,11--" , "busybox" , "true" )
2015-09-08 18:40:55 +00:00
c . Assert ( err , check . NotNil )
expected := "Error response from daemon: Invalid value 1-10,11-- for cpuset cpus.\n"
2015-07-29 12:21:16 +00:00
if ! ( strings . Contains ( out , expected ) || exitCode == 125 ) {
c . Fatalf ( "Expected output to contain %q with exitCode 125, got out: %q exitCode: %v" , expected , out , exitCode )
}
2015-09-08 18:40:55 +00:00
}
func ( s * DockerSuite ) TestRunWrongCpusetMemsFlagValue ( c * check . C ) {
2015-09-23 23:04:51 +00:00
// TODO Windows: This needs validation (error out) in the daemon.
testRequires ( c , DaemonIsLinux )
2015-07-29 12:21:16 +00:00
out , exitCode , err := dockerCmdWithError ( "run" , "--cpuset-mems" , "1-42--" , "busybox" , "true" )
2015-09-08 18:40:55 +00:00
c . Assert ( err , check . NotNil )
expected := "Error response from daemon: Invalid value 1-42-- for cpuset mems.\n"
2015-07-29 12:21:16 +00:00
if ! ( strings . Contains ( out , expected ) || exitCode == 125 ) {
c . Fatalf ( "Expected output to contain %q with exitCode 125, got out: %q exitCode: %v" , expected , out , exitCode )
}
}
// TestRunNonExecutableCmd checks that 'docker run busybox foo' exits with error code 127'
func ( s * DockerSuite ) TestRunNonExecutableCmd ( c * check . C ) {
name := "testNonExecutableCmd"
runCmd := exec . Command ( dockerBinary , "run" , "--name" , name , "busybox" , "foo" )
_ , exit , _ := runCommandWithOutput ( runCmd )
stateExitCode := findContainerExitCode ( c , name )
if ! ( exit == 127 && strings . Contains ( stateExitCode , "127" ) ) {
c . Fatalf ( "Run non-executable command should have errored with exit code 127, but we got exit: %d, State.ExitCode: %s" , exit , stateExitCode )
}
}
// TestRunNonExistingCmd checks that 'docker run busybox /bin/foo' exits with code 127.
func ( s * DockerSuite ) TestRunNonExistingCmd ( c * check . C ) {
name := "testNonExistingCmd"
runCmd := exec . Command ( dockerBinary , "run" , "--name" , name , "busybox" , "/bin/foo" )
_ , exit , _ := runCommandWithOutput ( runCmd )
stateExitCode := findContainerExitCode ( c , name )
if ! ( exit == 127 && strings . Contains ( stateExitCode , "127" ) ) {
c . Fatalf ( "Run non-existing command should have errored with exit code 127, but we got exit: %d, State.ExitCode: %s" , exit , stateExitCode )
}
}
2016-01-08 21:49:43 +00:00
// TestCmdCannotBeInvoked checks that 'docker run busybox /etc' exits with 126, or
// 127 on Windows. The difference is that in Windows, the container must be started
2016-07-21 10:03:37 +00:00
// as that's when the check is made (and yes, by its design...)
2015-07-29 12:21:16 +00:00
func ( s * DockerSuite ) TestCmdCannotBeInvoked ( c * check . C ) {
2016-01-08 21:49:43 +00:00
expected := 126
if daemonPlatform == "windows" {
expected = 127
}
2015-07-29 12:21:16 +00:00
name := "testCmdCannotBeInvoked"
runCmd := exec . Command ( dockerBinary , "run" , "--name" , name , "busybox" , "/etc" )
_ , exit , _ := runCommandWithOutput ( runCmd )
stateExitCode := findContainerExitCode ( c , name )
2016-01-08 21:49:43 +00:00
if ! ( exit == expected && strings . Contains ( stateExitCode , strconv . Itoa ( expected ) ) ) {
c . Fatalf ( "Run cmd that cannot be invoked should have errored with code %d, but we got exit: %d, State.ExitCode: %s" , expected , exit , stateExitCode )
2015-07-29 12:21:16 +00:00
}
}
// TestRunNonExistingImage checks that 'docker run foo' exits with error msg 125 and contains 'Unable to find image'
func ( s * DockerSuite ) TestRunNonExistingImage ( c * check . C ) {
runCmd := exec . Command ( dockerBinary , "run" , "foo" )
out , exit , err := runCommandWithOutput ( runCmd )
if ! ( err != nil && exit == 125 && strings . Contains ( out , "Unable to find image" ) ) {
c . Fatalf ( "Run non-existing image should have errored with 'Unable to find image' code 125, but we got out: %s, exit: %d, err: %s" , out , exit , err )
}
}
// TestDockerFails checks that 'docker run -foo busybox' exits with 125 to signal docker run failed
func ( s * DockerSuite ) TestDockerFails ( c * check . C ) {
runCmd := exec . Command ( dockerBinary , "run" , "-foo" , "busybox" )
out , exit , err := runCommandWithOutput ( runCmd )
if ! ( err != nil && exit == 125 ) {
c . Fatalf ( "Docker run with flag not defined should exit with 125, but we got out: %s, exit: %d, err: %s" , out , exit , err )
}
2015-09-08 18:40:55 +00:00
}
2015-11-18 22:20:54 +00:00
// TestRunInvalidReference invokes docker run with a bad reference.
func ( s * DockerSuite ) TestRunInvalidReference ( c * check . C ) {
out , exit , _ := dockerCmdWithError ( "run" , "busybox@foo" )
if exit == 0 {
c . Fatalf ( "expected non-zero exist code; received %d" , exit )
}
2015-12-10 07:31:38 +00:00
if ! strings . Contains ( out , "Error parsing reference" ) {
c . Fatalf ( ` Expected "Error parsing reference" in output; got: %s ` , out )
2015-11-18 22:20:54 +00:00
}
}
2015-10-13 09:26:27 +00:00
2015-12-04 17:18:05 +00:00
// Test fix for issue #17854
func ( s * DockerSuite ) TestRunInitLayerPathOwnership ( c * check . C ) {
// Not applicable on Windows as it does not support Linux uid/gid ownership
testRequires ( c , DaemonIsLinux )
name := "testetcfileownership"
_ , err := buildImage ( name ,
` FROM busybox
RUN echo ' dockerio : x : 1001 : 1001 : : / bin : / bin / false ' >> / etc / passwd
RUN echo ' dockerio : x : 1001 : ' >> / etc / group
RUN chown dockerio : dockerio / etc ` ,
true )
if err != nil {
c . Fatal ( err )
}
// Test that dockerio ownership of /etc is retained at runtime
out , _ := dockerCmd ( c , "run" , "--rm" , name , "stat" , "-c" , "%U:%G" , "/etc" )
out = strings . TrimSpace ( out )
if out != "dockerio:dockerio" {
c . Fatalf ( "Wrong /etc ownership: expected dockerio:dockerio, got %q" , out )
}
}
2015-10-13 09:26:27 +00:00
func ( s * DockerSuite ) TestRunWithOomScoreAdj ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
expected := "642"
out , _ := dockerCmd ( c , "run" , "--oom-score-adj" , expected , "busybox" , "cat" , "/proc/self/oom_score_adj" )
oomScoreAdj := strings . TrimSpace ( out )
if oomScoreAdj != "642" {
c . Fatalf ( "Expected oom_score_adj set to %q, got %q instead" , expected , oomScoreAdj )
}
}
func ( s * DockerSuite ) TestRunWithOomScoreAdjInvalidRange ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
out , _ , err := dockerCmdWithError ( "run" , "--oom-score-adj" , "1001" , "busybox" , "true" )
c . Assert ( err , check . NotNil )
expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
if ! strings . Contains ( out , expected ) {
c . Fatalf ( "Expected output to contain %q, got %q instead" , expected , out )
}
out , _ , err = dockerCmdWithError ( "run" , "--oom-score-adj" , "-1001" , "busybox" , "true" )
c . Assert ( err , check . NotNil )
expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
if ! strings . Contains ( out , expected ) {
c . Fatalf ( "Expected output to contain %q, got %q instead" , expected , out )
}
}
2015-12-14 15:39:53 +00:00
func ( s * DockerSuite ) TestRunVolumesMountedAsShared ( c * check . C ) {
// Volume propagation is linux only. Also it creates directories for
// bind mounting, so needs to be same host.
testRequires ( c , DaemonIsLinux , SameHostDaemon , NotUserNamespace )
// Prepare a source directory to bind mount
tmpDir , err := ioutil . TempDir ( "" , "volume-source" )
if err != nil {
c . Fatal ( err )
}
defer os . RemoveAll ( tmpDir )
if err := os . Mkdir ( path . Join ( tmpDir , "mnt1" ) , 0755 ) ; err != nil {
c . Fatal ( err )
}
// Convert this directory into a shared mount point so that we do
// not rely on propagation properties of parent mount.
cmd := exec . Command ( "mount" , "--bind" , tmpDir , tmpDir )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
cmd = exec . Command ( "mount" , "--make-private" , "--make-shared" , tmpDir )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
dockerCmd ( c , "run" , "--privileged" , "-v" , fmt . Sprintf ( "%s:/volume-dest:shared" , tmpDir ) , "busybox" , "mount" , "--bind" , "/volume-dest/mnt1" , "/volume-dest/mnt1" )
// Make sure a bind mount under a shared volume propagated to host.
if mounted , _ := mount . Mounted ( path . Join ( tmpDir , "mnt1" ) ) ; ! mounted {
c . Fatalf ( "Bind mount under shared volume did not propagate to host" )
}
mount . Unmount ( path . Join ( tmpDir , "mnt1" ) )
}
func ( s * DockerSuite ) TestRunVolumesMountedAsSlave ( c * check . C ) {
// Volume propagation is linux only. Also it creates directories for
// bind mounting, so needs to be same host.
testRequires ( c , DaemonIsLinux , SameHostDaemon , NotUserNamespace )
// Prepare a source directory to bind mount
tmpDir , err := ioutil . TempDir ( "" , "volume-source" )
if err != nil {
c . Fatal ( err )
}
defer os . RemoveAll ( tmpDir )
if err := os . Mkdir ( path . Join ( tmpDir , "mnt1" ) , 0755 ) ; err != nil {
c . Fatal ( err )
}
// Prepare a source directory with file in it. We will bind mount this
// direcotry and see if file shows up.
tmpDir2 , err := ioutil . TempDir ( "" , "volume-source2" )
if err != nil {
c . Fatal ( err )
}
defer os . RemoveAll ( tmpDir2 )
if err := ioutil . WriteFile ( path . Join ( tmpDir2 , "slave-testfile" ) , [ ] byte ( "Test" ) , 0644 ) ; err != nil {
c . Fatal ( err )
}
// Convert this directory into a shared mount point so that we do
// not rely on propagation properties of parent mount.
cmd := exec . Command ( "mount" , "--bind" , tmpDir , tmpDir )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
cmd = exec . Command ( "mount" , "--make-private" , "--make-shared" , tmpDir )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
dockerCmd ( c , "run" , "-i" , "-d" , "--name" , "parent" , "-v" , fmt . Sprintf ( "%s:/volume-dest:slave" , tmpDir ) , "busybox" , "top" )
// Bind mount tmpDir2/ onto tmpDir/mnt1. If mount propagates inside
// container then contents of tmpDir2/slave-testfile should become
// visible at "/volume-dest/mnt1/slave-testfile"
cmd = exec . Command ( "mount" , "--bind" , tmpDir2 , path . Join ( tmpDir , "mnt1" ) )
if _ , err = runCommand ( cmd ) ; err != nil {
c . Fatal ( err )
}
out , _ := dockerCmd ( c , "exec" , "parent" , "cat" , "/volume-dest/mnt1/slave-testfile" )
mount . Unmount ( path . Join ( tmpDir , "mnt1" ) )
if out != "Test" {
c . Fatalf ( "Bind mount under slave volume did not propagate to container" )
}
}
func ( s * DockerSuite ) TestRunNamedVolumesMountedAsShared ( c * check . C ) {
testRequires ( c , DaemonIsLinux , NotUserNamespace )
out , exitcode , _ := dockerCmdWithError ( "run" , "-v" , "foo:/test:shared" , "busybox" , "touch" , "/test/somefile" )
if exitcode == 0 {
c . Fatalf ( "expected non-zero exit code; received %d" , exitcode )
}
if expected := "Invalid volume specification" ; ! strings . Contains ( out , expected ) {
c . Fatalf ( ` Expected %q in output; got: %s ` , expected , out )
}
2016-01-08 02:38:38 +00:00
}
func ( s * DockerSuite ) TestRunNamedVolumeCopyImageData ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
2015-12-14 15:39:53 +00:00
2016-01-08 02:38:38 +00:00
testImg := "testvolumecopy"
_ , err := buildImage ( testImg , `
FROM busybox
RUN mkdir - p / foo && echo hello > / foo / hello
` , true )
c . Assert ( err , check . IsNil )
dockerCmd ( c , "run" , "-v" , "foo:/foo" , testImg )
out , _ := dockerCmd ( c , "run" , "-v" , "foo:/foo" , "busybox" , "cat" , "/foo/hello" )
c . Assert ( strings . TrimSpace ( out ) , check . Equals , "hello" )
2015-12-14 15:39:53 +00:00
}
2016-01-22 02:57:53 +00:00
func ( s * DockerSuite ) TestRunNamedVolumeNotRemoved ( c * check . C ) {
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2016-01-22 02:57:53 +00:00
dockerCmd ( c , "volume" , "create" , "--name" , "test" )
dockerCmd ( c , "run" , "--rm" , "-v" , "test:" + prefix + "/foo" , "-v" , prefix + "/bar" , "busybox" , "true" )
dockerCmd ( c , "volume" , "inspect" , "test" )
out , _ := dockerCmd ( c , "volume" , "ls" , "-q" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "test" )
dockerCmd ( c , "run" , "--name=test" , "-v" , "test:" + prefix + "/foo" , "-v" , prefix + "/bar" , "busybox" , "true" )
dockerCmd ( c , "rm" , "-fv" , "test" )
dockerCmd ( c , "volume" , "inspect" , "test" )
out , _ = dockerCmd ( c , "volume" , "ls" , "-q" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "test" )
}
func ( s * DockerSuite ) TestRunNamedVolumesFromNotRemoved ( c * check . C ) {
2016-02-03 14:16:00 +00:00
prefix , _ := getPrefixAndSlashFromDaemonPlatform ( )
2016-01-22 02:57:53 +00:00
dockerCmd ( c , "volume" , "create" , "--name" , "test" )
dockerCmd ( c , "run" , "--name=parent" , "-v" , "test:" + prefix + "/foo" , "-v" , prefix + "/bar" , "busybox" , "true" )
dockerCmd ( c , "run" , "--name=child" , "--volumes-from=parent" , "busybox" , "true" )
// Remove the parent so there are not other references to the volumes
dockerCmd ( c , "rm" , "-f" , "parent" )
// now remove the child and ensure the named volume (and only the named volume) still exists
dockerCmd ( c , "rm" , "-fv" , "child" )
dockerCmd ( c , "volume" , "inspect" , "test" )
out , _ := dockerCmd ( c , "volume" , "ls" , "-q" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "test" )
}
2016-03-09 00:54:33 +00:00
func ( s * DockerSuite ) TestRunAttachFailedNoLeak ( c * check . C ) {
2016-03-14 22:52:50 +00:00
nroutines , err := getGoroutineNumber ( )
c . Assert ( err , checker . IsNil )
2016-03-09 00:54:33 +00:00
runSleepingContainer ( c , "--name=test" , "-p" , "8000:8000" )
2016-03-16 15:21:29 +00:00
// Wait until container is fully up and running
c . Assert ( waitRun ( "test" ) , check . IsNil )
2016-03-19 03:24:57 +00:00
out , _ , err := dockerCmdWithError ( "run" , "--name=fail" , "-p" , "8000:8000" , "busybox" , "true" )
// We will need the following `inspect` to diagnose the issue if test fails (#21247)
out1 , err1 := dockerCmd ( c , "inspect" , "--format" , "{{json .State}}" , "test" )
out2 , err2 := dockerCmd ( c , "inspect" , "--format" , "{{json .State}}" , "fail" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Command should have failed but succeeded with: %s\nContainer 'test' [%+v]: %s\nContainer 'fail' [%+v]: %s" , out , err1 , out1 , err2 , out2 ) )
2016-03-09 00:54:33 +00:00
// check for windows error as well
2016-03-18 18:50:19 +00:00
// TODO Windows Post TP5. Fix the error message string
c . Assert ( strings . Contains ( string ( out ) , "port is already allocated" ) ||
strings . Contains ( string ( out ) , "were not connected because a duplicate name exists" ) ||
2016-05-09 23:35:35 +00:00
strings . Contains ( string ( out ) , "HNS failed with error : Failed to create endpoint" ) ||
strings . Contains ( string ( out ) , "HNS failed with error : The object already exists" ) , checker . Equals , true , check . Commentf ( "Output: %s" , out ) )
2016-03-09 00:54:33 +00:00
dockerCmd ( c , "rm" , "-f" , "test" )
// NGoroutines is not updated right away, so we need to wait before failing
2016-03-14 22:52:50 +00:00
c . Assert ( waitForGoroutines ( nroutines ) , checker . IsNil )
2016-03-09 00:54:33 +00:00
}
2016-03-11 01:06:53 +00:00
// Test for one character directory name case (#20122)
func ( s * DockerSuite ) TestRunVolumeWithOneCharacter ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
out , _ := dockerCmd ( c , "run" , "-v" , "/tmp/q:/foo" , "busybox" , "sh" , "-c" , "find /foo" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "/foo" )
}
2016-03-15 03:31:42 +00:00
func ( s * DockerSuite ) TestRunVolumeCopyFlag ( c * check . C ) {
testRequires ( c , DaemonIsLinux ) // Windows does not support copying data from image to the volume
_ , err := buildImage ( "volumecopy" ,
` FROM busybox
RUN mkdir / foo && echo hello > / foo / bar
CMD cat / foo / bar ` ,
true ,
)
c . Assert ( err , checker . IsNil )
dockerCmd ( c , "volume" , "create" , "--name=test" )
// test with the nocopy flag
out , _ , err := dockerCmdWithError ( "run" , "-v" , "test:/foo:nocopy" , "volumecopy" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
// test default behavior which is to copy for non-binds
out , _ = dockerCmd ( c , "run" , "-v" , "test:/foo" , "volumecopy" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "hello" )
// error out when the volume is already populated
out , _ , err = dockerCmdWithError ( "run" , "-v" , "test:/foo:copy" , "volumecopy" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
// do not error out when copy isn't explicitly set even though it's already populated
out , _ = dockerCmd ( c , "run" , "-v" , "test:/foo" , "volumecopy" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "hello" )
// do not allow copy modes on volumes-from
dockerCmd ( c , "run" , "--name=test" , "-v" , "/foo" , "busybox" , "true" )
out , _ , err = dockerCmdWithError ( "run" , "--volumes-from=test:copy" , "busybox" , "true" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
out , _ , err = dockerCmdWithError ( "run" , "--volumes-from=test:nocopy" , "busybox" , "true" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
// do not allow copy modes on binds
out , _ , err = dockerCmdWithError ( "run" , "-v" , "/foo:/bar:copy" , "busybox" , "true" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
out , _ , err = dockerCmdWithError ( "run" , "-v" , "/foo:/bar:nocopy" , "busybox" , "true" )
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
}
2016-03-24 04:41:21 +00:00
func ( s * DockerSuite ) TestRunTooLongHostname ( c * check . C ) {
// Test case in #21445
hostname1 := "this-is-a-way-too-long-hostname-but-it-should-give-a-nice-error.local"
out , _ , err := dockerCmdWithError ( "run" , "--hostname" , hostname1 , "busybox" , "echo" , "test" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Expected docker run to fail!" ) )
2016-03-29 16:37:43 +00:00
c . Assert ( out , checker . Contains , "invalid hostname format:" , check . Commentf ( "Expected to have 'invalid hostname format:' in the output, get: %s!" , out ) )
2016-03-24 04:41:21 +00:00
2016-05-25 01:43:22 +00:00
// Additional test cases
2016-03-29 16:37:43 +00:00
validHostnames := map [ string ] string {
"hostname" : "hostname" ,
"host-name" : "host-name" ,
"hostname123" : "hostname123" ,
"123hostname" : "123hostname" ,
"hostname-of-63-bytes-long-should-be-valid-and-without-any-error" : "hostname-of-63-bytes-long-should-be-valid-and-without-any-error" ,
}
for hostname := range validHostnames {
dockerCmd ( c , "run" , "--hostname" , hostname , "busybox" , "echo" , "test" )
}
2016-03-24 04:41:21 +00:00
2016-03-29 16:37:43 +00:00
invalidHostnames := map [ string ] string {
"^hostname" : "invalid hostname format: ^hostname" ,
"hostname%" : "invalid hostname format: hostname%" ,
"host&name" : "invalid hostname format: host&name" ,
"-hostname" : "invalid hostname format: -hostname" ,
"host_name" : "invalid hostname format: host_name" ,
"hostname-of-64-bytes-long-should-be-invalid-and-be-with-an-error" : "invalid hostname format: hostname-of-64-bytes-long-should-be-invalid-and-be-with-an-error" ,
}
for hostname , expectedError := range invalidHostnames {
out , _ , err = dockerCmdWithError ( "run" , "--hostname" , hostname , "busybox" , "echo" , "test" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Expected docker run to fail!" ) )
c . Assert ( out , checker . Contains , expectedError , check . Commentf ( "Expected to have '%s' in the output, get: %s!" , expectedError , out ) )
}
2016-03-24 04:41:21 +00:00
}
2016-04-29 05:46:57 +00:00
// Test case for #21976
func ( s * DockerSuite ) TestRunDnsInHostMode ( c * check . C ) {
testRequires ( c , DaemonIsLinux , NotUserNamespace )
expectedOutput := "nameserver 127.0.0.1"
expectedWarning := "Localhost DNS setting"
out , stderr , _ := dockerCmdWithStdoutStderr ( c , "run" , "--dns=127.0.0.1" , "--net=host" , "busybox" , "cat" , "/etc/resolv.conf" )
c . Assert ( out , checker . Contains , expectedOutput , check . Commentf ( "Expected '%s', but got %q" , expectedOutput , out ) )
c . Assert ( stderr , checker . Contains , expectedWarning , check . Commentf ( "Expected warning on stderr about localhost resolver, but got %q" , stderr ) )
expectedOutput = "nameserver 1.2.3.4"
out , _ = dockerCmd ( c , "run" , "--dns=1.2.3.4" , "--net=host" , "busybox" , "cat" , "/etc/resolv.conf" )
c . Assert ( out , checker . Contains , expectedOutput , check . Commentf ( "Expected '%s', but got %q" , expectedOutput , out ) )
expectedOutput = "search example.com"
out , _ = dockerCmd ( c , "run" , "--dns-search=example.com" , "--net=host" , "busybox" , "cat" , "/etc/resolv.conf" )
c . Assert ( out , checker . Contains , expectedOutput , check . Commentf ( "Expected '%s', but got %q" , expectedOutput , out ) )
expectedOutput = "options timeout:3"
out , _ = dockerCmd ( c , "run" , "--dns-opt=timeout:3" , "--net=host" , "busybox" , "cat" , "/etc/resolv.conf" )
c . Assert ( out , checker . Contains , expectedOutput , check . Commentf ( "Expected '%s', but got %q" , expectedOutput , out ) )
expectedOutput1 := "nameserver 1.2.3.4"
expectedOutput2 := "search example.com"
expectedOutput3 := "options timeout:3"
out , _ = dockerCmd ( c , "run" , "--dns=1.2.3.4" , "--dns-search=example.com" , "--dns-opt=timeout:3" , "--net=host" , "busybox" , "cat" , "/etc/resolv.conf" )
c . Assert ( out , checker . Contains , expectedOutput1 , check . Commentf ( "Expected '%s', but got %q" , expectedOutput1 , out ) )
c . Assert ( out , checker . Contains , expectedOutput2 , check . Commentf ( "Expected '%s', but got %q" , expectedOutput2 , out ) )
c . Assert ( out , checker . Contains , expectedOutput3 , check . Commentf ( "Expected '%s', but got %q" , expectedOutput3 , out ) )
}
2016-05-25 01:49:11 +00:00
// Test case for #21976
func ( s * DockerSuite ) TestRunAddHostInHostMode ( c * check . C ) {
testRequires ( c , DaemonIsLinux , NotUserNamespace )
expectedOutput := "1.2.3.4\textra"
out , _ := dockerCmd ( c , "run" , "--add-host=extra:1.2.3.4" , "--net=host" , "busybox" , "cat" , "/etc/hosts" )
c . Assert ( out , checker . Contains , expectedOutput , check . Commentf ( "Expected '%s', but got %q" , expectedOutput , out ) )
}