github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/swarm/join_test.go (about) 1 package swarm 2 3 import ( 4 "io/ioutil" 5 "strings" 6 "testing" 7 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/swarm" 11 "github.com/pkg/errors" 12 "gotest.tools/assert" 13 is "gotest.tools/assert/cmp" 14 ) 15 16 func TestSwarmJoinErrors(t *testing.T) { 17 testCases := []struct { 18 name string 19 args []string 20 swarmJoinFunc func() error 21 infoFunc func() (types.Info, error) 22 expectedError string 23 }{ 24 { 25 name: "not-enough-args", 26 expectedError: "requires exactly 1 argument", 27 }, 28 { 29 name: "too-many-args", 30 args: []string{"remote1", "remote2"}, 31 expectedError: "requires exactly 1 argument", 32 }, 33 { 34 name: "join-failed", 35 args: []string{"remote"}, 36 swarmJoinFunc: func() error { 37 return errors.Errorf("error joining the swarm") 38 }, 39 expectedError: "error joining the swarm", 40 }, 41 { 42 name: "join-failed-on-init", 43 args: []string{"remote"}, 44 infoFunc: func() (types.Info, error) { 45 return types.Info{}, errors.Errorf("error asking for node info") 46 }, 47 expectedError: "error asking for node info", 48 }, 49 } 50 for _, tc := range testCases { 51 cmd := newJoinCommand( 52 test.NewFakeCli(&fakeClient{ 53 swarmJoinFunc: tc.swarmJoinFunc, 54 infoFunc: tc.infoFunc, 55 })) 56 cmd.SetArgs(tc.args) 57 cmd.SetOutput(ioutil.Discard) 58 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 59 } 60 } 61 62 func TestSwarmJoin(t *testing.T) { 63 testCases := []struct { 64 name string 65 infoFunc func() (types.Info, error) 66 expected string 67 }{ 68 { 69 name: "join-as-manager", 70 infoFunc: func() (types.Info, error) { 71 return types.Info{ 72 Swarm: swarm.Info{ 73 ControlAvailable: true, 74 }, 75 }, nil 76 }, 77 expected: "This node joined a swarm as a manager.", 78 }, 79 { 80 name: "join-as-worker", 81 infoFunc: func() (types.Info, error) { 82 return types.Info{ 83 Swarm: swarm.Info{ 84 ControlAvailable: false, 85 }, 86 }, nil 87 }, 88 expected: "This node joined a swarm as a worker.", 89 }, 90 } 91 for _, tc := range testCases { 92 cli := test.NewFakeCli(&fakeClient{ 93 infoFunc: tc.infoFunc, 94 }) 95 cmd := newJoinCommand(cli) 96 cmd.SetArgs([]string{"remote"}) 97 assert.NilError(t, cmd.Execute()) 98 assert.Check(t, is.Equal(strings.TrimSpace(cli.OutBuffer().String()), tc.expected)) 99 } 100 }