2017-06-30 17:09:37 -04:00
|
|
|
package checkpoint
|
|
|
|
|
|
|
|
import (
|
2022-02-25 07:03:17 -05:00
|
|
|
"io"
|
2017-06-30 17:09:37 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-08-28 06:06:35 -04:00
|
|
|
"github.com/docker/docker/api/types/checkpoint"
|
2017-06-30 17:09:37 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-30 17:09:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckpointCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
2023-08-28 06:06:35 -04:00
|
|
|
checkpointCreateFunc func(container string, options checkpoint.CreateOptions) error
|
2017-06-30 17:09:37 -04:00
|
|
|
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"},
|
2023-08-28 06:06:35 -04:00
|
|
|
checkpointCreateFunc: func(container string, options checkpoint.CreateOptions) error {
|
2017-06-30 17:09:37 -04:00
|
|
|
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)
|
2022-02-25 07:03:17 -05:00
|
|
|
cmd.SetOut(io.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{
|
2023-08-28 06:06:35 -04:00
|
|
|
checkpointCreateFunc: func(container string, options checkpoint.CreateOptions) error {
|
2017-06-30 17:09:37 -04:00
|
|
|
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)
|
2023-08-28 06:06:35 -04:00
|
|
|
cp := "checkpoint-bar"
|
|
|
|
cmd.SetArgs([]string{"container-foo", cp})
|
2017-06-30 17:09:37 -04:00
|
|
|
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))
|
2023-08-28 06:06:35 -04:00
|
|
|
assert.Check(t, is.Equal(cp, checkpointID))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("/dir/foo", checkpointDir))
|
|
|
|
assert.Check(t, is.Equal(false, exit))
|
2023-08-28 06:06:35 -04:00
|
|
|
assert.Check(t, is.Equal(cp, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-06-30 17:09:37 -04:00
|
|
|
}
|