github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/volume/prune_test.go (about) 1 package volume 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "runtime" 7 "strings" 8 "testing" 9 10 "github.com/docker/cli/cli/streams" 11 "github.com/docker/cli/internal/test" 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/filters" 14 "github.com/pkg/errors" 15 "gotest.tools/v3/assert" 16 "gotest.tools/v3/golden" 17 "gotest.tools/v3/skip" 18 ) 19 20 func TestVolumePruneErrors(t *testing.T) { 21 testCases := []struct { 22 args []string 23 flags map[string]string 24 volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error) 25 expectedError string 26 }{ 27 { 28 args: []string{"foo"}, 29 expectedError: "accepts no argument", 30 }, 31 { 32 flags: map[string]string{ 33 "force": "true", 34 }, 35 volumePruneFunc: func(args filters.Args) (types.VolumesPruneReport, error) { 36 return types.VolumesPruneReport{}, errors.Errorf("error pruning volumes") 37 }, 38 expectedError: "error pruning volumes", 39 }, 40 } 41 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(ioutil.Discard) 52 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 53 } 54 } 55 56 func TestVolumePruneForce(t *testing.T) { 57 testCases := []struct { 58 name string 59 volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error) 60 }{ 61 { 62 name: "empty", 63 }, 64 { 65 name: "deletedVolumes", 66 volumePruneFunc: simplePruneFunc, 67 }, 68 } 69 for _, tc := range testCases { 70 cli := test.NewFakeCli(&fakeClient{ 71 volumePruneFunc: tc.volumePruneFunc, 72 }) 73 cmd := NewPruneCommand(cli) 74 cmd.Flags().Set("force", "true") 75 assert.NilError(t, cmd.Execute()) 76 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-prune.%s.golden", tc.name)) 77 } 78 } 79 80 func TestVolumePrunePromptYes(t *testing.T) { 81 // FIXME(vdemeester) make it work.. 82 skip.If(t, runtime.GOOS == "windows", "TODO: fix test on windows") 83 84 for _, input := range []string{"y", "Y"} { 85 cli := test.NewFakeCli(&fakeClient{ 86 volumePruneFunc: simplePruneFunc, 87 }) 88 89 cli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input)))) 90 cmd := NewPruneCommand(cli) 91 assert.NilError(t, cmd.Execute()) 92 golden.Assert(t, cli.OutBuffer().String(), "volume-prune-yes.golden") 93 } 94 } 95 96 func TestVolumePrunePromptNo(t *testing.T) { 97 // FIXME(vdemeester) make it work.. 98 skip.If(t, runtime.GOOS == "windows", "TODO: fix test on windows") 99 100 for _, input := range []string{"n", "N", "no", "anything", "really"} { 101 cli := test.NewFakeCli(&fakeClient{ 102 volumePruneFunc: simplePruneFunc, 103 }) 104 105 cli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input)))) 106 cmd := NewPruneCommand(cli) 107 assert.NilError(t, cmd.Execute()) 108 golden.Assert(t, cli.OutBuffer().String(), "volume-prune-no.golden") 109 } 110 } 111 112 func simplePruneFunc(args filters.Args) (types.VolumesPruneReport, error) { 113 return types.VolumesPruneReport{ 114 VolumesDeleted: []string{ 115 "foo", "bar", "baz", 116 }, 117 SpaceReclaimed: 2000, 118 }, nil 119 }