Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/ctr_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,7 @@ static gboolean shift_backup_files(void)
close(validation_fd);

/* Shift existing backups from highest to lowest: .N-1 -> .N, .N-2 -> .N-1, etc. */
int loop_start = (opt_log_max_files > 1) ? opt_log_max_files : 2;

for (int i = loop_start; i >= 2; i--) {
for (int i = opt_log_max_files; i >= 2; i--) {
_cleanup_free_ char *from = g_strdup_printf("%s.%d", k8s_log_path, i - 1);
_cleanup_free_ char *to = g_strdup_printf("%s.%d", k8s_log_path, i);

Expand Down
12 changes: 12 additions & 0 deletions test/02-ctr-logs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,15 @@ run_conmon_with_log_opts() {
run cat "$LOG_PATH"
assert "${output}" =~ "stdout P"
}

@test "ctr logs: should keep only log-max-files backup files" {
# The container's output rotates the 1024-byte log several times over, so
# with --log-max-files 1 a backup must exist, and it must be the only one
# left: rotation must atomically replace .1, not shift it to .2.
run_conmon_with_default_args \
--log-path "k8s-file:$LOG_PATH" \
--log-rotate --log-max-files 1 --log-size-max 1024

backups=("$LOG_PATH".*)
assert "${backups[*]}" == "$LOG_PATH.1" "only one backup file is left"
}
Loading