update_test.go 5.5 KB

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