2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2016-06-21 09:15:17 +00:00
|
|
|
"encoding/json"
|
2015-09-10 23:12:00 +00:00
|
|
|
"fmt"
|
2015-10-27 20:12:33 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIInfoSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIInfoSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIInfoSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
2014-02-25 16:17:48 +00:00
|
|
|
// ensure docker info succeeds
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIInfoSuite) TestInfoEnsureSucceeds(c *testing.T) {
|
2023-07-27 11:35:46 +00:00
|
|
|
out := cli.DockerCmd(c, "info").Stdout()
|
2014-02-25 16:17:48 +00:00
|
|
|
|
2015-04-09 11:01:39 +00:00
|
|
|
// always shown fields
|
|
|
|
stringsToCheck := []string{
|
|
|
|
"ID:",
|
|
|
|
"Containers:",
|
2015-10-27 20:12:33 +00:00
|
|
|
" Running:",
|
|
|
|
" Paused:",
|
|
|
|
" Stopped:",
|
2015-04-09 11:01:39 +00:00
|
|
|
"Images:",
|
2015-06-13 07:39:19 +00:00
|
|
|
"OSType:",
|
|
|
|
"Architecture:",
|
2015-04-09 11:01:39 +00:00
|
|
|
"Logging Driver:",
|
|
|
|
"Operating System:",
|
|
|
|
"CPUs:",
|
|
|
|
"Total Memory:",
|
|
|
|
"Kernel Version:",
|
2015-05-21 17:48:36 +00:00
|
|
|
"Storage Driver:",
|
2015-10-23 06:08:26 +00:00
|
|
|
"Volume:",
|
|
|
|
"Network:",
|
2016-07-24 20:00:15 +00:00
|
|
|
"Live Restore Enabled:",
|
2015-05-21 17:48:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 09:46:00 +00:00
|
|
|
if testEnv.DaemonInfo.OSType == "linux" {
|
2016-10-24 22:18:58 +00:00
|
|
|
stringsToCheck = append(stringsToCheck, "Init Binary:", "Security Options:", "containerd version:", "runc version:", "init version:")
|
2016-09-07 18:14:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:13:23 +00:00
|
|
|
if DaemonIsLinux() {
|
2016-06-20 19:14:27 +00:00
|
|
|
stringsToCheck = append(stringsToCheck, "Runtimes:", "Default Runtime: runc")
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 14:30:05 +00:00
|
|
|
if testEnv.DaemonInfo.ExperimentalBuild {
|
2015-05-21 17:48:36 +00:00
|
|
|
stringsToCheck = append(stringsToCheck, "Experimental: true")
|
2016-10-06 14:09:54 +00:00
|
|
|
} else {
|
|
|
|
stringsToCheck = append(stringsToCheck, "Experimental: false")
|
2015-05-21 17:48:36 +00:00
|
|
|
}
|
2014-02-25 16:17:48 +00:00
|
|
|
|
|
|
|
for _, linePrefix := range stringsToCheck {
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, linePrefix), "couldn't find string %v in output", linePrefix)
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 23:12:00 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIInfoSuite) TestInfoDisplaysRunningContainers(c *testing.T) {
|
2015-10-27 20:12:33 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2017-09-08 15:16:15 +00:00
|
|
|
existing := existingContainerStates(c)
|
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "busybox", "top")
|
|
|
|
out := cli.DockerCmd(c, "info").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1)))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]+1)))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"])))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"])))
|
2015-10-27 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIInfoSuite) TestInfoDisplaysPausedContainers(c *testing.T) {
|
2016-09-09 00:31:04 +00:00
|
|
|
testRequires(c, IsPausable)
|
2015-10-27 20:12:33 +00:00
|
|
|
|
2017-09-08 15:16:15 +00:00
|
|
|
existing := existingContainerStates(c)
|
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
id := runSleepingContainer(c, "-d")
|
2015-10-27 20:12:33 +00:00
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
cli.DockerCmd(c, "pause", id)
|
2015-10-27 20:12:33 +00:00
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
out := cli.DockerCmd(c, "info").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1)))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"])))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]+1)))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"])))
|
2015-10-27 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIInfoSuite) TestInfoDisplaysStoppedContainers(c *testing.T) {
|
2015-10-27 20:12:33 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2017-09-08 15:16:15 +00:00
|
|
|
existing := existingContainerStates(c)
|
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
2015-10-27 20:12:33 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
cli.DockerCmd(c, "stop", cleanedContainerID)
|
2015-10-27 20:12:33 +00:00
|
|
|
|
2023-07-27 11:35:46 +00:00
|
|
|
out = cli.DockerCmd(c, "info").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1)))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"])))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"])))
|
|
|
|
assert.Assert(c, strings.Contains(out, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]+1)))
|
2015-10-27 20:12:33 +00:00
|
|
|
}
|
2016-02-01 23:09:25 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func existingContainerStates(c *testing.T) map[string]int {
|
2023-07-27 11:35:46 +00:00
|
|
|
out := cli.DockerCmd(c, "info", "--format", "{{json .}}").Stdout()
|
2017-09-08 15:16:15 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
err := json.Unmarshal([]byte(out), &m)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-09-08 15:16:15 +00:00
|
|
|
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
|
|
|
|
}
|