@@ -13,22 +13,26 @@ import (
1313 "github.com/docker/docker/api/types/filters"
1414 "github.com/pkg/errors"
1515 "gotest.tools/v3/assert"
16+ is "gotest.tools/v3/assert/cmp"
1617 "gotest.tools/v3/golden"
1718 "gotest.tools/v3/skip"
1819)
1920
2021func TestVolumePruneErrors (t * testing.T ) {
2122 testCases := []struct {
23+ name string
2224 args []string
2325 flags map [string ]string
2426 volumePruneFunc func (args filters.Args ) (types.VolumesPruneReport , error )
2527 expectedError string
2628 }{
2729 {
30+ name : "accepts no arguments" ,
2831 args : []string {"foo" },
2932 expectedError : "accepts no argument" ,
3033 },
3134 {
35+ name : "forced but other error" ,
3236 flags : map [string ]string {
3337 "force" : "true" ,
3438 },
@@ -37,20 +41,75 @@ func TestVolumePruneErrors(t *testing.T) {
3741 },
3842 expectedError : "error pruning volumes" ,
3943 },
44+ {
45+ name : "conflicting options" ,
46+ flags : map [string ]string {
47+ "all" : "true" ,
48+ "filter" : "all=1" ,
49+ },
50+ expectedError : "conflicting options: cannot specify both --all and --filter all=1" ,
51+ },
52+ }
53+ for _ , tc := range testCases {
54+ tc := tc
55+ t .Run (tc .name , func (t * testing.T ) {
56+ cmd := NewPruneCommand (
57+ test .NewFakeCli (& fakeClient {
58+ volumePruneFunc : tc .volumePruneFunc ,
59+ }),
60+ )
61+ cmd .SetArgs (tc .args )
62+ for key , value := range tc .flags {
63+ cmd .Flags ().Set (key , value )
64+ }
65+ cmd .SetOut (io .Discard )
66+ cmd .SetErr (io .Discard )
67+ assert .ErrorContains (t , cmd .Execute (), tc .expectedError )
68+ })
69+ }
70+ }
71+
72+ func TestVolumePruneSuccess (t * testing.T ) {
73+ testCases := []struct {
74+ name string
75+ args []string
76+ volumePruneFunc func (args filters.Args ) (types.VolumesPruneReport , error )
77+ }{
78+ {
79+ name : "all" ,
80+ args : []string {"--all" },
81+ volumePruneFunc : func (pruneFilter filters.Args ) (types.VolumesPruneReport , error ) {
82+ assert .Check (t , is .Equal ([]string {"true" }, pruneFilter .Get ("all" )))
83+ return types.VolumesPruneReport {}, nil
84+ },
85+ },
86+ {
87+ name : "all-forced" ,
88+ args : []string {"--all" , "--force" },
89+ volumePruneFunc : func (pruneFilter filters.Args ) (types.VolumesPruneReport , error ) {
90+ return types.VolumesPruneReport {}, nil
91+ },
92+ },
93+ {
94+ name : "label-filter" ,
95+ args : []string {"--filter" , "label=foobar" },
96+ volumePruneFunc : func (pruneFilter filters.Args ) (types.VolumesPruneReport , error ) {
97+ assert .Check (t , is .Equal ([]string {"foobar" }, pruneFilter .Get ("label" )))
98+ return types.VolumesPruneReport {}, nil
99+ },
100+ },
40101 }
41102 for _ , tc := range testCases {
42- cmd := NewPruneCommand (
43- test .NewFakeCli (& fakeClient {
44- volumePruneFunc : tc .volumePruneFunc ,
45- }),
46- )
47- cmd .SetArgs (tc .args )
48- for key , value := range tc .flags {
49- cmd .Flags ().Set (key , value )
50- }
51- cmd .SetOut (io .Discard )
52- cmd .SetErr (io .Discard )
53- assert .ErrorContains (t , cmd .Execute (), tc .expectedError )
103+ tc := tc
104+ t .Run (tc .name , func (t * testing.T ) {
105+ cli := test .NewFakeCli (& fakeClient {volumePruneFunc : tc .volumePruneFunc })
106+ cmd := NewPruneCommand (cli )
107+ cmd .SetOut (io .Discard )
108+ cmd .SetArgs (tc .args )
109+ err := cmd .Execute ()
110+ assert .NilError (t , err )
111+ golden .Assert (t , cli .OutBuffer ().String (), fmt .Sprintf ("volume-prune-success.%s.golden" , tc .name ))
112+ })
54113 }
55114}
56115
0 commit comments