summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2021-02-17 07:52:42 -0500
committerDaniel J Walsh <dwalsh@redhat.com>2021-02-17 07:55:12 -0500
commite2d5a1d0512abed12e0844ab33fdbf023f206c7c (patch)
treee4504a9f342917919f0c00075cb0fa708dc63c41
parent50042120e947fc7aee601f0c65ea485daf604ee1 (diff)
downloadpodman-e2d5a1d0512abed12e0844ab33fdbf023f206c7c.tar.gz
podman-e2d5a1d0512abed12e0844ab33fdbf023f206c7c.tar.bz2
podman-e2d5a1d0512abed12e0844ab33fdbf023f206c7c.zip
podman ps --format '{{ .Size }}' requires --size option
Podman -s crashes when the user specifies the '{{ .Size }}` format on the podman ps command, without specifying the --size option. This PR will stop the crash and print out a logrus.Error stating that the caller should add the --size option. Fixes: https://github.com/containers/podman/issues/9408 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r--cmd/podman/containers/ps.go6
-rw-r--r--test/e2e/ps_test.go15
2 files changed, 21 insertions, 0 deletions
diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go
index 51e7bf5b5..55b53b4d9 100644
--- a/cmd/podman/containers/ps.go
+++ b/cmd/podman/containers/ps.go
@@ -22,6 +22,7 @@ import (
"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-units"
"github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -390,6 +391,11 @@ func (l psReporter) Command() string {
// Size returns the rootfs and virtual sizes in human duration in
// and output form (string) suitable for ps
func (l psReporter) Size() string {
+ if l.ListContainer.Size == nil {
+ logrus.Errorf("Size format requires --size option")
+ return ""
+ }
+
virt := units.HumanSizeWithPrecision(float64(l.ListContainer.Size.RootFsSize), 3)
s := units.HumanSizeWithPrecision(float64(l.ListContainer.Size.RwSize), 3)
return fmt.Sprintf("%s (virtual %s)", s, virt)
diff --git a/test/e2e/ps_test.go b/test/e2e/ps_test.go
index 225bd538e..016b4c8cd 100644
--- a/test/e2e/ps_test.go
+++ b/test/e2e/ps_test.go
@@ -350,6 +350,21 @@ var _ = Describe("Podman ps", func() {
Expect(session).To(ExitWithError())
})
+ It("podman --format by size", func() {
+ session := podmanTest.Podman([]string{"create", "busybox", "ls"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ session = podmanTest.Podman([]string{"create", "-t", ALPINE, "top"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ session = podmanTest.Podman([]string{"ps", "-a", "--format", "{{.Size}}"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ Expect(session.ErrorToString()).To(ContainSubstring("Size format requires --size option"))
+ })
+
It("podman --sort by size", func() {
session := podmanTest.Podman([]string{"create", "busybox", "ls"})
session.WaitWithDefaultTimeout()