|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/docker/cli/internal/test" |
| 9 | + "github.com/docker/docker/api/types" |
| 10 | + "github.com/docker/docker/api/types/container" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "gotest.tools/v3/assert" |
| 13 | + is "gotest.tools/v3/assert/cmp" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRunCommit(t *testing.T) { |
| 17 | + cli := test.NewFakeCli(&fakeClient{ |
| 18 | + containerCommitFunc: func( |
| 19 | + ctx context.Context, |
| 20 | + container string, |
| 21 | + options container.CommitOptions, |
| 22 | + ) (types.IDResponse, error) { |
| 23 | + assert.Check(t, is.Equal(options.Author, "Author Name <author@name.com>")) |
| 24 | + assert.Check(t, is.DeepEqual(options.Changes, []string{"EXPOSE 80"})) |
| 25 | + assert.Check(t, is.Equal(options.Comment, "commit message")) |
| 26 | + assert.Check(t, is.Equal(options.Pause, false)) |
| 27 | + assert.Check(t, is.Equal(container, "container-id")) |
| 28 | + |
| 29 | + return types.IDResponse{ID: "image-id"}, nil |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + cmd := NewCommitCommand(cli) |
| 34 | + cmd.SetOut(io.Discard) |
| 35 | + cmd.SetArgs( |
| 36 | + []string{ |
| 37 | + "--author", "Author Name <author@name.com>", |
| 38 | + "--change", "EXPOSE 80", |
| 39 | + "--message", "commit message", |
| 40 | + "--pause=false", |
| 41 | + "container-id", |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + err := cmd.Execute() |
| 46 | + assert.NilError(t, err) |
| 47 | + |
| 48 | + assert.Assert(t, is.Equal(cli.OutBuffer().String(), "image-id\n")) |
| 49 | +} |
| 50 | + |
| 51 | +func TestRunCommitClientError(t *testing.T) { |
| 52 | + clientError := errors.New("client error") |
| 53 | + |
| 54 | + cli := test.NewFakeCli(&fakeClient{ |
| 55 | + containerCommitFunc: func( |
| 56 | + ctx context.Context, |
| 57 | + container string, |
| 58 | + options container.CommitOptions, |
| 59 | + ) (types.IDResponse, error) { |
| 60 | + return types.IDResponse{}, clientError |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + cmd := NewCommitCommand(cli) |
| 65 | + cmd.SetOut(io.Discard) |
| 66 | + cmd.SetErr(io.Discard) |
| 67 | + cmd.SetArgs([]string{"container-id"}) |
| 68 | + |
| 69 | + err := cmd.Execute() |
| 70 | + assert.ErrorIs(t, err, clientError) |
| 71 | +} |
0 commit comments