github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/swarm/join_token_test.go (about)

     1  package swarm
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     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  	// Import builders to get the builder function as package function
    13  	. "github.com/docker/cli/internal/test/builders"
    14  	"gotest.tools/assert"
    15  	"gotest.tools/golden"
    16  )
    17  
    18  func TestSwarmJoinTokenErrors(t *testing.T) {
    19  	testCases := []struct {
    20  		name             string
    21  		args             []string
    22  		flags            map[string]string
    23  		infoFunc         func() (types.Info, error)
    24  		swarmInspectFunc func() (swarm.Swarm, error)
    25  		swarmUpdateFunc  func(swarm swarm.Spec, flags swarm.UpdateFlags) error
    26  		nodeInspectFunc  func() (swarm.Node, []byte, error)
    27  		expectedError    string
    28  	}{
    29  		{
    30  			name:          "not-enough-args",
    31  			expectedError: "requires exactly 1 argument",
    32  		},
    33  		{
    34  			name:          "too-many-args",
    35  			args:          []string{"worker", "manager"},
    36  			expectedError: "requires exactly 1 argument",
    37  		},
    38  		{
    39  			name:          "invalid-args",
    40  			args:          []string{"foo"},
    41  			expectedError: "unknown role foo",
    42  		},
    43  		{
    44  			name: "swarm-inspect-failed",
    45  			args: []string{"worker"},
    46  			swarmInspectFunc: func() (swarm.Swarm, error) {
    47  				return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
    48  			},
    49  			expectedError: "error inspecting the swarm",
    50  		},
    51  		{
    52  			name: "swarm-inspect-rotate-failed",
    53  			args: []string{"worker"},
    54  			flags: map[string]string{
    55  				flagRotate: "true",
    56  			},
    57  			swarmInspectFunc: func() (swarm.Swarm, error) {
    58  				return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
    59  			},
    60  			expectedError: "error inspecting the swarm",
    61  		},
    62  		{
    63  			name: "swarm-update-failed",
    64  			args: []string{"worker"},
    65  			flags: map[string]string{
    66  				flagRotate: "true",
    67  			},
    68  			swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
    69  				return errors.Errorf("error updating the swarm")
    70  			},
    71  			expectedError: "error updating the swarm",
    72  		},
    73  		{
    74  			name: "node-inspect-failed",
    75  			args: []string{"worker"},
    76  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
    77  				return swarm.Node{}, []byte{}, errors.Errorf("error inspecting node")
    78  			},
    79  			expectedError: "error inspecting node",
    80  		},
    81  		{
    82  			name: "info-failed",
    83  			args: []string{"worker"},
    84  			infoFunc: func() (types.Info, error) {
    85  				return types.Info{}, errors.Errorf("error asking for node info")
    86  			},
    87  			expectedError: "error asking for node info",
    88  		},
    89  	}
    90  	for _, tc := range testCases {
    91  		cli := test.NewFakeCli(&fakeClient{
    92  			swarmInspectFunc: tc.swarmInspectFunc,
    93  			swarmUpdateFunc:  tc.swarmUpdateFunc,
    94  			infoFunc:         tc.infoFunc,
    95  			nodeInspectFunc:  tc.nodeInspectFunc,
    96  		})
    97  		cmd := newJoinTokenCommand(cli)
    98  		cmd.SetArgs(tc.args)
    99  		for key, value := range tc.flags {
   100  			cmd.Flags().Set(key, value)
   101  		}
   102  		cmd.SetOutput(ioutil.Discard)
   103  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
   104  	}
   105  }
   106  
   107  func TestSwarmJoinToken(t *testing.T) {
   108  	testCases := []struct {
   109  		name             string
   110  		args             []string
   111  		flags            map[string]string
   112  		infoFunc         func() (types.Info, error)
   113  		swarmInspectFunc func() (swarm.Swarm, error)
   114  		nodeInspectFunc  func() (swarm.Node, []byte, error)
   115  	}{
   116  		{
   117  			name: "worker",
   118  			args: []string{"worker"},
   119  			infoFunc: func() (types.Info, error) {
   120  				return types.Info{
   121  					Swarm: swarm.Info{
   122  						NodeID: "nodeID",
   123  					},
   124  				}, nil
   125  			},
   126  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   127  				return *Node(Manager()), []byte{}, nil
   128  			},
   129  			swarmInspectFunc: func() (swarm.Swarm, error) {
   130  				return *Swarm(), nil
   131  			},
   132  		},
   133  		{
   134  			name: "manager",
   135  			args: []string{"manager"},
   136  			infoFunc: func() (types.Info, error) {
   137  				return types.Info{
   138  					Swarm: swarm.Info{
   139  						NodeID: "nodeID",
   140  					},
   141  				}, nil
   142  			},
   143  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   144  				return *Node(Manager()), []byte{}, nil
   145  			},
   146  			swarmInspectFunc: func() (swarm.Swarm, error) {
   147  				return *Swarm(), nil
   148  			},
   149  		},
   150  		{
   151  			name: "manager-rotate",
   152  			args: []string{"manager"},
   153  			flags: map[string]string{
   154  				flagRotate: "true",
   155  			},
   156  			infoFunc: func() (types.Info, error) {
   157  				return types.Info{
   158  					Swarm: swarm.Info{
   159  						NodeID: "nodeID",
   160  					},
   161  				}, nil
   162  			},
   163  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   164  				return *Node(Manager()), []byte{}, nil
   165  			},
   166  			swarmInspectFunc: func() (swarm.Swarm, error) {
   167  				return *Swarm(), nil
   168  			},
   169  		},
   170  		{
   171  			name: "worker-quiet",
   172  			args: []string{"worker"},
   173  			flags: map[string]string{
   174  				flagQuiet: "true",
   175  			},
   176  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   177  				return *Node(Manager()), []byte{}, nil
   178  			},
   179  			swarmInspectFunc: func() (swarm.Swarm, error) {
   180  				return *Swarm(), nil
   181  			},
   182  		},
   183  		{
   184  			name: "manager-quiet",
   185  			args: []string{"manager"},
   186  			flags: map[string]string{
   187  				flagQuiet: "true",
   188  			},
   189  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   190  				return *Node(Manager()), []byte{}, nil
   191  			},
   192  			swarmInspectFunc: func() (swarm.Swarm, error) {
   193  				return *Swarm(), nil
   194  			},
   195  		},
   196  	}
   197  	for _, tc := range testCases {
   198  		cli := test.NewFakeCli(&fakeClient{
   199  			swarmInspectFunc: tc.swarmInspectFunc,
   200  			infoFunc:         tc.infoFunc,
   201  			nodeInspectFunc:  tc.nodeInspectFunc,
   202  		})
   203  		cmd := newJoinTokenCommand(cli)
   204  		cmd.SetArgs(tc.args)
   205  		for key, value := range tc.flags {
   206  			cmd.Flags().Set(key, value)
   207  		}
   208  		assert.NilError(t, cmd.Execute())
   209  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("jointoken-%s.golden", tc.name))
   210  	}
   211  }