1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_info_test.go
Tibor Vass 1d92789b4f rm-gocheck: check.C -> testing.T
sed -E -i 's#\bcheck\.C\b#testing.T#g' \
-- "integration-cli/check_test.go" "integration-cli/daemon/daemon.go" "integration-cli/daemon/daemon_swarm.go" "integration-cli/daemon_swarm_hack_test.go" "integration-cli/docker_api_attach_test.go" "integration-cli/docker_api_build_test.go" "integration-cli/docker_api_build_windows_test.go" "integration-cli/docker_api_containers_test.go" "integration-cli/docker_api_containers_windows_test.go" "integration-cli/docker_api_exec_resize_test.go" "integration-cli/docker_api_exec_test.go" "integration-cli/docker_api_images_test.go" "integration-cli/docker_api_inspect_test.go" "integration-cli/docker_api_logs_test.go" "integration-cli/docker_api_network_test.go" "integration-cli/docker_api_stats_test.go" "integration-cli/docker_api_swarm_node_test.go" "integration-cli/docker_api_swarm_service_test.go" "integration-cli/docker_api_swarm_test.go" "integration-cli/docker_api_test.go" "integration-cli/docker_cli_attach_test.go" "integration-cli/docker_cli_attach_unix_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_build_unix_test.go" "integration-cli/docker_cli_by_digest_test.go" "integration-cli/docker_cli_commit_test.go" "integration-cli/docker_cli_cp_from_container_test.go" "integration-cli/docker_cli_cp_test.go" "integration-cli/docker_cli_cp_to_container_test.go" "integration-cli/docker_cli_cp_to_container_unix_test.go" "integration-cli/docker_cli_cp_utils_test.go" "integration-cli/docker_cli_create_test.go" "integration-cli/docker_cli_daemon_plugins_test.go" "integration-cli/docker_cli_daemon_test.go" "integration-cli/docker_cli_events_test.go" "integration-cli/docker_cli_events_unix_test.go" "integration-cli/docker_cli_exec_test.go" "integration-cli/docker_cli_exec_unix_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_health_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_import_test.go" "integration-cli/docker_cli_info_test.go" "integration-cli/docker_cli_info_unix_test.go" "integration-cli/docker_cli_inspect_test.go" "integration-cli/docker_cli_links_test.go" "integration-cli/docker_cli_login_test.go" "integration-cli/docker_cli_logout_test.go" "integration-cli/docker_cli_logs_test.go" "integration-cli/docker_cli_netmode_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_cli_plugins_logdriver_test.go" "integration-cli/docker_cli_plugins_test.go" "integration-cli/docker_cli_port_test.go" "integration-cli/docker_cli_proxy_test.go" "integration-cli/docker_cli_prune_unix_test.go" "integration-cli/docker_cli_ps_test.go" "integration-cli/docker_cli_pull_local_test.go" "integration-cli/docker_cli_pull_test.go" "integration-cli/docker_cli_push_test.go" "integration-cli/docker_cli_registry_user_agent_test.go" "integration-cli/docker_cli_restart_test.go" "integration-cli/docker_cli_rmi_test.go" "integration-cli/docker_cli_run_test.go" "integration-cli/docker_cli_run_unix_test.go" "integration-cli/docker_cli_save_load_test.go" "integration-cli/docker_cli_save_load_unix_test.go" "integration-cli/docker_cli_search_test.go" "integration-cli/docker_cli_service_create_test.go" "integration-cli/docker_cli_service_health_test.go" "integration-cli/docker_cli_service_logs_test.go" "integration-cli/docker_cli_service_scale_test.go" "integration-cli/docker_cli_sni_test.go" "integration-cli/docker_cli_start_test.go" "integration-cli/docker_cli_stats_test.go" "integration-cli/docker_cli_swarm_test.go" "integration-cli/docker_cli_swarm_unix_test.go" "integration-cli/docker_cli_top_test.go" "integration-cli/docker_cli_update_unix_test.go" "integration-cli/docker_cli_userns_test.go" "integration-cli/docker_cli_v2_only_test.go" "integration-cli/docker_cli_volume_test.go" "integration-cli/docker_deprecated_api_v124_test.go" "integration-cli/docker_deprecated_api_v124_unix_test.go" "integration-cli/docker_hub_pull_suite_test.go" "integration-cli/docker_utils_test.go" "integration-cli/events_utils_test.go" "integration-cli/fixtures_linux_daemon_test.go" "integration-cli/utils_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/generator_test.go" "pkg/discovery/kv/kv_test.go" "pkg/discovery/memory/memory_test.go" "pkg/discovery/nodes/nodes_test.go"

Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:05:55 +00:00

238 lines
8.9 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net"
"strings"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/daemon"
testdaemon "github.com/docker/docker/internal/test/daemon"
"github.com/go-check/check"
"gotest.tools/assert"
)
// ensure docker info succeeds
func (s *DockerSuite) TestInfoEnsureSucceeds(c *testing.T) {
out, _ := dockerCmd(c, "info")
// always shown fields
stringsToCheck := []string{
"ID:",
"Containers:",
" Running:",
" Paused:",
" Stopped:",
"Images:",
"OSType:",
"Architecture:",
"Logging Driver:",
"Operating System:",
"CPUs:",
"Total Memory:",
"Kernel Version:",
"Storage Driver:",
"Volume:",
"Network:",
"Live Restore Enabled:",
}
if testEnv.OSType == "linux" {
stringsToCheck = append(stringsToCheck, "Init Binary:", "Security Options:", "containerd version:", "runc version:", "init version:")
}
if DaemonIsLinux() {
stringsToCheck = append(stringsToCheck, "Runtimes:", "Default Runtime: runc")
}
if testEnv.DaemonInfo.ExperimentalBuild {
stringsToCheck = append(stringsToCheck, "Experimental: true")
} else {
stringsToCheck = append(stringsToCheck, "Experimental: false")
}
for _, linePrefix := range stringsToCheck {
assert.Assert(c, out, checker.Contains, linePrefix, check.Commentf("couldn't find string %v in output", linePrefix))
}
}
// TestInfoFormat tests `docker info --format`
func (s *DockerSuite) TestInfoFormat(c *testing.T) {
out, status := dockerCmd(c, "info", "--format", "{{json .}}")
assert.Assert(c, status, checker.Equals, 0)
var m map[string]interface{}
err := json.Unmarshal([]byte(out), &m)
assert.NilError(c, err)
_, _, err = dockerCmdWithError("info", "--format", "{{.badString}}")
assert.ErrorContains(c, err, "")
}
// TestInfoDiscoveryBackend verifies that a daemon run with `--cluster-advertise` and
// `--cluster-store` properly show the backend's endpoint in info output.
func (s *DockerSuite) TestInfoDiscoveryBackend(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
discoveryBackend := "consul://consuladdr:consulport/some/path"
discoveryAdvertise := "1.1.1.1:2375"
d.Start(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend), fmt.Sprintf("--cluster-advertise=%s", discoveryAdvertise))
defer d.Stop(c)
out, err := d.Cmd("info")
assert.NilError(c, err)
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Cluster Store: %s\n", discoveryBackend))
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Cluster Advertise: %s\n", discoveryAdvertise))
}
// TestInfoDiscoveryInvalidAdvertise verifies that a daemon run with
// an invalid `--cluster-advertise` configuration
func (s *DockerSuite) TestInfoDiscoveryInvalidAdvertise(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
discoveryBackend := "consul://consuladdr:consulport/some/path"
// --cluster-advertise with an invalid string is an error
err := d.StartWithError(fmt.Sprintf("--cluster-store=%s", discoveryBackend), "--cluster-advertise=invalid")
assert.ErrorContains(c, err, "")
// --cluster-advertise without --cluster-store is also an error
err = d.StartWithError("--cluster-advertise=1.1.1.1:2375")
assert.ErrorContains(c, err, "")
}
// TestInfoDiscoveryAdvertiseInterfaceName verifies that a daemon run with `--cluster-advertise`
// configured with interface name properly show the advertise ip-address in info output.
func (s *DockerSuite) TestInfoDiscoveryAdvertiseInterfaceName(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon, Network, DaemonIsLinux)
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
discoveryBackend := "consul://consuladdr:consulport/some/path"
discoveryAdvertise := "eth0"
d.Start(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend), fmt.Sprintf("--cluster-advertise=%s:2375", discoveryAdvertise))
defer d.Stop(c)
iface, err := net.InterfaceByName(discoveryAdvertise)
assert.NilError(c, err)
addrs, err := iface.Addrs()
assert.NilError(c, err)
assert.Assert(c, len(addrs) > 0)
ip, _, err := net.ParseCIDR(addrs[0].String())
assert.NilError(c, err)
out, err := d.Cmd("info")
assert.NilError(c, err)
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Cluster Store: %s\n", discoveryBackend))
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Cluster Advertise: %s:2375\n", ip.String()))
}
func (s *DockerSuite) TestInfoDisplaysRunningContainers(c *testing.T) {
testRequires(c, DaemonIsLinux)
existing := existingContainerStates(c)
dockerCmd(c, "run", "-d", "busybox", "top")
out, _ := dockerCmd(c, "info")
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]+1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
}
func (s *DockerSuite) TestInfoDisplaysPausedContainers(c *testing.T) {
testRequires(c, IsPausable)
existing := existingContainerStates(c)
out := runSleepingContainer(c, "-d")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "pause", cleanedContainerID)
out, _ = dockerCmd(c, "info")
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]+1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
}
func (s *DockerSuite) TestInfoDisplaysStoppedContainers(c *testing.T) {
testRequires(c, DaemonIsLinux)
existing := existingContainerStates(c)
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "stop", cleanedContainerID)
out, _ = dockerCmd(c, "info")
assert.Assert(c, out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]+1))
}
func (s *DockerSuite) TestInfoDebug(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
d.Start(c, "--debug")
defer d.Stop(c)
out, err := d.Cmd("--debug", "info")
assert.NilError(c, err)
assert.Assert(c, out, checker.Contains, "Debug Mode (client): true\n")
assert.Assert(c, out, checker.Contains, "Debug Mode (server): true\n")
assert.Assert(c, out, checker.Contains, "File Descriptors")
assert.Assert(c, out, checker.Contains, "Goroutines")
assert.Assert(c, out, checker.Contains, "System Time")
assert.Assert(c, out, checker.Contains, "EventsListeners")
assert.Assert(c, out, checker.Contains, "Docker Root Dir")
}
func (s *DockerSuite) TestInsecureRegistries(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
registryCIDR := "192.168.1.0/24"
registryHost := "insecurehost.com:5000"
d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
d.Start(c, "--insecure-registry="+registryCIDR, "--insecure-registry="+registryHost)
defer d.Stop(c)
out, err := d.Cmd("info")
assert.NilError(c, err)
assert.Assert(c, out, checker.Contains, "Insecure Registries:\n")
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" %s\n", registryHost))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" %s\n", registryCIDR))
}
func (s *DockerDaemonSuite) TestRegistryMirrors(c *testing.T) {
registryMirror1 := "https://192.168.1.2"
registryMirror2 := "http://registry.mirror.com:5000"
s.d.Start(c, "--registry-mirror="+registryMirror1, "--registry-mirror="+registryMirror2)
out, err := s.d.Cmd("info")
assert.NilError(c, err)
assert.Assert(c, out, checker.Contains, "Registry Mirrors:\n")
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" %s", registryMirror1))
assert.Assert(c, out, checker.Contains, fmt.Sprintf(" %s", registryMirror2))
}
func existingContainerStates(c *testing.T) map[string]int {
out, _ := dockerCmd(c, "info", "--format", "{{json .}}")
var m map[string]interface{}
err := json.Unmarshal([]byte(out), &m)
assert.NilError(c, err)
res := map[string]int{}
res["Containers"] = int(m["Containers"].(float64))
res["ContainersRunning"] = int(m["ContainersRunning"].(float64))
res["ContainersPaused"] = int(m["ContainersPaused"].(float64))
res["ContainersStopped"] = int(m["ContainersStopped"].(float64))
return res
}