Skip to content

Commit 46b360b

Browse files
command: add tests for container diff and rename
This commit adds tests for the commands docker diff and docker rename. Also, it creates the mock methods of the docker client ContainerDiff and ContainerRename so they can be used in the tests. For docker diff, it covers the cases that: - the command runs successfully - the client returns an error - the container id is empty For docker rename, it covers the cases that: - the command runs successfully - the container old name is empty - the container new name is empty - the client returns an error Co-authored-by: Laura Brehm <laurabrehm@hey.com> Signed-off-by: Stavros Panakakis <stavrospanakakis@gmail.com>
1 parent baa7a9f commit 46b360b

3 files changed

Lines changed: 188 additions & 0 deletions

File tree

cli/command/container/client_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type fakeClient struct {
3838
containerKillFunc func(ctx context.Context, containerID, signal string) error
3939
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
4040
containerAttachFunc func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
41+
containerDiffFunc func(ctx context.Context, containerID string) ([]container.FilesystemChange, error)
42+
containerRenameFunc func(ctx context.Context, oldName, newName string) error
4143
Version string
4244
}
4345

@@ -181,3 +183,19 @@ func (f *fakeClient) ContainerAttach(ctx context.Context, containerID string, op
181183
}
182184
return types.HijackedResponse{}, nil
183185
}
186+
187+
func (f *fakeClient) ContainerDiff(ctx context.Context, containerID string) ([]container.FilesystemChange, error) {
188+
if f.containerDiffFunc != nil {
189+
return f.containerDiffFunc(ctx, containerID)
190+
}
191+
192+
return []container.FilesystemChange{}, nil
193+
}
194+
195+
func (f *fakeClient) ContainerRename(ctx context.Context, oldName, newName string) error {
196+
if f.containerRenameFunc != nil {
197+
return f.containerRenameFunc(ctx, oldName, newName)
198+
}
199+
200+
return nil
201+
}

cli/command/container/diff_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"io"
6+
"strings"
7+
"testing"
8+
9+
"github.com/docker/cli/internal/test"
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 TestRunDiff(t *testing.T) {
17+
cli := test.NewFakeCli(&fakeClient{
18+
containerDiffFunc: func(
19+
ctx context.Context,
20+
containerID string,
21+
) ([]container.FilesystemChange, error) {
22+
return []container.FilesystemChange{
23+
{
24+
Kind: container.ChangeModify,
25+
Path: "/path/to/file0",
26+
},
27+
{
28+
Kind: container.ChangeAdd,
29+
Path: "/path/to/file1",
30+
},
31+
{
32+
Kind: container.ChangeDelete,
33+
Path: "/path/to/file2",
34+
},
35+
}, nil
36+
},
37+
})
38+
39+
cmd := NewDiffCommand(cli)
40+
cmd.SetOut(io.Discard)
41+
42+
cmd.SetArgs([]string{"container-id"})
43+
44+
err := cmd.Execute()
45+
assert.NilError(t, err)
46+
47+
diff := strings.SplitN(cli.OutBuffer().String(), "\n", 3)
48+
assert.Assert(t, is.Len(diff, 3))
49+
50+
file0 := strings.TrimSpace(diff[0])
51+
file1 := strings.TrimSpace(diff[1])
52+
file2 := strings.TrimSpace(diff[2])
53+
54+
assert.Check(t, is.Equal(file0, "C /path/to/file0"))
55+
assert.Check(t, is.Equal(file1, "A /path/to/file1"))
56+
assert.Check(t, is.Equal(file2, "D /path/to/file2"))
57+
}
58+
59+
func TestRunDiffClientError(t *testing.T) {
60+
clientError := errors.New("client error")
61+
62+
cli := test.NewFakeCli(&fakeClient{
63+
containerDiffFunc: func(
64+
ctx context.Context,
65+
containerID string,
66+
) ([]container.FilesystemChange, error) {
67+
return nil, clientError
68+
},
69+
})
70+
71+
cmd := NewDiffCommand(cli)
72+
cmd.SetOut(io.Discard)
73+
cmd.SetErr(io.Discard)
74+
75+
cmd.SetArgs([]string{"container-id"})
76+
77+
err := cmd.Execute()
78+
assert.ErrorIs(t, err, clientError)
79+
}
80+
81+
func TestRunDiffEmptyContainerError(t *testing.T) {
82+
cli := test.NewFakeCli(&fakeClient{})
83+
84+
cmd := NewDiffCommand(cli)
85+
cmd.SetOut(io.Discard)
86+
cmd.SetErr(io.Discard)
87+
88+
containerID := ""
89+
cmd.SetArgs([]string{containerID})
90+
91+
err := cmd.Execute()
92+
assert.Error(t, err, "Container name cannot be empty")
93+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"io"
6+
"testing"
7+
8+
"github.com/docker/cli/internal/test"
9+
"github.com/pkg/errors"
10+
"gotest.tools/v3/assert"
11+
is "gotest.tools/v3/assert/cmp"
12+
)
13+
14+
func TestRunRename(t *testing.T) {
15+
testcases := []struct {
16+
doc, oldName, newName, expectedErr string
17+
}{
18+
{
19+
doc: "success",
20+
oldName: "oldName",
21+
newName: "newName",
22+
expectedErr: "",
23+
},
24+
{
25+
doc: "empty old name",
26+
oldName: "",
27+
newName: "newName",
28+
expectedErr: "Error: Neither old nor new names may be empty",
29+
},
30+
{
31+
doc: "empty new name",
32+
oldName: "oldName",
33+
newName: "",
34+
expectedErr: "Error: Neither old nor new names may be empty",
35+
},
36+
}
37+
38+
for _, tc := range testcases {
39+
t.Run(tc.doc, func(t *testing.T) {
40+
cli := test.NewFakeCli(&fakeClient{
41+
containerRenameFunc: func(ctx context.Context, oldName, newName string) error {
42+
return nil
43+
},
44+
})
45+
46+
cmd := NewRenameCommand(cli)
47+
cmd.SetOut(io.Discard)
48+
cmd.SetErr(io.Discard)
49+
cmd.SetArgs([]string{tc.oldName, tc.newName})
50+
51+
err := cmd.Execute()
52+
53+
if tc.expectedErr != "" {
54+
assert.ErrorContains(t, err, tc.expectedErr)
55+
} else {
56+
assert.NilError(t, err)
57+
}
58+
})
59+
}
60+
}
61+
62+
func TestRunRenameClientError(t *testing.T) {
63+
cli := test.NewFakeCli(&fakeClient{
64+
containerRenameFunc: func(ctx context.Context, oldName, newName string) error {
65+
return errors.New("client error")
66+
},
67+
})
68+
69+
cmd := NewRenameCommand(cli)
70+
cmd.SetOut(io.Discard)
71+
cmd.SetErr(io.Discard)
72+
cmd.SetArgs([]string{"oldName", "newName"})
73+
74+
err := cmd.Execute()
75+
76+
assert.Check(t, is.Error(err, "Error: failed to rename container named oldName"))
77+
}

0 commit comments

Comments
 (0)