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