From 7ae9c582240c1ece63b7783ca4e4c13adb6032f6 Mon Sep 17 00:00:00 2001 From: Sami Chergui <83331159+Yshuro@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:48:29 +0100 Subject: [PATCH 1/2] du: pass until filter as DiskUsage age limit Signed-off-by: Sami Chergui <83331159+Yshuro@users.noreply.github.com> --- commands/diskusage.go | 11 +++++- commands/diskusage_test.go | 78 ++++++++++++++++++++++++++++++++++++++ tests/diskusage.go | 8 ++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 commands/diskusage_test.go diff --git a/commands/diskusage.go b/commands/diskusage.go index 5bcc82231f7c..994ab4e15960 100644 --- a/commands/diskusage.go +++ b/commands/diskusage.go @@ -120,7 +120,8 @@ func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) er if err != nil { return err } - du, err := c.DiskUsage(ctx, client.WithFilter(pi.Filter)) + duOpts := toDiskUsageOptions(pi) + du, err := c.DiskUsage(ctx, duOpts...) if err != nil { return err } @@ -302,3 +303,11 @@ func printSummary(w io.Writer, dus [][]*client.UsageInfo) { fmt.Fprintf(tw, "Total:\t%s\n", units.HumanSize(float64(total))) tw.Flush() } + +func toDiskUsageOptions(pi *client.PruneInfo) []client.DiskUsageOption { + duOpts := []client.DiskUsageOption{client.WithFilter(pi.Filter)} + if pi.KeepDuration > 0 { + duOpts = append(duOpts, client.WithAgeLimit(pi.KeepDuration)) + } + return duOpts +} diff --git a/commands/diskusage_test.go b/commands/diskusage_test.go new file mode 100644 index 000000000000..6f34601055d7 --- /dev/null +++ b/commands/diskusage_test.go @@ -0,0 +1,78 @@ +package commands + +import ( + "testing" + "time" + + "github.com/moby/buildkit/client" + dclient "github.com/moby/moby/client" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestToDiskUsageOptions(t *testing.T) { + t.Run("without keep duration", func(t *testing.T) { + pi := &client.PruneInfo{ + Filter: []string{"type==regular"}, + } + opts := toDiskUsageOptions(pi) + info := &client.DiskUsageInfo{} + for _, o := range opts { + o.SetDiskUsageOption(info) + } + assert.Equal(t, []string{"type==regular"}, info.Filter) + assert.Equal(t, time.Duration(0), info.AgeLimit) + }) + + t.Run("with keep duration", func(t *testing.T) { + pi := &client.PruneInfo{ + KeepDuration: 168 * time.Hour, + Filter: []string{"type==source.local"}, + } + opts := toDiskUsageOptions(pi) + info := &client.DiskUsageInfo{} + for _, o := range opts { + o.SetDiskUsageOption(info) + } + assert.Equal(t, []string{"type==source.local"}, info.Filter) + assert.Equal(t, 168*time.Hour, info.AgeLimit) + }) +} + +func TestToBuildkitPruneInfoUntilFilter(t *testing.T) { + t.Run("until filter duration parsed", func(t *testing.T) { + f := dclient.Filters{} + f["until"] = map[string]bool{"168h": true} + pi, err := toBuildkitPruneInfo(f) + require.NoError(t, err) + assert.Equal(t, 168*time.Hour, pi.KeepDuration) + assert.Empty(t, pi.Filter[0]) + }) + + t.Run("unused-for filter duration parsed", func(t *testing.T) { + f := dclient.Filters{} + f["unused-for"] = map[string]bool{"24h": true} + pi, err := toBuildkitPruneInfo(f) + require.NoError(t, err) + assert.Equal(t, 24*time.Hour, pi.KeepDuration) + assert.Empty(t, pi.Filter[0]) + }) + + t.Run("conflicting until and unused-for", func(t *testing.T) { + f := dclient.Filters{} + f["until"] = map[string]bool{"24h": true} + f["unused-for"] = map[string]bool{"48h": true} + _, err := toBuildkitPruneInfo(f) + require.Error(t, err) + }) + + t.Run("combined until and type filter", func(t *testing.T) { + f := dclient.Filters{} + f["until"] = map[string]bool{"72h": true} + f["type"] = map[string]bool{"regular": true} + pi, err := toBuildkitPruneInfo(f) + require.NoError(t, err) + assert.Equal(t, 72*time.Hour, pi.KeepDuration) + assert.Equal(t, []string{"type==regular"}, pi.Filter) + }) +} diff --git a/tests/diskusage.go b/tests/diskusage.go index 8cfb4caa95ca..08703341e01f 100644 --- a/tests/diskusage.go +++ b/tests/diskusage.go @@ -9,6 +9,7 @@ import ( var diskusageTests = []func(t *testing.T, sb integration.Sandbox){ testDiskusage, + testDiskusageFilterUntil, testDiskusageVerbose, testDiskusageVerboseFormatError, testDiskusageFormatJSON, @@ -22,6 +23,13 @@ func testDiskusage(t *testing.T, sb integration.Sandbox) { require.NoError(t, err, string(out)) } +func testDiskusageFilterUntil(t *testing.T, sb integration.Sandbox) { + buildTestProject(t, sb) + cmd := buildxCmd(sb, withArgs("du", "--filter=until=24h")) + out, err := cmd.Output() + require.NoError(t, err, string(out)) +} + func testDiskusageVerbose(t *testing.T, sb integration.Sandbox) { buildTestProject(t, sb) cmd := buildxCmd(sb, withArgs("du", "--verbose")) From 4445e9ea7bc719cd7f29a9f9ab0ebcdb860f9a8e Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:29:40 +0100 Subject: [PATCH 2/2] du: simplify du age filtering and verify its behavior Pass the parsed age limit directly to DiskUsage instead of introducing a single-use option helper. Verify that fresh local-source cache is present before filtering and excluded by both until and unused-for. Combine each age filter with a type filter so the integration test catches dropped age limits and checks filter composition. Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- commands/diskusage.go | 11 +---------- commands/diskusage_test.go | 30 ------------------------------ tests/diskusage.go | 12 +++++++++++- 3 files changed, 12 insertions(+), 41 deletions(-) diff --git a/commands/diskusage.go b/commands/diskusage.go index 994ab4e15960..f4bb92ca7a12 100644 --- a/commands/diskusage.go +++ b/commands/diskusage.go @@ -120,8 +120,7 @@ func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) er if err != nil { return err } - duOpts := toDiskUsageOptions(pi) - du, err := c.DiskUsage(ctx, duOpts...) + du, err := c.DiskUsage(ctx, client.WithFilter(pi.Filter), client.WithAgeLimit(pi.KeepDuration)) if err != nil { return err } @@ -303,11 +302,3 @@ func printSummary(w io.Writer, dus [][]*client.UsageInfo) { fmt.Fprintf(tw, "Total:\t%s\n", units.HumanSize(float64(total))) tw.Flush() } - -func toDiskUsageOptions(pi *client.PruneInfo) []client.DiskUsageOption { - duOpts := []client.DiskUsageOption{client.WithFilter(pi.Filter)} - if pi.KeepDuration > 0 { - duOpts = append(duOpts, client.WithAgeLimit(pi.KeepDuration)) - } - return duOpts -} diff --git a/commands/diskusage_test.go b/commands/diskusage_test.go index 6f34601055d7..cdf1828ccc73 100644 --- a/commands/diskusage_test.go +++ b/commands/diskusage_test.go @@ -4,41 +4,11 @@ import ( "testing" "time" - "github.com/moby/buildkit/client" dclient "github.com/moby/moby/client" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestToDiskUsageOptions(t *testing.T) { - t.Run("without keep duration", func(t *testing.T) { - pi := &client.PruneInfo{ - Filter: []string{"type==regular"}, - } - opts := toDiskUsageOptions(pi) - info := &client.DiskUsageInfo{} - for _, o := range opts { - o.SetDiskUsageOption(info) - } - assert.Equal(t, []string{"type==regular"}, info.Filter) - assert.Equal(t, time.Duration(0), info.AgeLimit) - }) - - t.Run("with keep duration", func(t *testing.T) { - pi := &client.PruneInfo{ - KeepDuration: 168 * time.Hour, - Filter: []string{"type==source.local"}, - } - opts := toDiskUsageOptions(pi) - info := &client.DiskUsageInfo{} - for _, o := range opts { - o.SetDiskUsageOption(info) - } - assert.Equal(t, []string{"type==source.local"}, info.Filter) - assert.Equal(t, 168*time.Hour, info.AgeLimit) - }) -} - func TestToBuildkitPruneInfoUntilFilter(t *testing.T) { t.Run("until filter duration parsed", func(t *testing.T) { f := dclient.Filters{} diff --git a/tests/diskusage.go b/tests/diskusage.go index 08703341e01f..ccfad6d01906 100644 --- a/tests/diskusage.go +++ b/tests/diskusage.go @@ -24,10 +24,20 @@ func testDiskusage(t *testing.T, sb integration.Sandbox) { } func testDiskusageFilterUntil(t *testing.T, sb integration.Sandbox) { + skipNoCompatBuildKit(t, sb, ">= 0.22.0-0", "disk usage age filter") buildTestProject(t, sb) - cmd := buildxCmd(sb, withArgs("du", "--filter=until=24h")) + cmd := buildxCmd(sb, withArgs("du", "--format={{.ID}}", "--filter=type=source.local")) out, err := cmd.Output() require.NoError(t, err, string(out)) + require.NotEmpty(t, out) + for _, filter := range []string{"until=24h", "unused-for=24h"} { + t.Run(filter, func(t *testing.T) { + cmd := buildxCmd(sb, withArgs("du", "--format={{.ID}}", "--filter=type=source.local", "--filter="+filter)) + out, err := cmd.Output() + require.NoError(t, err, string(out)) + require.Empty(t, out) + }) + } } func testDiskusageVerbose(t *testing.T, sb integration.Sandbox) {