github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/swarm/update_test.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 "time" 8 9 "github.com/docker/cli/internal/test" 10 "github.com/docker/cli/internal/test/builders" 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 *builders.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 assert.Check(t, cmd.Flags().Set(key, value)) 77 } 78 cmd.SetOut(io.Discard) 79 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 80 } 81 } 82 83 func TestSwarmUpdate(t *testing.T) { 84 swarmInfo := builders.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 }, 109 swarmInspectFunc: func() (swarm.Swarm, error) { 110 return *swarmInfo, nil 111 }, 112 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 113 if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 { 114 return errors.Errorf("historyLimit not correctly set") 115 } 116 heartbeatDuration, err := time.ParseDuration("10s") 117 if err != nil { 118 return err 119 } 120 if swarm.Dispatcher.HeartbeatPeriod != heartbeatDuration { 121 return errors.Errorf("heartbeatPeriodLimit not correctly set") 122 } 123 certExpiryDuration, err := time.ParseDuration("20s") 124 if err != nil { 125 return err 126 } 127 if swarm.CAConfig.NodeCertExpiry != certExpiryDuration { 128 return errors.Errorf("certExpiry not correctly set") 129 } 130 if len(swarm.CAConfig.ExternalCAs) != 1 || swarm.CAConfig.ExternalCAs[0].CACert != "trustroot" { 131 return errors.Errorf("externalCA not correctly set") 132 } 133 if *swarm.Raft.KeepOldSnapshots != 10 { 134 return errors.Errorf("keepOldSnapshots not correctly set") 135 } 136 if swarm.Raft.SnapshotInterval != 100 { 137 return errors.Errorf("snapshotInterval not correctly set") 138 } 139 if !swarm.EncryptionConfig.AutoLockManagers { 140 return errors.Errorf("autolock not correctly set") 141 } 142 return nil 143 }, 144 }, 145 { 146 name: "autolock-unlock-key", 147 flags: map[string]string{ 148 flagTaskHistoryLimit: "10", 149 flagAutolock: "true", 150 }, 151 swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error { 152 if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 { 153 return errors.Errorf("historyLimit not correctly set") 154 } 155 return nil 156 }, 157 swarmInspectFunc: func() (swarm.Swarm, error) { 158 return *builders.Swarm(), nil 159 }, 160 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 161 return types.SwarmUnlockKeyResponse{ 162 UnlockKey: "unlock-key", 163 }, nil 164 }, 165 }, 166 } 167 for _, tc := range testCases { 168 cli := test.NewFakeCli(&fakeClient{ 169 swarmInspectFunc: tc.swarmInspectFunc, 170 swarmUpdateFunc: tc.swarmUpdateFunc, 171 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 172 }) 173 cmd := newUpdateCommand(cli) 174 cmd.SetArgs(tc.args) 175 for key, value := range tc.flags { 176 assert.Check(t, cmd.Flags().Set(key, value)) 177 } 178 cmd.SetOut(cli.OutBuffer()) 179 assert.NilError(t, cmd.Execute()) 180 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("update-%s.golden", tc.name)) 181 } 182 }