diff --git a/machinery/src/capture/cleanup_test.go b/machinery/src/capture/cleanup_test.go index c7fb6bb..92e5274 100644 --- a/machinery/src/capture/cleanup_test.go +++ b/machinery/src/capture/cleanup_test.go @@ -196,3 +196,24 @@ func TestRecordingsNeedCleanup_DefaultDiskReserve(t *testing.T) { t.Fatalf("expected no cleanup when free space (%dMB) exceeds the 1MB reserve", availableMB) } } + +// The default 5% reserve must never truncate to 0MB on small disks, otherwise +// cleanup would only trigger once the disk is completely full. +func TestDefaultReserveMB(t *testing.T) { + cases := []struct { + totalMB int64 + want int64 + }{ + {totalMB: 0, want: 1}, // no/unknown disk size still reserves 1MB + {totalMB: 10, want: 1}, // 5% = 0MB -> floored to 1MB + {totalMB: 19, want: 1}, // 5% = 0MB -> floored to 1MB + {totalMB: 20, want: 1}, // 5% = exactly 1MB + {totalMB: 100, want: 5}, // 5% = 5MB + {totalMB: 1000, want: 50}, + } + for _, c := range cases { + if got := defaultReserveMB(c.totalMB); got != c.want { + t.Errorf("defaultReserveMB(%d) = %d, want %d", c.totalMB, got, c.want) + } + } +} diff --git a/machinery/src/capture/main.go b/machinery/src/capture/main.go index 6640e42..cccb1e5 100644 --- a/machinery/src/capture/main.go +++ b/machinery/src/capture/main.go @@ -118,12 +118,25 @@ func recordingsNeedCleanup(recordingsDirectory string, configuration *models.Con reserveMB := configuration.Config.MinFreeSpace if reserveMB <= 0 { - reserveMB = totalMB * 5 / 100 // keep 5% of the disk free by default + reserveMB = defaultReserveMB(totalMB) } return availableMB <= reserveMB, nil } +// defaultReserveMB returns the free-space reserve (MB) to keep on the recordings +// disk when AGENT_AUTO_CLEAN_MIN_FREE_SPACE is not set: 5% of the disk total, +// but never below 1MB. On very small disks 5% truncates to 0MB, which would +// disable the reserve entirely (cleanup only once availableMB <= 0), so we floor +// it at 1MB to preserve the intended "keep some space free" behaviour. +func defaultReserveMB(totalMB int64) int64 { + reserveMB := totalMB * 5 / 100 + if reserveMB < 1 { + reserveMB = 1 + } + return reserveMB +} + // pickRecordingToCleanup chooses which recording to delete to free space in the // recordings directory. It returns the oldest recording that has already been // uploaded (no pending marker with the same name in cloudDirectory). Only when