Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: add tests for container kill, commit, and pause #5506

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/command/container/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type fakeClient struct {
containerAttachFunc func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
containerDiffFunc func(ctx context.Context, containerID string) ([]container.FilesystemChange, error)
containerRenameFunc func(ctx context.Context, oldName, newName string) error
containerCommitFunc func(ctx context.Context, container string, options container.CommitOptions) (types.IDResponse, error)
containerPauseFunc func(ctx context.Context, container string) error
Version string
}

Expand Down Expand Up @@ -215,3 +217,18 @@ func (f *fakeClient) ContainerRename(ctx context.Context, oldName, newName strin

return nil
}

func (f *fakeClient) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (types.IDResponse, error) {
if f.containerCommitFunc != nil {
return f.containerCommitFunc(ctx, containerID, options)
}
return types.IDResponse{}, nil
}

func (f *fakeClient) ContainerPause(ctx context.Context, containerID string) error {
if f.containerPauseFunc != nil {
return f.containerPauseFunc(ctx, containerID)
}

return nil
}
71 changes: 71 additions & 0 deletions cli/command/container/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package container

import (
"context"
"io"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestRunCommit(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
containerCommitFunc: func(
ctx context.Context,
container string,
options container.CommitOptions,
) (types.IDResponse, error) {
assert.Check(t, is.Equal(options.Author, "Author Name <author@name.com>"))
assert.Check(t, is.DeepEqual(options.Changes, []string{"EXPOSE 80"}))
assert.Check(t, is.Equal(options.Comment, "commit message"))
assert.Check(t, is.Equal(options.Pause, false))
assert.Check(t, is.Equal(container, "container-id"))

return types.IDResponse{ID: "image-id"}, nil
},
})

cmd := NewCommitCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetArgs(
[]string{
"--author", "Author Name <author@name.com>",
"--change", "EXPOSE 80",
"--message", "commit message",
"--pause=false",
"container-id",
},
)

err := cmd.Execute()
assert.NilError(t, err)

assert.Assert(t, is.Equal(cli.OutBuffer().String(), "image-id\n"))
}

func TestRunCommitClientError(t *testing.T) {
clientError := errors.New("client error")

cli := test.NewFakeCli(&fakeClient{
containerCommitFunc: func(
ctx context.Context,
container string,
options container.CommitOptions,
) (types.IDResponse, error) {
return types.IDResponse{}, clientError
},
})

cmd := NewCommitCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"container-id"})

err := cmd.Execute()
assert.ErrorIs(t, err, clientError)
}
74 changes: 74 additions & 0 deletions cli/command/container/kill_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package container

import (
"context"
"fmt"
"io"
"strings"
"testing"

"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestRunKill(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
containerKillFunc: func(
ctx context.Context,
container string,
signal string,
) error {
assert.Assert(t, is.Equal(signal, "STOP"))
return nil
},
})

cmd := NewKillCommand(cli)
cmd.SetOut(io.Discard)

cmd.SetArgs([]string{
"--signal", "STOP",
"container-id-1",
"container-id-2",
})
err := cmd.Execute()
assert.NilError(t, err)

containerIDs := strings.SplitN(cli.OutBuffer().String(), "\n", 2)
assert.Assert(t, is.Len(containerIDs, 2))

containerID1 := strings.TrimSpace(containerIDs[0])
containerID2 := strings.TrimSpace(containerIDs[1])

assert.Check(t, is.Equal(containerID1, "container-id-1"))
assert.Check(t, is.Equal(containerID2, "container-id-2"))
}

func TestRunKillClientError(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
containerKillFunc: func(
ctx context.Context,
container string,
signal string,
) error {
return fmt.Errorf("client error for container %s", container)
},
})

cmd := NewKillCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

cmd.SetArgs([]string{"container-id-1", "container-id-2"})
err := cmd.Execute()

errs := strings.SplitN(err.Error(), "\n", 2)
assert.Assert(t, is.Len(errs, 2))

errContainerID1 := errs[0]
errContainerID2 := errs[1]

assert.Assert(t, is.Equal(errContainerID1, "client error for container container-id-1"))
assert.Assert(t, is.Equal(errContainerID2, "client error for container container-id-2"))
}
65 changes: 65 additions & 0 deletions cli/command/container/pause_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package container

import (
"context"
"fmt"
"io"
"strings"
"testing"

"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestRunPause(t *testing.T) {
cli := test.NewFakeCli(
&fakeClient{
containerPauseFunc: func(ctx context.Context, container string) error {
return nil
},
},
)

cmd := NewPauseCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetArgs([]string{"container-id-1", "container-id-2"})

err := cmd.Execute()
assert.NilError(t, err)

containerIDs := strings.SplitN(cli.OutBuffer().String(), "\n", 2)
assert.Assert(t, is.Len(containerIDs, 2))

containerID1 := strings.TrimSpace(containerIDs[0])
containerID2 := strings.TrimSpace(containerIDs[1])

assert.Check(t, is.Equal(containerID1, "container-id-1"))
assert.Check(t, is.Equal(containerID2, "container-id-2"))
}

func TestRunPauseClientError(t *testing.T) {
cli := test.NewFakeCli(
&fakeClient{
containerPauseFunc: func(ctx context.Context, container string) error {
return fmt.Errorf("client error for container %s", container)
},
},
)

cmd := NewPauseCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"container-id-1", "container-id-2"})

err := cmd.Execute()

errs := strings.SplitN(err.Error(), "\n", 2)
assert.Assert(t, is.Len(errs, 2))

errContainerID1 := errs[0]
errContainerID2 := errs[1]

assert.Assert(t, is.Equal(errContainerID1, "client error for container container-id-1"))
assert.Assert(t, is.Equal(errContainerID2, "client error for container container-id-2"))
}
Loading