github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/config/inspect_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 "time" 8 9 "github.com/docker/cli/internal/test" 10 . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 "gotest.tools/v3/golden" 15 ) 16 17 func TestConfigInspectErrors(t *testing.T) { 18 testCases := []struct { 19 args []string 20 flags map[string]string 21 configInspectFunc func(configID string) (swarm.Config, []byte, error) 22 expectedError string 23 }{ 24 { 25 expectedError: "requires at least 1 argument", 26 }, 27 { 28 args: []string{"foo"}, 29 configInspectFunc: func(configID string) (swarm.Config, []byte, error) { 30 return swarm.Config{}, nil, errors.Errorf("error while inspecting the config") 31 }, 32 expectedError: "error while inspecting the config", 33 }, 34 { 35 args: []string{"foo"}, 36 flags: map[string]string{ 37 "format": "{{invalid format}}", 38 }, 39 expectedError: "Template parsing error", 40 }, 41 { 42 args: []string{"foo", "bar"}, 43 configInspectFunc: func(configID string) (swarm.Config, []byte, error) { 44 if configID == "foo" { 45 return *Config(ConfigName("foo")), nil, nil 46 } 47 return swarm.Config{}, nil, errors.Errorf("error while inspecting the config") 48 }, 49 expectedError: "error while inspecting the config", 50 }, 51 } 52 for _, tc := range testCases { 53 cmd := newConfigInspectCommand( 54 test.NewFakeCli(&fakeClient{ 55 configInspectFunc: tc.configInspectFunc, 56 }), 57 ) 58 cmd.SetArgs(tc.args) 59 for key, value := range tc.flags { 60 cmd.Flags().Set(key, value) 61 } 62 cmd.SetOut(ioutil.Discard) 63 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 64 } 65 } 66 67 func TestConfigInspectWithoutFormat(t *testing.T) { 68 testCases := []struct { 69 name string 70 args []string 71 configInspectFunc func(configID string) (swarm.Config, []byte, error) 72 }{ 73 { 74 name: "single-config", 75 args: []string{"foo"}, 76 configInspectFunc: func(name string) (swarm.Config, []byte, error) { 77 if name != "foo" { 78 return swarm.Config{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name) 79 } 80 return *Config(ConfigID("ID-foo"), ConfigName("foo")), nil, nil 81 }, 82 }, 83 { 84 name: "multiple-configs-with-labels", 85 args: []string{"foo", "bar"}, 86 configInspectFunc: func(name string) (swarm.Config, []byte, error) { 87 return *Config(ConfigID("ID-"+name), ConfigName(name), ConfigLabels(map[string]string{ 88 "label1": "label-foo", 89 })), nil, nil 90 }, 91 }, 92 } 93 for _, tc := range testCases { 94 cli := test.NewFakeCli(&fakeClient{configInspectFunc: tc.configInspectFunc}) 95 cmd := newConfigInspectCommand(cli) 96 cmd.SetArgs(tc.args) 97 assert.NilError(t, cmd.Execute()) 98 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-without-format.%s.golden", tc.name)) 99 } 100 } 101 102 func TestConfigInspectWithFormat(t *testing.T) { 103 configInspectFunc := func(name string) (swarm.Config, []byte, error) { 104 return *Config(ConfigName("foo"), ConfigLabels(map[string]string{ 105 "label1": "label-foo", 106 })), nil, nil 107 } 108 testCases := []struct { 109 name string 110 format string 111 args []string 112 configInspectFunc func(name string) (swarm.Config, []byte, error) 113 }{ 114 { 115 name: "simple-template", 116 format: "{{.Spec.Name}}", 117 args: []string{"foo"}, 118 configInspectFunc: configInspectFunc, 119 }, 120 { 121 name: "json-template", 122 format: "{{json .Spec.Labels}}", 123 args: []string{"foo"}, 124 configInspectFunc: configInspectFunc, 125 }, 126 } 127 for _, tc := range testCases { 128 cli := test.NewFakeCli(&fakeClient{ 129 configInspectFunc: tc.configInspectFunc, 130 }) 131 cmd := newConfigInspectCommand(cli) 132 cmd.SetArgs(tc.args) 133 cmd.Flags().Set("format", tc.format) 134 assert.NilError(t, cmd.Execute()) 135 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-with-format.%s.golden", tc.name)) 136 } 137 } 138 139 func TestConfigInspectPretty(t *testing.T) { 140 testCases := []struct { 141 name string 142 configInspectFunc func(string) (swarm.Config, []byte, error) 143 }{ 144 { 145 name: "simple", 146 configInspectFunc: func(id string) (swarm.Config, []byte, error) { 147 return *Config( 148 ConfigLabels(map[string]string{ 149 "lbl1": "value1", 150 }), 151 ConfigID("configID"), 152 ConfigName("configName"), 153 ConfigCreatedAt(time.Time{}), 154 ConfigUpdatedAt(time.Time{}), 155 ConfigData([]byte("payload here")), 156 ), []byte{}, nil 157 }, 158 }, 159 } 160 for _, tc := range testCases { 161 cli := test.NewFakeCli(&fakeClient{ 162 configInspectFunc: tc.configInspectFunc, 163 }) 164 cmd := newConfigInspectCommand(cli) 165 166 cmd.SetArgs([]string{"configID"}) 167 cmd.Flags().Set("pretty", "true") 168 assert.NilError(t, cmd.Execute()) 169 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-pretty.%s.golden", tc.name)) 170 } 171 }