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