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

     1  #!/usr/bin/env bats   -*- bats -*-
     2  #
     3  # tests for podman rm
     4  #
     5  
     6  load helpers
     7  
     8  @test "podman rm" {
     9      rand=$(random_string 30)
    10      run_podman run --name $rand $IMAGE /bin/true
    11  
    12      # Don't care about output, just check exit status (it should exist)
    13      run_podman 0 inspect $rand
    14  
    15      # container should be in output of 'ps -a'
    16      run_podman ps -a
    17      is "$output" ".* $IMAGE .*/true .* $rand" "Container present in 'ps -a'"
    18  
    19      # Remove container; now 'inspect' should fail
    20      run_podman rm $rand
    21      is "$output" "$rand" "display raw input"
    22      run_podman 125 inspect $rand
    23      run_podman 125 wait $rand
    24      run_podman wait --ignore $rand
    25      is "$output" "-1" "wait --ignore will mark missing containers with -1"
    26  
    27      if !is_remote; then
    28          # remote does not support the --latest flag
    29          run_podman wait --ignore --latest
    30          is "$output" "-1" "wait --ignore will mark missing containers with -1"
    31      fi
    32  }
    33  
    34  @test "podman rm - running container, w/o and w/ force" {
    35      run_podman run -d $IMAGE sleep 5
    36      cid="$output"
    37  
    38      # rm should fail
    39      run_podman 2 rm $cid
    40      is "$output" "Error: cannot remove container $cid as it is running - running or paused containers cannot be removed without force: container state improper" "error message"
    41  
    42      # rm -f should succeed
    43      run_podman rm -t 0 -f $cid
    44  }
    45  
    46  @test "podman rm container from storage" {
    47      if is_remote; then
    48          skip "only applicable for local podman"
    49      fi
    50      rand=$(random_string 30)
    51      run_podman create --name $rand $IMAGE /bin/true
    52  
    53      # Create a container that podman does not know about
    54      external_cid=$(buildah from $IMAGE)
    55  
    56      # Plain 'exists' should fail, but should succeed with --external
    57      run_podman 1 container exists $external_cid
    58      run_podman container exists --external $external_cid
    59  
    60      # rm should succeed
    61      run_podman rm $rand $external_cid
    62  }
    63  
    64  @test "podman rm <-> run --rm race" {
    65      OCIDir=/run/$(podman_runtime)
    66  
    67      if is_rootless; then
    68          OCIDir=/run/user/$(id -u)/$(podman_runtime)
    69      fi
    70  
    71      # A container's lock is released before attempting to stop it.  This opens
    72      # the window for race conditions that led to #9479.
    73      run_podman run --rm -d $IMAGE sleep infinity
    74      local cid="$output"
    75      run_podman rm -af -t0
    76  
    77      # Check the OCI runtime directory has removed.
    78      is "$(ls $OCIDir | grep $cid)" "" "The OCI runtime directory should have been removed"
    79  }
    80  
    81  @test "podman rm --depend" {
    82      run_podman create $IMAGE
    83      dependCid=$output
    84      run_podman create --net=container:$dependCid $IMAGE
    85      cid=$output
    86      run_podman 125 rm $dependCid
    87      is "$output" "Error: container $dependCid has dependent containers which must be removed before it:.*" "Fail to remove because of dependencies"
    88      run_podman rm --depend $dependCid
    89      is "$output" ".*$cid" "Container should have been removed"
    90      is "$output" ".*$dependCid" "Depend container should have been removed"
    91  }
    92  
    93  # I'm sorry! This test takes 13 seconds. There's not much I can do about it,
    94  # please know that I think it's justified: podman 1.5.0 had a strange bug
    95  # in with exit status was not preserved on some code paths with 'rm -f'
    96  # or 'podman run --rm' (see also 030-run.bats). The test below is a bit
    97  # kludgy: what we care about is the exit status of the killed container,
    98  # not 'podman rm', but BATS has no provision (that I know of) for forking,
    99  # so what we do is start the 'rm' beforehand and monitor the exit status
   100  # of the 'sleep' container.
   101  #
   102  # See https://github.com/containers/podman/issues/3795
   103  @test "podman rm -f" {
   104      rand=$(random_string 30)
   105      ( sleep 3; run_podman rm -t 0 -f $rand ) &
   106      run_podman 137 run --name $rand $IMAGE sleep 30
   107  }
   108  
   109  @test "podman container rm --force bogus" {
   110      run_podman 1 container rm bogus
   111      is "$output" "Error: no container with ID or name \"bogus\" found: no such container" "Should print error"
   112      run_podman container rm --force bogus
   113      is "$output" "" "Should print no output"
   114  
   115      run_podman create --name test $IMAGE
   116      run_podman container rm --force bogus test
   117      assert "$output" = "test" "should delete test"
   118  
   119      run_podman ps -a -q
   120      assert "$output" = "" "container should be removed"
   121  }
   122  
   123  function __run_healthcheck_container() {
   124      run_podman run -d --name $1 \
   125                 --health-cmd /bin/false \
   126                 --health-interval 1s \
   127                 --health-retries 2 \
   128                 --health-timeout 1s \
   129                 --health-on-failure=stop \
   130                 --stop-timeout=2 \
   131                 --health-start-period 0 \
   132                 --stop-signal SIGTERM \
   133                 $IMAGE sleep infinity
   134  }
   135  
   136  @test "podman container rm doesn't affect stopping containers" {
   137      local cname=c$(random_string 30)
   138      __run_healthcheck_container $cname
   139      local cid=$output
   140  
   141      # We'll use the PID later to confirm that container is not running
   142      run_podman inspect --format '{{.State.Pid}}' $cname
   143      local pid=$output
   144  
   145      # rm without -f should fail, because container is running/stopping.
   146      # We have no way to guarantee that we see 'stopping', but at a very
   147      # minimum we need to check at least one rm failure
   148      local rm_failures=0
   149      for i in {1..10}; do
   150          run_podman '?' rm $cname
   151          if [[ $status -eq 0 ]]; then
   152              break
   153          fi
   154  
   155          # rm failed. Confirm that it's for the right reason.
   156          assert "$output" =~ "Error: cannot remove container $cid as it is .* - running or paused containers cannot be removed without force: container state improper" \
   157                 "Expected error message from podman rm"
   158          rm_failures=$((rm_failures + 1))
   159          sleep 1
   160      done
   161  
   162      # At this point, container should be gone
   163      run_podman 1 container exists $cname
   164      run_podman 1 container exists $cid
   165  
   166      assert "$rm_failures" -gt 0 "we want at least one failure from podman-rm"
   167  
   168      if kill -0 $pid; then
   169          die "Container $cname process is still running (pid $pid)"
   170      fi
   171  }
   172  
   173  @test "podman container rm --force doesn't leave running processes" {
   174      local cname=c$(random_string 30)
   175      __run_healthcheck_container $cname
   176      local cid=$output
   177  
   178      # We'll use the PID later to confirm that container is not running
   179      run_podman inspect --format '{{.State.Pid}}' $cname
   180      local pid=$output
   181  
   182      for i in {1..10}; do
   183          run_podman inspect $cname --format '{{.State.Status}}'
   184          if [ "$output" = "stopping" ]; then
   185              break
   186          fi
   187  
   188          sleep 0.5
   189      done
   190  
   191      run_podman rm -f $cname
   192  
   193      if kill -0 $pid; then
   194          die "Container $cname process is still running (pid $pid)"
   195      fi
   196  }
   197  
   198  # vim: filetype=sh