github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/swarm/unlock_key_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 TestSwarmUnlockKeyErrors(t *testing.T) {
    19  	testCases := []struct {
    20  		name                  string
    21  		args                  []string
    22  		flags                 map[string]string
    23  		swarmInspectFunc      func() (swarm.Swarm, error)
    24  		swarmUpdateFunc       func(swarm swarm.Spec, flags swarm.UpdateFlags) error
    25  		swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
    26  		expectedError         string
    27  	}{
    28  		{
    29  			name:          "too-many-args",
    30  			args:          []string{"foo"},
    31  			expectedError: "accepts no argument(s)",
    32  		},
    33  		{
    34  			name: "swarm-inspect-rotate-failed",
    35  			flags: map[string]string{
    36  				flagRotate: "true",
    37  			},
    38  			swarmInspectFunc: func() (swarm.Swarm, error) {
    39  				return swarm.Swarm{}, fmt.Errorf("error inspecting the swarm")
    40  			},
    41  			expectedError: "error inspecting the swarm",
    42  		},
    43  		{
    44  			name: "swarm-rotate-no-autolock-failed",
    45  			flags: map[string]string{
    46  				flagRotate: "true",
    47  			},
    48  			swarmInspectFunc: func() (swarm.Swarm, error) {
    49  				return *Swarm(), nil
    50  			},
    51  			expectedError: "cannot rotate because autolock is not turned on",
    52  		},
    53  		{
    54  			name: "swarm-update-failed",
    55  			flags: map[string]string{
    56  				flagRotate: "true",
    57  			},
    58  			swarmInspectFunc: func() (swarm.Swarm, error) {
    59  				return *Swarm(Autolock()), nil
    60  			},
    61  			swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
    62  				return fmt.Errorf("error updating the swarm")
    63  			},
    64  			expectedError: "error updating the swarm",
    65  		},
    66  		{
    67  			name: "swarm-get-unlock-key-failed",
    68  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
    69  				return types.SwarmUnlockKeyResponse{}, fmt.Errorf("error getting unlock key")
    70  			},
    71  			expectedError: "error getting unlock key",
    72  		},
    73  		{
    74  			name: "swarm-no-unlock-key-failed",
    75  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
    76  				return types.SwarmUnlockKeyResponse{
    77  					UnlockKey: "",
    78  				}, nil
    79  			},
    80  			expectedError: "no unlock key is set",
    81  		},
    82  	}
    83  	for _, tc := range testCases {
    84  		buf := new(bytes.Buffer)
    85  		cmd := newUnlockKeyCommand(
    86  			test.NewFakeCli(&fakeClient{
    87  				swarmInspectFunc:      tc.swarmInspectFunc,
    88  				swarmUpdateFunc:       tc.swarmUpdateFunc,
    89  				swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
    90  			}, buf))
    91  		cmd.SetArgs(tc.args)
    92  		for key, value := range tc.flags {
    93  			cmd.Flags().Set(key, value)
    94  		}
    95  		cmd.SetOutput(ioutil.Discard)
    96  		assert.Error(t, cmd.Execute(), tc.expectedError)
    97  	}
    98  }
    99  
   100  func TestSwarmUnlockKey(t *testing.T) {
   101  	testCases := []struct {
   102  		name                  string
   103  		args                  []string
   104  		flags                 map[string]string
   105  		swarmInspectFunc      func() (swarm.Swarm, error)
   106  		swarmUpdateFunc       func(swarm swarm.Spec, flags swarm.UpdateFlags) error
   107  		swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
   108  	}{
   109  		{
   110  			name: "unlock-key",
   111  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
   112  				return types.SwarmUnlockKeyResponse{
   113  					UnlockKey: "unlock-key",
   114  				}, nil
   115  			},
   116  		},
   117  		{
   118  			name: "unlock-key-quiet",
   119  			flags: map[string]string{
   120  				flagQuiet: "true",
   121  			},
   122  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
   123  				return types.SwarmUnlockKeyResponse{
   124  					UnlockKey: "unlock-key",
   125  				}, nil
   126  			},
   127  		},
   128  		{
   129  			name: "unlock-key-rotate",
   130  			flags: map[string]string{
   131  				flagRotate: "true",
   132  			},
   133  			swarmInspectFunc: func() (swarm.Swarm, error) {
   134  				return *Swarm(Autolock()), nil
   135  			},
   136  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
   137  				return types.SwarmUnlockKeyResponse{
   138  					UnlockKey: "unlock-key",
   139  				}, nil
   140  			},
   141  		},
   142  		{
   143  			name: "unlock-key-rotate-quiet",
   144  			flags: map[string]string{
   145  				flagQuiet:  "true",
   146  				flagRotate: "true",
   147  			},
   148  			swarmInspectFunc: func() (swarm.Swarm, error) {
   149  				return *Swarm(Autolock()), nil
   150  			},
   151  			swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
   152  				return types.SwarmUnlockKeyResponse{
   153  					UnlockKey: "unlock-key",
   154  				}, nil
   155  			},
   156  		},
   157  	}
   158  	for _, tc := range testCases {
   159  		buf := new(bytes.Buffer)
   160  		cmd := newUnlockKeyCommand(
   161  			test.NewFakeCli(&fakeClient{
   162  				swarmInspectFunc:      tc.swarmInspectFunc,
   163  				swarmUpdateFunc:       tc.swarmUpdateFunc,
   164  				swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
   165  			}, buf))
   166  		cmd.SetArgs(tc.args)
   167  		for key, value := range tc.flags {
   168  			cmd.Flags().Set(key, value)
   169  		}
   170  		assert.NilError(t, cmd.Execute())
   171  		actual := buf.String()
   172  		expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name))
   173  		assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected))
   174  	}
   175  }