github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_cli_daemon_test.go (about)

     1  // +build linux
     2  
     3  package main
     4  
     5  import (
     6  	"bufio"
     7  	"bytes"
     8  	"encoding/json"
     9  	"fmt"
    10  	"io"
    11  	"io/ioutil"
    12  	"net"
    13  	"os"
    14  	"os/exec"
    15  	"path"
    16  	"path/filepath"
    17  	"regexp"
    18  	"strconv"
    19  	"strings"
    20  	"sync"
    21  	"syscall"
    22  	"time"
    23  
    24  	"crypto/tls"
    25  	"crypto/x509"
    26  
    27  	"github.com/cloudflare/cfssl/helpers"
    28  	"github.com/docker/docker/integration-cli/checker"
    29  	"github.com/docker/docker/integration-cli/cli"
    30  	"github.com/docker/docker/integration-cli/daemon"
    31  	"github.com/docker/docker/opts"
    32  	"github.com/docker/docker/pkg/mount"
    33  	"github.com/docker/docker/pkg/stringid"
    34  	"github.com/docker/docker/pkg/testutil"
    35  	icmd "github.com/docker/docker/pkg/testutil/cmd"
    36  	units "github.com/docker/go-units"
    37  	"github.com/docker/libnetwork/iptables"
    38  	"github.com/docker/libtrust"
    39  	"github.com/go-check/check"
    40  	"github.com/kr/pty"
    41  )
    42  
    43  // TestLegacyDaemonCommand test starting docker daemon using "deprecated" docker daemon
    44  // command. Remove this test when we remove this.
    45  func (s *DockerDaemonSuite) TestLegacyDaemonCommand(c *check.C) {
    46  	cmd := exec.Command(dockerBinary, "daemon", "--storage-driver=vfs", "--debug")
    47  	err := cmd.Start()
    48  	c.Assert(err, checker.IsNil, check.Commentf("could not start daemon using 'docker daemon'"))
    49  
    50  	c.Assert(cmd.Process.Kill(), checker.IsNil)
    51  }
    52  
    53  func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
    54  	s.d.StartWithBusybox(c)
    55  
    56  	cli.Docker(
    57  		cli.Args("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"),
    58  		cli.Daemon(s.d),
    59  	).Assert(c, icmd.Success)
    60  
    61  	cli.Docker(
    62  		cli.Args("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"),
    63  		cli.Daemon(s.d),
    64  	).Assert(c, icmd.Success)
    65  
    66  	testRun := func(m map[string]bool, prefix string) {
    67  		var format string
    68  		for cont, shouldRun := range m {
    69  			out := cli.Docker(cli.Args("ps"), cli.Daemon(s.d)).Assert(c, icmd.Success).Combined()
    70  			if shouldRun {
    71  				format = "%scontainer %q is not running"
    72  			} else {
    73  				format = "%scontainer %q is running"
    74  			}
    75  			if shouldRun != strings.Contains(out, cont) {
    76  				c.Fatalf(format, prefix, cont)
    77  			}
    78  		}
    79  	}
    80  
    81  	testRun(map[string]bool{"top1": true, "top2": true}, "")
    82  
    83  	s.d.Restart(c)
    84  	testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
    85  }
    86  
    87  func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
    88  	s.d.StartWithBusybox(c)
    89  
    90  	if out, err := s.d.Cmd("run", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
    91  		c.Fatal(err, out)
    92  	}
    93  
    94  	s.d.Restart(c)
    95  
    96  	if out, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
    97  		c.Fatal(err, out)
    98  	}
    99  
   100  	if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
   101  		c.Fatal(err, out)
   102  	}
   103  
   104  	out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1")
   105  	c.Assert(err, check.IsNil)
   106  
   107  	if _, err := inspectMountPointJSON(out, "/foo"); err != nil {
   108  		c.Fatalf("Expected volume to exist: /foo, error: %v\n", err)
   109  	}
   110  }
   111  
   112  // #11008
   113  func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
   114  	s.d.StartWithBusybox(c)
   115  
   116  	out, err := s.d.Cmd("run", "-d", "--name", "top1", "--restart", "always", "busybox:latest", "top")
   117  	c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
   118  
   119  	out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top")
   120  	c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out))
   121  
   122  	testRun := func(m map[string]bool, prefix string) {
   123  		var format string
   124  		for name, shouldRun := range m {
   125  			out, err := s.d.Cmd("ps")
   126  			c.Assert(err, check.IsNil, check.Commentf("run ps: %v", out))
   127  			if shouldRun {
   128  				format = "%scontainer %q is not running"
   129  			} else {
   130  				format = "%scontainer %q is running"
   131  			}
   132  			c.Assert(strings.Contains(out, name), check.Equals, shouldRun, check.Commentf(format, prefix, name))
   133  		}
   134  	}
   135  
   136  	// both running
   137  	testRun(map[string]bool{"top1": true, "top2": true}, "")
   138  
   139  	out, err = s.d.Cmd("stop", "top1")
   140  	c.Assert(err, check.IsNil, check.Commentf(out))
   141  
   142  	out, err = s.d.Cmd("stop", "top2")
   143  	c.Assert(err, check.IsNil, check.Commentf(out))
   144  
   145  	// both stopped
   146  	testRun(map[string]bool{"top1": false, "top2": false}, "")
   147  
   148  	s.d.Restart(c)
   149  
   150  	// restart=always running
   151  	testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
   152  
   153  	out, err = s.d.Cmd("start", "top2")
   154  	c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out))
   155  
   156  	s.d.Restart(c)
   157  
   158  	// both running
   159  	testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ")
   160  
   161  }
   162  
   163  func (s *DockerDaemonSuite) TestDaemonRestartOnFailure(c *check.C) {
   164  	s.d.StartWithBusybox(c)
   165  
   166  	out, err := s.d.Cmd("run", "-d", "--name", "test1", "--restart", "on-failure:3", "busybox:latest", "false")
   167  	c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
   168  
   169  	// wait test1 to stop
   170  	hostArgs := []string{"--host", s.d.Sock()}
   171  	err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 10*time.Second, hostArgs...)
   172  	c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not"))
   173  
   174  	// record last start time
   175  	out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1")
   176  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
   177  	lastStartTime := out
   178  
   179  	s.d.Restart(c)
   180  
   181  	// test1 shouldn't restart at all
   182  	err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 0, hostArgs...)
   183  	c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not"))
   184  
   185  	// make sure test1 isn't restarted when daemon restart
   186  	// if "StartAt" time updates, means test1 was once restarted.
   187  	out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1")
   188  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
   189  	c.Assert(out, checker.Equals, lastStartTime, check.Commentf("test1 shouldn't start after daemon restarts"))
   190  }
   191  
   192  func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
   193  	s.d.Start(c, "--iptables=false")
   194  }
   195  
   196  // Make sure we cannot shrink base device at daemon restart.
   197  func (s *DockerDaemonSuite) TestDaemonRestartWithInvalidBasesize(c *check.C) {
   198  	testRequires(c, Devicemapper)
   199  	s.d.Start(c)
   200  
   201  	oldBasesizeBytes := s.d.GetBaseDeviceSize(c)
   202  	var newBasesizeBytes int64 = 1073741824 //1GB in bytes
   203  
   204  	if newBasesizeBytes < oldBasesizeBytes {
   205  		err := s.d.RestartWithError("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes))
   206  		c.Assert(err, check.NotNil, check.Commentf("daemon should not have started as new base device size is less than existing base device size: %v", err))
   207  		// 'err != nil' is expected behaviour, no new daemon started,
   208  		// so no need to stop daemon.
   209  		if err != nil {
   210  			return
   211  		}
   212  	}
   213  	s.d.Stop(c)
   214  }
   215  
   216  // Make sure we can grow base device at daemon restart.
   217  func (s *DockerDaemonSuite) TestDaemonRestartWithIncreasedBasesize(c *check.C) {
   218  	testRequires(c, Devicemapper)
   219  	s.d.Start(c)
   220  
   221  	oldBasesizeBytes := s.d.GetBaseDeviceSize(c)
   222  
   223  	var newBasesizeBytes int64 = 53687091200 //50GB in bytes
   224  
   225  	if newBasesizeBytes < oldBasesizeBytes {
   226  		c.Skip(fmt.Sprintf("New base device size (%v) must be greater than (%s)", units.HumanSize(float64(newBasesizeBytes)), units.HumanSize(float64(oldBasesizeBytes))))
   227  	}
   228  
   229  	err := s.d.RestartWithError("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes))
   230  	c.Assert(err, check.IsNil, check.Commentf("we should have been able to start the daemon with increased base device size: %v", err))
   231  
   232  	basesizeAfterRestart := s.d.GetBaseDeviceSize(c)
   233  	newBasesize, err := convertBasesize(newBasesizeBytes)
   234  	c.Assert(err, check.IsNil, check.Commentf("Error in converting base device size: %v", err))
   235  	c.Assert(newBasesize, check.Equals, basesizeAfterRestart, check.Commentf("Basesize passed is not equal to Basesize set"))
   236  	s.d.Stop(c)
   237  }
   238  
   239  func convertBasesize(basesizeBytes int64) (int64, error) {
   240  	basesize := units.HumanSize(float64(basesizeBytes))
   241  	basesize = strings.Trim(basesize, " ")[:len(basesize)-3]
   242  	basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
   243  	if err != nil {
   244  		return 0, err
   245  	}
   246  	return int64(basesizeFloat) * 1024 * 1024 * 1024, nil
   247  }
   248  
   249  // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
   250  // no longer has an IP associated, we should gracefully handle that case and associate
   251  // an IP with it rather than fail daemon start
   252  func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
   253  	// rather than depending on brctl commands to verify docker0 is created and up
   254  	// let's start the daemon and stop it, and then make a modification to run the
   255  	// actual test
   256  	s.d.Start(c)
   257  	s.d.Stop(c)
   258  
   259  	// now we will remove the ip from docker0 and then try starting the daemon
   260  	icmd.RunCommand("ip", "addr", "flush", "dev", "docker0").Assert(c, icmd.Success)
   261  
   262  	if err := s.d.StartWithError(); err != nil {
   263  		warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
   264  		c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
   265  	}
   266  }
   267  
   268  func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
   269  	s.d.StartWithBusybox(c)
   270  
   271  	if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
   272  		c.Fatalf("Could not run top: %s, %v", out, err)
   273  	}
   274  
   275  	ipTablesSearchString := "tcp dpt:80"
   276  
   277  	// get output from iptables with container running
   278  	verifyIPTablesContains(c, ipTablesSearchString)
   279  
   280  	s.d.Stop(c)
   281  
   282  	// get output from iptables after restart
   283  	verifyIPTablesDoesNotContains(c, ipTablesSearchString)
   284  }
   285  
   286  func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
   287  	s.d.StartWithBusybox(c)
   288  
   289  	if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
   290  		c.Fatalf("Could not run top: %s, %v", out, err)
   291  	}
   292  
   293  	// get output from iptables with container running
   294  	ipTablesSearchString := "tcp dpt:80"
   295  	verifyIPTablesContains(c, ipTablesSearchString)
   296  
   297  	s.d.Restart(c)
   298  
   299  	// make sure the container is not running
   300  	runningOut, err := s.d.Cmd("inspect", "--format={{.State.Running}}", "top")
   301  	if err != nil {
   302  		c.Fatalf("Could not inspect on container: %s, %v", runningOut, err)
   303  	}
   304  	if strings.TrimSpace(runningOut) != "true" {
   305  		c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
   306  	}
   307  
   308  	// get output from iptables after restart
   309  	verifyIPTablesContains(c, ipTablesSearchString)
   310  }
   311  
   312  func verifyIPTablesContains(c *check.C, ipTablesSearchString string) {
   313  	result := icmd.RunCommand("iptables", "-nvL")
   314  	result.Assert(c, icmd.Success)
   315  	if !strings.Contains(result.Combined(), ipTablesSearchString) {
   316  		c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, result.Combined())
   317  	}
   318  }
   319  
   320  func verifyIPTablesDoesNotContains(c *check.C, ipTablesSearchString string) {
   321  	result := icmd.RunCommand("iptables", "-nvL")
   322  	result.Assert(c, icmd.Success)
   323  	if strings.Contains(result.Combined(), ipTablesSearchString) {
   324  		c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, result.Combined())
   325  	}
   326  }
   327  
   328  // TestDaemonIPv6Enabled checks that when the daemon is started with --ipv6=true that the docker0 bridge
   329  // has the fe80::1 address and that a container is assigned a link-local address
   330  func (s *DockerDaemonSuite) TestDaemonIPv6Enabled(c *check.C) {
   331  	testRequires(c, IPv6)
   332  
   333  	setupV6(c)
   334  	defer teardownV6(c)
   335  
   336  	s.d.StartWithBusybox(c, "--ipv6")
   337  
   338  	iface, err := net.InterfaceByName("docker0")
   339  	if err != nil {
   340  		c.Fatalf("Error getting docker0 interface: %v", err)
   341  	}
   342  
   343  	addrs, err := iface.Addrs()
   344  	if err != nil {
   345  		c.Fatalf("Error getting addresses for docker0 interface: %v", err)
   346  	}
   347  
   348  	var found bool
   349  	expected := "fe80::1/64"
   350  
   351  	for i := range addrs {
   352  		if addrs[i].String() == expected {
   353  			found = true
   354  			break
   355  		}
   356  	}
   357  
   358  	if !found {
   359  		c.Fatalf("Bridge does not have an IPv6 Address")
   360  	}
   361  
   362  	if out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil {
   363  		c.Fatalf("Could not run container: %s, %v", out, err)
   364  	}
   365  
   366  	out, err := s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.LinkLocalIPv6Address}}'", "ipv6test")
   367  	out = strings.Trim(out, " \r\n'")
   368  
   369  	if err != nil {
   370  		c.Fatalf("Error inspecting container: %s, %v", out, err)
   371  	}
   372  
   373  	if ip := net.ParseIP(out); ip == nil {
   374  		c.Fatalf("Container should have a link-local IPv6 address")
   375  	}
   376  
   377  	out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test")
   378  	out = strings.Trim(out, " \r\n'")
   379  
   380  	if err != nil {
   381  		c.Fatalf("Error inspecting container: %s, %v", out, err)
   382  	}
   383  
   384  	if ip := net.ParseIP(out); ip != nil {
   385  		c.Fatalf("Container should not have a global IPv6 address: %v", out)
   386  	}
   387  }
   388  
   389  // TestDaemonIPv6FixedCIDR checks that when the daemon is started with --ipv6=true and a fixed CIDR
   390  // that running containers are given a link-local and global IPv6 address
   391  func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDR(c *check.C) {
   392  	// IPv6 setup is messing with local bridge address.
   393  	testRequires(c, SameHostDaemon)
   394  	// Delete the docker0 bridge if its left around from previous daemon. It has to be recreated with
   395  	// ipv6 enabled
   396  	deleteInterface(c, "docker0")
   397  
   398  	s.d.StartWithBusybox(c, "--ipv6", "--fixed-cidr-v6=2001:db8:2::/64", "--default-gateway-v6=2001:db8:2::100")
   399  
   400  	out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest")
   401  	c.Assert(err, checker.IsNil, check.Commentf("Could not run container: %s, %v", out, err))
   402  
   403  	out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}", "ipv6test")
   404  	out = strings.Trim(out, " \r\n'")
   405  
   406  	c.Assert(err, checker.IsNil, check.Commentf(out))
   407  
   408  	ip := net.ParseIP(out)
   409  	c.Assert(ip, checker.NotNil, check.Commentf("Container should have a global IPv6 address"))
   410  
   411  	out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.IPv6Gateway}}", "ipv6test")
   412  	c.Assert(err, checker.IsNil, check.Commentf(out))
   413  
   414  	c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:2::100", check.Commentf("Container should have a global IPv6 gateway"))
   415  }
   416  
   417  // TestDaemonIPv6FixedCIDRAndMac checks that when the daemon is started with ipv6 fixed CIDR
   418  // the running containers are given an IPv6 address derived from the MAC address and the ipv6 fixed CIDR
   419  func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDRAndMac(c *check.C) {
   420  	// IPv6 setup is messing with local bridge address.
   421  	testRequires(c, SameHostDaemon)
   422  	// Delete the docker0 bridge if its left around from previous daemon. It has to be recreated with
   423  	// ipv6 enabled
   424  	deleteInterface(c, "docker0")
   425  
   426  	s.d.StartWithBusybox(c, "--ipv6", "--fixed-cidr-v6=2001:db8:1::/64")
   427  
   428  	out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "--mac-address", "AA:BB:CC:DD:EE:FF", "busybox")
   429  	c.Assert(err, checker.IsNil)
   430  
   431  	out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}", "ipv6test")
   432  	c.Assert(err, checker.IsNil)
   433  	c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:1::aabb:ccdd:eeff")
   434  }
   435  
   436  // TestDaemonIPv6HostMode checks that when the running a container with
   437  // network=host the host ipv6 addresses are not removed
   438  func (s *DockerDaemonSuite) TestDaemonIPv6HostMode(c *check.C) {
   439  	testRequires(c, SameHostDaemon)
   440  	deleteInterface(c, "docker0")
   441  
   442  	s.d.StartWithBusybox(c, "--ipv6", "--fixed-cidr-v6=2001:db8:2::/64")
   443  	out, err := s.d.Cmd("run", "-itd", "--name=hostcnt", "--network=host", "busybox:latest")
   444  	c.Assert(err, checker.IsNil, check.Commentf("Could not run container: %s, %v", out, err))
   445  
   446  	out, err = s.d.Cmd("exec", "hostcnt", "ip", "-6", "addr", "show", "docker0")
   447  	out = strings.Trim(out, " \r\n'")
   448  
   449  	c.Assert(out, checker.Contains, "2001:db8:2::1")
   450  }
   451  
   452  func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
   453  	c.Assert(s.d.StartWithError("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
   454  }
   455  
   456  func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
   457  	s.d.Start(c, "--log-level=debug")
   458  	content, err := s.d.ReadLogFile()
   459  	c.Assert(err, checker.IsNil)
   460  	if !strings.Contains(string(content), `level=debug`) {
   461  		c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
   462  	}
   463  }
   464  
   465  func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
   466  	// we creating new daemons to create new logFile
   467  	s.d.Start(c, "--log-level=fatal")
   468  	content, err := s.d.ReadLogFile()
   469  	c.Assert(err, checker.IsNil)
   470  	if strings.Contains(string(content), `level=debug`) {
   471  		c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
   472  	}
   473  }
   474  
   475  func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
   476  	s.d.Start(c, "-D")
   477  	content, err := s.d.ReadLogFile()
   478  	c.Assert(err, checker.IsNil)
   479  	if !strings.Contains(string(content), `level=debug`) {
   480  		c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content))
   481  	}
   482  }
   483  
   484  func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
   485  	s.d.Start(c, "--debug")
   486  	content, err := s.d.ReadLogFile()
   487  	c.Assert(err, checker.IsNil)
   488  	if !strings.Contains(string(content), `level=debug`) {
   489  		c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content))
   490  	}
   491  }
   492  
   493  func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
   494  	s.d.Start(c, "--debug", "--log-level=fatal")
   495  	content, err := s.d.ReadLogFile()
   496  	c.Assert(err, checker.IsNil)
   497  	if !strings.Contains(string(content), `level=debug`) {
   498  		c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
   499  	}
   500  }
   501  
   502  func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
   503  	listeningPorts := [][]string{
   504  		{"0.0.0.0", "0.0.0.0", "5678"},
   505  		{"127.0.0.1", "127.0.0.1", "1234"},
   506  		{"localhost", "127.0.0.1", "1235"},
   507  	}
   508  
   509  	cmdArgs := make([]string, 0, len(listeningPorts)*2)
   510  	for _, hostDirective := range listeningPorts {
   511  		cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
   512  	}
   513  
   514  	s.d.StartWithBusybox(c, cmdArgs...)
   515  
   516  	for _, hostDirective := range listeningPorts {
   517  		output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
   518  		if err == nil {
   519  			c.Fatalf("Container should not start, expected port already allocated error: %q", output)
   520  		} else if !strings.Contains(output, "port is already allocated") {
   521  			c.Fatalf("Expected port is already allocated error: %q", output)
   522  		}
   523  	}
   524  }
   525  
   526  func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
   527  	// TODO: skip or update for Windows daemon
   528  	os.Remove("/etc/docker/key.json")
   529  	s.d.Start(c)
   530  	s.d.Stop(c)
   531  
   532  	k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
   533  	if err != nil {
   534  		c.Fatalf("Error opening key file")
   535  	}
   536  	kid := k.KeyID()
   537  	// Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
   538  	if len(kid) != 59 {
   539  		c.Fatalf("Bad key ID: %s", kid)
   540  	}
   541  }
   542  
   543  // GH#11320 - verify that the daemon exits on failure properly
   544  // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
   545  // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
   546  func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
   547  	//attempt to start daemon with incorrect flags (we know -b and --bip conflict)
   548  	if err := s.d.StartWithError("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
   549  		//verify we got the right error
   550  		if !strings.Contains(err.Error(), "Daemon exited") {
   551  			c.Fatalf("Expected daemon not to start, got %v", err)
   552  		}
   553  		// look in the log and make sure we got the message that daemon is shutting down
   554  		icmd.RunCommand("grep", "Error starting daemon", s.d.LogFileName()).Assert(c, icmd.Success)
   555  	} else {
   556  		//if we didn't get an error and the daemon is running, this is a failure
   557  		c.Fatal("Conflicting options should cause the daemon to error out with a failure")
   558  	}
   559  }
   560  
   561  func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
   562  	d := s.d
   563  	err := d.StartWithError("--bridge", "nosuchbridge")
   564  	c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail"))
   565  	defer d.Restart(c)
   566  
   567  	bridgeName := "external-bridge"
   568  	bridgeIP := "192.169.1.1/24"
   569  	_, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
   570  
   571  	createInterface(c, "bridge", bridgeName, bridgeIP)
   572  	defer deleteInterface(c, bridgeName)
   573  
   574  	d.StartWithBusybox(c, "--bridge", bridgeName)
   575  
   576  	ipTablesSearchString := bridgeIPNet.String()
   577  	icmd.RunCommand("iptables", "-t", "nat", "-nvL").Assert(c, icmd.Expected{
   578  		Out: ipTablesSearchString,
   579  	})
   580  
   581  	_, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
   582  	c.Assert(err, check.IsNil)
   583  
   584  	containerIP, err := d.FindContainerIP("ExtContainer")
   585  	c.Assert(err, checker.IsNil)
   586  	ip := net.ParseIP(containerIP)
   587  	c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
   588  		check.Commentf("Container IP-Address must be in the same subnet range : %s",
   589  			containerIP))
   590  }
   591  
   592  func (s *DockerDaemonSuite) TestDaemonBridgeNone(c *check.C) {
   593  	// start with bridge none
   594  	d := s.d
   595  	d.StartWithBusybox(c, "--bridge", "none")
   596  	defer d.Restart(c)
   597  
   598  	// verify docker0 iface is not there
   599  	icmd.RunCommand("ifconfig", "docker0").Assert(c, icmd.Expected{
   600  		ExitCode: 1,
   601  		Error:    "exit status 1",
   602  		Err:      "Device not found",
   603  	})
   604  
   605  	// verify default "bridge" network is not there
   606  	out, err := d.Cmd("network", "inspect", "bridge")
   607  	c.Assert(err, check.NotNil, check.Commentf("\"bridge\" network should not be present if daemon started with --bridge=none"))
   608  	c.Assert(strings.Contains(out, "No such network"), check.Equals, true)
   609  }
   610  
   611  func createInterface(c *check.C, ifType string, ifName string, ipNet string) {
   612  	icmd.RunCommand("ip", "link", "add", "name", ifName, "type", ifType).Assert(c, icmd.Success)
   613  	icmd.RunCommand("ifconfig", ifName, ipNet, "up").Assert(c, icmd.Success)
   614  }
   615  
   616  func deleteInterface(c *check.C, ifName string) {
   617  	icmd.RunCommand("ip", "link", "delete", ifName).Assert(c, icmd.Success)
   618  	icmd.RunCommand("iptables", "-t", "nat", "--flush").Assert(c, icmd.Success)
   619  	icmd.RunCommand("iptables", "--flush").Assert(c, icmd.Success)
   620  }
   621  
   622  func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
   623  	// TestDaemonBridgeIP Steps
   624  	// 1. Delete the existing docker0 Bridge
   625  	// 2. Set --bip daemon configuration and start the new Docker Daemon
   626  	// 3. Check if the bip config has taken effect using ifconfig and iptables commands
   627  	// 4. Launch a Container and make sure the IP-Address is in the expected subnet
   628  	// 5. Delete the docker0 Bridge
   629  	// 6. Restart the Docker Daemon (via deferred action)
   630  	//    This Restart takes care of bringing docker0 interface back to auto-assigned IP
   631  
   632  	defaultNetworkBridge := "docker0"
   633  	deleteInterface(c, defaultNetworkBridge)
   634  
   635  	d := s.d
   636  
   637  	bridgeIP := "192.169.1.1/24"
   638  	ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
   639  
   640  	d.StartWithBusybox(c, "--bip", bridgeIP)
   641  	defer d.Restart(c)
   642  
   643  	ifconfigSearchString := ip.String()
   644  	icmd.RunCommand("ifconfig", defaultNetworkBridge).Assert(c, icmd.Expected{
   645  		Out: ifconfigSearchString,
   646  	})
   647  
   648  	ipTablesSearchString := bridgeIPNet.String()
   649  	icmd.RunCommand("iptables", "-t", "nat", "-nvL").Assert(c, icmd.Expected{
   650  		Out: ipTablesSearchString,
   651  	})
   652  
   653  	_, err := d.Cmd("run", "-d", "--name", "test", "busybox", "top")
   654  	c.Assert(err, check.IsNil)
   655  
   656  	containerIP, err := d.FindContainerIP("test")
   657  	c.Assert(err, checker.IsNil)
   658  	ip = net.ParseIP(containerIP)
   659  	c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
   660  		check.Commentf("Container IP-Address must be in the same subnet range : %s",
   661  			containerIP))
   662  	deleteInterface(c, defaultNetworkBridge)
   663  }
   664  
   665  func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) {
   666  	s.d.Start(c)
   667  	defer s.d.Restart(c)
   668  	s.d.Stop(c)
   669  
   670  	// now we will change the docker0's IP and then try starting the daemon
   671  	bridgeIP := "192.169.100.1/24"
   672  	_, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
   673  
   674  	icmd.RunCommand("ifconfig", "docker0", bridgeIP).Assert(c, icmd.Success)
   675  
   676  	s.d.Start(c, "--bip", bridgeIP)
   677  
   678  	//check if the iptables contains new bridgeIP MASQUERADE rule
   679  	ipTablesSearchString := bridgeIPNet.String()
   680  	icmd.RunCommand("iptables", "-t", "nat", "-nvL").Assert(c, icmd.Expected{
   681  		Out: ipTablesSearchString,
   682  	})
   683  }
   684  
   685  func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
   686  	d := s.d
   687  
   688  	bridgeName := "external-bridge"
   689  	bridgeIP := "192.169.1.1/24"
   690  
   691  	createInterface(c, "bridge", bridgeName, bridgeIP)
   692  	defer deleteInterface(c, bridgeName)
   693  
   694  	args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
   695  	d.StartWithBusybox(c, args...)
   696  	defer d.Restart(c)
   697  
   698  	for i := 0; i < 4; i++ {
   699  		cName := "Container" + strconv.Itoa(i)
   700  		out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
   701  		if err != nil {
   702  			c.Assert(strings.Contains(out, "no available IPv4 addresses"), check.Equals, true,
   703  				check.Commentf("Could not run a Container : %s %s", err.Error(), out))
   704  		}
   705  	}
   706  }
   707  
   708  func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr2(c *check.C) {
   709  	d := s.d
   710  
   711  	bridgeName := "external-bridge"
   712  	bridgeIP := "10.2.2.1/16"
   713  
   714  	createInterface(c, "bridge", bridgeName, bridgeIP)
   715  	defer deleteInterface(c, bridgeName)
   716  
   717  	d.StartWithBusybox(c, "--bip", bridgeIP, "--fixed-cidr", "10.2.2.0/24")
   718  	defer s.d.Restart(c)
   719  
   720  	out, err := d.Cmd("run", "-d", "--name", "bb", "busybox", "top")
   721  	c.Assert(err, checker.IsNil, check.Commentf(out))
   722  	defer d.Cmd("stop", "bb")
   723  
   724  	out, err = d.Cmd("exec", "bb", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
   725  	c.Assert(out, checker.Equals, "10.2.2.0\n")
   726  
   727  	out, err = d.Cmd("run", "--rm", "busybox", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
   728  	c.Assert(err, checker.IsNil, check.Commentf(out))
   729  	c.Assert(out, checker.Equals, "10.2.2.2\n")
   730  }
   731  
   732  func (s *DockerDaemonSuite) TestDaemonBridgeFixedCIDREqualBridgeNetwork(c *check.C) {
   733  	d := s.d
   734  
   735  	bridgeName := "external-bridge"
   736  	bridgeIP := "172.27.42.1/16"
   737  
   738  	createInterface(c, "bridge", bridgeName, bridgeIP)
   739  	defer deleteInterface(c, bridgeName)
   740  
   741  	d.StartWithBusybox(c, "--bridge", bridgeName, "--fixed-cidr", bridgeIP)
   742  	defer s.d.Restart(c)
   743  
   744  	out, err := d.Cmd("run", "-d", "busybox", "top")
   745  	c.Assert(err, check.IsNil, check.Commentf(out))
   746  	cid1 := strings.TrimSpace(out)
   747  	defer d.Cmd("stop", cid1)
   748  }
   749  
   750  func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
   751  	defaultNetworkBridge := "docker0"
   752  	deleteInterface(c, defaultNetworkBridge)
   753  
   754  	d := s.d
   755  
   756  	bridgeIP := "192.169.1.1"
   757  	bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
   758  
   759  	d.StartWithBusybox(c, "--bip", bridgeIPNet)
   760  	defer d.Restart(c)
   761  
   762  	expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP)
   763  	out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
   764  	c.Assert(err, checker.IsNil)
   765  	c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
   766  		check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'",
   767  			bridgeIP, strings.TrimSpace(out)))
   768  	deleteInterface(c, defaultNetworkBridge)
   769  }
   770  
   771  func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
   772  	defaultNetworkBridge := "docker0"
   773  	deleteInterface(c, defaultNetworkBridge)
   774  
   775  	d := s.d
   776  
   777  	bridgeIP := "192.169.1.1"
   778  	bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
   779  	gatewayIP := "192.169.1.254"
   780  
   781  	d.StartWithBusybox(c, "--bip", bridgeIPNet, "--default-gateway", gatewayIP)
   782  	defer d.Restart(c)
   783  
   784  	expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP)
   785  	out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
   786  	c.Assert(err, checker.IsNil)
   787  	c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
   788  		check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
   789  			gatewayIP, strings.TrimSpace(out)))
   790  	deleteInterface(c, defaultNetworkBridge)
   791  }
   792  
   793  func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) {
   794  	defaultNetworkBridge := "docker0"
   795  	deleteInterface(c, defaultNetworkBridge)
   796  
   797  	// Program a custom default gateway outside of the container subnet, daemon should accept it and start
   798  	s.d.StartWithBusybox(c, "--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254")
   799  
   800  	deleteInterface(c, defaultNetworkBridge)
   801  	s.d.Restart(c)
   802  }
   803  
   804  func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *check.C) {
   805  	testRequires(c, DaemonIsLinux, SameHostDaemon)
   806  
   807  	// Start daemon without docker0 bridge
   808  	defaultNetworkBridge := "docker0"
   809  	deleteInterface(c, defaultNetworkBridge)
   810  
   811  	discoveryBackend := "consul://consuladdr:consulport/some/path"
   812  	s.d.Start(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend))
   813  
   814  	// Start daemon with docker0 bridge
   815  	result := icmd.RunCommand("ifconfig", defaultNetworkBridge)
   816  	c.Assert(result, icmd.Matches, icmd.Success)
   817  
   818  	s.d.Restart(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend))
   819  }
   820  
   821  func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
   822  	d := s.d
   823  
   824  	ipStr := "192.170.1.1/24"
   825  	ip, _, _ := net.ParseCIDR(ipStr)
   826  	args := []string{"--ip", ip.String()}
   827  	d.StartWithBusybox(c, args...)
   828  	defer d.Restart(c)
   829  
   830  	out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
   831  	c.Assert(err, check.NotNil,
   832  		check.Commentf("Running a container must fail with an invalid --ip option"))
   833  	c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
   834  
   835  	ifName := "dummy"
   836  	createInterface(c, "dummy", ifName, ipStr)
   837  	defer deleteInterface(c, ifName)
   838  
   839  	_, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
   840  	c.Assert(err, check.IsNil)
   841  
   842  	result := icmd.RunCommand("iptables", "-t", "nat", "-nvL")
   843  	result.Assert(c, icmd.Success)
   844  	regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
   845  	matched, _ := regexp.MatchString(regex, result.Combined())
   846  	c.Assert(matched, check.Equals, true,
   847  		check.Commentf("iptables output should have contained %q, but was %q", regex, result.Combined()))
   848  }
   849  
   850  func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
   851  	testRequires(c, bridgeNfIptables)
   852  	d := s.d
   853  
   854  	bridgeName := "external-bridge"
   855  	bridgeIP := "192.169.1.1/24"
   856  
   857  	createInterface(c, "bridge", bridgeName, bridgeIP)
   858  	defer deleteInterface(c, bridgeName)
   859  
   860  	d.StartWithBusybox(c, "--bridge", bridgeName, "--icc=false")
   861  	defer d.Restart(c)
   862  
   863  	result := icmd.RunCommand("iptables", "-nvL", "FORWARD")
   864  	result.Assert(c, icmd.Success)
   865  	regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
   866  	matched, _ := regexp.MatchString(regex, result.Combined())
   867  	c.Assert(matched, check.Equals, true,
   868  		check.Commentf("iptables output should have contained %q, but was %q", regex, result.Combined()))
   869  
   870  	// Pinging another container must fail with --icc=false
   871  	pingContainers(c, d, true)
   872  
   873  	ipStr := "192.171.1.1/24"
   874  	ip, _, _ := net.ParseCIDR(ipStr)
   875  	ifName := "icc-dummy"
   876  
   877  	createInterface(c, "dummy", ifName, ipStr)
   878  
   879  	// But, Pinging external or a Host interface must succeed
   880  	pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
   881  	runArgs := []string{"run", "--rm", "busybox", "sh", "-c", pingCmd}
   882  	_, err := d.Cmd(runArgs...)
   883  	c.Assert(err, check.IsNil)
   884  }
   885  
   886  func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
   887  	d := s.d
   888  
   889  	bridgeName := "external-bridge"
   890  	bridgeIP := "192.169.1.1/24"
   891  
   892  	createInterface(c, "bridge", bridgeName, bridgeIP)
   893  	defer deleteInterface(c, bridgeName)
   894  
   895  	d.StartWithBusybox(c, "--bridge", bridgeName, "--icc=false")
   896  	defer d.Restart(c)
   897  
   898  	result := icmd.RunCommand("iptables", "-nvL", "FORWARD")
   899  	result.Assert(c, icmd.Success)
   900  	regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
   901  	matched, _ := regexp.MatchString(regex, result.Combined())
   902  	c.Assert(matched, check.Equals, true,
   903  		check.Commentf("iptables output should have contained %q, but was %q", regex, result.Combined()))
   904  
   905  	out, err := d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
   906  	c.Assert(err, check.IsNil, check.Commentf(out))
   907  
   908  	out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
   909  	c.Assert(err, check.IsNil, check.Commentf(out))
   910  }
   911  
   912  func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
   913  	bridgeName := "external-bridge"
   914  	bridgeIP := "192.169.1.1/24"
   915  
   916  	createInterface(c, "bridge", bridgeName, bridgeIP)
   917  	defer deleteInterface(c, bridgeName)
   918  
   919  	s.d.StartWithBusybox(c, "--bridge", bridgeName, "--icc=false")
   920  	defer s.d.Restart(c)
   921  
   922  	_, err := s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
   923  	c.Assert(err, check.IsNil)
   924  	_, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
   925  	c.Assert(err, check.IsNil)
   926  
   927  	childIP, err := s.d.FindContainerIP("child")
   928  	c.Assert(err, checker.IsNil)
   929  	parentIP, err := s.d.FindContainerIP("parent")
   930  	c.Assert(err, checker.IsNil)
   931  
   932  	sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
   933  	destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
   934  	if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
   935  		c.Fatal("Iptables rules not found")
   936  	}
   937  
   938  	s.d.Cmd("rm", "--link", "parent/http")
   939  	if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
   940  		c.Fatal("Iptables rules should be removed when unlink")
   941  	}
   942  
   943  	s.d.Cmd("kill", "child")
   944  	s.d.Cmd("kill", "parent")
   945  }
   946  
   947  func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
   948  	testRequires(c, DaemonIsLinux)
   949  
   950  	s.d.StartWithBusybox(c, "--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024")
   951  
   952  	out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
   953  	if err != nil {
   954  		c.Fatal(out, err)
   955  	}
   956  
   957  	outArr := strings.Split(out, "\n")
   958  	if len(outArr) < 2 {
   959  		c.Fatalf("got unexpected output: %s", out)
   960  	}
   961  	nofile := strings.TrimSpace(outArr[0])
   962  	nproc := strings.TrimSpace(outArr[1])
   963  
   964  	if nofile != "42" {
   965  		c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
   966  	}
   967  	if nproc != "2048" {
   968  		c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
   969  	}
   970  
   971  	// Now restart daemon with a new default
   972  	s.d.Restart(c, "--default-ulimit", "nofile=43")
   973  
   974  	out, err = s.d.Cmd("start", "-a", "test")
   975  	if err != nil {
   976  		c.Fatal(err)
   977  	}
   978  
   979  	outArr = strings.Split(out, "\n")
   980  	if len(outArr) < 2 {
   981  		c.Fatalf("got unexpected output: %s", out)
   982  	}
   983  	nofile = strings.TrimSpace(outArr[0])
   984  	nproc = strings.TrimSpace(outArr[1])
   985  
   986  	if nofile != "43" {
   987  		c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
   988  	}
   989  	if nproc != "2048" {
   990  		c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
   991  	}
   992  }
   993  
   994  // #11315
   995  func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
   996  	s.d.StartWithBusybox(c)
   997  
   998  	if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
   999  		c.Fatal(err, out)
  1000  	}
  1001  
  1002  	if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  1003  		c.Fatal(err, out)
  1004  	}
  1005  
  1006  	s.d.Restart(c)
  1007  
  1008  	if out, err := s.d.Cmd("start", "test2"); err != nil {
  1009  		c.Fatal(err, out)
  1010  	}
  1011  }
  1012  
  1013  func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  1014  	s.d.StartWithBusybox(c)
  1015  
  1016  	out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline")
  1017  	c.Assert(err, check.IsNil, check.Commentf(out))
  1018  	id, err := s.d.GetIDByName("test")
  1019  	c.Assert(err, check.IsNil)
  1020  
  1021  	logPath := filepath.Join(s.d.Root, "containers", id, id+"-json.log")
  1022  
  1023  	if _, err := os.Stat(logPath); err != nil {
  1024  		c.Fatal(err)
  1025  	}
  1026  	f, err := os.Open(logPath)
  1027  	if err != nil {
  1028  		c.Fatal(err)
  1029  	}
  1030  	defer f.Close()
  1031  
  1032  	var res struct {
  1033  		Log    string    `json:"log"`
  1034  		Stream string    `json:"stream"`
  1035  		Time   time.Time `json:"time"`
  1036  	}
  1037  	if err := json.NewDecoder(f).Decode(&res); err != nil {
  1038  		c.Fatal(err)
  1039  	}
  1040  	if res.Log != "testline\n" {
  1041  		c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  1042  	}
  1043  	if res.Stream != "stdout" {
  1044  		c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  1045  	}
  1046  	if !time.Now().After(res.Time) {
  1047  		c.Fatalf("Log time %v in future", res.Time)
  1048  	}
  1049  }
  1050  
  1051  func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  1052  	s.d.StartWithBusybox(c)
  1053  
  1054  	out, err := s.d.Cmd("run", "--name=test", "--log-driver=none", "busybox", "echo", "testline")
  1055  	if err != nil {
  1056  		c.Fatal(out, err)
  1057  	}
  1058  	id, err := s.d.GetIDByName("test")
  1059  	c.Assert(err, check.IsNil)
  1060  
  1061  	logPath := filepath.Join(s.d.Root, "containers", id, id+"-json.log")
  1062  
  1063  	if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  1064  		c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  1065  	}
  1066  }
  1067  
  1068  func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  1069  	s.d.StartWithBusybox(c, "--log-driver=none")
  1070  
  1071  	out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline")
  1072  	if err != nil {
  1073  		c.Fatal(out, err)
  1074  	}
  1075  	id, err := s.d.GetIDByName("test")
  1076  	c.Assert(err, check.IsNil)
  1077  
  1078  	logPath := filepath.Join(s.d.Root, "containers", id, id+"-json.log")
  1079  
  1080  	if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  1081  		c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  1082  	}
  1083  }
  1084  
  1085  func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  1086  	s.d.StartWithBusybox(c, "--log-driver=none")
  1087  
  1088  	out, err := s.d.Cmd("run", "--name=test", "--log-driver=json-file", "busybox", "echo", "testline")
  1089  	if err != nil {
  1090  		c.Fatal(out, err)
  1091  	}
  1092  	id, err := s.d.GetIDByName("test")
  1093  	c.Assert(err, check.IsNil)
  1094  
  1095  	logPath := filepath.Join(s.d.Root, "containers", id, id+"-json.log")
  1096  
  1097  	if _, err := os.Stat(logPath); err != nil {
  1098  		c.Fatal(err)
  1099  	}
  1100  	f, err := os.Open(logPath)
  1101  	if err != nil {
  1102  		c.Fatal(err)
  1103  	}
  1104  	defer f.Close()
  1105  
  1106  	var res struct {
  1107  		Log    string    `json:"log"`
  1108  		Stream string    `json:"stream"`
  1109  		Time   time.Time `json:"time"`
  1110  	}
  1111  	if err := json.NewDecoder(f).Decode(&res); err != nil {
  1112  		c.Fatal(err)
  1113  	}
  1114  	if res.Log != "testline\n" {
  1115  		c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  1116  	}
  1117  	if res.Stream != "stdout" {
  1118  		c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  1119  	}
  1120  	if !time.Now().After(res.Time) {
  1121  		c.Fatalf("Log time %v in future", res.Time)
  1122  	}
  1123  }
  1124  
  1125  func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  1126  	s.d.StartWithBusybox(c, "--log-driver=none")
  1127  
  1128  	out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline")
  1129  	c.Assert(err, checker.IsNil, check.Commentf(out))
  1130  
  1131  	out, err = s.d.Cmd("logs", "test")
  1132  	c.Assert(err, check.NotNil, check.Commentf("Logs should fail with 'none' driver"))
  1133  	expected := `configured logging driver does not support reading`
  1134  	c.Assert(out, checker.Contains, expected)
  1135  }
  1136  
  1137  func (s *DockerDaemonSuite) TestDaemonLoggingDriverShouldBeIgnoredForBuild(c *check.C) {
  1138  	s.d.StartWithBusybox(c, "--log-driver=splunk")
  1139  
  1140  	out, err := s.d.Cmd("build")
  1141  	out, code, err := s.d.BuildImageWithOut("busyboxs", `
  1142          FROM busybox
  1143          RUN echo foo`, false)
  1144  	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err)
  1145  	c.Assert(err, check.IsNil, comment)
  1146  	c.Assert(code, check.Equals, 0, comment)
  1147  	c.Assert(out, checker.Contains, "foo", comment)
  1148  }
  1149  
  1150  func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  1151  	dir, err := ioutil.TempDir("", "socket-cleanup-test")
  1152  	if err != nil {
  1153  		c.Fatal(err)
  1154  	}
  1155  	defer os.RemoveAll(dir)
  1156  
  1157  	sockPath := filepath.Join(dir, "docker.sock")
  1158  	s.d.Start(c, "--host", "unix://"+sockPath)
  1159  
  1160  	if _, err := os.Stat(sockPath); err != nil {
  1161  		c.Fatal("socket does not exist")
  1162  	}
  1163  
  1164  	s.d.Stop(c)
  1165  
  1166  	if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  1167  		c.Fatal("unix socket is not cleaned up")
  1168  	}
  1169  }
  1170  
  1171  func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  1172  	type Config struct {
  1173  		Crv string `json:"crv"`
  1174  		D   string `json:"d"`
  1175  		Kid string `json:"kid"`
  1176  		Kty string `json:"kty"`
  1177  		X   string `json:"x"`
  1178  		Y   string `json:"y"`
  1179  	}
  1180  
  1181  	os.Remove("/etc/docker/key.json")
  1182  	s.d.Start(c)
  1183  	s.d.Stop(c)
  1184  
  1185  	config := &Config{}
  1186  	bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  1187  	if err != nil {
  1188  		c.Fatalf("Error reading key.json file: %s", err)
  1189  	}
  1190  
  1191  	// byte[] to Data-Struct
  1192  	if err := json.Unmarshal(bytes, &config); err != nil {
  1193  		c.Fatalf("Error Unmarshal: %s", err)
  1194  	}
  1195  
  1196  	//replace config.Kid with the fake value
  1197  	config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  1198  
  1199  	// NEW Data-Struct to byte[]
  1200  	newBytes, err := json.Marshal(&config)
  1201  	if err != nil {
  1202  		c.Fatalf("Error Marshal: %s", err)
  1203  	}
  1204  
  1205  	// write back
  1206  	if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  1207  		c.Fatalf("Error ioutil.WriteFile: %s", err)
  1208  	}
  1209  
  1210  	defer os.Remove("/etc/docker/key.json")
  1211  
  1212  	if err := s.d.StartWithError(); err == nil {
  1213  		c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  1214  	}
  1215  
  1216  	content, err := s.d.ReadLogFile()
  1217  	c.Assert(err, checker.IsNil)
  1218  
  1219  	if !strings.Contains(string(content), "Public Key ID does not match") {
  1220  		c.Fatalf("Missing KeyID message from daemon logs: %s", string(content))
  1221  	}
  1222  }
  1223  
  1224  func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  1225  	s.d.StartWithBusybox(c)
  1226  
  1227  	out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  1228  	if err != nil {
  1229  		c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  1230  	}
  1231  	containerID := strings.TrimSpace(out)
  1232  
  1233  	if out, err := s.d.Cmd("kill", containerID); err != nil {
  1234  		c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  1235  	}
  1236  
  1237  	s.d.Restart(c)
  1238  
  1239  	errchan := make(chan error)
  1240  	go func() {
  1241  		if out, err := s.d.Cmd("wait", containerID); err != nil {
  1242  			errchan <- fmt.Errorf("%v:\n%s", err, out)
  1243  		}
  1244  		close(errchan)
  1245  	}()
  1246  
  1247  	select {
  1248  	case <-time.After(5 * time.Second):
  1249  		c.Fatal("Waiting on a stopped (killed) container timed out")
  1250  	case err := <-errchan:
  1251  		if err != nil {
  1252  			c.Fatal(err)
  1253  		}
  1254  	}
  1255  }
  1256  
  1257  // TestHTTPSInfo connects via two-way authenticated HTTPS to the info endpoint
  1258  func (s *DockerDaemonSuite) TestHTTPSInfo(c *check.C) {
  1259  	const (
  1260  		testDaemonHTTPSAddr = "tcp://localhost:4271"
  1261  	)
  1262  
  1263  	s.d.Start(c,
  1264  		"--tlsverify",
  1265  		"--tlscacert", "fixtures/https/ca.pem",
  1266  		"--tlscert", "fixtures/https/server-cert.pem",
  1267  		"--tlskey", "fixtures/https/server-key.pem",
  1268  		"-H", testDaemonHTTPSAddr)
  1269  
  1270  	args := []string{
  1271  		"--host", testDaemonHTTPSAddr,
  1272  		"--tlsverify",
  1273  		"--tlscacert", "fixtures/https/ca.pem",
  1274  		"--tlscert", "fixtures/https/client-cert.pem",
  1275  		"--tlskey", "fixtures/https/client-key.pem",
  1276  		"info",
  1277  	}
  1278  	out, err := s.d.Cmd(args...)
  1279  	if err != nil {
  1280  		c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1281  	}
  1282  }
  1283  
  1284  // TestHTTPSRun connects via two-way authenticated HTTPS to the create, attach, start, and wait endpoints.
  1285  // https://github.com/docker/docker/issues/19280
  1286  func (s *DockerDaemonSuite) TestHTTPSRun(c *check.C) {
  1287  	const (
  1288  		testDaemonHTTPSAddr = "tcp://localhost:4271"
  1289  	)
  1290  
  1291  	s.d.StartWithBusybox(c, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1292  		"--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr)
  1293  
  1294  	args := []string{
  1295  		"--host", testDaemonHTTPSAddr,
  1296  		"--tlsverify", "--tlscacert", "fixtures/https/ca.pem",
  1297  		"--tlscert", "fixtures/https/client-cert.pem",
  1298  		"--tlskey", "fixtures/https/client-key.pem",
  1299  		"run", "busybox", "echo", "TLS response",
  1300  	}
  1301  	out, err := s.d.Cmd(args...)
  1302  	if err != nil {
  1303  		c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1304  	}
  1305  
  1306  	if !strings.Contains(out, "TLS response") {
  1307  		c.Fatalf("expected output to include `TLS response`, got %v", out)
  1308  	}
  1309  }
  1310  
  1311  // TestTLSVerify verifies that --tlsverify=false turns on tls
  1312  func (s *DockerDaemonSuite) TestTLSVerify(c *check.C) {
  1313  	out, err := exec.Command(dockerdBinary, "--tlsverify=false").CombinedOutput()
  1314  	if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") {
  1315  		c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out))
  1316  	}
  1317  }
  1318  
  1319  // TestHTTPSInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  1320  // by using a rogue client certificate and checks that it fails with the expected error.
  1321  func (s *DockerDaemonSuite) TestHTTPSInfoRogueCert(c *check.C) {
  1322  	const (
  1323  		errBadCertificate   = "bad certificate"
  1324  		testDaemonHTTPSAddr = "tcp://localhost:4271"
  1325  	)
  1326  
  1327  	s.d.Start(c,
  1328  		"--tlsverify",
  1329  		"--tlscacert", "fixtures/https/ca.pem",
  1330  		"--tlscert", "fixtures/https/server-cert.pem",
  1331  		"--tlskey", "fixtures/https/server-key.pem",
  1332  		"-H", testDaemonHTTPSAddr)
  1333  
  1334  	args := []string{
  1335  		"--host", testDaemonHTTPSAddr,
  1336  		"--tlsverify",
  1337  		"--tlscacert", "fixtures/https/ca.pem",
  1338  		"--tlscert", "fixtures/https/client-rogue-cert.pem",
  1339  		"--tlskey", "fixtures/https/client-rogue-key.pem",
  1340  		"info",
  1341  	}
  1342  	out, err := s.d.Cmd(args...)
  1343  	if err == nil || !strings.Contains(out, errBadCertificate) {
  1344  		c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  1345  	}
  1346  }
  1347  
  1348  // TestHTTPSInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  1349  // which provides a rogue server certificate and checks that it fails with the expected error
  1350  func (s *DockerDaemonSuite) TestHTTPSInfoRogueServerCert(c *check.C) {
  1351  	const (
  1352  		errCaUnknown             = "x509: certificate signed by unknown authority"
  1353  		testDaemonRogueHTTPSAddr = "tcp://localhost:4272"
  1354  	)
  1355  	s.d.Start(c,
  1356  		"--tlsverify",
  1357  		"--tlscacert", "fixtures/https/ca.pem",
  1358  		"--tlscert", "fixtures/https/server-rogue-cert.pem",
  1359  		"--tlskey", "fixtures/https/server-rogue-key.pem",
  1360  		"-H", testDaemonRogueHTTPSAddr)
  1361  
  1362  	args := []string{
  1363  		"--host", testDaemonRogueHTTPSAddr,
  1364  		"--tlsverify",
  1365  		"--tlscacert", "fixtures/https/ca.pem",
  1366  		"--tlscert", "fixtures/https/client-rogue-cert.pem",
  1367  		"--tlskey", "fixtures/https/client-rogue-key.pem",
  1368  		"info",
  1369  	}
  1370  	out, err := s.d.Cmd(args...)
  1371  	if err == nil || !strings.Contains(out, errCaUnknown) {
  1372  		c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  1373  	}
  1374  }
  1375  
  1376  func pingContainers(c *check.C, d *daemon.Daemon, expectFailure bool) {
  1377  	var dargs []string
  1378  	if d != nil {
  1379  		dargs = []string{"--host", d.Sock()}
  1380  	}
  1381  
  1382  	args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  1383  	dockerCmd(c, args...)
  1384  
  1385  	args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  1386  	pingCmd := "ping -c 1 %s -W 1"
  1387  	args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  1388  	_, _, err := dockerCmdWithError(args...)
  1389  
  1390  	if expectFailure {
  1391  		c.Assert(err, check.NotNil)
  1392  	} else {
  1393  		c.Assert(err, check.IsNil)
  1394  	}
  1395  
  1396  	args = append(dargs, "rm", "-f", "container1")
  1397  	dockerCmd(c, args...)
  1398  }
  1399  
  1400  func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  1401  	s.d.StartWithBusybox(c)
  1402  
  1403  	socket := filepath.Join(s.d.Folder, "docker.sock")
  1404  
  1405  	out, err := s.d.Cmd("run", "--restart=always", "-v", socket+":/sock", "busybox")
  1406  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1407  	s.d.Restart(c)
  1408  }
  1409  
  1410  // os.Kill should kill daemon ungracefully, leaving behind container mounts.
  1411  // A subsequent daemon restart shoud clean up said mounts.
  1412  func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonAndContainerKill(c *check.C) {
  1413  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  1414  		Experimental: testEnv.ExperimentalDaemon(),
  1415  	})
  1416  	d.StartWithBusybox(c)
  1417  
  1418  	out, err := d.Cmd("run", "-d", "busybox", "top")
  1419  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1420  	id := strings.TrimSpace(out)
  1421  	c.Assert(d.Signal(os.Kill), check.IsNil)
  1422  	mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1423  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1424  
  1425  	// container mounts should exist even after daemon has crashed.
  1426  	comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, d.Root, mountOut)
  1427  	c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment)
  1428  
  1429  	// kill the container
  1430  	icmd.RunCommand(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", id).Assert(c, icmd.Success)
  1431  
  1432  	// restart daemon.
  1433  	d.Restart(c)
  1434  
  1435  	// Now, container mounts should be gone.
  1436  	mountOut, err = ioutil.ReadFile("/proc/self/mountinfo")
  1437  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1438  	comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, d.Root, mountOut)
  1439  	c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1440  
  1441  	d.Stop(c)
  1442  }
  1443  
  1444  // os.Interrupt should perform a graceful daemon shutdown and hence cleanup mounts.
  1445  func (s *DockerDaemonSuite) TestCleanupMountsAfterGracefulShutdown(c *check.C) {
  1446  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  1447  		Experimental: testEnv.ExperimentalDaemon(),
  1448  	})
  1449  	d.StartWithBusybox(c)
  1450  
  1451  	out, err := d.Cmd("run", "-d", "busybox", "top")
  1452  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1453  	id := strings.TrimSpace(out)
  1454  
  1455  	// Send SIGINT and daemon should clean up
  1456  	c.Assert(d.Signal(os.Interrupt), check.IsNil)
  1457  	// Wait for the daemon to stop.
  1458  	c.Assert(<-d.Wait, checker.IsNil)
  1459  
  1460  	mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1461  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1462  
  1463  	comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, d.Root, mountOut)
  1464  	c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1465  }
  1466  
  1467  func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1468  	testRequires(c, DaemonIsLinux, NotUserNamespace)
  1469  	s.d.StartWithBusybox(c, "-b", "none")
  1470  
  1471  	out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1472  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1473  	c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1474  		check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
  1475  
  1476  	out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
  1477  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1478  	c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1479  		check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
  1480  	// the extra grep and awk clean up the output of `ip` to only list the number and name of
  1481  	// interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
  1482  	// be used while still verifying that the interface list is the exact same
  1483  	cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1484  	stdout := bytes.NewBuffer(nil)
  1485  	cmd.Stdout = stdout
  1486  	if err := cmd.Run(); err != nil {
  1487  		c.Fatal("Failed to get host network interface")
  1488  	}
  1489  	out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1490  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1491  	c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
  1492  		check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
  1493  }
  1494  
  1495  func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1496  	s.d.StartWithBusybox(t)
  1497  	if out, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top"); err != nil {
  1498  		t.Fatal(out, err)
  1499  	}
  1500  
  1501  	s.d.Restart(t)
  1502  	// Container 'test' should be removed without error
  1503  	if out, err := s.d.Cmd("rm", "test"); err != nil {
  1504  		t.Fatal(out, err)
  1505  	}
  1506  }
  1507  
  1508  func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) {
  1509  	s.d.StartWithBusybox(c)
  1510  	out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top")
  1511  	if err != nil {
  1512  		c.Fatal(out, err)
  1513  	}
  1514  
  1515  	// Get sandbox key via inspect
  1516  	out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns")
  1517  	if err != nil {
  1518  		c.Fatalf("Error inspecting container: %s, %v", out, err)
  1519  	}
  1520  	fileName := strings.Trim(out, " \r\n'")
  1521  
  1522  	if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1523  		c.Fatal(out, err)
  1524  	}
  1525  
  1526  	// Test if the file still exists
  1527  	icmd.RunCommand("stat", "-c", "%n", fileName).Assert(c, icmd.Expected{
  1528  		Out: fileName,
  1529  	})
  1530  
  1531  	// Remove the container and restart the daemon
  1532  	if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1533  		c.Fatal(out, err)
  1534  	}
  1535  
  1536  	s.d.Restart(c)
  1537  
  1538  	// Test again and see now the netns file does not exist
  1539  	icmd.RunCommand("stat", "-c", "%n", fileName).Assert(c, icmd.Expected{
  1540  		Err:      "No such file or directory",
  1541  		ExitCode: 1,
  1542  	})
  1543  }
  1544  
  1545  // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1546  func (s *DockerDaemonSuite) TestDaemonTLSVerifyIssue13964(c *check.C) {
  1547  	host := "tcp://localhost:4271"
  1548  	s.d.Start(c, "-H", host)
  1549  	icmd.RunCmd(icmd.Cmd{
  1550  		Command: []string{dockerBinary, "-H", host, "info"},
  1551  		Env:     []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"},
  1552  	}).Assert(c, icmd.Expected{
  1553  		ExitCode: 1,
  1554  		Err:      "error during connect",
  1555  	})
  1556  }
  1557  
  1558  func setupV6(c *check.C) {
  1559  	// Hack to get the right IPv6 address on docker0, which has already been created
  1560  	result := icmd.RunCommand("ip", "addr", "add", "fe80::1/64", "dev", "docker0")
  1561  	result.Assert(c, icmd.Success)
  1562  }
  1563  
  1564  func teardownV6(c *check.C) {
  1565  	result := icmd.RunCommand("ip", "addr", "del", "fe80::1/64", "dev", "docker0")
  1566  	result.Assert(c, icmd.Success)
  1567  }
  1568  
  1569  func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1570  	s.d.StartWithBusybox(c)
  1571  
  1572  	out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1573  	c.Assert(err, check.IsNil)
  1574  	id := strings.TrimSpace(out)
  1575  
  1576  	_, err = s.d.Cmd("stop", id)
  1577  	c.Assert(err, check.IsNil)
  1578  	_, err = s.d.Cmd("wait", id)
  1579  	c.Assert(err, check.IsNil)
  1580  
  1581  	out, err = s.d.Cmd("ps", "-q")
  1582  	c.Assert(err, check.IsNil)
  1583  	c.Assert(out, check.Equals, "")
  1584  
  1585  	s.d.Restart(c)
  1586  
  1587  	out, err = s.d.Cmd("ps", "-q")
  1588  	c.Assert(err, check.IsNil)
  1589  	c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1590  }
  1591  
  1592  func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
  1593  	s.d.StartWithBusybox(c, "--log-opt=max-size=1k")
  1594  	name := "logtest"
  1595  	out, err := s.d.Cmd("run", "-d", "--log-opt=max-file=5", "--name", name, "busybox", "top")
  1596  	c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
  1597  
  1598  	out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", name)
  1599  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1600  	c.Assert(out, checker.Contains, "max-size:1k")
  1601  	c.Assert(out, checker.Contains, "max-file:5")
  1602  
  1603  	out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Type }}", name)
  1604  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1605  	c.Assert(strings.TrimSpace(out), checker.Equals, "json-file")
  1606  }
  1607  
  1608  func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
  1609  	s.d.StartWithBusybox(c)
  1610  	if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil {
  1611  		c.Fatal(err, out)
  1612  	}
  1613  	if out, err := s.d.Cmd("pause", "test"); err != nil {
  1614  		c.Fatal(err, out)
  1615  	}
  1616  	s.d.Restart(c)
  1617  
  1618  	errchan := make(chan error)
  1619  	go func() {
  1620  		out, err := s.d.Cmd("start", "test")
  1621  		if err != nil {
  1622  			errchan <- fmt.Errorf("%v:\n%s", err, out)
  1623  		}
  1624  		name := strings.TrimSpace(out)
  1625  		if name != "test" {
  1626  			errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name)
  1627  		}
  1628  		close(errchan)
  1629  	}()
  1630  
  1631  	select {
  1632  	case <-time.After(5 * time.Second):
  1633  		c.Fatal("Waiting on start a container timed out")
  1634  	case err := <-errchan:
  1635  		if err != nil {
  1636  			c.Fatal(err)
  1637  		}
  1638  	}
  1639  }
  1640  
  1641  func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) {
  1642  	s.d.StartWithBusybox(c)
  1643  
  1644  	out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox")
  1645  	c.Assert(err, check.IsNil, check.Commentf(out))
  1646  
  1647  	s.d.Restart(c)
  1648  
  1649  	out, err = s.d.Cmd("volume", "rm", "test")
  1650  	c.Assert(err, check.NotNil, check.Commentf("should not be able to remove in use volume after daemon restart"))
  1651  	c.Assert(out, checker.Contains, "in use")
  1652  }
  1653  
  1654  func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
  1655  	s.d.Start(c)
  1656  
  1657  	_, err := s.d.Cmd("volume", "create", "test")
  1658  	c.Assert(err, check.IsNil)
  1659  	s.d.Restart(c)
  1660  
  1661  	_, err = s.d.Cmd("volume", "inspect", "test")
  1662  	c.Assert(err, check.IsNil)
  1663  }
  1664  
  1665  // FIXME(vdemeester) should be a unit test
  1666  func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
  1667  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  1668  		Experimental: testEnv.ExperimentalDaemon(),
  1669  	})
  1670  	c.Assert(d.StartWithError("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil)
  1671  	expected := "Failed to set log opts: syslog-address should be in form proto://address"
  1672  	icmd.RunCommand("grep", expected, d.LogFileName()).Assert(c, icmd.Success)
  1673  }
  1674  
  1675  // FIXME(vdemeester) should be a unit test
  1676  func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
  1677  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  1678  		Experimental: testEnv.ExperimentalDaemon(),
  1679  	})
  1680  	c.Assert(d.StartWithError("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
  1681  	expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
  1682  	icmd.RunCommand("grep", expected, d.LogFileName()).Assert(c, icmd.Success)
  1683  }
  1684  
  1685  // FIXME(vdemeester) Use a new daemon instance instead of the Suite one
  1686  func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
  1687  	s.d.UseDefaultHost = true
  1688  	defer func() {
  1689  		s.d.UseDefaultHost = false
  1690  	}()
  1691  	s.d.Start(c)
  1692  }
  1693  
  1694  // FIXME(vdemeester) Use a new daemon instance instead of the Suite one
  1695  func (s *DockerDaemonSuite) TestDaemonStartWithDefaultTLSHost(c *check.C) {
  1696  	s.d.UseDefaultTLSHost = true
  1697  	defer func() {
  1698  		s.d.UseDefaultTLSHost = false
  1699  	}()
  1700  	s.d.Start(c,
  1701  		"--tlsverify",
  1702  		"--tlscacert", "fixtures/https/ca.pem",
  1703  		"--tlscert", "fixtures/https/server-cert.pem",
  1704  		"--tlskey", "fixtures/https/server-key.pem")
  1705  
  1706  	// The client with --tlsverify should also use default host localhost:2376
  1707  	tmpHost := os.Getenv("DOCKER_HOST")
  1708  	defer func() {
  1709  		os.Setenv("DOCKER_HOST", tmpHost)
  1710  	}()
  1711  
  1712  	os.Setenv("DOCKER_HOST", "")
  1713  
  1714  	out, _ := dockerCmd(
  1715  		c,
  1716  		"--tlsverify",
  1717  		"--tlscacert", "fixtures/https/ca.pem",
  1718  		"--tlscert", "fixtures/https/client-cert.pem",
  1719  		"--tlskey", "fixtures/https/client-key.pem",
  1720  		"version",
  1721  	)
  1722  	if !strings.Contains(out, "Server") {
  1723  		c.Fatalf("docker version should return information of server side")
  1724  	}
  1725  
  1726  	// ensure when connecting to the server that only a single acceptable CA is requested
  1727  	contents, err := ioutil.ReadFile("fixtures/https/ca.pem")
  1728  	c.Assert(err, checker.IsNil)
  1729  	rootCert, err := helpers.ParseCertificatePEM(contents)
  1730  	c.Assert(err, checker.IsNil)
  1731  	rootPool := x509.NewCertPool()
  1732  	rootPool.AddCert(rootCert)
  1733  
  1734  	var certRequestInfo *tls.CertificateRequestInfo
  1735  	conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort), &tls.Config{
  1736  		RootCAs: rootPool,
  1737  		GetClientCertificate: func(cri *tls.CertificateRequestInfo) (*tls.Certificate, error) {
  1738  			certRequestInfo = cri
  1739  			cert, err := tls.LoadX509KeyPair("fixtures/https/client-cert.pem", "fixtures/https/client-key.pem")
  1740  			if err != nil {
  1741  				return nil, err
  1742  			}
  1743  			return &cert, nil
  1744  		},
  1745  	})
  1746  	c.Assert(err, checker.IsNil)
  1747  	conn.Close()
  1748  
  1749  	c.Assert(certRequestInfo, checker.NotNil)
  1750  	c.Assert(certRequestInfo.AcceptableCAs, checker.HasLen, 1)
  1751  	c.Assert(certRequestInfo.AcceptableCAs[0], checker.DeepEquals, rootCert.RawSubject)
  1752  }
  1753  
  1754  func (s *DockerDaemonSuite) TestBridgeIPIsExcludedFromAllocatorPool(c *check.C) {
  1755  	defaultNetworkBridge := "docker0"
  1756  	deleteInterface(c, defaultNetworkBridge)
  1757  
  1758  	bridgeIP := "192.169.1.1"
  1759  	bridgeRange := bridgeIP + "/30"
  1760  
  1761  	s.d.StartWithBusybox(c, "--bip", bridgeRange)
  1762  	defer s.d.Restart(c)
  1763  
  1764  	var cont int
  1765  	for {
  1766  		contName := fmt.Sprintf("container%d", cont)
  1767  		_, err := s.d.Cmd("run", "--name", contName, "-d", "busybox", "/bin/sleep", "2")
  1768  		if err != nil {
  1769  			// pool exhausted
  1770  			break
  1771  		}
  1772  		ip, err := s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.IPAddress}}'", contName)
  1773  		c.Assert(err, check.IsNil)
  1774  
  1775  		c.Assert(ip, check.Not(check.Equals), bridgeIP)
  1776  		cont++
  1777  	}
  1778  }
  1779  
  1780  // Test daemon for no space left on device error
  1781  func (s *DockerDaemonSuite) TestDaemonNoSpaceLeftOnDeviceError(c *check.C) {
  1782  	testRequires(c, SameHostDaemon, DaemonIsLinux, Network)
  1783  
  1784  	testDir, err := ioutil.TempDir("", "no-space-left-on-device-test")
  1785  	c.Assert(err, checker.IsNil)
  1786  	defer os.RemoveAll(testDir)
  1787  	c.Assert(mount.MakeRShared(testDir), checker.IsNil)
  1788  	defer mount.Unmount(testDir)
  1789  
  1790  	// create a 2MiB image and mount it as graph root
  1791  	// Why in a container? Because `mount` sometimes behaves weirdly and often fails outright on this test in debian:jessie (which is what the test suite runs under if run from the Makefile)
  1792  	dockerCmd(c, "run", "--rm", "-v", testDir+":/test", "busybox", "sh", "-c", "dd of=/test/testfs.img bs=1M seek=2 count=0")
  1793  	icmd.RunCommand("mkfs.ext4", "-F", filepath.Join(testDir, "testfs.img")).Assert(c, icmd.Success)
  1794  
  1795  	result := icmd.RunCommand("losetup", "-f", "--show", filepath.Join(testDir, "testfs.img"))
  1796  	result.Assert(c, icmd.Success)
  1797  	loopname := strings.TrimSpace(string(result.Combined()))
  1798  	defer exec.Command("losetup", "-d", loopname).Run()
  1799  
  1800  	dockerCmd(c, "run", "--privileged", "--rm", "-v", testDir+":/test:shared", "busybox", "sh", "-c", fmt.Sprintf("mkdir -p /test/test-mount && mount -t ext4 -no loop,rw %v /test/test-mount", loopname))
  1801  	defer mount.Unmount(filepath.Join(testDir, "test-mount"))
  1802  
  1803  	s.d.Start(c, "--data-root", filepath.Join(testDir, "test-mount"))
  1804  	defer s.d.Stop(c)
  1805  
  1806  	// pull a repository large enough to fill the mount point
  1807  	pullOut, err := s.d.Cmd("pull", "registry:2")
  1808  	c.Assert(err, checker.NotNil, check.Commentf(pullOut))
  1809  	c.Assert(pullOut, checker.Contains, "no space left on device")
  1810  }
  1811  
  1812  // Test daemon restart with container links + auto restart
  1813  func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
  1814  	s.d.StartWithBusybox(c)
  1815  
  1816  	parent1Args := []string{}
  1817  	parent2Args := []string{}
  1818  	wg := sync.WaitGroup{}
  1819  	maxChildren := 10
  1820  	chErr := make(chan error, maxChildren)
  1821  
  1822  	for i := 0; i < maxChildren; i++ {
  1823  		wg.Add(1)
  1824  		name := fmt.Sprintf("test%d", i)
  1825  
  1826  		if i < maxChildren/2 {
  1827  			parent1Args = append(parent1Args, []string{"--link", name}...)
  1828  		} else {
  1829  			parent2Args = append(parent2Args, []string{"--link", name}...)
  1830  		}
  1831  
  1832  		go func() {
  1833  			_, err := s.d.Cmd("run", "-d", "--name", name, "--restart=always", "busybox", "top")
  1834  			chErr <- err
  1835  			wg.Done()
  1836  		}()
  1837  	}
  1838  
  1839  	wg.Wait()
  1840  	close(chErr)
  1841  	for err := range chErr {
  1842  		c.Assert(err, check.IsNil)
  1843  	}
  1844  
  1845  	parent1Args = append([]string{"run", "-d"}, parent1Args...)
  1846  	parent1Args = append(parent1Args, []string{"--name=parent1", "--restart=always", "busybox", "top"}...)
  1847  	parent2Args = append([]string{"run", "-d"}, parent2Args...)
  1848  	parent2Args = append(parent2Args, []string{"--name=parent2", "--restart=always", "busybox", "top"}...)
  1849  
  1850  	_, err := s.d.Cmd(parent1Args...)
  1851  	c.Assert(err, check.IsNil)
  1852  	_, err = s.d.Cmd(parent2Args...)
  1853  	c.Assert(err, check.IsNil)
  1854  
  1855  	s.d.Stop(c)
  1856  	// clear the log file -- we don't need any of it but may for the next part
  1857  	// can ignore the error here, this is just a cleanup
  1858  	os.Truncate(s.d.LogFileName(), 0)
  1859  	s.d.Start(c)
  1860  
  1861  	for _, num := range []string{"1", "2"} {
  1862  		out, err := s.d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num)
  1863  		c.Assert(err, check.IsNil)
  1864  		if strings.TrimSpace(out) != "true" {
  1865  			log, _ := ioutil.ReadFile(s.d.LogFileName())
  1866  			c.Fatalf("parent container is not running\n%s", string(log))
  1867  		}
  1868  	}
  1869  }
  1870  
  1871  func (s *DockerDaemonSuite) TestDaemonCgroupParent(c *check.C) {
  1872  	testRequires(c, DaemonIsLinux)
  1873  
  1874  	cgroupParent := "test"
  1875  	name := "cgroup-test"
  1876  
  1877  	s.d.StartWithBusybox(c, "--cgroup-parent", cgroupParent)
  1878  	defer s.d.Restart(c)
  1879  
  1880  	out, err := s.d.Cmd("run", "--name", name, "busybox", "cat", "/proc/self/cgroup")
  1881  	c.Assert(err, checker.IsNil)
  1882  	cgroupPaths := testutil.ParseCgroupPaths(string(out))
  1883  	c.Assert(len(cgroupPaths), checker.Not(checker.Equals), 0, check.Commentf("unexpected output - %q", string(out)))
  1884  	out, err = s.d.Cmd("inspect", "-f", "{{.Id}}", name)
  1885  	c.Assert(err, checker.IsNil)
  1886  	id := strings.TrimSpace(string(out))
  1887  	expectedCgroup := path.Join(cgroupParent, id)
  1888  	found := false
  1889  	for _, path := range cgroupPaths {
  1890  		if strings.HasSuffix(path, expectedCgroup) {
  1891  			found = true
  1892  			break
  1893  		}
  1894  	}
  1895  	c.Assert(found, checker.True, check.Commentf("Cgroup path for container (%s) doesn't found in cgroups file: %s", expectedCgroup, cgroupPaths))
  1896  }
  1897  
  1898  func (s *DockerDaemonSuite) TestDaemonRestartWithLinks(c *check.C) {
  1899  	testRequires(c, DaemonIsLinux) // Windows does not support links
  1900  	s.d.StartWithBusybox(c)
  1901  
  1902  	out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1903  	c.Assert(err, check.IsNil, check.Commentf(out))
  1904  
  1905  	out, err = s.d.Cmd("run", "--name=test2", "--link", "test:abc", "busybox", "sh", "-c", "ping -c 1 -w 1 abc")
  1906  	c.Assert(err, check.IsNil, check.Commentf(out))
  1907  
  1908  	s.d.Restart(c)
  1909  
  1910  	// should fail since test is not running yet
  1911  	out, err = s.d.Cmd("start", "test2")
  1912  	c.Assert(err, check.NotNil, check.Commentf(out))
  1913  
  1914  	out, err = s.d.Cmd("start", "test")
  1915  	c.Assert(err, check.IsNil, check.Commentf(out))
  1916  	out, err = s.d.Cmd("start", "-a", "test2")
  1917  	c.Assert(err, check.IsNil, check.Commentf(out))
  1918  	c.Assert(strings.Contains(out, "1 packets transmitted, 1 packets received"), check.Equals, true, check.Commentf(out))
  1919  }
  1920  
  1921  func (s *DockerDaemonSuite) TestDaemonRestartWithNames(c *check.C) {
  1922  	testRequires(c, DaemonIsLinux) // Windows does not support links
  1923  	s.d.StartWithBusybox(c)
  1924  
  1925  	out, err := s.d.Cmd("create", "--name=test", "busybox")
  1926  	c.Assert(err, check.IsNil, check.Commentf(out))
  1927  
  1928  	out, err = s.d.Cmd("run", "-d", "--name=test2", "busybox", "top")
  1929  	c.Assert(err, check.IsNil, check.Commentf(out))
  1930  	test2ID := strings.TrimSpace(out)
  1931  
  1932  	out, err = s.d.Cmd("run", "-d", "--name=test3", "--link", "test2:abc", "busybox", "top")
  1933  	test3ID := strings.TrimSpace(out)
  1934  
  1935  	s.d.Restart(c)
  1936  
  1937  	out, err = s.d.Cmd("create", "--name=test", "busybox")
  1938  	c.Assert(err, check.NotNil, check.Commentf("expected error trying to create container with duplicate name"))
  1939  	// this one is no longer needed, removing simplifies the remainder of the test
  1940  	out, err = s.d.Cmd("rm", "-f", "test")
  1941  	c.Assert(err, check.IsNil, check.Commentf(out))
  1942  
  1943  	out, err = s.d.Cmd("ps", "-a", "--no-trunc")
  1944  	c.Assert(err, check.IsNil, check.Commentf(out))
  1945  
  1946  	lines := strings.Split(strings.TrimSpace(out), "\n")[1:]
  1947  
  1948  	test2validated := false
  1949  	test3validated := false
  1950  	for _, line := range lines {
  1951  		fields := strings.Fields(line)
  1952  		names := fields[len(fields)-1]
  1953  		switch fields[0] {
  1954  		case test2ID:
  1955  			c.Assert(names, check.Equals, "test2,test3/abc")
  1956  			test2validated = true
  1957  		case test3ID:
  1958  			c.Assert(names, check.Equals, "test3")
  1959  			test3validated = true
  1960  		}
  1961  	}
  1962  
  1963  	c.Assert(test2validated, check.Equals, true)
  1964  	c.Assert(test3validated, check.Equals, true)
  1965  }
  1966  
  1967  // TestDaemonRestartWithKilledRunningContainer requires live restore of running containers
  1968  func (s *DockerDaemonSuite) TestDaemonRestartWithKilledRunningContainer(t *check.C) {
  1969  	// TODO(mlaventure): Not sure what would the exit code be on windows
  1970  	testRequires(t, DaemonIsLinux)
  1971  	s.d.StartWithBusybox(t)
  1972  
  1973  	cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  1974  	defer s.d.Stop(t)
  1975  	if err != nil {
  1976  		t.Fatal(cid, err)
  1977  	}
  1978  	cid = strings.TrimSpace(cid)
  1979  
  1980  	pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", cid)
  1981  	t.Assert(err, check.IsNil)
  1982  	pid = strings.TrimSpace(pid)
  1983  
  1984  	// Kill the daemon
  1985  	if err := s.d.Kill(); err != nil {
  1986  		t.Fatal(err)
  1987  	}
  1988  
  1989  	// kill the container
  1990  	icmd.RunCommand(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", cid).Assert(t, icmd.Success)
  1991  
  1992  	// Give time to containerd to process the command if we don't
  1993  	// the exit event might be received after we do the inspect
  1994  	result := icmd.RunCommand("kill", "-0", pid)
  1995  	for result.ExitCode == 0 {
  1996  		time.Sleep(1 * time.Second)
  1997  		// FIXME(vdemeester) should we check it doesn't error out ?
  1998  		result = icmd.RunCommand("kill", "-0", pid)
  1999  	}
  2000  
  2001  	// restart the daemon
  2002  	s.d.Start(t)
  2003  
  2004  	// Check that we've got the correct exit code
  2005  	out, err := s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", cid)
  2006  	t.Assert(err, check.IsNil)
  2007  
  2008  	out = strings.TrimSpace(out)
  2009  	if out != "143" {
  2010  		t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "143", out, cid)
  2011  	}
  2012  
  2013  }
  2014  
  2015  // os.Kill should kill daemon ungracefully, leaving behind live containers.
  2016  // The live containers should be known to the restarted daemon. Stopping
  2017  // them now, should remove the mounts.
  2018  func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonCrash(c *check.C) {
  2019  	testRequires(c, DaemonIsLinux)
  2020  	s.d.StartWithBusybox(c, "--live-restore")
  2021  
  2022  	out, err := s.d.Cmd("run", "-d", "busybox", "top")
  2023  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2024  	id := strings.TrimSpace(out)
  2025  
  2026  	c.Assert(s.d.Signal(os.Kill), check.IsNil)
  2027  	mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  2028  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  2029  
  2030  	// container mounts should exist even after daemon has crashed.
  2031  	comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.Root, mountOut)
  2032  	c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment)
  2033  
  2034  	// restart daemon.
  2035  	s.d.Start(c, "--live-restore")
  2036  
  2037  	// container should be running.
  2038  	out, err = s.d.Cmd("inspect", "--format={{.State.Running}}", id)
  2039  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2040  	out = strings.TrimSpace(out)
  2041  	if out != "true" {
  2042  		c.Fatalf("Container %s expected to stay alive after daemon restart", id)
  2043  	}
  2044  
  2045  	// 'docker stop' should work.
  2046  	out, err = s.d.Cmd("stop", id)
  2047  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2048  
  2049  	// Now, container mounts should be gone.
  2050  	mountOut, err = ioutil.ReadFile("/proc/self/mountinfo")
  2051  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  2052  	comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.Root, mountOut)
  2053  	c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  2054  }
  2055  
  2056  // TestDaemonRestartWithUnpausedRunningContainer requires live restore of running containers.
  2057  func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *check.C) {
  2058  	// TODO(mlaventure): Not sure what would the exit code be on windows
  2059  	testRequires(t, DaemonIsLinux)
  2060  	s.d.StartWithBusybox(t, "--live-restore")
  2061  
  2062  	cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  2063  	defer s.d.Stop(t)
  2064  	if err != nil {
  2065  		t.Fatal(cid, err)
  2066  	}
  2067  	cid = strings.TrimSpace(cid)
  2068  
  2069  	pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", cid)
  2070  	t.Assert(err, check.IsNil)
  2071  
  2072  	// pause the container
  2073  	if _, err := s.d.Cmd("pause", cid); err != nil {
  2074  		t.Fatal(cid, err)
  2075  	}
  2076  
  2077  	// Kill the daemon
  2078  	if err := s.d.Kill(); err != nil {
  2079  		t.Fatal(err)
  2080  	}
  2081  
  2082  	// resume the container
  2083  	result := icmd.RunCommand(
  2084  		ctrBinary,
  2085  		"--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock",
  2086  		"containers", "resume", cid)
  2087  	t.Assert(result, icmd.Matches, icmd.Success)
  2088  
  2089  	// Give time to containerd to process the command if we don't
  2090  	// the resume event might be received after we do the inspect
  2091  	waitAndAssert(t, defaultReconciliationTimeout, func(*check.C) (interface{}, check.CommentInterface) {
  2092  		result := icmd.RunCommand("kill", "-0", strings.TrimSpace(pid))
  2093  		return result.ExitCode, nil
  2094  	}, checker.Equals, 0)
  2095  
  2096  	// restart the daemon
  2097  	s.d.Start(t, "--live-restore")
  2098  
  2099  	// Check that we've got the correct status
  2100  	out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  2101  	t.Assert(err, check.IsNil)
  2102  
  2103  	out = strings.TrimSpace(out)
  2104  	if out != "running" {
  2105  		t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "running", out, cid)
  2106  	}
  2107  	if _, err := s.d.Cmd("kill", cid); err != nil {
  2108  		t.Fatal(err)
  2109  	}
  2110  }
  2111  
  2112  // TestRunLinksChanged checks that creating a new container with the same name does not update links
  2113  // this ensures that the old, pre gh#16032 functionality continues on
  2114  func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) {
  2115  	testRequires(c, DaemonIsLinux) // Windows does not support links
  2116  	s.d.StartWithBusybox(c)
  2117  
  2118  	out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  2119  	c.Assert(err, check.IsNil, check.Commentf(out))
  2120  
  2121  	out, err = s.d.Cmd("run", "--name=test2", "--link=test:abc", "busybox", "sh", "-c", "ping -c 1 abc")
  2122  	c.Assert(err, check.IsNil, check.Commentf(out))
  2123  	c.Assert(out, checker.Contains, "1 packets transmitted, 1 packets received")
  2124  
  2125  	out, err = s.d.Cmd("rm", "-f", "test")
  2126  	c.Assert(err, check.IsNil, check.Commentf(out))
  2127  
  2128  	out, err = s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  2129  	c.Assert(err, check.IsNil, check.Commentf(out))
  2130  	out, err = s.d.Cmd("start", "-a", "test2")
  2131  	c.Assert(err, check.NotNil, check.Commentf(out))
  2132  	c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  2133  
  2134  	s.d.Restart(c)
  2135  	out, err = s.d.Cmd("start", "-a", "test2")
  2136  	c.Assert(err, check.NotNil, check.Commentf(out))
  2137  	c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  2138  }
  2139  
  2140  func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) {
  2141  	testRequires(c, DaemonIsLinux, NotPpc64le)
  2142  
  2143  	infoLog := "\x1b[34mINFO\x1b"
  2144  
  2145  	b := bytes.NewBuffer(nil)
  2146  	done := make(chan bool)
  2147  
  2148  	p, tty, err := pty.Open()
  2149  	c.Assert(err, checker.IsNil)
  2150  	defer func() {
  2151  		tty.Close()
  2152  		p.Close()
  2153  	}()
  2154  
  2155  	go func() {
  2156  		io.Copy(b, p)
  2157  		done <- true
  2158  	}()
  2159  
  2160  	// Enable coloring explicitly
  2161  	s.d.StartWithLogFile(tty, "--raw-logs=false")
  2162  	s.d.Stop(c)
  2163  	// Wait for io.Copy() before checking output
  2164  	<-done
  2165  	c.Assert(b.String(), checker.Contains, infoLog)
  2166  
  2167  	b.Reset()
  2168  
  2169  	// "tty" is already closed in prev s.d.Stop(),
  2170  	// we have to close the other side "p" and open another pair of
  2171  	// pty for the next test.
  2172  	p.Close()
  2173  	p, tty, err = pty.Open()
  2174  	c.Assert(err, checker.IsNil)
  2175  
  2176  	go func() {
  2177  		io.Copy(b, p)
  2178  		done <- true
  2179  	}()
  2180  
  2181  	// Disable coloring explicitly
  2182  	s.d.StartWithLogFile(tty, "--raw-logs=true")
  2183  	s.d.Stop(c)
  2184  	// Wait for io.Copy() before checking output
  2185  	<-done
  2186  	c.Assert(b.String(), check.Not(check.Equals), "")
  2187  	c.Assert(b.String(), check.Not(checker.Contains), infoLog)
  2188  }
  2189  
  2190  func (s *DockerDaemonSuite) TestDaemonDebugLog(c *check.C) {
  2191  	testRequires(c, DaemonIsLinux, NotPpc64le)
  2192  
  2193  	debugLog := "\x1b[37mDEBU\x1b"
  2194  
  2195  	p, tty, err := pty.Open()
  2196  	c.Assert(err, checker.IsNil)
  2197  	defer func() {
  2198  		tty.Close()
  2199  		p.Close()
  2200  	}()
  2201  
  2202  	b := bytes.NewBuffer(nil)
  2203  	go io.Copy(b, p)
  2204  
  2205  	s.d.StartWithLogFile(tty, "--debug")
  2206  	s.d.Stop(c)
  2207  	c.Assert(b.String(), checker.Contains, debugLog)
  2208  }
  2209  
  2210  func (s *DockerDaemonSuite) TestDaemonDiscoveryBackendConfigReload(c *check.C) {
  2211  	testRequires(c, SameHostDaemon, DaemonIsLinux)
  2212  
  2213  	// daemon config file
  2214  	daemonConfig := `{ "debug" : false }`
  2215  	configFile, err := ioutil.TempFile("", "test-daemon-discovery-backend-config-reload-config")
  2216  	c.Assert(err, checker.IsNil, check.Commentf("could not create temp file for config reload"))
  2217  	configFilePath := configFile.Name()
  2218  	defer func() {
  2219  		configFile.Close()
  2220  		os.RemoveAll(configFile.Name())
  2221  	}()
  2222  
  2223  	_, err = configFile.Write([]byte(daemonConfig))
  2224  	c.Assert(err, checker.IsNil)
  2225  
  2226  	// --log-level needs to be set so that d.Start() doesn't add --debug causing
  2227  	// a conflict with the config
  2228  	s.d.Start(c, "--config-file", configFilePath, "--log-level=info")
  2229  
  2230  	// daemon config file
  2231  	daemonConfig = `{
  2232  	      "cluster-store": "consul://consuladdr:consulport/some/path",
  2233  	      "cluster-advertise": "192.168.56.100:0",
  2234  	      "debug" : false
  2235  	}`
  2236  
  2237  	err = configFile.Truncate(0)
  2238  	c.Assert(err, checker.IsNil)
  2239  	_, err = configFile.Seek(0, os.SEEK_SET)
  2240  	c.Assert(err, checker.IsNil)
  2241  
  2242  	_, err = configFile.Write([]byte(daemonConfig))
  2243  	c.Assert(err, checker.IsNil)
  2244  
  2245  	err = s.d.ReloadConfig()
  2246  	c.Assert(err, checker.IsNil, check.Commentf("error reloading daemon config"))
  2247  
  2248  	out, err := s.d.Cmd("info")
  2249  	c.Assert(err, checker.IsNil)
  2250  
  2251  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Store: consul://consuladdr:consulport/some/path"))
  2252  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Advertise: 192.168.56.100:0"))
  2253  }
  2254  
  2255  // Test for #21956
  2256  func (s *DockerDaemonSuite) TestDaemonLogOptions(c *check.C) {
  2257  	s.d.StartWithBusybox(c, "--log-driver=syslog", "--log-opt=syslog-address=udp://127.0.0.1:514")
  2258  
  2259  	out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "top")
  2260  	c.Assert(err, check.IsNil, check.Commentf(out))
  2261  	id := strings.TrimSpace(out)
  2262  
  2263  	out, err = s.d.Cmd("inspect", "--format='{{.HostConfig.LogConfig}}'", id)
  2264  	c.Assert(err, check.IsNil, check.Commentf(out))
  2265  	c.Assert(out, checker.Contains, "{json-file map[]}")
  2266  }
  2267  
  2268  // Test case for #20936, #22443
  2269  func (s *DockerDaemonSuite) TestDaemonMaxConcurrency(c *check.C) {
  2270  	s.d.Start(c, "--max-concurrent-uploads=6", "--max-concurrent-downloads=8")
  2271  
  2272  	expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 6"`
  2273  	expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 8"`
  2274  	content, err := s.d.ReadLogFile()
  2275  	c.Assert(err, checker.IsNil)
  2276  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2277  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2278  }
  2279  
  2280  // Test case for #20936, #22443
  2281  func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFile(c *check.C) {
  2282  	testRequires(c, SameHostDaemon, DaemonIsLinux)
  2283  
  2284  	// daemon config file
  2285  	configFilePath := "test.json"
  2286  	configFile, err := os.Create(configFilePath)
  2287  	c.Assert(err, checker.IsNil)
  2288  	defer os.Remove(configFilePath)
  2289  
  2290  	daemonConfig := `{ "max-concurrent-downloads" : 8 }`
  2291  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2292  	configFile.Close()
  2293  	s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath))
  2294  
  2295  	expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 5"`
  2296  	expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 8"`
  2297  	content, err := s.d.ReadLogFile()
  2298  	c.Assert(err, checker.IsNil)
  2299  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2300  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2301  
  2302  	configFile, err = os.Create(configFilePath)
  2303  	c.Assert(err, checker.IsNil)
  2304  	daemonConfig = `{ "max-concurrent-uploads" : 7, "max-concurrent-downloads" : 9 }`
  2305  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2306  	configFile.Close()
  2307  
  2308  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2309  	// syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2310  
  2311  	time.Sleep(3 * time.Second)
  2312  
  2313  	expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 7"`
  2314  	expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 9"`
  2315  	content, err = s.d.ReadLogFile()
  2316  	c.Assert(err, checker.IsNil)
  2317  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2318  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2319  }
  2320  
  2321  // Test case for #20936, #22443
  2322  func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFileReload(c *check.C) {
  2323  	testRequires(c, SameHostDaemon, DaemonIsLinux)
  2324  
  2325  	// daemon config file
  2326  	configFilePath := "test.json"
  2327  	configFile, err := os.Create(configFilePath)
  2328  	c.Assert(err, checker.IsNil)
  2329  	defer os.Remove(configFilePath)
  2330  
  2331  	daemonConfig := `{ "max-concurrent-uploads" : null }`
  2332  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2333  	configFile.Close()
  2334  	s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath))
  2335  
  2336  	expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 5"`
  2337  	expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 3"`
  2338  	content, err := s.d.ReadLogFile()
  2339  	c.Assert(err, checker.IsNil)
  2340  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2341  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2342  
  2343  	configFile, err = os.Create(configFilePath)
  2344  	c.Assert(err, checker.IsNil)
  2345  	daemonConfig = `{ "max-concurrent-uploads" : 1, "max-concurrent-downloads" : null }`
  2346  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2347  	configFile.Close()
  2348  
  2349  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2350  	// syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2351  
  2352  	time.Sleep(3 * time.Second)
  2353  
  2354  	expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 1"`
  2355  	expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 3"`
  2356  	content, err = s.d.ReadLogFile()
  2357  	c.Assert(err, checker.IsNil)
  2358  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2359  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2360  
  2361  	configFile, err = os.Create(configFilePath)
  2362  	c.Assert(err, checker.IsNil)
  2363  	daemonConfig = `{ "labels":["foo=bar"] }`
  2364  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2365  	configFile.Close()
  2366  
  2367  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2368  
  2369  	time.Sleep(3 * time.Second)
  2370  
  2371  	expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 5"`
  2372  	expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 3"`
  2373  	content, err = s.d.ReadLogFile()
  2374  	c.Assert(err, checker.IsNil)
  2375  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2376  	c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2377  }
  2378  
  2379  func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) {
  2380  	s.d.StartWithBusybox(c, "-b=none", "--iptables=false")
  2381  	out, code, err := s.d.BuildImageWithOut("busyboxs",
  2382  		`FROM busybox
  2383                  RUN cat /etc/hosts`, false)
  2384  	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err)
  2385  	c.Assert(err, check.IsNil, comment)
  2386  	c.Assert(code, check.Equals, 0, comment)
  2387  }
  2388  
  2389  // Test case for #21976
  2390  func (s *DockerDaemonSuite) TestDaemonDNSFlagsInHostMode(c *check.C) {
  2391  	testRequires(c, SameHostDaemon, DaemonIsLinux)
  2392  
  2393  	s.d.StartWithBusybox(c, "--dns", "1.2.3.4", "--dns-search", "example.com", "--dns-opt", "timeout:3")
  2394  
  2395  	expectedOutput := "nameserver 1.2.3.4"
  2396  	out, _ := s.d.Cmd("run", "--net=host", "busybox", "cat", "/etc/resolv.conf")
  2397  	c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2398  	expectedOutput = "search example.com"
  2399  	c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2400  	expectedOutput = "options timeout:3"
  2401  	c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2402  }
  2403  
  2404  func (s *DockerDaemonSuite) TestRunWithRuntimeFromConfigFile(c *check.C) {
  2405  	conf, err := ioutil.TempFile("", "config-file-")
  2406  	c.Assert(err, check.IsNil)
  2407  	configName := conf.Name()
  2408  	conf.Close()
  2409  	defer os.Remove(configName)
  2410  
  2411  	config := `
  2412  {
  2413      "runtimes": {
  2414          "oci": {
  2415              "path": "docker-runc"
  2416          },
  2417          "vm": {
  2418              "path": "/usr/local/bin/vm-manager",
  2419              "runtimeArgs": [
  2420                  "--debug"
  2421              ]
  2422          }
  2423      }
  2424  }
  2425  `
  2426  	ioutil.WriteFile(configName, []byte(config), 0644)
  2427  	s.d.StartWithBusybox(c, "--config-file", configName)
  2428  
  2429  	// Run with default runtime
  2430  	out, err := s.d.Cmd("run", "--rm", "busybox", "ls")
  2431  	c.Assert(err, check.IsNil, check.Commentf(out))
  2432  
  2433  	// Run with default runtime explicitly
  2434  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2435  	c.Assert(err, check.IsNil, check.Commentf(out))
  2436  
  2437  	// Run with oci (same path as default) but keep it around
  2438  	out, err = s.d.Cmd("run", "--name", "oci-runtime-ls", "--runtime=oci", "busybox", "ls")
  2439  	c.Assert(err, check.IsNil, check.Commentf(out))
  2440  
  2441  	// Run with "vm"
  2442  	out, err = s.d.Cmd("run", "--rm", "--runtime=vm", "busybox", "ls")
  2443  	c.Assert(err, check.NotNil, check.Commentf(out))
  2444  	c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2445  
  2446  	// Reset config to only have the default
  2447  	config = `
  2448  {
  2449      "runtimes": {
  2450      }
  2451  }
  2452  `
  2453  	ioutil.WriteFile(configName, []byte(config), 0644)
  2454  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2455  	// Give daemon time to reload config
  2456  	<-time.After(1 * time.Second)
  2457  
  2458  	// Run with default runtime
  2459  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2460  	c.Assert(err, check.IsNil, check.Commentf(out))
  2461  
  2462  	// Run with "oci"
  2463  	out, err = s.d.Cmd("run", "--rm", "--runtime=oci", "busybox", "ls")
  2464  	c.Assert(err, check.NotNil, check.Commentf(out))
  2465  	c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2466  
  2467  	// Start previously created container with oci
  2468  	out, err = s.d.Cmd("start", "oci-runtime-ls")
  2469  	c.Assert(err, check.NotNil, check.Commentf(out))
  2470  	c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2471  
  2472  	// Check that we can't override the default runtime
  2473  	config = `
  2474  {
  2475      "runtimes": {
  2476          "runc": {
  2477              "path": "my-runc"
  2478          }
  2479      }
  2480  }
  2481  `
  2482  	ioutil.WriteFile(configName, []byte(config), 0644)
  2483  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2484  	// Give daemon time to reload config
  2485  	<-time.After(1 * time.Second)
  2486  
  2487  	content, err := s.d.ReadLogFile()
  2488  	c.Assert(err, checker.IsNil)
  2489  	c.Assert(string(content), checker.Contains, `file configuration validation failed (runtime name 'runc' is reserved)`)
  2490  
  2491  	// Check that we can select a default runtime
  2492  	config = `
  2493  {
  2494      "default-runtime": "vm",
  2495      "runtimes": {
  2496          "oci": {
  2497              "path": "docker-runc"
  2498          },
  2499          "vm": {
  2500              "path": "/usr/local/bin/vm-manager",
  2501              "runtimeArgs": [
  2502                  "--debug"
  2503              ]
  2504          }
  2505      }
  2506  }
  2507  `
  2508  	ioutil.WriteFile(configName, []byte(config), 0644)
  2509  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2510  	// Give daemon time to reload config
  2511  	<-time.After(1 * time.Second)
  2512  
  2513  	out, err = s.d.Cmd("run", "--rm", "busybox", "ls")
  2514  	c.Assert(err, check.NotNil, check.Commentf(out))
  2515  	c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2516  
  2517  	// Run with default runtime explicitly
  2518  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2519  	c.Assert(err, check.IsNil, check.Commentf(out))
  2520  }
  2521  
  2522  func (s *DockerDaemonSuite) TestRunWithRuntimeFromCommandLine(c *check.C) {
  2523  	s.d.StartWithBusybox(c, "--add-runtime", "oci=docker-runc", "--add-runtime", "vm=/usr/local/bin/vm-manager")
  2524  
  2525  	// Run with default runtime
  2526  	out, err := s.d.Cmd("run", "--rm", "busybox", "ls")
  2527  	c.Assert(err, check.IsNil, check.Commentf(out))
  2528  
  2529  	// Run with default runtime explicitly
  2530  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2531  	c.Assert(err, check.IsNil, check.Commentf(out))
  2532  
  2533  	// Run with oci (same path as default) but keep it around
  2534  	out, err = s.d.Cmd("run", "--name", "oci-runtime-ls", "--runtime=oci", "busybox", "ls")
  2535  	c.Assert(err, check.IsNil, check.Commentf(out))
  2536  
  2537  	// Run with "vm"
  2538  	out, err = s.d.Cmd("run", "--rm", "--runtime=vm", "busybox", "ls")
  2539  	c.Assert(err, check.NotNil, check.Commentf(out))
  2540  	c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2541  
  2542  	// Start a daemon without any extra runtimes
  2543  	s.d.Stop(c)
  2544  	s.d.StartWithBusybox(c)
  2545  
  2546  	// Run with default runtime
  2547  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2548  	c.Assert(err, check.IsNil, check.Commentf(out))
  2549  
  2550  	// Run with "oci"
  2551  	out, err = s.d.Cmd("run", "--rm", "--runtime=oci", "busybox", "ls")
  2552  	c.Assert(err, check.NotNil, check.Commentf(out))
  2553  	c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2554  
  2555  	// Start previously created container with oci
  2556  	out, err = s.d.Cmd("start", "oci-runtime-ls")
  2557  	c.Assert(err, check.NotNil, check.Commentf(out))
  2558  	c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2559  
  2560  	// Check that we can't override the default runtime
  2561  	s.d.Stop(c)
  2562  	c.Assert(s.d.StartWithError("--add-runtime", "runc=my-runc"), checker.NotNil)
  2563  
  2564  	content, err := s.d.ReadLogFile()
  2565  	c.Assert(err, checker.IsNil)
  2566  	c.Assert(string(content), checker.Contains, `runtime name 'runc' is reserved`)
  2567  
  2568  	// Check that we can select a default runtime
  2569  	s.d.Stop(c)
  2570  	s.d.StartWithBusybox(c, "--default-runtime=vm", "--add-runtime", "oci=docker-runc", "--add-runtime", "vm=/usr/local/bin/vm-manager")
  2571  
  2572  	out, err = s.d.Cmd("run", "--rm", "busybox", "ls")
  2573  	c.Assert(err, check.NotNil, check.Commentf(out))
  2574  	c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2575  
  2576  	// Run with default runtime explicitly
  2577  	out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2578  	c.Assert(err, check.IsNil, check.Commentf(out))
  2579  }
  2580  
  2581  func (s *DockerDaemonSuite) TestDaemonRestartWithAutoRemoveContainer(c *check.C) {
  2582  	s.d.StartWithBusybox(c)
  2583  
  2584  	// top1 will exist after daemon restarts
  2585  	out, err := s.d.Cmd("run", "-d", "--name", "top1", "busybox:latest", "top")
  2586  	c.Assert(err, checker.IsNil, check.Commentf("run top1: %v", out))
  2587  	// top2 will be removed after daemon restarts
  2588  	out, err = s.d.Cmd("run", "-d", "--rm", "--name", "top2", "busybox:latest", "top")
  2589  	c.Assert(err, checker.IsNil, check.Commentf("run top2: %v", out))
  2590  
  2591  	out, err = s.d.Cmd("ps")
  2592  	c.Assert(out, checker.Contains, "top1", check.Commentf("top1 should be running"))
  2593  	c.Assert(out, checker.Contains, "top2", check.Commentf("top2 should be running"))
  2594  
  2595  	// now restart daemon gracefully
  2596  	s.d.Restart(c)
  2597  
  2598  	out, err = s.d.Cmd("ps", "-a")
  2599  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  2600  	c.Assert(out, checker.Contains, "top1", check.Commentf("top1 should exist after daemon restarts"))
  2601  	c.Assert(out, checker.Not(checker.Contains), "top2", check.Commentf("top2 should be removed after daemon restarts"))
  2602  }
  2603  
  2604  func (s *DockerDaemonSuite) TestDaemonRestartSaveContainerExitCode(c *check.C) {
  2605  	s.d.StartWithBusybox(c)
  2606  
  2607  	containerName := "error-values"
  2608  	// Make a container with both a non 0 exit code and an error message
  2609  	// We explicitly disable `--init` for this test, because `--init` is enabled by default
  2610  	// on "experimental". Enabling `--init` results in a different behavior; because the "init"
  2611  	// process itself is PID1, the container does not fail on _startup_ (i.e., `docker-init` starting),
  2612  	// but directly after. The exit code of the container is still 127, but the Error Message is not
  2613  	// captured, so `.State.Error` is empty.
  2614  	// See the discussion on https://github.com/docker/docker/pull/30227#issuecomment-274161426,
  2615  	// and https://github.com/docker/docker/pull/26061#r78054578 for more information.
  2616  	out, err := s.d.Cmd("run", "--name", containerName, "--init=false", "busybox", "toto")
  2617  	c.Assert(err, checker.NotNil)
  2618  
  2619  	// Check that those values were saved on disk
  2620  	out, err = s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", containerName)
  2621  	out = strings.TrimSpace(out)
  2622  	c.Assert(err, checker.IsNil)
  2623  	c.Assert(out, checker.Equals, "127")
  2624  
  2625  	errMsg1, err := s.d.Cmd("inspect", "-f", "{{.State.Error}}", containerName)
  2626  	errMsg1 = strings.TrimSpace(errMsg1)
  2627  	c.Assert(err, checker.IsNil)
  2628  	c.Assert(errMsg1, checker.Contains, "executable file not found")
  2629  
  2630  	// now restart daemon
  2631  	s.d.Restart(c)
  2632  
  2633  	// Check that those values are still around
  2634  	out, err = s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", containerName)
  2635  	out = strings.TrimSpace(out)
  2636  	c.Assert(err, checker.IsNil)
  2637  	c.Assert(out, checker.Equals, "127")
  2638  
  2639  	out, err = s.d.Cmd("inspect", "-f", "{{.State.Error}}", containerName)
  2640  	out = strings.TrimSpace(out)
  2641  	c.Assert(err, checker.IsNil)
  2642  	c.Assert(out, checker.Equals, errMsg1)
  2643  }
  2644  
  2645  func (s *DockerDaemonSuite) TestDaemonBackcompatPre17Volumes(c *check.C) {
  2646  	testRequires(c, SameHostDaemon)
  2647  	d := s.d
  2648  	d.StartWithBusybox(c)
  2649  
  2650  	// hack to be able to side-load a container config
  2651  	out, err := d.Cmd("create", "busybox:latest")
  2652  	c.Assert(err, checker.IsNil, check.Commentf(out))
  2653  	id := strings.TrimSpace(out)
  2654  
  2655  	out, err = d.Cmd("inspect", "--type=image", "--format={{.ID}}", "busybox:latest")
  2656  	c.Assert(err, checker.IsNil, check.Commentf(out))
  2657  	d.Stop(c)
  2658  	<-d.Wait
  2659  
  2660  	imageID := strings.TrimSpace(out)
  2661  	volumeID := stringid.GenerateNonCryptoID()
  2662  	vfsPath := filepath.Join(d.Root, "vfs", "dir", volumeID)
  2663  	c.Assert(os.MkdirAll(vfsPath, 0755), checker.IsNil)
  2664  
  2665  	config := []byte(`
  2666  		{
  2667  			"ID": "` + id + `",
  2668  			"Name": "hello",
  2669  			"Driver": "` + d.StorageDriver() + `",
  2670  			"Image": "` + imageID + `",
  2671  			"Config": {"Image": "busybox:latest"},
  2672  			"NetworkSettings": {},
  2673  			"Volumes": {
  2674  				"/bar":"/foo",
  2675  				"/foo": "` + vfsPath + `",
  2676  				"/quux":"/quux"
  2677  			},
  2678  			"VolumesRW": {
  2679  				"/bar": true,
  2680  				"/foo": true,
  2681  				"/quux": false
  2682  			}
  2683  		}
  2684  	`)
  2685  
  2686  	configPath := filepath.Join(d.Root, "containers", id, "config.v2.json")
  2687  	err = ioutil.WriteFile(configPath, config, 600)
  2688  	d.Start(c)
  2689  
  2690  	out, err = d.Cmd("inspect", "--type=container", "--format={{ json .Mounts }}", id)
  2691  	c.Assert(err, checker.IsNil, check.Commentf(out))
  2692  	type mount struct {
  2693  		Name        string
  2694  		Source      string
  2695  		Destination string
  2696  		Driver      string
  2697  		RW          bool
  2698  	}
  2699  
  2700  	ls := []mount{}
  2701  	err = json.NewDecoder(strings.NewReader(out)).Decode(&ls)
  2702  	c.Assert(err, checker.IsNil)
  2703  
  2704  	expected := []mount{
  2705  		{Source: "/foo", Destination: "/bar", RW: true},
  2706  		{Name: volumeID, Destination: "/foo", RW: true},
  2707  		{Source: "/quux", Destination: "/quux", RW: false},
  2708  	}
  2709  	c.Assert(ls, checker.HasLen, len(expected))
  2710  
  2711  	for _, m := range ls {
  2712  		var matched bool
  2713  		for _, x := range expected {
  2714  			if m.Source == x.Source && m.Destination == x.Destination && m.RW == x.RW || m.Name != x.Name {
  2715  				matched = true
  2716  				break
  2717  			}
  2718  		}
  2719  		c.Assert(matched, checker.True, check.Commentf("did find match for %+v", m))
  2720  	}
  2721  }
  2722  
  2723  func (s *DockerDaemonSuite) TestDaemonWithUserlandProxyPath(c *check.C) {
  2724  	testRequires(c, SameHostDaemon, DaemonIsLinux)
  2725  
  2726  	dockerProxyPath, err := exec.LookPath("docker-proxy")
  2727  	c.Assert(err, checker.IsNil)
  2728  	tmpDir, err := ioutil.TempDir("", "test-docker-proxy")
  2729  	c.Assert(err, checker.IsNil)
  2730  
  2731  	newProxyPath := filepath.Join(tmpDir, "docker-proxy")
  2732  	cmd := exec.Command("cp", dockerProxyPath, newProxyPath)
  2733  	c.Assert(cmd.Run(), checker.IsNil)
  2734  
  2735  	// custom one
  2736  	s.d.StartWithBusybox(c, "--userland-proxy-path", newProxyPath)
  2737  	out, err := s.d.Cmd("run", "-p", "5000:5000", "busybox:latest", "true")
  2738  	c.Assert(err, checker.IsNil, check.Commentf(out))
  2739  
  2740  	// try with the original one
  2741  	s.d.Restart(c, "--userland-proxy-path", dockerProxyPath)
  2742  	out, err = s.d.Cmd("run", "-p", "5000:5000", "busybox:latest", "true")
  2743  	c.Assert(err, checker.IsNil, check.Commentf(out))
  2744  
  2745  	// not exist
  2746  	s.d.Restart(c, "--userland-proxy-path", "/does/not/exist")
  2747  	out, err = s.d.Cmd("run", "-p", "5000:5000", "busybox:latest", "true")
  2748  	c.Assert(err, checker.NotNil, check.Commentf(out))
  2749  	c.Assert(out, checker.Contains, "driver failed programming external connectivity on endpoint")
  2750  	c.Assert(out, checker.Contains, "/does/not/exist: no such file or directory")
  2751  }
  2752  
  2753  // Test case for #22471
  2754  func (s *DockerDaemonSuite) TestDaemonShutdownTimeout(c *check.C) {
  2755  	testRequires(c, SameHostDaemon)
  2756  	s.d.StartWithBusybox(c, "--shutdown-timeout=3")
  2757  
  2758  	_, err := s.d.Cmd("run", "-d", "busybox", "top")
  2759  	c.Assert(err, check.IsNil)
  2760  
  2761  	c.Assert(s.d.Signal(syscall.SIGINT), checker.IsNil)
  2762  
  2763  	select {
  2764  	case <-s.d.Wait:
  2765  	case <-time.After(5 * time.Second):
  2766  	}
  2767  
  2768  	expectedMessage := `level=debug msg="start clean shutdown of all containers with a 3 seconds timeout..."`
  2769  	content, err := s.d.ReadLogFile()
  2770  	c.Assert(err, checker.IsNil)
  2771  	c.Assert(string(content), checker.Contains, expectedMessage)
  2772  }
  2773  
  2774  // Test case for #22471
  2775  func (s *DockerDaemonSuite) TestDaemonShutdownTimeoutWithConfigFile(c *check.C) {
  2776  	testRequires(c, SameHostDaemon)
  2777  
  2778  	// daemon config file
  2779  	configFilePath := "test.json"
  2780  	configFile, err := os.Create(configFilePath)
  2781  	c.Assert(err, checker.IsNil)
  2782  	defer os.Remove(configFilePath)
  2783  
  2784  	daemonConfig := `{ "shutdown-timeout" : 8 }`
  2785  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2786  	configFile.Close()
  2787  	s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath))
  2788  
  2789  	configFile, err = os.Create(configFilePath)
  2790  	c.Assert(err, checker.IsNil)
  2791  	daemonConfig = `{ "shutdown-timeout" : 5 }`
  2792  	fmt.Fprintf(configFile, "%s", daemonConfig)
  2793  	configFile.Close()
  2794  
  2795  	c.Assert(s.d.Signal(syscall.SIGHUP), checker.IsNil)
  2796  
  2797  	select {
  2798  	case <-s.d.Wait:
  2799  	case <-time.After(3 * time.Second):
  2800  	}
  2801  
  2802  	expectedMessage := `level=debug msg="Reset Shutdown Timeout: 5"`
  2803  	content, err := s.d.ReadLogFile()
  2804  	c.Assert(err, checker.IsNil)
  2805  	c.Assert(string(content), checker.Contains, expectedMessage)
  2806  }
  2807  
  2808  // Test case for 29342
  2809  func (s *DockerDaemonSuite) TestExecWithUserAfterLiveRestore(c *check.C) {
  2810  	testRequires(c, DaemonIsLinux)
  2811  	s.d.StartWithBusybox(c, "--live-restore")
  2812  
  2813  	out, err := s.d.Cmd("run", "-d", "--name=top", "busybox", "sh", "-c", "addgroup -S test && adduser -S -G test test -D -s /bin/sh && touch /adduser_end && top")
  2814  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2815  
  2816  	s.d.WaitRun("top")
  2817  
  2818  	// Wait for shell command to be completed
  2819  	_, err = s.d.Cmd("exec", "top", "sh", "-c", `for i in $(seq 1 5); do if [ -e /adduser_end ]; then rm -f /adduser_end && break; else sleep 1 && false; fi; done`)
  2820  	c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for shell command to be completed"))
  2821  
  2822  	out1, err := s.d.Cmd("exec", "-u", "test", "top", "id")
  2823  	// uid=100(test) gid=101(test) groups=101(test)
  2824  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out1))
  2825  
  2826  	// restart daemon.
  2827  	s.d.Restart(c, "--live-restore")
  2828  
  2829  	out2, err := s.d.Cmd("exec", "-u", "test", "top", "id")
  2830  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out2))
  2831  	c.Assert(out1, check.Equals, out2, check.Commentf("Output: before restart '%s', after restart '%s'", out1, out2))
  2832  
  2833  	out, err = s.d.Cmd("stop", "top")
  2834  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2835  }
  2836  
  2837  func (s *DockerDaemonSuite) TestRemoveContainerAfterLiveRestore(c *check.C) {
  2838  	testRequires(c, DaemonIsLinux, overlayFSSupported, SameHostDaemon)
  2839  	s.d.StartWithBusybox(c, "--live-restore", "--storage-driver", "overlay")
  2840  	out, err := s.d.Cmd("run", "-d", "--name=top", "busybox", "top")
  2841  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2842  
  2843  	s.d.WaitRun("top")
  2844  
  2845  	// restart daemon.
  2846  	s.d.Restart(c, "--live-restore", "--storage-driver", "overlay")
  2847  
  2848  	out, err = s.d.Cmd("stop", "top")
  2849  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2850  
  2851  	// test if the rootfs mountpoint still exist
  2852  	mountpoint, err := s.d.InspectField("top", ".GraphDriver.Data.MergedDir")
  2853  	c.Assert(err, check.IsNil)
  2854  	f, err := os.Open("/proc/self/mountinfo")
  2855  	c.Assert(err, check.IsNil)
  2856  	defer f.Close()
  2857  	sc := bufio.NewScanner(f)
  2858  	for sc.Scan() {
  2859  		line := sc.Text()
  2860  		if strings.Contains(line, mountpoint) {
  2861  			c.Fatalf("mountinfo should not include the mountpoint of stop container")
  2862  		}
  2863  	}
  2864  
  2865  	out, err = s.d.Cmd("rm", "top")
  2866  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2867  }
  2868  
  2869  // #29598
  2870  func (s *DockerDaemonSuite) TestRestartPolicyWithLiveRestore(c *check.C) {
  2871  	testRequires(c, DaemonIsLinux, SameHostDaemon)
  2872  	s.d.StartWithBusybox(c, "--live-restore")
  2873  
  2874  	out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  2875  	c.Assert(err, check.IsNil, check.Commentf("output: %s", out))
  2876  	id := strings.TrimSpace(out)
  2877  
  2878  	type state struct {
  2879  		Running   bool
  2880  		StartedAt time.Time
  2881  	}
  2882  	out, err = s.d.Cmd("inspect", "-f", "{{json .State}}", id)
  2883  	c.Assert(err, checker.IsNil, check.Commentf("output: %s", out))
  2884  
  2885  	var origState state
  2886  	err = json.Unmarshal([]byte(strings.TrimSpace(out)), &origState)
  2887  	c.Assert(err, checker.IsNil)
  2888  
  2889  	s.d.Restart(c, "--live-restore")
  2890  
  2891  	pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", id)
  2892  	c.Assert(err, check.IsNil)
  2893  	pidint, err := strconv.Atoi(strings.TrimSpace(pid))
  2894  	c.Assert(err, check.IsNil)
  2895  	c.Assert(pidint, checker.GreaterThan, 0)
  2896  	c.Assert(syscall.Kill(pidint, syscall.SIGKILL), check.IsNil)
  2897  
  2898  	ticker := time.NewTicker(50 * time.Millisecond)
  2899  	timeout := time.After(10 * time.Second)
  2900  
  2901  	for range ticker.C {
  2902  		select {
  2903  		case <-timeout:
  2904  			c.Fatal("timeout waiting for container restart")
  2905  		default:
  2906  		}
  2907  
  2908  		out, err := s.d.Cmd("inspect", "-f", "{{json .State}}", id)
  2909  		c.Assert(err, checker.IsNil, check.Commentf("output: %s", out))
  2910  
  2911  		var newState state
  2912  		err = json.Unmarshal([]byte(strings.TrimSpace(out)), &newState)
  2913  		c.Assert(err, checker.IsNil)
  2914  
  2915  		if !newState.Running {
  2916  			continue
  2917  		}
  2918  		if newState.StartedAt.After(origState.StartedAt) {
  2919  			break
  2920  		}
  2921  	}
  2922  
  2923  	out, err = s.d.Cmd("stop", id)
  2924  	c.Assert(err, check.IsNil, check.Commentf("output: %s", out))
  2925  }
  2926  
  2927  func (s *DockerDaemonSuite) TestShmSize(c *check.C) {
  2928  	testRequires(c, DaemonIsLinux)
  2929  
  2930  	size := 67108864 * 2
  2931  	pattern := regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
  2932  
  2933  	s.d.StartWithBusybox(c, "--default-shm-size", fmt.Sprintf("%v", size))
  2934  
  2935  	name := "shm1"
  2936  	out, err := s.d.Cmd("run", "--name", name, "busybox", "mount")
  2937  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2938  	c.Assert(pattern.MatchString(out), checker.True)
  2939  	out, err = s.d.Cmd("inspect", "--format", "{{.HostConfig.ShmSize}}", name)
  2940  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2941  	c.Assert(strings.TrimSpace(out), check.Equals, fmt.Sprintf("%v", size))
  2942  }
  2943  
  2944  func (s *DockerDaemonSuite) TestShmSizeReload(c *check.C) {
  2945  	testRequires(c, DaemonIsLinux)
  2946  
  2947  	configPath, err := ioutil.TempDir("", "test-daemon-shm-size-reload-config")
  2948  	c.Assert(err, checker.IsNil, check.Commentf("could not create temp file for config reload"))
  2949  	defer os.RemoveAll(configPath) // clean up
  2950  	configFile := filepath.Join(configPath, "config.json")
  2951  
  2952  	size := 67108864 * 2
  2953  	configData := []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
  2954  	c.Assert(ioutil.WriteFile(configFile, configData, 0666), checker.IsNil, check.Commentf("could not write temp file for config reload"))
  2955  	pattern := regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
  2956  
  2957  	s.d.StartWithBusybox(c, "--config-file", configFile)
  2958  
  2959  	name := "shm1"
  2960  	out, err := s.d.Cmd("run", "--name", name, "busybox", "mount")
  2961  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2962  	c.Assert(pattern.MatchString(out), checker.True)
  2963  	out, err = s.d.Cmd("inspect", "--format", "{{.HostConfig.ShmSize}}", name)
  2964  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2965  	c.Assert(strings.TrimSpace(out), check.Equals, fmt.Sprintf("%v", size))
  2966  
  2967  	size = 67108864 * 3
  2968  	configData = []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
  2969  	c.Assert(ioutil.WriteFile(configFile, configData, 0666), checker.IsNil, check.Commentf("could not write temp file for config reload"))
  2970  	pattern = regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
  2971  
  2972  	err = s.d.ReloadConfig()
  2973  	c.Assert(err, checker.IsNil, check.Commentf("error reloading daemon config"))
  2974  
  2975  	name = "shm2"
  2976  	out, err = s.d.Cmd("run", "--name", name, "busybox", "mount")
  2977  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2978  	c.Assert(pattern.MatchString(out), checker.True)
  2979  	out, err = s.d.Cmd("inspect", "--format", "{{.HostConfig.ShmSize}}", name)
  2980  	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  2981  	c.Assert(strings.TrimSpace(out), check.Equals, fmt.Sprintf("%v", size))
  2982  }