diff --git a/src/ctr_logging.c b/src/ctr_logging.c index 33356afb..deff65ba 100644 --- a/src/ctr_logging.c +++ b/src/ctr_logging.c @@ -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); diff --git a/test/02-ctr-logs.bats b/test/02-ctr-logs.bats index 88f07052..4493c52c 100644 --- a/test/02-ctr-logs.bats +++ b/test/02-ctr-logs.bats @@ -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" +}