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