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

     1  package swarm
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/cli/streams"
     9  	"github.com/docker/cli/internal/test"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/swarm"
    12  	"github.com/pkg/errors"
    13  	"gotest.tools/assert"
    14  )
    15  
    16  func TestSwarmUnlockErrors(t *testing.T) {
    17  	testCases := []struct {
    18  		name            string
    19  		args            []string
    20  		swarmUnlockFunc func(req swarm.UnlockRequest) error
    21  		infoFunc        func() (types.Info, error)
    22  		expectedError   string
    23  	}{
    24  		{
    25  			name:          "too-many-args",
    26  			args:          []string{"foo"},
    27  			expectedError: "accepts no arguments",
    28  		},
    29  		{
    30  			name: "is-not-part-of-a-swarm",
    31  			infoFunc: func() (types.Info, error) {
    32  				return types.Info{
    33  					Swarm: swarm.Info{
    34  						LocalNodeState: swarm.LocalNodeStateInactive,
    35  					},
    36  				}, nil
    37  			},
    38  			expectedError: "This node is not part of a swarm",
    39  		},
    40  		{
    41  			name: "is-not-locked",
    42  			infoFunc: func() (types.Info, error) {
    43  				return types.Info{
    44  					Swarm: swarm.Info{
    45  						LocalNodeState: swarm.LocalNodeStateActive,
    46  					},
    47  				}, nil
    48  			},
    49  			expectedError: "Error: swarm is not locked",
    50  		},
    51  		{
    52  			name: "unlockrequest-failed",
    53  			infoFunc: func() (types.Info, error) {
    54  				return types.Info{
    55  					Swarm: swarm.Info{
    56  						LocalNodeState: swarm.LocalNodeStateLocked,
    57  					},
    58  				}, nil
    59  			},
    60  			swarmUnlockFunc: func(req swarm.UnlockRequest) error {
    61  				return errors.Errorf("error unlocking the swarm")
    62  			},
    63  			expectedError: "error unlocking the swarm",
    64  		},
    65  	}
    66  	for _, tc := range testCases {
    67  		cmd := newUnlockCommand(
    68  			test.NewFakeCli(&fakeClient{
    69  				infoFunc:        tc.infoFunc,
    70  				swarmUnlockFunc: tc.swarmUnlockFunc,
    71  			}))
    72  		cmd.SetArgs(tc.args)
    73  		cmd.SetOutput(ioutil.Discard)
    74  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    75  	}
    76  }
    77  
    78  func TestSwarmUnlock(t *testing.T) {
    79  	input := "unlockKey"
    80  	dockerCli := test.NewFakeCli(&fakeClient{
    81  		infoFunc: func() (types.Info, error) {
    82  			return types.Info{
    83  				Swarm: swarm.Info{
    84  					LocalNodeState: swarm.LocalNodeStateLocked,
    85  				},
    86  			}, nil
    87  		},
    88  		swarmUnlockFunc: func(req swarm.UnlockRequest) error {
    89  			if req.UnlockKey != input {
    90  				return errors.Errorf("Invalid unlock key")
    91  			}
    92  			return nil
    93  		},
    94  	})
    95  	dockerCli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input))))
    96  	cmd := newUnlockCommand(dockerCli)
    97  	assert.NilError(t, cmd.Execute())
    98  }