github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/swarm/update_test.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 "time" 8 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 14 // Import builders to get the builder function as package function 15 . "github.com/docker/cli/internal/test/builders" 16 "gotest.tools/assert" 17 "gotest.tools/golden" 18 ) 19 20 func TestSwarmUpdateErrors(t *testing.T) { 21 testCases := []struct { 22 name string 23 args []string 24 flags map[string]string 25 swarmInspectFunc func() (swarm.Swarm, error) 26 swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error 27 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 28 expectedError string 29 }{ 30 { 31 name: "too-many-args", 32 args: []string{"foo"}, 33 expectedError: "accepts no arguments", 34 }, 35 { 36 name: "swarm-inspect-error", 37 flags: map[string]string{ 38 flagTaskHistoryLimit: "10", 39 }, 40 swarmInspectFunc: func() (swarm.Swarm, error) { 41 return swarm.Swarm{}, errors.Errorf("error inspecting the swarm") 42 }, 43 expectedError: "error inspecting the swarm", 44 }, 45 { 46 name: "swarm-update-error", 47 flags: map[string]string{ 48 flagTaskHistoryLimit: "10", 49 }, 50 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 51 return errors.Errorf("error updating the swarm") 52 }, 53 expectedError: "error updating the swarm", 54 }, 55 { 56 name: "swarm-unlockkey-error", 57 flags: map[string]string{ 58 flagAutolock: "true", 59 }, 60 swarmInspectFunc: func() (swarm.Swarm, error) { 61 return *Swarm(), nil 62 }, 63 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 64 return types.SwarmUnlockKeyResponse{}, errors.Errorf("error getting unlock key") 65 }, 66 expectedError: "error getting unlock key", 67 }, 68 } 69 for _, tc := range testCases { 70 cmd := newUpdateCommand( 71 test.NewFakeCli(&fakeClient{ 72 swarmInspectFunc: tc.swarmInspectFunc, 73 swarmUpdateFunc: tc.swarmUpdateFunc, 74 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 75 })) 76 cmd.SetArgs(tc.args) 77 for key, value := range tc.flags { 78 cmd.Flags().Set(key, value) 79 } 80 cmd.SetOutput(ioutil.Discard) 81 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 82 } 83 } 84 85 func TestSwarmUpdate(t *testing.T) { 86 swarmInfo := Swarm() 87 swarmInfo.ClusterInfo.TLSInfo.TrustRoot = "trustroot" 88 89 testCases := []struct { 90 name string 91 args []string 92 flags map[string]string 93 swarmInspectFunc func() (swarm.Swarm, error) 94 swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error 95 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 96 }{ 97 { 98 name: "noargs", 99 }, 100 { 101 name: "all-flags-quiet", 102 flags: map[string]string{ 103 flagTaskHistoryLimit: "10", 104 flagDispatcherHeartbeat: "10s", 105 flagCertExpiry: "20s", 106 flagExternalCA: "protocol=cfssl,url=https://example.com.", 107 flagMaxSnapshots: "10", 108 flagSnapshotInterval: "100", 109 flagAutolock: "true", 110 flagQuiet: "true", 111 }, 112 swarmInspectFunc: func() (swarm.Swarm, error) { 113 return *swarmInfo, nil 114 }, 115 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 116 if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 { 117 return errors.Errorf("historyLimit not correctly set") 118 } 119 heartbeatDuration, err := time.ParseDuration("10s") 120 if err != nil { 121 return err 122 } 123 if swarm.Dispatcher.HeartbeatPeriod != heartbeatDuration { 124 return errors.Errorf("heartbeatPeriodLimit not correctly set") 125 } 126 certExpiryDuration, err := time.ParseDuration("20s") 127 if err != nil { 128 return err 129 } 130 if swarm.CAConfig.NodeCertExpiry != certExpiryDuration { 131 return errors.Errorf("certExpiry not correctly set") 132 } 133 if len(swarm.CAConfig.ExternalCAs) != 1 || swarm.CAConfig.ExternalCAs[0].CACert != "trustroot" { 134 return errors.Errorf("externalCA not correctly set") 135 } 136 if *swarm.Raft.KeepOldSnapshots != 10 { 137 return errors.Errorf("keepOldSnapshots not correctly set") 138 } 139 if swarm.Raft.SnapshotInterval != 100 { 140 return errors.Errorf("snapshotInterval not correctly set") 141 } 142 if !swarm.EncryptionConfig.AutoLockManagers { 143 return errors.Errorf("autolock not correctly set") 144 } 145 return nil 146 }, 147 }, 148 { 149 name: "autolock-unlock-key", 150 flags: map[string]string{ 151 flagTaskHistoryLimit: "10", 152 flagAutolock: "true", 153 }, 154 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 155 if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 { 156 return errors.Errorf("historyLimit not correctly set") 157 } 158 return nil 159 }, 160 swarmInspectFunc: func() (swarm.Swarm, error) { 161 return *Swarm(), nil 162 }, 163 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 164 return types.SwarmUnlockKeyResponse{ 165 UnlockKey: "unlock-key", 166 }, nil 167 }, 168 }, 169 } 170 for _, tc := range testCases { 171 cli := test.NewFakeCli(&fakeClient{ 172 swarmInspectFunc: tc.swarmInspectFunc, 173 swarmUpdateFunc: tc.swarmUpdateFunc, 174 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 175 }) 176 cmd := newUpdateCommand(cli) 177 cmd.SetArgs(tc.args) 178 for key, value := range tc.flags { 179 cmd.Flags().Set(key, value) 180 } 181 cmd.SetOutput(cli.OutBuffer()) 182 assert.NilError(t, cmd.Execute()) 183 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("update-%s.golden", tc.name)) 184 } 185 }