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

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