github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/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/docker/api/types" 10 "github.com/docker/docker/api/types/swarm" 11 "github.com/pkg/errors" 12 // Import builders to get the builder function as package function 13 . "github.com/docker/cli/internal/test/builders" 14 "gotest.tools/assert" 15 "gotest.tools/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 arguments", 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{}, errors.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 errors.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{}, errors.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 cmd := newUnlockKeyCommand( 85 test.NewFakeCli(&fakeClient{ 86 swarmInspectFunc: tc.swarmInspectFunc, 87 swarmUpdateFunc: tc.swarmUpdateFunc, 88 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 89 })) 90 cmd.SetArgs(tc.args) 91 for key, value := range tc.flags { 92 cmd.Flags().Set(key, value) 93 } 94 cmd.SetOutput(ioutil.Discard) 95 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 96 } 97 } 98 99 func TestSwarmUnlockKey(t *testing.T) { 100 testCases := []struct { 101 name string 102 args []string 103 flags map[string]string 104 swarmInspectFunc func() (swarm.Swarm, error) 105 swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error 106 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 107 }{ 108 { 109 name: "unlock-key", 110 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 111 return types.SwarmUnlockKeyResponse{ 112 UnlockKey: "unlock-key", 113 }, nil 114 }, 115 }, 116 { 117 name: "unlock-key-quiet", 118 flags: map[string]string{ 119 flagQuiet: "true", 120 }, 121 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 122 return types.SwarmUnlockKeyResponse{ 123 UnlockKey: "unlock-key", 124 }, nil 125 }, 126 }, 127 { 128 name: "unlock-key-rotate", 129 flags: map[string]string{ 130 flagRotate: "true", 131 }, 132 swarmInspectFunc: func() (swarm.Swarm, error) { 133 return *Swarm(Autolock()), nil 134 }, 135 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 136 return types.SwarmUnlockKeyResponse{ 137 UnlockKey: "unlock-key", 138 }, nil 139 }, 140 }, 141 { 142 name: "unlock-key-rotate-quiet", 143 flags: map[string]string{ 144 flagQuiet: "true", 145 flagRotate: "true", 146 }, 147 swarmInspectFunc: func() (swarm.Swarm, error) { 148 return *Swarm(Autolock()), nil 149 }, 150 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 151 return types.SwarmUnlockKeyResponse{ 152 UnlockKey: "unlock-key", 153 }, nil 154 }, 155 }, 156 } 157 for _, tc := range testCases { 158 cli := test.NewFakeCli(&fakeClient{ 159 swarmInspectFunc: tc.swarmInspectFunc, 160 swarmUpdateFunc: tc.swarmUpdateFunc, 161 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 162 }) 163 cmd := newUnlockKeyCommand(cli) 164 cmd.SetArgs(tc.args) 165 for key, value := range tc.flags { 166 cmd.Flags().Set(key, value) 167 } 168 assert.NilError(t, cmd.Execute()) 169 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) 170 } 171 }