unlock_key_test.go 4.9 KB

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