moby/integration-cli/docker_cli_stats_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

182 lines
5.8 KiB
Go

package main
import (
"bufio"
"os/exec"
"regexp"
"strings"
"time"
"github.com/docker/docker/integration-cli/cli"
"github.com/go-check/check"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func (s *DockerSuite) TestStatsNoStream(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
id := strings.TrimSpace(out)
assert.NilError(c, waitRun(id))
statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
type output struct {
out []byte
err error
}
ch := make(chan output)
go func() {
out, err := statsCmd.Output()
ch <- output{out, err}
}()
select {
case outerr := <-ch:
assert.NilError(c, outerr.err, "Error running stats: %v", outerr.err)
assert.Assert(c, is.Contains(string(outerr.out), id[:12]), "running container wasn't present in output")
case <-time.After(3 * time.Second):
statsCmd.Process.Kill()
c.Fatalf("stats did not return immediately when not streaming")
}
}
func (s *DockerSuite) TestStatsContainerNotFound(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
out, _, err := dockerCmdWithError("stats", "notfound")
assert.ErrorContains(c, err, "")
assert.Assert(c, is.Contains(out, "No such container: notfound"), "Expected to fail on not found container stats, got %q instead", out)
out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
assert.ErrorContains(c, err, "")
assert.Assert(c, is.Contains(out, "No such container: notfound"), "Expected to fail on not found container stats with --no-stream, got %q instead", out)
}
func (s *DockerSuite) TestStatsAllRunningNoStream(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
id1 := strings.TrimSpace(out)[:12]
assert.NilError(c, waitRun(id1))
out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
id2 := strings.TrimSpace(out)[:12]
assert.NilError(c, waitRun(id2))
out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
id3 := strings.TrimSpace(out)[:12]
assert.NilError(c, waitRun(id3))
dockerCmd(c, "stop", id3)
out, _ = dockerCmd(c, "stats", "--no-stream")
if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
}
if strings.Contains(out, id3) {
c.Fatalf("Did not expect %s in stats, got %s", id3, out)
}
// check output contains real data, but not all zeros
reg, _ := regexp.Compile("[1-9]+")
// split output with "\n", outLines[1] is id2's output
// outLines[2] is id1's output
outLines := strings.Split(out, "\n")
// check stat result of id2 contains real data
realData := reg.Find([]byte(outLines[1][12:]))
assert.Assert(c, realData != nil, "stat result are empty: %s", out)
// check stat result of id1 contains real data
realData = reg.Find([]byte(outLines[2][12:]))
assert.Assert(c, realData != nil, "stat result are empty: %s", out)
}
func (s *DockerSuite) TestStatsAllNoStream(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
id1 := strings.TrimSpace(out)[:12]
assert.NilError(c, waitRun(id1))
dockerCmd(c, "stop", id1)
out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
id2 := strings.TrimSpace(out)[:12]
assert.NilError(c, waitRun(id2))
out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
}
// check output contains real data, but not all zeros
reg, _ := regexp.Compile("[1-9]+")
// split output with "\n", outLines[1] is id2's output
outLines := strings.Split(out, "\n")
// check stat result of id2 contains real data
realData := reg.Find([]byte(outLines[1][12:]))
assert.Assert(c, realData != nil, "stat result of %s is empty: %s", id2, out)
// check stat result of id1 contains all zero
realData = reg.Find([]byte(outLines[2][12:]))
assert.Assert(c, realData == nil, "stat result of %s should be empty : %s", id1, out)
}
func (s *DockerSuite) TestStatsAllNewContainersAdded(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
id := make(chan string)
addedChan := make(chan struct{})
runSleepingContainer(c, "-d")
statsCmd := exec.Command(dockerBinary, "stats")
stdout, err := statsCmd.StdoutPipe()
assert.NilError(c, err)
assert.NilError(c, statsCmd.Start())
go statsCmd.Wait()
defer statsCmd.Process.Kill()
go func() {
containerID := <-id
matchID := regexp.MustCompile(containerID)
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
switch {
case matchID.MatchString(scanner.Text()):
close(addedChan)
return
}
}
}()
out := runSleepingContainer(c, "-d")
assert.NilError(c, waitRun(strings.TrimSpace(out)))
id <- strings.TrimSpace(out)[:12]
select {
case <-time.After(30 * time.Second):
c.Fatal("failed to observe new container created added to stats")
case <-addedChan:
// ignore, done
}
}
func (s *DockerSuite) TestStatsFormatAll(c *testing.T) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
cli.DockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
cli.WaitRun(c, "RunningOne")
cli.DockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
cli.DockerCmd(c, "stop", "ExitedOne")
cli.WaitExited(c, "ExitedOne", 5*time.Second)
out := cli.DockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}").Combined()
assert.Assert(c, is.Contains(out, "RunningOne"))
assert.Assert(c, !strings.Contains(out, "ExitedOne"))
out = cli.DockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}").Combined()
assert.Assert(c, is.Contains(out, "RunningOne"))
assert.Assert(c, is.Contains(out, "ExitedOne"))
}