Skip to content

Commit

Permalink
Merge pull request #16607 from vrothberg/BZ-2144754
Browse files Browse the repository at this point in the history
container restart: clean up healthcheck state
  • Loading branch information
openshift-merge-robot authored Nov 28, 2022
2 parents 9e8f5db + e2fa94e commit d5f806e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,15 @@ func (c *Container) init(ctx context.Context, retainRetries bool) error {
c.state.RestartCount = 0
}

// bugzilla.redhat.com/show_bug.cgi?id=2144754:
// In case of a restart, make sure to remove the healthcheck log to
// have a clean state.
if path := c.healthCheckLogPath(); path != "" {
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
logrus.Error(err)
}
}

if err := c.save(); err != nil {
return err
}
Expand Down
30 changes: 30 additions & 0 deletions test/system/220-healthcheck.bats
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ Log[-1].Output | \"Uh-oh on stdout!\\\nUh-oh on stderr!\"
run_podman rmi healthcheck_i
}

@test "podman healthcheck - restart cleans up old state" {
ctr="healthcheck_c"
img="healthcheck_i"

_build_health_check_image $img cleanfile
run_podman run -d --name $ctr \
--health-cmd /healthcheck \
--health-retries=2 \
--health-interval=disable \
$img

run_podman container inspect $ctr --format "{{.State.Healthcheck.FailingStreak}}"
is "$output" "0" "Failing streak of fresh container should be 0"

# Get the healthcheck to fail
run_podman exec $ctr touch /uh-oh
run_podman 1 healthcheck run $ctr
is "$output" "unhealthy" "output from 'podman healthcheck run'"
run_podman container inspect $ctr --format "{{.State.Healthcheck.FailingStreak}}"
is "$output" "1" "Failing streak after one failed healthcheck should be 1"

run_podman container restart $ctr
run_podman container inspect $ctr --format "{{.State.Healthcheck.FailingStreak}}"
is "$output" "0" "Failing streak of restarted container should be 0 again"

run_podman rm -f -t0 $ctr
}

@test "podman healthcheck --health-on-failure" {
run_podman 125 create --health-on-failure=kill $IMAGE
is "$output" "Error: cannot set on-failure action to kill without a health check"
Expand Down Expand Up @@ -114,6 +142,8 @@ Log[-1].Output | \"Uh-oh on stdout!\\\nUh-oh on stderr!\"
if [[ $policy == "restart" ]];then
# Container has been restarted and health check works again
is "$output" "running $policy" "container has been restarted"
run_podman container inspect $ctr --format "{{.State.Healthcheck.FailingStreak}}"
is "$output" "0" "Failing streak of restarted container should be 0 again"
run_podman healthcheck run $ctr
elif [[ $policy == "none" ]];then
# Container is still running and health check still broken
Expand Down

0 comments on commit d5f806e

Please sign in to comment.