Skip to content

Commit 7908982

Browse files
authored
Merge pull request #5467 from Stavrospanakakis/cli-container-tests
command: add tests for container diff and rename
2 parents ff3f94a + 46b360b commit 7908982

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
@@ -40,6 +40,8 @@ type fakeClient struct {
4040
containerKillFunc func(ctx context.Context, containerID, signal string) error
4141
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
4242
containerAttachFunc func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error)
43+
containerDiffFunc func(ctx context.Context, containerID string) ([]container.FilesystemChange, error)
44+
containerRenameFunc func(ctx context.Context, oldName, newName string) error
4345
Version string
4446
}
4547

@@ -197,3 +199,19 @@ func (f *fakeClient) ContainerAttach(ctx context.Context, containerID string, op
197199
}
198200
return types.HijackedResponse{}, nil
199201
}
202+
203+
func (f *fakeClient) ContainerDiff(ctx context.Context, containerID string) ([]container.FilesystemChange, error) {
204+
if f.containerDiffFunc != nil {
205+
return f.containerDiffFunc(ctx, containerID)
206+
}
207+
208+
return []container.FilesystemChange{}, nil
209+
}
210+
211+
func (f *fakeClient) ContainerRename(ctx context.Context, oldName, newName string) error {
212+
if f.containerRenameFunc != nil {
213+
return f.containerRenameFunc(ctx, oldName, newName)
214+
}
215+
216+
return nil
217+
}

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)