github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/swarm/unlock_key_test.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 8 "github.com/docker/cli/internal/test" 9 . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 "gotest.tools/v3/golden" 15 ) 16 17 func TestSwarmUnlockKeyErrors(t *testing.T) { 18 testCases := []struct { 19 name string 20 args []string 21 flags map[string]string 22 swarmInspectFunc func() (swarm.Swarm, error) 23 swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error 24 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 25 expectedError string 26 }{ 27 { 28 name: "too-many-args", 29 args: []string{"foo"}, 30 expectedError: "accepts no arguments", 31 }, 32 { 33 name: "swarm-inspect-rotate-failed", 34 flags: map[string]string{ 35 flagRotate: "true", 36 }, 37 swarmInspectFunc: func() (swarm.Swarm, error) { 38 return swarm.Swarm{}, errors.Errorf("error inspecting the swarm") 39 }, 40 expectedError: "error inspecting the swarm", 41 }, 42 { 43 name: "swarm-rotate-no-autolock-failed", 44 flags: map[string]string{ 45 flagRotate: "true", 46 }, 47 swarmInspectFunc: func() (swarm.Swarm, error) { 48 return *Swarm(), nil 49 }, 50 expectedError: "cannot rotate because autolock is not turned on", 51 }, 52 { 53 name: "swarm-update-failed", 54 flags: map[string]string{ 55 flagRotate: "true", 56 }, 57 swarmInspectFunc: func() (swarm.Swarm, error) { 58 return *Swarm(Autolock()), nil 59 }, 60 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 61 return errors.Errorf("error updating the swarm") 62 }, 63 expectedError: "error updating the swarm", 64 }, 65 { 66 name: "swarm-get-unlock-key-failed", 67 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 68 return types.SwarmUnlockKeyResponse{}, errors.Errorf("error getting unlock key") 69 }, 70 expectedError: "error getting unlock key", 71 }, 72 { 73 name: "swarm-no-unlock-key-failed", 74 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 75 return types.SwarmUnlockKeyResponse{ 76 UnlockKey: "", 77 }, nil 78 }, 79 expectedError: "no unlock key is set", 80 }, 81 } 82 for _, tc := range testCases { 83 cmd := newUnlockKeyCommand( 84 test.NewFakeCli(&fakeClient{ 85 swarmInspectFunc: tc.swarmInspectFunc, 86 swarmUpdateFunc: tc.swarmUpdateFunc, 87 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 88 })) 89 cmd.SetArgs(tc.args) 90 for key, value := range tc.flags { 91 cmd.Flags().Set(key, value) 92 } 93 cmd.SetOut(ioutil.Discard) 94 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 95 } 96 } 97 98 func TestSwarmUnlockKey(t *testing.T) { 99 testCases := []struct { 100 name string 101 args []string 102 flags map[string]string 103 swarmInspectFunc func() (swarm.Swarm, error) 104 swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error 105 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 106 }{ 107 { 108 name: "unlock-key", 109 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 110 return types.SwarmUnlockKeyResponse{ 111 UnlockKey: "unlock-key", 112 }, nil 113 }, 114 }, 115 { 116 name: "unlock-key-quiet", 117 flags: map[string]string{ 118 flagQuiet: "true", 119 }, 120 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 121 return types.SwarmUnlockKeyResponse{ 122 UnlockKey: "unlock-key", 123 }, nil 124 }, 125 }, 126 { 127 name: "unlock-key-rotate", 128 flags: map[string]string{ 129 flagRotate: "true", 130 }, 131 swarmInspectFunc: func() (swarm.Swarm, error) { 132 return *Swarm(Autolock()), nil 133 }, 134 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 135 return types.SwarmUnlockKeyResponse{ 136 UnlockKey: "unlock-key", 137 }, nil 138 }, 139 }, 140 { 141 name: "unlock-key-rotate-quiet", 142 flags: map[string]string{ 143 flagQuiet: "true", 144 flagRotate: "true", 145 }, 146 swarmInspectFunc: func() (swarm.Swarm, error) { 147 return *Swarm(Autolock()), nil 148 }, 149 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 150 return types.SwarmUnlockKeyResponse{ 151 UnlockKey: "unlock-key", 152 }, nil 153 }, 154 }, 155 } 156 for _, tc := range testCases { 157 cli := test.NewFakeCli(&fakeClient{ 158 swarmInspectFunc: tc.swarmInspectFunc, 159 swarmUpdateFunc: tc.swarmUpdateFunc, 160 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 161 }) 162 cmd := newUnlockKeyCommand(cli) 163 cmd.SetArgs(tc.args) 164 for key, value := range tc.flags { 165 cmd.Flags().Set(key, value) 166 } 167 assert.NilError(t, cmd.Execute()) 168 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) 169 } 170 }