github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/swarm/leave_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/pkg/errors"
    10  	"gotest.tools/assert"
    11  	is "gotest.tools/assert/cmp"
    12  )
    13  
    14  func TestSwarmLeaveErrors(t *testing.T) {
    15  	testCases := []struct {
    16  		name           string
    17  		args           []string
    18  		swarmLeaveFunc func() error
    19  		expectedError  string
    20  	}{
    21  		{
    22  			name:          "too-many-args",
    23  			args:          []string{"foo"},
    24  			expectedError: "accepts no arguments",
    25  		},
    26  		{
    27  			name: "leave-failed",
    28  			swarmLeaveFunc: func() error {
    29  				return errors.Errorf("error leaving the swarm")
    30  			},
    31  			expectedError: "error leaving the swarm",
    32  		},
    33  	}
    34  	for _, tc := range testCases {
    35  		cmd := newLeaveCommand(
    36  			test.NewFakeCli(&fakeClient{
    37  				swarmLeaveFunc: tc.swarmLeaveFunc,
    38  			}))
    39  		cmd.SetArgs(tc.args)
    40  		cmd.SetOutput(ioutil.Discard)
    41  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    42  	}
    43  }
    44  
    45  func TestSwarmLeave(t *testing.T) {
    46  	cli := test.NewFakeCli(&fakeClient{})
    47  	cmd := newLeaveCommand(cli)
    48  	assert.NilError(t, cmd.Execute())
    49  	assert.Check(t, is.Equal("Node left the swarm.", strings.TrimSpace(cli.OutBuffer().String())))
    50  }