github.com/containers/podman/v4@v4.9.4/test/e2e/pod_ps_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	. "github.com/containers/podman/v4/test/utils"
     8  	"github.com/containers/storage/pkg/stringid"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Podman ps", func() {
    14  
    15  	It("podman pod ps no pods", func() {
    16  		session := podmanTest.Podman([]string{"pod", "ps"})
    17  		session.WaitWithDefaultTimeout()
    18  		Expect(session).Should(ExitCleanly())
    19  	})
    20  
    21  	It("podman pod ps default", func() {
    22  		_, ec, podid := podmanTest.CreatePod(nil)
    23  		Expect(ec).To(Equal(0))
    24  
    25  		session := podmanTest.RunTopContainerInPod("", podid)
    26  		session.WaitWithDefaultTimeout()
    27  		Expect(session).Should(ExitCleanly())
    28  
    29  		result := podmanTest.Podman([]string{"pod", "ps"})
    30  		result.WaitWithDefaultTimeout()
    31  		Expect(result).Should(ExitCleanly())
    32  		Expect(result.OutputToStringArray()).ShouldNot(BeEmpty())
    33  	})
    34  
    35  	It("podman pod ps quiet flag", func() {
    36  		_, ec, podid := podmanTest.CreatePod(nil)
    37  		Expect(ec).To(Equal(0))
    38  
    39  		_, ec, _ = podmanTest.RunLsContainerInPod("", podid)
    40  		Expect(ec).To(Equal(0))
    41  
    42  		result := podmanTest.Podman([]string{"pod", "ps", "-q"})
    43  		result.WaitWithDefaultTimeout()
    44  		Expect(result).To(ExitCleanly())
    45  		Expect(result.OutputToStringArray()).ShouldNot(BeEmpty())
    46  		Expect(podid).To(ContainSubstring(result.OutputToStringArray()[0]))
    47  	})
    48  
    49  	It("podman pod ps no-trunc", func() {
    50  		_, ec, podid := podmanTest.CreatePod(nil)
    51  		Expect(ec).To(Equal(0))
    52  
    53  		_, ec2, _ := podmanTest.RunLsContainerInPod("", podid)
    54  		Expect(ec2).To(Equal(0))
    55  
    56  		result := podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc"})
    57  		result.WaitWithDefaultTimeout()
    58  		Expect(result).Should(ExitCleanly())
    59  		Expect(result.OutputToStringArray()).ShouldNot(BeEmpty())
    60  		Expect(podid).To(Equal(result.OutputToStringArray()[0]))
    61  	})
    62  
    63  	It("podman pod ps latest", func() {
    64  		SkipIfRemote("--latest flag n/a")
    65  		_, ec, podid1 := podmanTest.CreatePod(nil)
    66  		Expect(ec).To(Equal(0))
    67  
    68  		_, ec2, podid2 := podmanTest.CreatePod(nil)
    69  		Expect(ec2).To(Equal(0))
    70  
    71  		result := podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--latest"})
    72  		result.WaitWithDefaultTimeout()
    73  		Expect(result).Should(ExitCleanly())
    74  		Expect(result.OutputToString()).To(ContainSubstring(podid2))
    75  		Expect(result.OutputToString()).To(Not(ContainSubstring(podid1)))
    76  	})
    77  
    78  	It("podman pod ps id filter flag", func() {
    79  		_, ec, podid := podmanTest.CreatePod(nil)
    80  		Expect(ec).To(Equal(0))
    81  
    82  		result := podmanTest.Podman([]string{"pod", "ps", "--filter", fmt.Sprintf("id=%s", podid)})
    83  		result.WaitWithDefaultTimeout()
    84  		Expect(result).Should(ExitCleanly())
    85  	})
    86  
    87  	It("podman pod ps --filter until", func() {
    88  		name := "mypod"
    89  		_, ec, _ := podmanTest.CreatePod(map[string][]string{"--name": {name}, "--label": {"test=with,comma"}})
    90  		Expect(ec).To(Equal(0))
    91  
    92  		result := podmanTest.Podman([]string{"pod", "ps", "--filter", "until=50"})
    93  		result.WaitWithDefaultTimeout()
    94  		Expect(result).Should(ExitCleanly())
    95  		Expect(result.OutputToString()).To(Not(ContainSubstring(name)))
    96  
    97  		result = podmanTest.Podman([]string{"pod", "ps", "--filter", "until=5000000000"})
    98  		result.WaitWithDefaultTimeout()
    99  		Expect(result).Should(ExitCleanly())
   100  		Expect(result.OutputToString()).To(ContainSubstring(name))
   101  
   102  		result = podmanTest.Podman([]string{"pod", "ps", "--filter", "label=test=with,comma"})
   103  		result.WaitWithDefaultTimeout()
   104  		Expect(result).Should(ExitCleanly())
   105  		Expect(result.OutputToString()).To(ContainSubstring(name))
   106  	})
   107  
   108  	It("podman pod ps filter name regexp", func() {
   109  		_, ec, podid := podmanTest.CreatePod(map[string][]string{"--name": {"mypod"}})
   110  		Expect(ec).To(Equal(0))
   111  		_, ec2, _ := podmanTest.CreatePod(map[string][]string{"--name": {"mypod1"}})
   112  		Expect(ec2).To(Equal(0))
   113  
   114  		result := podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "name=mypod"})
   115  		result.WaitWithDefaultTimeout()
   116  		Expect(result).Should(ExitCleanly())
   117  
   118  		output := result.OutputToStringArray()
   119  		Expect(output).To(HaveLen(2))
   120  
   121  		result = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "name=mypod$"})
   122  		result.WaitWithDefaultTimeout()
   123  		Expect(result).Should(ExitCleanly())
   124  
   125  		output = result.OutputToStringArray()
   126  		Expect(output).To(HaveLen(1))
   127  		Expect(output[0]).To(Equal(podid))
   128  	})
   129  
   130  	It("podman pod ps mutually exclusive flags", func() {
   131  		session := podmanTest.Podman([]string{"pod", "ps", "-q", "--format", "{{.ID}}"})
   132  		session.WaitWithDefaultTimeout()
   133  		Expect(session).To(ExitWithError())
   134  
   135  	})
   136  
   137  	It("podman pod ps --sort by name", func() {
   138  		_, ec, _ := podmanTest.CreatePod(nil)
   139  		Expect(ec).To(Equal(0))
   140  
   141  		_, ec2, _ := podmanTest.CreatePod(nil)
   142  		Expect(ec2).To(Equal(0))
   143  
   144  		_, ec3, _ := podmanTest.CreatePod(nil)
   145  		Expect(ec3).To(Equal(0))
   146  
   147  		session := podmanTest.Podman([]string{"pod", "ps", "--sort=name", "--format", "{{.Name}}"})
   148  
   149  		session.WaitWithDefaultTimeout()
   150  		Expect(session).Should(ExitCleanly())
   151  
   152  		sortedArr := session.OutputToStringArray()
   153  
   154  		Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue(), "slice is sorted")
   155  	})
   156  
   157  	It("podman pod ps --ctr-names", func() {
   158  		SkipIfRootlessCgroupsV1("Not supported for rootless + CgroupsV1")
   159  		_, ec, podid := podmanTest.CreatePod(nil)
   160  		Expect(ec).To(Equal(0))
   161  
   162  		session := podmanTest.RunTopContainerInPod("test1", podid)
   163  		session.WaitWithDefaultTimeout()
   164  		Expect(session).Should(ExitCleanly())
   165  
   166  		_, ec, _ = podmanTest.RunLsContainerInPod("test2", podid)
   167  		Expect(ec).To(Equal(0))
   168  
   169  		session = podmanTest.Podman([]string{"pod", "ps", "--format={{.ContainerNames}}", "--ctr-names"})
   170  		session.WaitWithDefaultTimeout()
   171  		Expect(session).Should(ExitCleanly())
   172  		Expect(session.OutputToString()).To(ContainSubstring("test1"))
   173  		Expect(session.OutputToString()).To(ContainSubstring("test2"))
   174  	})
   175  
   176  	It("podman pod ps filter ctr attributes", func() {
   177  		_, ec, podid1 := podmanTest.CreatePod(nil)
   178  		Expect(ec).To(Equal(0))
   179  
   180  		session := podmanTest.RunTopContainerInPod("test1", podid1)
   181  		session.WaitWithDefaultTimeout()
   182  		Expect(session).Should(ExitCleanly())
   183  
   184  		_, ec2, podid2 := podmanTest.CreatePod(nil)
   185  		Expect(ec2).To(Equal(0))
   186  
   187  		_, ec3, cid := podmanTest.RunLsContainerInPod("test2", podid2)
   188  		Expect(ec3).To(Equal(0))
   189  
   190  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-names=test1"})
   191  		session.WaitWithDefaultTimeout()
   192  		Expect(session).Should(ExitCleanly())
   193  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   194  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid2)))
   195  
   196  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-names=test", "--filter", "ctr-status=running"})
   197  		session.WaitWithDefaultTimeout()
   198  		Expect(session).Should(ExitCleanly())
   199  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   200  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid2)))
   201  
   202  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", fmt.Sprintf("ctr-ids=%s", cid)})
   203  		session.WaitWithDefaultTimeout()
   204  		Expect(session).Should(ExitCleanly())
   205  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   206  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid1)))
   207  
   208  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-ids=" + cid[:40], "--filter", "ctr-ids=" + cid + "$"})
   209  		session.WaitWithDefaultTimeout()
   210  		Expect(session).Should(ExitCleanly())
   211  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   212  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid1)))
   213  
   214  		_, ec3, podid3 := podmanTest.CreatePod(nil)
   215  		Expect(ec3).To(Equal(0))
   216  
   217  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-number=1"})
   218  		session.WaitWithDefaultTimeout()
   219  		Expect(session).Should(ExitCleanly())
   220  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   221  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   222  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
   223  
   224  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-number=1", "--filter", "ctr-number=0"})
   225  		session.WaitWithDefaultTimeout()
   226  		Expect(session).Should(ExitCleanly())
   227  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   228  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   229  		Expect(session.OutputToString()).To(ContainSubstring(podid3))
   230  
   231  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-status=running"})
   232  		session.WaitWithDefaultTimeout()
   233  		Expect(session).Should(ExitCleanly())
   234  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   235  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid2)))
   236  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
   237  
   238  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-status=exited"})
   239  		session.WaitWithDefaultTimeout()
   240  		Expect(session).Should(ExitCleanly())
   241  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   242  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid1)))
   243  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
   244  
   245  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-status=exited", "--filter", "ctr-status=running"})
   246  		session.WaitWithDefaultTimeout()
   247  		Expect(session).Should(ExitCleanly())
   248  		Expect(session.OutputToString()).To(ContainSubstring(podid1))
   249  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   250  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
   251  
   252  		session = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "ctr-status=created"})
   253  		session.WaitWithDefaultTimeout()
   254  		Expect(session).Should(ExitCleanly())
   255  		Expect(session.OutputToString()).To(BeEmpty())
   256  	})
   257  
   258  	It("podman pod ps filter labels", func() {
   259  		s, _, podid1 := podmanTest.CreatePod(nil)
   260  		Expect(s).To(ExitCleanly())
   261  
   262  		s, _, podid2 := podmanTest.CreatePod(map[string][]string{
   263  			"--label": {"app=myapp", "io.podman.test.key=irrelevant-value"},
   264  		})
   265  		Expect(s).To(ExitCleanly())
   266  
   267  		s, _, podid3 := podmanTest.CreatePod(map[string][]string{"--label": {"app=test"}})
   268  		Expect(s).To(ExitCleanly())
   269  
   270  		session := podmanTest.Podman([]string{"pod", "ps", "--no-trunc", "--filter", "label=app", "--filter", "label=app=myapp"})
   271  		session.WaitWithDefaultTimeout()
   272  		Expect(session).To(ExitCleanly())
   273  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid1)))
   274  		Expect(session.OutputToString()).To(ContainSubstring(podid2))
   275  		Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
   276  	})
   277  
   278  	It("podman pod ps filter network", func() {
   279  		net := stringid.GenerateRandomID()
   280  		session := podmanTest.Podman([]string{"network", "create", net})
   281  		session.WaitWithDefaultTimeout()
   282  		Expect(session).Should(ExitCleanly())
   283  		defer podmanTest.removeNetwork(net)
   284  
   285  		session = podmanTest.Podman([]string{"pod", "create", "--network", net})
   286  		session.WaitWithDefaultTimeout()
   287  		Expect(session).Should(ExitCleanly())
   288  		podWithNet := session.OutputToString()
   289  
   290  		session = podmanTest.Podman([]string{"pod", "create"})
   291  		session.WaitWithDefaultTimeout()
   292  		Expect(session).Should(ExitCleanly())
   293  		podWithoutNet := session.OutputToString()
   294  
   295  		session = podmanTest.Podman([]string{"pod", "ps", "--no-trunc", "--filter", "network=" + net})
   296  		session.WaitWithDefaultTimeout()
   297  		Expect(session).Should(ExitCleanly())
   298  		Expect(session.OutputToString()).To(ContainSubstring(podWithNet))
   299  		Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
   300  	})
   301  
   302  	It("podman pod ps --format networks", func() {
   303  		session := podmanTest.Podman([]string{"pod", "create"})
   304  		session.WaitWithDefaultTimeout()
   305  		Expect(session).Should(ExitCleanly())
   306  
   307  		session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}"})
   308  		session.WaitWithDefaultTimeout()
   309  		Expect(session).Should(ExitCleanly())
   310  		if isRootless() {
   311  			// rootless container don't have a network by default
   312  			Expect(session.OutputToString()).To(Equal(""))
   313  		} else {
   314  			// default network name is podman
   315  			Expect(session.OutputToString()).To(Equal("podman"))
   316  		}
   317  
   318  		net1 := stringid.GenerateRandomID()
   319  		session = podmanTest.Podman([]string{"network", "create", net1})
   320  		session.WaitWithDefaultTimeout()
   321  		Expect(session).Should(ExitCleanly())
   322  		defer podmanTest.removeNetwork(net1)
   323  		net2 := stringid.GenerateRandomID()
   324  		session = podmanTest.Podman([]string{"network", "create", net2})
   325  		session.WaitWithDefaultTimeout()
   326  		Expect(session).Should(ExitCleanly())
   327  		defer podmanTest.removeNetwork(net2)
   328  
   329  		session = podmanTest.Podman([]string{"pod", "create", "--network", net1 + "," + net2})
   330  		session.WaitWithDefaultTimeout()
   331  		Expect(session).Should(ExitCleanly())
   332  		pid := session.OutputToString()
   333  
   334  		session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}", "--filter", "id=" + pid})
   335  		session.WaitWithDefaultTimeout()
   336  		Expect(session).Should(ExitCleanly())
   337  		// the output is not deterministic so check both possible orders
   338  		Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
   339  	})
   340  
   341  	It("pod no infra should ps", func() {
   342  		session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
   343  		session.WaitWithDefaultTimeout()
   344  		Expect(session).Should(ExitCleanly())
   345  
   346  		ps := podmanTest.Podman([]string{"pod", "ps"})
   347  		ps.WaitWithDefaultTimeout()
   348  		Expect(ps).Should(ExitCleanly())
   349  
   350  		infra := podmanTest.Podman([]string{"pod", "ps", "--format", "{{.InfraId}}"})
   351  		infra.WaitWithDefaultTimeout()
   352  		Expect(infra.OutputToString()).To(BeEmpty())
   353  	})
   354  
   355  	It("podman pod ps format with labels", func() {
   356  		_, ec, _ := podmanTest.CreatePod(nil)
   357  		Expect(ec).To(Equal(0))
   358  
   359  		_, ec1, _ := podmanTest.CreatePod(map[string][]string{"--label": {
   360  			"io.podman.test.label=value1",
   361  			"io.podman.test.key=irrelevant-value",
   362  		}})
   363  		Expect(ec1).To(Equal(0))
   364  
   365  		session := podmanTest.Podman([]string{"pod", "ps", "--format", "{{.Labels}}"})
   366  		session.WaitWithDefaultTimeout()
   367  		Expect(session).Should(ExitCleanly())
   368  		Expect(session.OutputToString()).To(ContainSubstring("value1"))
   369  	})
   370  
   371  	It("podman pod ps headers", func() {
   372  		session := podmanTest.Podman([]string{"pod", "ps", "--ctr-ids", "--ctr-names", "--ctr-status", "--ns"})
   373  		session.WaitWithDefaultTimeout()
   374  		Expect(session).Should(ExitCleanly())
   375  		Expect(session.OutputToString()).To(MatchRegexp(`^POD ID\s+NAME\s+STATUS\s+CREATED\s+INFRA ID\s+IDS\s+NAMES\s+STATUS\s+CGROUP\s+NAMESPACES$`))
   376  	})
   377  
   378  })