github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/stack/services_test.go (about) 1 package stack 2 3 import ( 4 "io/ioutil" 5 "testing" 6 7 "github.com/docker/cli/cli/config/configfile" 8 "github.com/docker/cli/internal/test" 9 . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 is "gotest.tools/v3/assert/cmp" 15 "gotest.tools/v3/golden" 16 ) 17 18 func TestStackServicesErrors(t *testing.T) { 19 testCases := []struct { 20 args []string 21 flags map[string]string 22 serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error) 23 nodeListFunc func(options types.NodeListOptions) ([]swarm.Node, error) 24 taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) 25 expectedError string 26 }{ 27 { 28 args: []string{"foo"}, 29 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 30 return nil, errors.Errorf("error getting services") 31 }, 32 expectedError: "error getting services", 33 }, 34 { 35 args: []string{"foo"}, 36 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 37 return []swarm.Service{*Service(GlobalService())}, nil 38 }, 39 nodeListFunc: func(options types.NodeListOptions) ([]swarm.Node, error) { 40 return nil, errors.Errorf("error getting nodes") 41 }, 42 taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 43 return []swarm.Task{*Task()}, nil 44 }, 45 expectedError: "error getting nodes", 46 }, 47 { 48 args: []string{"foo"}, 49 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 50 return []swarm.Service{*Service(GlobalService())}, nil 51 }, 52 taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 53 return nil, errors.Errorf("error getting tasks") 54 }, 55 expectedError: "error getting tasks", 56 }, 57 { 58 args: []string{"foo"}, 59 flags: map[string]string{ 60 "format": "{{invalid format}}", 61 }, 62 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 63 return []swarm.Service{*Service()}, nil 64 }, 65 expectedError: "Template parsing error", 66 }, 67 } 68 69 for _, tc := range testCases { 70 tc := tc 71 t.Run(tc.expectedError, func(t *testing.T) { 72 cli := test.NewFakeCli(&fakeClient{ 73 serviceListFunc: tc.serviceListFunc, 74 nodeListFunc: tc.nodeListFunc, 75 taskListFunc: tc.taskListFunc, 76 }) 77 cmd := newServicesCommand(cli, &orchestrator) 78 cmd.SetArgs(tc.args) 79 for key, value := range tc.flags { 80 cmd.Flags().Set(key, value) 81 } 82 cmd.SetOut(ioutil.Discard) 83 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 84 }) 85 } 86 } 87 88 func TestRunServicesWithEmptyName(t *testing.T) { 89 cmd := newServicesCommand(test.NewFakeCli(&fakeClient{}), &orchestrator) 90 cmd.SetArgs([]string{"' '"}) 91 cmd.SetOut(ioutil.Discard) 92 93 assert.ErrorContains(t, cmd.Execute(), `invalid stack name: "' '"`) 94 } 95 96 func TestStackServicesEmptyServiceList(t *testing.T) { 97 fakeCli := test.NewFakeCli(&fakeClient{ 98 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 99 return []swarm.Service{}, nil 100 }, 101 }) 102 cmd := newServicesCommand(fakeCli, &orchestrator) 103 cmd.SetArgs([]string{"foo"}) 104 assert.NilError(t, cmd.Execute()) 105 assert.Check(t, is.Equal("", fakeCli.OutBuffer().String())) 106 assert.Check(t, is.Equal("Nothing found in stack: foo\n", fakeCli.ErrBuffer().String())) 107 } 108 109 func TestStackServicesWithQuietOption(t *testing.T) { 110 cli := test.NewFakeCli(&fakeClient{ 111 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 112 return []swarm.Service{*Service(ServiceID("id-foo"))}, nil 113 }, 114 }) 115 cmd := newServicesCommand(cli, &orchestrator) 116 cmd.Flags().Set("quiet", "true") 117 cmd.SetArgs([]string{"foo"}) 118 assert.NilError(t, cmd.Execute()) 119 golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-quiet-option.golden") 120 } 121 122 func TestStackServicesWithFormat(t *testing.T) { 123 cli := test.NewFakeCli(&fakeClient{ 124 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 125 return []swarm.Service{ 126 *Service(ServiceName("service-name-foo")), 127 }, nil 128 }, 129 }) 130 cmd := newServicesCommand(cli, &orchestrator) 131 cmd.SetArgs([]string{"foo"}) 132 cmd.Flags().Set("format", "{{ .Name }}") 133 assert.NilError(t, cmd.Execute()) 134 golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-format.golden") 135 } 136 137 func TestStackServicesWithConfigFormat(t *testing.T) { 138 cli := test.NewFakeCli(&fakeClient{ 139 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 140 return []swarm.Service{ 141 *Service(ServiceName("service-name-foo")), 142 }, nil 143 }, 144 }) 145 cli.SetConfigFile(&configfile.ConfigFile{ 146 ServicesFormat: "{{ .Name }}", 147 }) 148 cmd := newServicesCommand(cli, &orchestrator) 149 cmd.SetArgs([]string{"foo"}) 150 assert.NilError(t, cmd.Execute()) 151 golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-config-format.golden") 152 } 153 154 func TestStackServicesWithoutFormat(t *testing.T) { 155 cli := test.NewFakeCli(&fakeClient{ 156 serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 157 return []swarm.Service{*Service( 158 ServiceName("name-foo"), 159 ServiceID("id-foo"), 160 ReplicatedService(2), 161 ServiceImage("busybox:latest"), 162 ServicePort(swarm.PortConfig{ 163 PublishMode: swarm.PortConfigPublishModeIngress, 164 PublishedPort: 0, 165 TargetPort: 3232, 166 Protocol: swarm.PortConfigProtocolTCP, 167 }), 168 )}, nil 169 }, 170 }) 171 cmd := newServicesCommand(cli, &orchestrator) 172 cmd.SetArgs([]string{"foo"}) 173 assert.NilError(t, cmd.Execute()) 174 golden.Assert(t, cli.OutBuffer().String(), "stack-services-without-format.golden") 175 }