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