github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/swarm/join_token_test.go (about)

     1  package swarm
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/swarm"
    11  	"github.com/docker/docker/cli/internal/test"
    12  	// Import builders to get the builder function as package function
    13  	. "github.com/docker/docker/cli/internal/test/builders"
    14  	"github.com/docker/docker/pkg/testutil/assert"
    15  	"github.com/docker/docker/pkg/testutil/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{}, fmt.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{}, fmt.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 fmt.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{}, fmt.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{}, fmt.Errorf("error asking for node info")
    86  			},
    87  			expectedError: "error asking for node info",
    88  		},
    89  	}
    90  	for _, tc := range testCases {
    91  		buf := new(bytes.Buffer)
    92  		cmd := newJoinTokenCommand(
    93  			test.NewFakeCli(&fakeClient{
    94  				swarmInspectFunc: tc.swarmInspectFunc,
    95  				swarmUpdateFunc:  tc.swarmUpdateFunc,
    96  				infoFunc:         tc.infoFunc,
    97  				nodeInspectFunc:  tc.nodeInspectFunc,
    98  			}, buf))
    99  		cmd.SetArgs(tc.args)
   100  		for key, value := range tc.flags {
   101  			cmd.Flags().Set(key, value)
   102  		}
   103  		cmd.SetOutput(ioutil.Discard)
   104  		assert.Error(t, cmd.Execute(), tc.expectedError)
   105  	}
   106  }
   107  
   108  func TestSwarmJoinToken(t *testing.T) {
   109  	testCases := []struct {
   110  		name             string
   111  		args             []string
   112  		flags            map[string]string
   113  		infoFunc         func() (types.Info, error)
   114  		swarmInspectFunc func() (swarm.Swarm, error)
   115  		nodeInspectFunc  func() (swarm.Node, []byte, error)
   116  	}{
   117  		{
   118  			name: "worker",
   119  			args: []string{"worker"},
   120  			infoFunc: func() (types.Info, error) {
   121  				return types.Info{
   122  					Swarm: swarm.Info{
   123  						NodeID: "nodeID",
   124  					},
   125  				}, nil
   126  			},
   127  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   128  				return *Node(Manager()), []byte{}, nil
   129  			},
   130  			swarmInspectFunc: func() (swarm.Swarm, error) {
   131  				return *Swarm(), nil
   132  			},
   133  		},
   134  		{
   135  			name: "manager",
   136  			args: []string{"manager"},
   137  			infoFunc: func() (types.Info, error) {
   138  				return types.Info{
   139  					Swarm: swarm.Info{
   140  						NodeID: "nodeID",
   141  					},
   142  				}, nil
   143  			},
   144  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   145  				return *Node(Manager()), []byte{}, nil
   146  			},
   147  			swarmInspectFunc: func() (swarm.Swarm, error) {
   148  				return *Swarm(), nil
   149  			},
   150  		},
   151  		{
   152  			name: "manager-rotate",
   153  			args: []string{"manager"},
   154  			flags: map[string]string{
   155  				flagRotate: "true",
   156  			},
   157  			infoFunc: func() (types.Info, error) {
   158  				return types.Info{
   159  					Swarm: swarm.Info{
   160  						NodeID: "nodeID",
   161  					},
   162  				}, nil
   163  			},
   164  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   165  				return *Node(Manager()), []byte{}, nil
   166  			},
   167  			swarmInspectFunc: func() (swarm.Swarm, error) {
   168  				return *Swarm(), nil
   169  			},
   170  		},
   171  		{
   172  			name: "worker-quiet",
   173  			args: []string{"worker"},
   174  			flags: map[string]string{
   175  				flagQuiet: "true",
   176  			},
   177  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   178  				return *Node(Manager()), []byte{}, nil
   179  			},
   180  			swarmInspectFunc: func() (swarm.Swarm, error) {
   181  				return *Swarm(), nil
   182  			},
   183  		},
   184  		{
   185  			name: "manager-quiet",
   186  			args: []string{"manager"},
   187  			flags: map[string]string{
   188  				flagQuiet: "true",
   189  			},
   190  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   191  				return *Node(Manager()), []byte{}, nil
   192  			},
   193  			swarmInspectFunc: func() (swarm.Swarm, error) {
   194  				return *Swarm(), nil
   195  			},
   196  		},
   197  	}
   198  	for _, tc := range testCases {
   199  		buf := new(bytes.Buffer)
   200  		cmd := newJoinTokenCommand(
   201  			test.NewFakeCli(&fakeClient{
   202  				swarmInspectFunc: tc.swarmInspectFunc,
   203  				infoFunc:         tc.infoFunc,
   204  				nodeInspectFunc:  tc.nodeInspectFunc,
   205  			}, buf))
   206  		cmd.SetArgs(tc.args)
   207  		for key, value := range tc.flags {
   208  			cmd.Flags().Set(key, value)
   209  		}
   210  		assert.NilError(t, cmd.Execute())
   211  		actual := buf.String()
   212  		expected := golden.Get(t, []byte(actual), fmt.Sprintf("jointoken-%s.golden", tc.name))
   213  		assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected))
   214  	}
   215  }