update_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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"
  15. "github.com/docker/docker/pkg/testutil/golden"
  16. "github.com/stretchr/testify/assert"
  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 argument(s)",
  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. buf := new(bytes.Buffer)
  69. cmd := newUpdateCommand(
  70. test.NewFakeCli(&fakeClient{
  71. swarmInspectFunc: tc.swarmInspectFunc,
  72. swarmUpdateFunc: tc.swarmUpdateFunc,
  73. swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
  74. }, buf))
  75. cmd.SetArgs(tc.args)
  76. for key, value := range tc.flags {
  77. cmd.Flags().Set(key, value)
  78. }
  79. cmd.SetOutput(ioutil.Discard)
  80. testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
  81. }
  82. }
  83. func TestSwarmUpdate(t *testing.T) {
  84. testCases := []struct {
  85. name string
  86. args []string
  87. flags map[string]string
  88. swarmInspectFunc func() (swarm.Swarm, error)
  89. swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error
  90. swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
  91. }{
  92. {
  93. name: "noargs",
  94. },
  95. {
  96. name: "all-flags-quiet",
  97. flags: map[string]string{
  98. flagTaskHistoryLimit: "10",
  99. flagDispatcherHeartbeat: "10s",
  100. flagCertExpiry: "20s",
  101. flagExternalCA: "protocol=cfssl,url=https://example.com.",
  102. flagMaxSnapshots: "10",
  103. flagSnapshotInterval: "100",
  104. flagAutolock: "true",
  105. flagQuiet: "true",
  106. },
  107. swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
  108. if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 {
  109. return errors.Errorf("historyLimit not correctly set")
  110. }
  111. heartbeatDuration, err := time.ParseDuration("10s")
  112. if err != nil {
  113. return err
  114. }
  115. if swarm.Dispatcher.HeartbeatPeriod != heartbeatDuration {
  116. return errors.Errorf("heartbeatPeriodLimit not correctly set")
  117. }
  118. certExpiryDuration, err := time.ParseDuration("20s")
  119. if err != nil {
  120. return err
  121. }
  122. if swarm.CAConfig.NodeCertExpiry != certExpiryDuration {
  123. return errors.Errorf("certExpiry not correctly set")
  124. }
  125. if len(swarm.CAConfig.ExternalCAs) != 1 {
  126. return errors.Errorf("externalCA not correctly set")
  127. }
  128. if *swarm.Raft.KeepOldSnapshots != 10 {
  129. return errors.Errorf("keepOldSnapshots not correctly set")
  130. }
  131. if swarm.Raft.SnapshotInterval != 100 {
  132. return errors.Errorf("snapshotInterval not correctly set")
  133. }
  134. if !swarm.EncryptionConfig.AutoLockManagers {
  135. return errors.Errorf("autolock not correctly set")
  136. }
  137. return nil
  138. },
  139. },
  140. {
  141. name: "autolock-unlock-key",
  142. flags: map[string]string{
  143. flagTaskHistoryLimit: "10",
  144. flagAutolock: "true",
  145. },
  146. swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
  147. if *swarm.Orchestration.TaskHistoryRetentionLimit != 10 {
  148. return errors.Errorf("historyLimit not correctly set")
  149. }
  150. return nil
  151. },
  152. swarmInspectFunc: func() (swarm.Swarm, error) {
  153. return *Swarm(), nil
  154. },
  155. swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
  156. return types.SwarmUnlockKeyResponse{
  157. UnlockKey: "unlock-key",
  158. }, nil
  159. },
  160. },
  161. }
  162. for _, tc := range testCases {
  163. buf := new(bytes.Buffer)
  164. cmd := newUpdateCommand(
  165. test.NewFakeCli(&fakeClient{
  166. swarmInspectFunc: tc.swarmInspectFunc,
  167. swarmUpdateFunc: tc.swarmUpdateFunc,
  168. swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
  169. }, buf))
  170. cmd.SetArgs(tc.args)
  171. for key, value := range tc.flags {
  172. cmd.Flags().Set(key, value)
  173. }
  174. cmd.SetOutput(buf)
  175. assert.NoError(t, cmd.Execute())
  176. actual := buf.String()
  177. expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name))
  178. testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
  179. }
  180. }