Skip to content

Commit 3ea56aa

Browse files
authored
Merge pull request #1191 from adshmh/add-unit-tests-to-plugin-inspect-command
add unit tests to plugin inspect command
2 parents 1546d71 + 14b696a commit 3ea56aa

5 files changed

Lines changed: 216 additions & 0 deletions

File tree

cli/command/plugin/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type fakeClient struct {
1717
pluginRemoveFunc func(name string, options types.PluginRemoveOptions) error
1818
pluginInstallFunc func(name string, options types.PluginInstallOptions) (io.ReadCloser, error)
1919
pluginListFunc func(filter filters.Args) (types.PluginsListResponse, error)
20+
pluginInspectFunc func(name string) (*types.Plugin, []byte, error)
2021
}
2122

2223
func (c *fakeClient) PluginCreate(ctx context.Context, createContext io.Reader, createOptions types.PluginCreateOptions) error {
@@ -61,3 +62,11 @@ func (c *fakeClient) PluginList(context context.Context, filter filters.Args) (t
6162

6263
return types.PluginsListResponse{}, nil
6364
}
65+
66+
func (c *fakeClient) PluginInspectWithRaw(ctx context.Context, name string) (*types.Plugin, []byte, error) {
67+
if c.pluginInspectFunc != nil {
68+
return c.pluginInspectFunc(name)
69+
}
70+
71+
return nil, nil, nil
72+
}

cli/command/plugin/inspect_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package plugin
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/docker/cli/internal/test"
9+
"github.com/docker/docker/api/types"
10+
11+
"gotest.tools/assert"
12+
"gotest.tools/golden"
13+
)
14+
15+
var pluginFoo = &types.Plugin{
16+
ID: "id-foo",
17+
Name: "name-foo",
18+
Config: types.PluginConfig{
19+
Description: "plugin foo description",
20+
DockerVersion: "17.12.1-ce",
21+
Documentation: "plugin foo documentation",
22+
Entrypoint: []string{"/foo"},
23+
Interface: types.PluginConfigInterface{
24+
Socket: "pluginfoo.sock",
25+
},
26+
Linux: types.PluginConfigLinux{
27+
Capabilities: []string{"CAP_SYS_ADMIN"},
28+
},
29+
WorkDir: "workdir-foo",
30+
Rootfs: &types.PluginConfigRootfs{
31+
DiffIds: []string{"sha256:8603eedd4ea52cebb2f22b45405a3dc8f78ba3e31bf18f27b4547a9ff930e0bd"},
32+
Type: "layers",
33+
},
34+
},
35+
}
36+
37+
func TestInspectErrors(t *testing.T) {
38+
testCases := []struct {
39+
description string
40+
args []string
41+
flags map[string]string
42+
expectedError string
43+
inspectFunc func(name string) (*types.Plugin, []byte, error)
44+
}{
45+
{
46+
description: "too few arguments",
47+
args: []string{},
48+
expectedError: "requires at least 1 argument",
49+
},
50+
{
51+
description: "error inspecting plugin",
52+
args: []string{"foo"},
53+
expectedError: "error inspecting plugin",
54+
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
55+
return nil, nil, fmt.Errorf("error inspecting plugin")
56+
},
57+
},
58+
{
59+
description: "invalid format",
60+
args: []string{"foo"},
61+
flags: map[string]string{
62+
"format": "{{invalid format}}",
63+
},
64+
expectedError: "Template parsing error",
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
t.Run(tc.description, func(t *testing.T) {
70+
cli := test.NewFakeCli(&fakeClient{pluginInspectFunc: tc.inspectFunc})
71+
cmd := newInspectCommand(cli)
72+
cmd.SetArgs(tc.args)
73+
for key, value := range tc.flags {
74+
cmd.Flags().Set(key, value)
75+
}
76+
cmd.SetOutput(ioutil.Discard)
77+
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
78+
})
79+
}
80+
}
81+
82+
func TestInspect(t *testing.T) {
83+
testCases := []struct {
84+
description string
85+
args []string
86+
flags map[string]string
87+
golden string
88+
inspectFunc func(name string) (*types.Plugin, []byte, error)
89+
}{
90+
{
91+
description: "inspect single plugin with format",
92+
args: []string{"foo"},
93+
flags: map[string]string{
94+
"format": "{{ .Name }}",
95+
},
96+
golden: "plugin-inspect-single-with-format.golden",
97+
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
98+
return &types.Plugin{
99+
ID: "id-foo",
100+
Name: "name-foo",
101+
}, []byte{}, nil
102+
},
103+
},
104+
{
105+
description: "inspect single plugin without format",
106+
args: []string{"foo"},
107+
golden: "plugin-inspect-single-without-format.golden",
108+
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
109+
return pluginFoo, nil, nil
110+
},
111+
},
112+
{
113+
description: "inspect multiple plugins with format",
114+
args: []string{"foo", "bar"},
115+
flags: map[string]string{
116+
"format": "{{ .Name }}",
117+
},
118+
golden: "plugin-inspect-multiple-with-format.golden",
119+
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
120+
switch name {
121+
case "foo":
122+
return &types.Plugin{
123+
ID: "id-foo",
124+
Name: "name-foo",
125+
}, []byte{}, nil
126+
case "bar":
127+
return &types.Plugin{
128+
ID: "id-bar",
129+
Name: "name-bar",
130+
}, []byte{}, nil
131+
default:
132+
return nil, nil, fmt.Errorf("unexpected plugin name: %s", name)
133+
}
134+
},
135+
},
136+
}
137+
138+
for _, tc := range testCases {
139+
t.Run(tc.description, func(t *testing.T) {
140+
cli := test.NewFakeCli(&fakeClient{pluginInspectFunc: tc.inspectFunc})
141+
cmd := newInspectCommand(cli)
142+
cmd.SetArgs(tc.args)
143+
for key, value := range tc.flags {
144+
cmd.Flags().Set(key, value)
145+
}
146+
assert.NilError(t, cmd.Execute())
147+
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
148+
})
149+
}
150+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name-foo
2+
name-bar
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name-foo
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[
2+
{
3+
"Config": {
4+
"Args": {
5+
"Description": "",
6+
"Name": "",
7+
"Settable": null,
8+
"Value": null
9+
},
10+
"Description": "plugin foo description",
11+
"DockerVersion": "17.12.1-ce",
12+
"Documentation": "plugin foo documentation",
13+
"Entrypoint": [
14+
"/foo"
15+
],
16+
"Env": null,
17+
"Interface": {
18+
"Socket": "pluginfoo.sock",
19+
"Types": null
20+
},
21+
"IpcHost": false,
22+
"Linux": {
23+
"AllowAllDevices": false,
24+
"Capabilities": [
25+
"CAP_SYS_ADMIN"
26+
],
27+
"Devices": null
28+
},
29+
"Mounts": null,
30+
"Network": {
31+
"Type": ""
32+
},
33+
"PidHost": false,
34+
"PropagatedMount": "",
35+
"User": {},
36+
"WorkDir": "workdir-foo",
37+
"rootfs": {
38+
"diff_ids": [
39+
"sha256:8603eedd4ea52cebb2f22b45405a3dc8f78ba3e31bf18f27b4547a9ff930e0bd"
40+
],
41+
"type": "layers"
42+
}
43+
},
44+
"Enabled": false,
45+
"Id": "id-foo",
46+
"Name": "name-foo",
47+
"Settings": {
48+
"Args": null,
49+
"Devices": null,
50+
"Env": null,
51+
"Mounts": null
52+
}
53+
}
54+
]

0 commit comments

Comments
 (0)