update_test.go 5.4 KB

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