github.com/containers/podman/v5@v5.1.0-rc1/test/system/050-stop.bats (about) 1 #!/usr/bin/env bats 2 3 load helpers 4 5 # Very simple test 6 @test "podman stop - basic test" { 7 run_podman run -d $IMAGE sleep 60 8 cid="$output" 9 10 # Run 'stop'. Time how long it takes. If local, require a warning. 11 local plusw="+w" 12 if is_remote; then 13 plusw= 14 fi 15 t0=$SECONDS 16 run_podman 0$plusw stop $cid 17 t1=$SECONDS 18 if [[ -n "$plusw" ]]; then 19 require_warning "StopSignal SIGTERM failed to stop container .*, resorting to SIGKILL" 20 fi 21 22 # Confirm that container is stopped. Podman-remote unfortunately 23 # cannot tell the difference between "stopped" and "exited", and 24 # spits them out interchangeably, so we need to recognize either. 25 run_podman inspect --format '{{.State.Status}} {{.State.ExitCode}}' $cid 26 is "$output" "\\(stopped\|exited\\) \+137" \ 27 "Status and exit code of stopped container" 28 29 # The initial SIGTERM is ignored, so this operation should take 30 # exactly 10 seconds. Give it some leeway. 31 delta_t=$(( $t1 - $t0 )) 32 assert $delta_t -gt 8 "podman stop: ran too quickly!" 33 assert $delta_t -le 14 "podman stop: took too long" 34 35 run_podman rm $cid 36 } 37 38 # #9051 : podman stop --all was not working with podman-remote 39 @test "podman stop --all" { 40 # Start three containers, create (without running) a fourth 41 run_podman run -d --name c1 $IMAGE sleep 20 42 run_podman run -d --name c2 $IMAGE sleep 40 43 run_podman run -d --name c3 $IMAGE sleep 60 44 run_podman create --name c4 $IMAGE sleep 80 45 46 # podman ps (without -a) should show the three running containers 47 run_podman ps --sort names --format '{{.Names}}--{{.Status}}' 48 is "${#lines[*]}" "3" "podman ps shows exactly three containers" 49 is "${lines[0]}" "c1--Up.*" "podman ps shows running container (1)" 50 is "${lines[1]}" "c2--Up.*" "podman ps shows running container (2)" 51 is "${lines[2]}" "c3--Up.*" "podman ps shows running container (3)" 52 53 # Stop -a. Local podman issues a warning, check for it. 54 local plusw="+w" 55 if is_remote; then 56 plusw= 57 fi 58 run_podman 0$plusw stop -a -t 1 59 if [[ -n "$plusw" ]]; then 60 require_warning "StopSignal SIGTERM failed to stop container .*, resorting to SIGKILL" 61 fi 62 63 # Now podman ps (without -a) should show nothing. 64 run_podman ps --format '{{.Names}}' 65 is "$output" "" "podman ps, after stop -a, shows no running containers" 66 67 # ...but with -a, containers are shown 68 run_podman ps -a --sort names --format '{{.Names}}--{{.Status}}' 69 is "${#lines[*]}" "4" "podman ps -a shows exactly four containers" 70 is "${lines[0]}" "c1--Exited.*" "ps -a, first stopped container" 71 is "${lines[1]}" "c2--Exited.*" "ps -a, second stopped container" 72 is "${lines[2]}" "c3--Exited.*" "ps -a, third stopped container" 73 is "${lines[3]}" "c4--Created.*" "ps -a, created container (unaffected)" 74 } 75 76 @test "podman stop print IDs or raw input" { 77 # stop -a must print the IDs 78 run_podman run -d $IMAGE top 79 ctrID="$output" 80 run_podman stop -t0 --all 81 is "$output" "$ctrID" 82 83 # stop $input must print $input 84 cname=$(random_string) 85 run_podman run -d --name $cname $IMAGE top 86 run_podman stop -t0 $cname 87 is "$output" $cname 88 89 run_podman rm -t 0 -f $ctrID $cname 90 } 91 92 # #9051 : podman stop --ignore was not working with podman-remote 93 @test "podman stop --ignore" { 94 name=thiscontainerdoesnotexist 95 run_podman 125 stop $name 96 is "$output" \ 97 "Error: no container with name or ID \"$name\" found: no such container" \ 98 "podman stop nonexistent container" 99 100 run_podman stop --ignore $name 101 is "$output" "" "podman stop nonexistent container, with --ignore" 102 103 nosuchfile=$PODMAN_TMPDIR/no-such-file 104 run_podman 125 stop --cidfile=$nosuchfile 105 is "$output" "Error: reading CIDFile: open $nosuchfile: no such file or directory" "podman stop with missing cidfile, without --ignore" 106 107 run_podman stop --cidfile=$nosuchfile --ignore 108 is "$output" "" "podman stop with missing cidfile, with --ignore" 109 } 110 111 112 # Test fallback 113 114 115 # Regression test for #2472 116 @test "podman stop - can trap signal" { 117 # Because the --time and --timeout options can be wonky, try three 118 # different variations of this test. 119 for t_opt in '' '--time=5' '--timeout=5'; do 120 # Run a simple container that logs output on SIGTERM 121 run_podman run -d $IMAGE sh -c \ 122 "trap 'echo Received SIGTERM, finishing; exit' SIGTERM; echo READY; while :; do sleep 1; done" 123 cid="$output" 124 wait_for_ready $cid 125 126 # Run 'stop' against it... 127 t0=$SECONDS 128 run_podman stop $t_opt $cid 129 t1=$SECONDS 130 131 # ...the container should trap the signal, log it, and exit. 132 run_podman logs $cid 133 is "$output" ".*READY.*Received SIGTERM, finishing" "podman stop $t_opt" 134 135 # Exit code should be 0, because container did its own exit 136 run_podman inspect --format '{{.State.ExitCode}}' $cid 137 is "$output" "0" "Exit code of stopped container" 138 139 # The 'stop' command should return almost instantaneously 140 delta_t=$(( $t1 - $t0 )) 141 assert $delta_t -le 2 "podman stop: took too long" 142 143 run_podman rm $cid 144 done 145 } 146 147 # Regression test for #8501 148 @test "podman stop - unlock while waiting for timeout" { 149 # Test that the container state transitions to "stopping" and that other 150 # commands can get the container's lock. To do that, run a container that 151 # ignores SIGTERM such that the Podman would wait 20 seconds for the stop 152 # to finish. This gives us enough time to try some commands and inspect 153 # the container's status. 154 155 run_podman run --name stopme -d $IMAGE sh -c \ 156 "trap 'echo Received SIGTERM, ignoring' SIGTERM; echo READY; while :; do sleep 0.2; done" 157 158 wait_for_ready stopme 159 160 local t0=$SECONDS 161 # Stop the container, but do so in the background so we can inspect 162 # the container status while it's stopping. Use $PODMAN because we 163 # don't want the overhead and error checks of run_podman. 164 $PODMAN stop -t 20 stopme & 165 166 # Wait for container to acknowledge the signal. We can't use wait_for_output 167 # because that aborts if .State.Running != true 168 local timeout=5 169 while [[ $timeout -gt 0 ]]; do 170 run_podman logs stopme 171 if [[ "$output" =~ "Received SIGTERM, ignoring" ]]; then 172 break 173 fi 174 timeout=$((timeout - 1)) 175 assert $timeout -gt 0 "Timed out waiting for container to receive SIGTERM" 176 sleep 0.5 177 done 178 179 # Other commands can acquire the lock 180 run_podman ps -a 181 182 # The container state transitioned to "stopping" 183 run_podman inspect --format '{{.State.Status}}' stopme 184 is "$output" "stopping" "Status of container should be 'stopping'" 185 186 # Time check: make sure we were able to run 'ps' before the container 187 # exited. If this takes too long, it means ps had to wait for lock. 188 local delta_t=$(( $SECONDS - t0 )) 189 assert $delta_t -le 5 "Operations took too long" 190 191 run_podman kill stopme 192 run_podman wait stopme 193 194 # Exit code should be 137 as it was killed 195 run_podman inspect --format '{{.State.ExitCode}}' stopme 196 is "$output" "137" "Exit code of killed container" 197 } 198 199 @test "podman stop -t 1 Generate warning" { 200 skip_if_remote "warning only happens on server side" 201 run_podman run --rm --name stopme -d $IMAGE sleep 100 202 203 local plusw="+w" 204 if is_remote; then 205 plusw= 206 fi 207 run_podman 0$plusw stop -t 1 stopme 208 if [[ -n "$plusw" ]]; then 209 require_warning ".*StopSignal SIGTERM failed to stop container stopme in 1 seconds, resorting to SIGKILL" 210 fi 211 } 212 213 @test "podman stop --noout" { 214 run_podman run --rm --name stopme -d $IMAGE top 215 run_podman --noout stop -t 0 stopme 216 is "$output" "" "output should be empty" 217 } 218 219 @test "podman stop, with --rm container" { 220 OCIDir=/run/$(podman_runtime) 221 222 if is_rootless; then 223 OCIDir=/run/user/$(id -u)/$(podman_runtime) 224 fi 225 226 run_podman run --rm -d --name rmstop $IMAGE sleep infinity 227 local cid="$output" 228 run_podman stop -t0 rmstop 229 230 # Check the OCI runtime directory has removed. 231 is "$(ls $OCIDir | grep $cid)" "" "The OCI runtime directory should have been removed" 232 } 233 # vim: filetype=sh