github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/inspect_test.go (about) 1 package plugin 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/docker/api/types" 10 11 "gotest.tools/v3/assert" 12 "gotest.tools/v3/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.SetOut(io.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 }