| 51 | } |
| 52 | |
| 53 | func TestNewPruneCommandSuccess(t *testing.T) { |
| 54 | testCases := []struct { |
| 55 | name string |
| 56 | args []string |
| 57 | imagePruneFunc func(client.ImagePruneOptions) (client.ImagePruneResult, error) |
| 58 | }{ |
| 59 | { |
| 60 | name: "all", |
| 61 | args: []string{"--all"}, |
| 62 | imagePruneFunc: func(opts client.ImagePruneOptions) (client.ImagePruneResult, error) { |
| 63 | assert.Check(t, opts.Filters["dangling"]["false"]) |
| 64 | return client.ImagePruneResult{}, nil |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | name: "force-deleted", |
| 69 | args: []string{"--force"}, |
| 70 | imagePruneFunc: func(opts client.ImagePruneOptions) (client.ImagePruneResult, error) { |
| 71 | assert.Check(t, opts.Filters["dangling"]["true"]) |
| 72 | return client.ImagePruneResult{ |
| 73 | Report: image.PruneReport{ |
| 74 | ImagesDeleted: []image.DeleteResponse{{Deleted: "image1"}}, |
| 75 | SpaceReclaimed: 1, |
| 76 | }, |
| 77 | }, nil |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | name: "label-filter", |
| 82 | args: []string{"--force", "--filter", "label=foobar"}, |
| 83 | imagePruneFunc: func(opts client.ImagePruneOptions) (client.ImagePruneResult, error) { |
| 84 | assert.Check(t, opts.Filters["label"]["foobar"]) |
| 85 | return client.ImagePruneResult{}, nil |
| 86 | }, |
| 87 | }, |
| 88 | { |
| 89 | name: "force-untagged", |
| 90 | args: []string{"--force"}, |
| 91 | imagePruneFunc: func(opts client.ImagePruneOptions) (client.ImagePruneResult, error) { |
| 92 | assert.Check(t, opts.Filters["dangling"]["true"]) |
| 93 | return client.ImagePruneResult{ |
| 94 | Report: image.PruneReport{ |
| 95 | ImagesDeleted: []image.DeleteResponse{{Untagged: "image1"}}, |
| 96 | SpaceReclaimed: 2, |
| 97 | }, |
| 98 | }, nil |
| 99 | }, |
| 100 | }, |
| 101 | } |
| 102 | for _, tc := range testCases { |
| 103 | t.Run(tc.name, func(t *testing.T) { |
| 104 | cli := test.NewFakeCli(&fakeClient{imagePruneFunc: tc.imagePruneFunc}) |
| 105 | // when prompted, answer "Y" to confirm the prune. |
| 106 | // will not be prompted if --force is used. |
| 107 | cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader("Y\n")))) |
| 108 | cmd := newPruneCommand(cli) |
| 109 | cmd.SetOut(io.Discard) |
| 110 | cmd.SetArgs(tc.args) |