2017-02-27 12:39:35 -05:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
2017-02-27 12:39:35 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-08-16 13:50:28 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVolumePruneErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flags: map[string]string{
|
|
|
|
"force": "true",
|
|
|
|
},
|
|
|
|
volumePruneFunc: func(args filters.Args) (types.VolumesPruneReport, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.VolumesPruneReport{}, errors.Errorf("error pruning volumes")
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error pruning volumes",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := NewPruneCommand(
|
2017-08-16 13:50:28 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-02-27 12:39:35 -05:00
|
|
|
volumePruneFunc: tc.volumePruneFunc,
|
2017-08-16 13:50:28 -04:00
|
|
|
}),
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumePruneForce(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "deletedVolumes",
|
|
|
|
volumePruneFunc: simplePruneFunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumePruneFunc: tc.volumePruneFunc,
|
|
|
|
})
|
|
|
|
cmd := NewPruneCommand(cli)
|
2017-02-27 12:39:35 -05:00
|
|
|
cmd.Flags().Set("force", "true")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-prune.%s.golden", tc.name))
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-16 13:50:28 -04:00
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
func TestVolumePrunePromptYes(t *testing.T) {
|
2017-08-16 13:50:28 -04:00
|
|
|
// FIXME(vdemeester) make it work..
|
|
|
|
skip.IfCondition(t, runtime.GOOS == "windows", "TODO: fix test on windows")
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
for _, input := range []string{"y", "Y"} {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-02-27 12:39:35 -05:00
|
|
|
volumePruneFunc: simplePruneFunc,
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
|
2017-08-16 13:50:28 -04:00
|
|
|
cmd := NewPruneCommand(cli)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-yes.golden")
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumePrunePromptNo(t *testing.T) {
|
2017-08-16 13:50:28 -04:00
|
|
|
// FIXME(vdemeester) make it work..
|
|
|
|
skip.IfCondition(t, runtime.GOOS == "windows", "TODO: fix test on windows")
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
for _, input := range []string{"n", "N", "no", "anything", "really"} {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-02-27 12:39:35 -05:00
|
|
|
volumePruneFunc: simplePruneFunc,
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
|
2017-08-16 13:50:28 -04:00
|
|
|
cmd := NewPruneCommand(cli)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-no.golden")
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func simplePruneFunc(args filters.Args) (types.VolumesPruneReport, error) {
|
|
|
|
return types.VolumesPruneReport{
|
|
|
|
VolumesDeleted: []string{
|
|
|
|
"foo", "bar", "baz",
|
|
|
|
},
|
|
|
|
SpaceReclaimed: 2000,
|
|
|
|
}, nil
|
|
|
|
}
|