Skip to content

Commit ac502b5

Browse files
committed
cli/command/container: add unit tests for container stop
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 16aa994 commit ac502b5

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

cli/command/container/client_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type fakeClient struct {
3636
containerExecResizeFunc func(id string, options container.ResizeOptions) error
3737
containerRemoveFunc func(ctx context.Context, containerID string, options container.RemoveOptions) error
3838
containerRestartFunc func(ctx context.Context, containerID string, options container.StopOptions) error
39+
containerStopFunc func(ctx context.Context, containerID string, options container.StopOptions) error
3940
containerKillFunc func(ctx context.Context, containerID, signal string) error
4041
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
4142
containerAttachFunc func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
@@ -183,6 +184,13 @@ func (f *fakeClient) ContainerRestart(ctx context.Context, containerID string, o
183184
return nil
184185
}
185186

187+
func (f *fakeClient) ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error {
188+
if f.containerStopFunc != nil {
189+
return f.containerStopFunc(ctx, containerID, options)
190+
}
191+
return nil
192+
}
193+
186194
func (f *fakeClient) ContainerAttach(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) {
187195
if f.containerAttachFunc != nil {
188196
return f.containerAttachFunc(ctx, containerID, options)

cli/command/container/stop_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"io"
6+
"sort"
7+
"sync"
8+
"testing"
9+
10+
"github.com/docker/cli/internal/test"
11+
"github.com/docker/docker/api/types/container"
12+
"github.com/docker/docker/errdefs"
13+
"github.com/pkg/errors"
14+
"gotest.tools/v3/assert"
15+
is "gotest.tools/v3/assert/cmp"
16+
)
17+
18+
func TestStop(t *testing.T) {
19+
for _, tc := range []struct {
20+
name string
21+
args []string
22+
stopped []string
23+
expectedOpts container.StopOptions
24+
expectedErr string
25+
}{
26+
{
27+
name: "without options",
28+
args: []string{"container-1", "container-2"},
29+
stopped: []string{"container-1", "container-2"},
30+
},
31+
{
32+
name: "with unknown container",
33+
args: []string{"container-1", "nosuchcontainer", "container-2"},
34+
expectedErr: "no such container",
35+
stopped: []string{"container-1", "container-2"},
36+
},
37+
{
38+
name: "with -t",
39+
args: []string{"-t", "2", "container-1"},
40+
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)},
41+
stopped: []string{"container-1"},
42+
},
43+
{
44+
name: "with --time",
45+
args: []string{"--time", "2", "container-1"},
46+
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)},
47+
stopped: []string{"container-1"},
48+
},
49+
} {
50+
tc := tc
51+
t.Run(tc.name, func(t *testing.T) {
52+
var stopped []string
53+
mutex := new(sync.Mutex)
54+
55+
cli := test.NewFakeCli(&fakeClient{
56+
containerStopFunc: func(ctx context.Context, containerID string, options container.StopOptions) error {
57+
assert.Check(t, is.DeepEqual(options, tc.expectedOpts))
58+
if containerID == "nosuchcontainer" {
59+
return errdefs.NotFound(errors.New("Error: no such container: " + containerID))
60+
}
61+
62+
// containerStopFunc is called in parallel for each container
63+
// so append must be synchronized.
64+
mutex.Lock()
65+
stopped = append(stopped, containerID)
66+
mutex.Unlock()
67+
return nil
68+
},
69+
Version: "1.36",
70+
})
71+
cmd := NewStopCommand(cli)
72+
cmd.SetOut(io.Discard)
73+
cmd.SetErr(io.Discard)
74+
cmd.SetArgs(tc.args)
75+
76+
err := cmd.Execute()
77+
if tc.expectedErr != "" {
78+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
79+
} else {
80+
assert.Check(t, is.Nil(err))
81+
}
82+
sort.Strings(stopped)
83+
assert.Check(t, is.DeepEqual(stopped, tc.stopped))
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)