github.com/containers/podman/v5@v5.1.0-rc1/test/system/040-ps.bats (about)

     1  #!/usr/bin/env bats
     2  
     3  load helpers
     4  
     5  @test "podman ps - basic tests" {
     6      rand_name=$(random_string 30)
     7  
     8      run_podman ps --noheading
     9      is "$output" "" "baseline: empty results from ps --noheading"
    10  
    11      run_podman run -d --name $rand_name $IMAGE sleep 5
    12      cid=$output
    13      is "$cid" "[0-9a-f]\{64\}$"
    14  
    15      # Special case: formatted ps
    16      run_podman ps --no-trunc \
    17                 --format '{{.ID}} {{.Image}} {{.Command}} {{.Names}} {{.State}}'
    18      is "$output" "$cid $IMAGE sleep 5 $rand_name running" "podman ps"
    19  
    20  
    21      # Plain old regular ps
    22      run_podman ps
    23      is "${lines[1]}" \
    24         "${cid:0:12} \+$IMAGE \+sleep [0-9]\+ .*second.* $cname"\
    25         "output from podman ps"
    26  
    27      # OK. Wait for sleep to finish...
    28      run_podman wait $cid
    29  
    30      # ...then make sure container shows up as stopped
    31      run_podman ps -a
    32      is "${lines[1]}" \
    33         "${cid:0:12} \+$IMAGE *sleep .* Exited .* $rand_name" \
    34         "podman ps -a"
    35  
    36      run_podman rm $cid
    37  }
    38  
    39  @test "podman ps --filter" {
    40      local -A cid
    41  
    42      # Create three containers, each of whose CID begins with a different char
    43      run_podman run -d --name running $IMAGE top
    44      cid[running]=$output
    45  
    46      cid[stopped]=$output
    47      while [[ ${cid[stopped]:0:1} == ${cid[running]:0:1} ]]; do
    48          run_podman rm -f stopped
    49          run_podman run -d --name stopped $IMAGE true
    50          cid[stopped]=$output
    51          run_podman wait stopped
    52      done
    53  
    54      cid[failed]=${cid[stopped]}
    55      while [[ ${cid[failed]:0:1} == ${cid[running]:0:1} ]] || [[ ${cid[failed]:0:1} == ${cid[stopped]:0:1} ]]; do
    56          run_podman rm -f failed
    57          run_podman run -d --name failed $IMAGE false
    58          cid[failed]=$output
    59          run_podman wait failed
    60      done
    61  
    62      # This one is never tested in the id filter, so its cid can be anything
    63      run_podman create --name created $IMAGE echo hi
    64      cid[created]=$output
    65  
    66      # For debugging
    67      run_podman ps -a
    68  
    69      run_podman ps --filter name=running --format '{{.ID}}'
    70      is "$output" "${cid[running]:0:12}" "filter: name=running"
    71  
    72      # Stopped container should not appear (because we're not using -a)
    73      run_podman ps --filter name=stopped --format '{{.ID}}'
    74      is "$output" "" "filter: name=stopped (without -a)"
    75  
    76      # Again, but with -a
    77      run_podman ps -a --filter name=stopped --format '{{.ID}}'
    78      is "$output" "${cid[stopped]:0:12}" "filter: name=stopped (with -a)"
    79  
    80      run_podman ps --filter status=stopped --format '{{.Names}}' --sort names
    81      is "${lines[0]}" "failed"  "status=stopped: 1 of 2"
    82      is "${lines[1]}" "stopped" "status=stopped: 2 of 2"
    83  
    84      run_podman ps --filter status=exited --filter exited=0 --format '{{.Names}}'
    85      is "$output" "stopped" "exited=0"
    86  
    87      run_podman ps --filter status=exited --filter exited=1 --format '{{.Names}}'
    88      is "$output" "failed" "exited=1"
    89  
    90      run_podman ps --filter status=created --format '{{.Names}}'
    91      is "$output" "created" "state=created"
    92  
    93      # Multiple statuses allowed; and test sort=created
    94      run_podman ps -a --filter status=exited --filter status=running \
    95                 --format '{{.Names}}' --sort created
    96      is "${lines[0]}" "running" "status=stopped: 1 of 3"
    97      is "${lines[1]}" "stopped" "status=stopped: 2 of 3"
    98      is "${lines[2]}" "failed"  "status=stopped: 3 of 3"
    99  
   100      # ID filtering: if filter is only hex chars, it's a prefix; if it has
   101      # anything else, it's a regex
   102      run_podman rm created
   103      for state in running stopped failed; do
   104          local test_cid=${cid[$state]}
   105          for prefix in ${test_cid:0:1} ${test_cid:0:2} ${test_cid:0:13}; do
   106              # Test lower-case (default), upper-case, and with '^' anchor
   107              for uclc in ${prefix,,} ${prefix^^} "^$prefix"; do
   108                  run_podman ps -a --filter id=$uclc --format '{{.Names}}'
   109                  assert "$output" = "$state" "ps --filter id=$uclc"
   110              done
   111          done
   112  
   113          # Regex check
   114          local f="^[^${test_cid:0:1}]"
   115          run_podman ps -a --filter id="$f" --format '{{.Names}}'
   116          assert "${#lines[*]}" == "2" "filter id=$f: number of lines"
   117          assert "$output" !~ $state   "filter id=$f: '$state' not in results"
   118      done
   119  
   120      # All CIDs will have hex characters
   121      run_podman ps -a --filter id="[0-9a-f]" --format '{{.Names}}' --sort names
   122      assert "${lines[0]}" == "failed"  "filter id=[0-9a-f], line 1"
   123      assert "${lines[1]}" == "running" "filter id=[0-9a-f], line 2"
   124      assert "${lines[2]}" == "stopped" "filter id=[0-9a-f], line 3"
   125  
   126      run_podman ps -a --filter id="[^0-9a-f]" --noheading
   127      assert "$output" = "" "id=[^0-9a-f], should match no containers"
   128  
   129      # Finally, multiple filters
   130      run_podman ps -a --filter id=${cid[running]} --filter id=${cid[failed]} \
   131                 --format '{{.Names}}' --sort names
   132      assert "${lines[0]}" == "failed"  "filter id=running+failed, line 1"
   133      assert "${lines[1]}" == "running" "filter id=running+failed, line 2"
   134  
   135      run_podman stop -t 1 running
   136      run_podman rm -a
   137  }
   138  
   139  @test "podman ps --external" {
   140  
   141      # Setup: ensure that we have no hidden storage containers
   142      run_podman ps --external
   143      is "${#lines[@]}" "1" "setup check: no storage containers at start of test"
   144  
   145      # Force a buildah timeout; this leaves a buildah container behind
   146      local t0=$SECONDS
   147      PODMAN_TIMEOUT=5 run_podman 124 build -t thiswillneverexist - <<EOF
   148  FROM $IMAGE
   149  RUN touch /intermediate.image.to.be.pruned
   150  RUN sleep 30
   151  EOF
   152      local t1=$SECONDS
   153      local delta_t=$((t1 - t0))
   154      assert $delta_t -le 10 \
   155             "podman build did not get killed within 10 seconds"
   156  
   157      run_podman ps -a
   158      is "${#lines[@]}" "1" "podman ps -a does not see buildah containers"
   159  
   160      run_podman ps --external
   161      is "${#lines[@]}" "3" "podman ps -a --external sees buildah containers"
   162      is "${lines[1]}" \
   163         "[0-9a-f]\{12\} \+$IMAGE *buildah .* seconds ago .* Storage .* ${PODMAN_TEST_IMAGE_NAME}-working-container" \
   164         "podman ps --external"
   165  
   166      # 'rm -a' should be a NOP
   167      run_podman rm -a
   168      run_podman ps --external
   169      is "${#lines[@]}" "3" "podman ps -a --external sees buildah containers"
   170  
   171      # Cannot prune intermediate image as it's being used by a buildah
   172      # container.
   173      run_podman image prune -f
   174      is "$output" "" "No image is pruned"
   175  
   176      # --external for removing buildah containers.
   177      run_podman image prune -f --external
   178      is "${#lines[@]}" "1" "Image used by build container is pruned"
   179  
   180      # One buildah container has been removed.
   181      run_podman ps --external
   182      is "${#lines[@]}" "2" "podman ps -a --external sees buildah containers"
   183  
   184      cid="${lines[1]:0:12}"
   185  
   186      # We can't rm it without -f, but podman should issue a helpful message
   187      run_podman 2 rm "$cid"
   188      is "$output" "Error: container .* is mounted and cannot be removed without using force: container state improper" "podman rm <buildah container> without -f"
   189  
   190      # With -f, we can remove it.
   191      run_podman rm -t 0 -f "$cid"
   192  
   193      run_podman ps --external
   194      is "${#lines[@]}" "1" "storage container has been removed"
   195  }
   196  
   197  @test "podman ps --format label" {
   198      rand_value=$(random_string 10)
   199  
   200      run_podman run -d --label mylabel=$rand_value $IMAGE sleep inf
   201      cid=$output
   202      is "$cid" "[0-9a-f]\{64\}$"
   203  
   204      run_podman ps --format '{{ .Label "mylabel" }}'
   205      is "$output" "$rand_value"
   206  
   207      run_podman rm -t 0 -f $cid
   208  }
   209  
   210  @test "podman pod ps --format label" {
   211      rand_value=$(random_string 10)
   212  
   213      run_podman pod create --label mylabel=${rand_value} test
   214  
   215      run_podman pod ps --format '{{ .Label "mylabel" }}'
   216      is "$output" "$rand_value"
   217  
   218      run_podman pod rm -t 0 -f test
   219      run_podman rmi $(pause_image)
   220  }
   221  
   222  @test "podman ps --format PodName" {
   223      rand_value=$(random_string 10)
   224  
   225      run_podman run -d --pod new:${rand_value} --label mylabel=$rand_value $IMAGE sleep inf
   226      cid=$output
   227      is "$cid" "[0-9a-f]\{64\}$"
   228  
   229      run_podman ps --format '{{ .PodName }}'
   230      is "$output" ".*$rand_value"
   231  
   232      run_podman rm -t 0 -f $cid
   233      run_podman pod rm -t 0 -f $rand_value
   234      run_podman rmi $(pause_image)
   235  }
   236  
   237  # vim: filetype=sh