Skip to content

Commit 15233a7

Browse files
committed
tests: add a unit test for log rotation
This commit adds a test that simulates log rotation, making sure that the log file can be re-opened and used as expected. Signed-off-by: Julien Ropé <[email protected]>
1 parent fb183a2 commit 15233a7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/logs.bats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,32 @@ function teardown() {
5151
output=$(crictl inspect "$ctr_id" | jq -r ".status.state")
5252
[[ "$output" == "CONTAINER_RUNNING" ]]
5353
}
54+
55+
@test "Log file rotation should work" {
56+
start_crio
57+
58+
jq '.metadata.name = "logger"
59+
| .command = ["/bin/sh", "-c", "while true; do echo hello; sleep 1; done"]' \
60+
"$TESTDATA"/container_config.json > "$TESTDIR"/logger.json
61+
62+
ctr_id=$(crictl run "$TESTDIR"/logger.json "$TESTDATA"/sandbox_config.json)
63+
# Especially when using kata, it sometimes takes a few seconds to actually run container
64+
sleep 5
65+
66+
logpath=$(crictl inspect "$ctr_id" | jq -r ".status.logPath")
67+
[[ -f "$logpath" ]]
68+
69+
# Move log file away, then ask for re-open.
70+
# It will fail if the new log file is not created
71+
mv "$logpath" "$logpath".rotated
72+
crictl logs -r "$ctr_id"
73+
74+
[[ -f "$logpath" ]]
75+
76+
# Verify that the rotated log file is not written to anymore
77+
initial_size=$(stat -c %s "$logpath.rotated")
78+
[ "$initial_size" -gt 0 ]
79+
sleep 2 # our logger writes every second, leave enough time for at least one write
80+
new_size=$(stat -c %s "$logpath.rotated")
81+
[ "$new_size" -eq "$initial_size" ]
82+
}

0 commit comments

Comments
 (0)