stats: list all running containers unless specified otherwise

Unless specified otherwise by --all, --latest or via arguments, list all
running containers.  This matches the behaviour of Docker and is also
illustrated in the man pages where containers and options are marked to
be optional.

Fixes: 
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2019-10-16 10:30:57 +02:00
parent 46ad6bc1fa
commit 77a2bbb1c0
3 changed files with 17 additions and 9 deletions
cmd/podman
test

@ -35,7 +35,7 @@ var (
statsDescription = "Display percentage of CPU, memory, network I/O, block I/O and PIDs for one or more containers." statsDescription = "Display percentage of CPU, memory, network I/O, block I/O and PIDs for one or more containers."
_statsCommand = &cobra.Command{ _statsCommand = &cobra.Command{
Use: "stats [flags] CONTAINER [CONTAINER...]", Use: "stats [flags] [CONTAINER...]",
Short: "Display a live stream of container resource usage statistics", Short: "Display a live stream of container resource usage statistics",
Long: statsDescription, Long: statsDescription,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@ -44,9 +44,6 @@ var (
statsCommand.Remote = remoteclient statsCommand.Remote = remoteclient
return statsCmd(&statsCommand) return statsCmd(&statsCommand)
}, },
Args: func(cmd *cobra.Command, args []string) error {
return checkAllAndLatest(cmd, args, false)
},
Example: `podman stats --all --no-stream Example: `podman stats --all --no-stream
podman stats ctrID podman stats ctrID
podman stats --no-stream --format "table {{.ID}} {{.Name}} {{.MemUsage}}" ctrID`, podman stats --no-stream --format "table {{.ID}} {{.Name}} {{.MemUsage}}" ctrID`,
@ -92,8 +89,6 @@ func statsCmd(c *cliconfig.StatsValues) error {
if ctr > 1 { if ctr > 1 {
return errors.Errorf("--all, --latest and containers cannot be used together") return errors.Errorf("--all, --latest and containers cannot be used together")
} else if ctr == 0 {
return errors.Errorf("you must specify --all, --latest, or at least one container")
} }
runtime, err := libpodruntime.GetRuntime(getContext(), &c.PodmanCommand) runtime, err := libpodruntime.GetRuntime(getContext(), &c.PodmanCommand)

@ -11,6 +11,8 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
// TODO: we need to check the output. Currently, we only check the exit codes
// which is not enough.
var _ = Describe("Podman stats", func() { var _ = Describe("Podman stats", func() {
var ( var (
tempdir string tempdir string
@ -61,6 +63,15 @@ var _ = Describe("Podman stats", func() {
Expect(session.ExitCode()).To(Equal(0)) Expect(session.ExitCode()).To(Equal(0))
}) })
It("podman stats on all running containers", func() {
session := podmanTest.RunTopContainer("")
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
session = podmanTest.Podman([]string{"stats", "--no-stream"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
})
It("podman stats only output cids", func() { It("podman stats only output cids", func() {
session := podmanTest.RunTopContainer("") session := podmanTest.RunTopContainer("")
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()

@ -55,9 +55,11 @@ function check_help() {
# If usage has required arguments, try running without them # If usage has required arguments, try running without them
if expr "$usage" : '.*\[flags\] [A-Z]' >/dev/null; then if expr "$usage" : '.*\[flags\] [A-Z]' >/dev/null; then
dprint "podman $@ $cmd (without required args)" if [ "$cmd" != "stats"]; then
run_podman 125 "$@" $cmd dprint "podman $@ $cmd (without required args)"
is "$output" "Error:" run_podman 125 "$@" $cmd
is "$output" "Error:"
fi
fi fi
count=$(expr $count + 1) count=$(expr $count + 1)