github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/swarm/join_test.go (about) 1 package swarm 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/docker/docker/cli/internal/test" 13 "github.com/docker/docker/pkg/testutil/assert" 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 fmt.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{}, fmt.Errorf("error asking for node info") 46 }, 47 expectedError: "error asking for node info", 48 }, 49 } 50 for _, tc := range testCases { 51 buf := new(bytes.Buffer) 52 cmd := newJoinCommand( 53 test.NewFakeCli(&fakeClient{ 54 swarmJoinFunc: tc.swarmJoinFunc, 55 infoFunc: tc.infoFunc, 56 }, buf)) 57 cmd.SetArgs(tc.args) 58 cmd.SetOutput(ioutil.Discard) 59 assert.Error(t, cmd.Execute(), tc.expectedError) 60 } 61 } 62 63 func TestSwarmJoin(t *testing.T) { 64 testCases := []struct { 65 name string 66 infoFunc func() (types.Info, error) 67 expected string 68 }{ 69 { 70 name: "join-as-manager", 71 infoFunc: func() (types.Info, error) { 72 return types.Info{ 73 Swarm: swarm.Info{ 74 ControlAvailable: true, 75 }, 76 }, nil 77 }, 78 expected: "This node joined a swarm as a manager.", 79 }, 80 { 81 name: "join-as-worker", 82 infoFunc: func() (types.Info, error) { 83 return types.Info{ 84 Swarm: swarm.Info{ 85 ControlAvailable: false, 86 }, 87 }, nil 88 }, 89 expected: "This node joined a swarm as a worker.", 90 }, 91 } 92 for _, tc := range testCases { 93 buf := new(bytes.Buffer) 94 cmd := newJoinCommand( 95 test.NewFakeCli(&fakeClient{ 96 infoFunc: tc.infoFunc, 97 }, buf)) 98 cmd.SetArgs([]string{"remote"}) 99 assert.NilError(t, cmd.Execute()) 100 assert.Equal(t, strings.TrimSpace(buf.String()), tc.expected) 101 } 102 }