Skip to content

Commit

Permalink
podman logs: journald fix --since and --follow
Browse files Browse the repository at this point in the history
The `containerCouldBeLogging` bool should not be false by default, when
--since is used we seek in the journal and can miss the start event so
that bool would stay false forever. This means that a running container
is not followed even when it should.

To fix this we can just set the `containerCouldBeLogging` bool based on
the current contianer state.

Fixes containers#16950

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
  • Loading branch information
Luap99 committed Jan 4, 2023
1 parent 28d04bc commit fb235ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libpod/container_log_linux.go
Expand Up @@ -97,7 +97,16 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
// exponential backoff.
var cursor string
var cursorError error
var containerCouldBeLogging bool

c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
return err
}
// the initial could be logging state must be correct, we cannot rely on the start event being still in the journal
// https://github.com/containers/podman/issues/16950
containerCouldBeLogging := c.ensureState(define.ContainerStateRunning, define.ContainerStateStopping)

for i := 1; i <= 3; i++ {
cursor, cursorError = journal.GetCursor()
hundreds := 1
Expand Down
45 changes: 45 additions & 0 deletions test/system/035-logs.bats
Expand Up @@ -298,4 +298,49 @@ $contentC" "logs -f on exitted container works"

_log_test_follow journald
}

function _log_test_follow_since() {
local driver=$1
cname=$(random_string)
content=$(random_string)
local events_backend=$(_additional_events_backend $driver)


if [[ -n "${events_backend}" ]]; then
skip_if_remote "remote does not support --events-backend"
fi

run_podman ${events_backend} run --log-driver=$driver --name $cname $IMAGE sh -c "echo $content"
run_podman ${events_backend} logs --since 0s -f $cname
assert "$output" == "" "logs --since -f on exited container works"

run_podman ${events_backend} rm -t 0 -f $cname

# Now do the same with a running container to check #16950.
run_podman ${events_backend} run --log-driver=$driver --name $cname -d $IMAGE \
sh -c "sleep 0.5; while :; do echo $content && sleep 3; done"

# sleep is required to make sure the podman event backend no longer sees the start event in the log
# This value must be greater or equal than the the value given in --since below
sleep 0.2

# Make sure podman logs actually follows by giving a low timeout and check that the command times out
PODMAN_TIMEOUT=2 run_podman 124 ${events_backend} logs --since 0.1s -f $cname
assert "$output" == "$content
timeout: sending signal TERM to command ‘$PODMAN" "logs --since -f on running container works"

run_podman ${events_backend} rm -t 0 -f $cname
}

@test "podman logs - --since --follow k8s-file" {
_log_test_follow_since k8s-file
}

@test "podman logs - --since --follow journald" {
# We can't use journald on RHEL as rootless: rhbz#1895105
skip_if_journald_unavailable

_log_test_follow_since journald
}

# vim: filetype=sh

0 comments on commit fb235ec

Please sign in to comment.