github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_info_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/integration-cli/checker"
    10  	"github.com/docker/docker/integration-cli/daemon"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  // ensure docker info succeeds
    15  func (s *DockerSuite) TestInfoEnsureSucceeds(c *check.C) {
    16  	out, _ := dockerCmd(c, "info")
    17  
    18  	// always shown fields
    19  	stringsToCheck := []string{
    20  		"ID:",
    21  		"Containers:",
    22  		" Running:",
    23  		" Paused:",
    24  		" Stopped:",
    25  		"Images:",
    26  		"OSType:",
    27  		"Architecture:",
    28  		"Logging Driver:",
    29  		"Operating System:",
    30  		"CPUs:",
    31  		"Total Memory:",
    32  		"Kernel Version:",
    33  		"Storage Driver:",
    34  		"Volume:",
    35  		"Network:",
    36  		"Live Restore Enabled:",
    37  	}
    38  
    39  	if testEnv.DaemonPlatform() == "linux" {
    40  		stringsToCheck = append(stringsToCheck, "Init Binary:", "Security Options:", "containerd version:", "runc version:", "init version:")
    41  	}
    42  
    43  	if DaemonIsLinux() {
    44  		stringsToCheck = append(stringsToCheck, "Runtimes:", "Default Runtime: runc")
    45  	}
    46  
    47  	if testEnv.ExperimentalDaemon() {
    48  		stringsToCheck = append(stringsToCheck, "Experimental: true")
    49  	} else {
    50  		stringsToCheck = append(stringsToCheck, "Experimental: false")
    51  	}
    52  
    53  	for _, linePrefix := range stringsToCheck {
    54  		c.Assert(out, checker.Contains, linePrefix, check.Commentf("couldn't find string %v in output", linePrefix))
    55  	}
    56  }
    57  
    58  // TestInfoFormat tests `docker info --format`
    59  func (s *DockerSuite) TestInfoFormat(c *check.C) {
    60  	out, status := dockerCmd(c, "info", "--format", "{{json .}}")
    61  	c.Assert(status, checker.Equals, 0)
    62  	var m map[string]interface{}
    63  	err := json.Unmarshal([]byte(out), &m)
    64  	c.Assert(err, checker.IsNil)
    65  	_, _, err = dockerCmdWithError("info", "--format", "{{.badString}}")
    66  	c.Assert(err, checker.NotNil)
    67  }
    68  
    69  // TestInfoDiscoveryBackend verifies that a daemon run with `--cluster-advertise` and
    70  // `--cluster-store` properly show the backend's endpoint in info output.
    71  func (s *DockerSuite) TestInfoDiscoveryBackend(c *check.C) {
    72  	testRequires(c, SameHostDaemon, DaemonIsLinux)
    73  
    74  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
    75  		Experimental: testEnv.ExperimentalDaemon(),
    76  	})
    77  	discoveryBackend := "consul://consuladdr:consulport/some/path"
    78  	discoveryAdvertise := "1.1.1.1:2375"
    79  	d.Start(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend), fmt.Sprintf("--cluster-advertise=%s", discoveryAdvertise))
    80  	defer d.Stop(c)
    81  
    82  	out, err := d.Cmd("info")
    83  	c.Assert(err, checker.IsNil)
    84  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Store: %s\n", discoveryBackend))
    85  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Advertise: %s\n", discoveryAdvertise))
    86  }
    87  
    88  // TestInfoDiscoveryInvalidAdvertise verifies that a daemon run with
    89  // an invalid `--cluster-advertise` configuration
    90  func (s *DockerSuite) TestInfoDiscoveryInvalidAdvertise(c *check.C) {
    91  	testRequires(c, SameHostDaemon, DaemonIsLinux)
    92  
    93  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
    94  		Experimental: testEnv.ExperimentalDaemon(),
    95  	})
    96  	discoveryBackend := "consul://consuladdr:consulport/some/path"
    97  
    98  	// --cluster-advertise with an invalid string is an error
    99  	err := d.StartWithError(fmt.Sprintf("--cluster-store=%s", discoveryBackend), "--cluster-advertise=invalid")
   100  	c.Assert(err, checker.NotNil)
   101  
   102  	// --cluster-advertise without --cluster-store is also an error
   103  	err = d.StartWithError("--cluster-advertise=1.1.1.1:2375")
   104  	c.Assert(err, checker.NotNil)
   105  }
   106  
   107  // TestInfoDiscoveryAdvertiseInterfaceName verifies that a daemon run with `--cluster-advertise`
   108  // configured with interface name properly show the advertise ip-address in info output.
   109  func (s *DockerSuite) TestInfoDiscoveryAdvertiseInterfaceName(c *check.C) {
   110  	testRequires(c, SameHostDaemon, Network, DaemonIsLinux)
   111  
   112  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   113  		Experimental: testEnv.ExperimentalDaemon(),
   114  	})
   115  	discoveryBackend := "consul://consuladdr:consulport/some/path"
   116  	discoveryAdvertise := "eth0"
   117  
   118  	d.Start(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend), fmt.Sprintf("--cluster-advertise=%s:2375", discoveryAdvertise))
   119  	defer d.Stop(c)
   120  
   121  	iface, err := net.InterfaceByName(discoveryAdvertise)
   122  	c.Assert(err, checker.IsNil)
   123  	addrs, err := iface.Addrs()
   124  	c.Assert(err, checker.IsNil)
   125  	c.Assert(len(addrs), checker.GreaterThan, 0)
   126  	ip, _, err := net.ParseCIDR(addrs[0].String())
   127  	c.Assert(err, checker.IsNil)
   128  
   129  	out, err := d.Cmd("info")
   130  	c.Assert(err, checker.IsNil)
   131  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Store: %s\n", discoveryBackend))
   132  	c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Advertise: %s:2375\n", ip.String()))
   133  }
   134  
   135  func (s *DockerSuite) TestInfoDisplaysRunningContainers(c *check.C) {
   136  	testRequires(c, DaemonIsLinux)
   137  
   138  	existing := existingContainerStates(c)
   139  
   140  	dockerCmd(c, "run", "-d", "busybox", "top")
   141  	out, _ := dockerCmd(c, "info")
   142  	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
   143  	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]+1))
   144  	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
   145  	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
   146  }
   147  
   148  func (s *DockerSuite) TestInfoDisplaysPausedContainers(c *check.C) {
   149  	testRequires(c, IsPausable)
   150  
   151  	existing := existingContainerStates(c)
   152  
   153  	out := runSleepingContainer(c, "-d")
   154  	cleanedContainerID := strings.TrimSpace(out)
   155  
   156  	dockerCmd(c, "pause", cleanedContainerID)
   157  
   158  	out, _ = dockerCmd(c, "info")
   159  	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
   160  	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
   161  	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]+1))
   162  	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
   163  }
   164  
   165  func (s *DockerSuite) TestInfoDisplaysStoppedContainers(c *check.C) {
   166  	testRequires(c, DaemonIsLinux)
   167  
   168  	existing := existingContainerStates(c)
   169  
   170  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
   171  	cleanedContainerID := strings.TrimSpace(out)
   172  
   173  	dockerCmd(c, "stop", cleanedContainerID)
   174  
   175  	out, _ = dockerCmd(c, "info")
   176  	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
   177  	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
   178  	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
   179  	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]+1))
   180  }
   181  
   182  func (s *DockerSuite) TestInfoDebug(c *check.C) {
   183  	testRequires(c, SameHostDaemon, DaemonIsLinux)
   184  
   185  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   186  		Experimental: testEnv.ExperimentalDaemon(),
   187  	})
   188  	d.Start(c, "--debug")
   189  	defer d.Stop(c)
   190  
   191  	out, err := d.Cmd("--debug", "info")
   192  	c.Assert(err, checker.IsNil)
   193  	c.Assert(out, checker.Contains, "Debug Mode (client): true\n")
   194  	c.Assert(out, checker.Contains, "Debug Mode (server): true\n")
   195  	c.Assert(out, checker.Contains, "File Descriptors")
   196  	c.Assert(out, checker.Contains, "Goroutines")
   197  	c.Assert(out, checker.Contains, "System Time")
   198  	c.Assert(out, checker.Contains, "EventsListeners")
   199  	c.Assert(out, checker.Contains, "Docker Root Dir")
   200  }
   201  
   202  func (s *DockerSuite) TestInsecureRegistries(c *check.C) {
   203  	testRequires(c, SameHostDaemon, DaemonIsLinux)
   204  
   205  	registryCIDR := "192.168.1.0/24"
   206  	registryHost := "insecurehost.com:5000"
   207  
   208  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   209  		Experimental: testEnv.ExperimentalDaemon(),
   210  	})
   211  	d.Start(c, "--insecure-registry="+registryCIDR, "--insecure-registry="+registryHost)
   212  	defer d.Stop(c)
   213  
   214  	out, err := d.Cmd("info")
   215  	c.Assert(err, checker.IsNil)
   216  	c.Assert(out, checker.Contains, "Insecure Registries:\n")
   217  	c.Assert(out, checker.Contains, fmt.Sprintf(" %s\n", registryHost))
   218  	c.Assert(out, checker.Contains, fmt.Sprintf(" %s\n", registryCIDR))
   219  }
   220  
   221  func (s *DockerDaemonSuite) TestRegistryMirrors(c *check.C) {
   222  	testRequires(c, SameHostDaemon, DaemonIsLinux)
   223  
   224  	registryMirror1 := "https://192.168.1.2"
   225  	registryMirror2 := "http://registry.mirror.com:5000"
   226  
   227  	s.d.Start(c, "--registry-mirror="+registryMirror1, "--registry-mirror="+registryMirror2)
   228  
   229  	out, err := s.d.Cmd("info")
   230  	c.Assert(err, checker.IsNil)
   231  	c.Assert(out, checker.Contains, "Registry Mirrors:\n")
   232  	c.Assert(out, checker.Contains, fmt.Sprintf(" %s", registryMirror1))
   233  	c.Assert(out, checker.Contains, fmt.Sprintf(" %s", registryMirror2))
   234  }
   235  
   236  func existingContainerStates(c *check.C) map[string]int {
   237  	out, _ := dockerCmd(c, "info", "--format", "{{json .}}")
   238  	var m map[string]interface{}
   239  	err := json.Unmarshal([]byte(out), &m)
   240  	c.Assert(err, checker.IsNil)
   241  	res := map[string]int{}
   242  	res["Containers"] = int(m["Containers"].(float64))
   243  	res["ContainersRunning"] = int(m["ContainersRunning"].(float64))
   244  	res["ContainersPaused"] = int(m["ContainersPaused"].(float64))
   245  	res["ContainersStopped"] = int(m["ContainersStopped"].(float64))
   246  	return res
   247  }