docker_cli_info_test.go 8.7 KB

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