github.com/shishir-a412ed/docker@v1.3.2-0.20180103180333-fda904911d87/integration-cli/docker_cli_daemon_test.go (about)

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