2017-06-30 17:09:37 -04:00
|
|
|
package checkpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-30 17:09:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-06-30 17:09:37 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckpointCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
checkpointCreateFunc func(container string, options types.CheckpointCreateOptions) error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"too-few-arguments"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires exactly 2 arguments",
|
2017-06-30 17:09:37 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"too", "many", "arguments"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires exactly 2 arguments",
|
2017-06-30 17:09:37 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo", "bar"},
|
|
|
|
checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error {
|
|
|
|
return errors.Errorf("error creating checkpoint for container foo")
|
|
|
|
},
|
|
|
|
expectedError: "error creating checkpoint for container foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-30 17:09:37 -04:00
|
|
|
checkpointCreateFunc: tc.checkpointCreateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-06-30 17:09:37 -04:00
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-06-30 17:09:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckpointCreateWithOptions(t *testing.T) {
|
|
|
|
var containerID, checkpointID, checkpointDir string
|
|
|
|
var exit bool
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-30 17:09:37 -04:00
|
|
|
checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error {
|
|
|
|
containerID = container
|
|
|
|
checkpointID = options.CheckpointID
|
|
|
|
checkpointDir = options.CheckpointDir
|
|
|
|
exit = options.Exit
|
|
|
|
return nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-06-30 17:09:37 -04:00
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
checkpoint := "checkpoint-bar"
|
|
|
|
cmd.SetArgs([]string{"container-foo", checkpoint})
|
|
|
|
cmd.Flags().Set("leave-running", "true")
|
|
|
|
cmd.Flags().Set("checkpoint-dir", "/dir/foo")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("container-foo", containerID))
|
|
|
|
assert.Check(t, is.Equal(checkpoint, checkpointID))
|
|
|
|
assert.Check(t, is.Equal("/dir/foo", checkpointDir))
|
|
|
|
assert.Check(t, is.Equal(false, exit))
|
|
|
|
assert.Check(t, is.Equal(checkpoint, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-06-30 17:09:37 -04:00
|
|
|
}
|