join_token_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "github.com/pkg/errors"
  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"
  14. "github.com/docker/docker/pkg/testutil/golden"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestSwarmJoinTokenErrors(t *testing.T) {
  18. testCases := []struct {
  19. name string
  20. args []string
  21. flags map[string]string
  22. infoFunc func() (types.Info, error)
  23. swarmInspectFunc func() (swarm.Swarm, error)
  24. swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error
  25. nodeInspectFunc func() (swarm.Node, []byte, error)
  26. expectedError string
  27. }{
  28. {
  29. name: "not-enough-args",
  30. expectedError: "requires exactly 1 argument",
  31. },
  32. {
  33. name: "too-many-args",
  34. args: []string{"worker", "manager"},
  35. expectedError: "requires exactly 1 argument",
  36. },
  37. {
  38. name: "invalid-args",
  39. args: []string{"foo"},
  40. expectedError: "unknown role foo",
  41. },
  42. {
  43. name: "swarm-inspect-failed",
  44. args: []string{"worker"},
  45. swarmInspectFunc: func() (swarm.Swarm, error) {
  46. return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
  47. },
  48. expectedError: "error inspecting the swarm",
  49. },
  50. {
  51. name: "swarm-inspect-rotate-failed",
  52. args: []string{"worker"},
  53. flags: map[string]string{
  54. flagRotate: "true",
  55. },
  56. swarmInspectFunc: func() (swarm.Swarm, error) {
  57. return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
  58. },
  59. expectedError: "error inspecting the swarm",
  60. },
  61. {
  62. name: "swarm-update-failed",
  63. args: []string{"worker"},
  64. flags: map[string]string{
  65. flagRotate: "true",
  66. },
  67. swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
  68. return errors.Errorf("error updating the swarm")
  69. },
  70. expectedError: "error updating the swarm",
  71. },
  72. {
  73. name: "node-inspect-failed",
  74. args: []string{"worker"},
  75. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  76. return swarm.Node{}, []byte{}, errors.Errorf("error inspecting node")
  77. },
  78. expectedError: "error inspecting node",
  79. },
  80. {
  81. name: "info-failed",
  82. args: []string{"worker"},
  83. infoFunc: func() (types.Info, error) {
  84. return types.Info{}, errors.Errorf("error asking for node info")
  85. },
  86. expectedError: "error asking for node info",
  87. },
  88. }
  89. for _, tc := range testCases {
  90. buf := new(bytes.Buffer)
  91. cmd := newJoinTokenCommand(
  92. test.NewFakeCli(&fakeClient{
  93. swarmInspectFunc: tc.swarmInspectFunc,
  94. swarmUpdateFunc: tc.swarmUpdateFunc,
  95. infoFunc: tc.infoFunc,
  96. nodeInspectFunc: tc.nodeInspectFunc,
  97. }, buf))
  98. cmd.SetArgs(tc.args)
  99. for key, value := range tc.flags {
  100. cmd.Flags().Set(key, value)
  101. }
  102. cmd.SetOutput(ioutil.Discard)
  103. testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
  104. }
  105. }
  106. func TestSwarmJoinToken(t *testing.T) {
  107. testCases := []struct {
  108. name string
  109. args []string
  110. flags map[string]string
  111. infoFunc func() (types.Info, error)
  112. swarmInspectFunc func() (swarm.Swarm, error)
  113. nodeInspectFunc func() (swarm.Node, []byte, error)
  114. }{
  115. {
  116. name: "worker",
  117. args: []string{"worker"},
  118. infoFunc: func() (types.Info, error) {
  119. return types.Info{
  120. Swarm: swarm.Info{
  121. NodeID: "nodeID",
  122. },
  123. }, nil
  124. },
  125. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  126. return *Node(Manager()), []byte{}, nil
  127. },
  128. swarmInspectFunc: func() (swarm.Swarm, error) {
  129. return *Swarm(), nil
  130. },
  131. },
  132. {
  133. name: "manager",
  134. args: []string{"manager"},
  135. infoFunc: func() (types.Info, error) {
  136. return types.Info{
  137. Swarm: swarm.Info{
  138. NodeID: "nodeID",
  139. },
  140. }, nil
  141. },
  142. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  143. return *Node(Manager()), []byte{}, nil
  144. },
  145. swarmInspectFunc: func() (swarm.Swarm, error) {
  146. return *Swarm(), nil
  147. },
  148. },
  149. {
  150. name: "manager-rotate",
  151. args: []string{"manager"},
  152. flags: map[string]string{
  153. flagRotate: "true",
  154. },
  155. infoFunc: func() (types.Info, error) {
  156. return types.Info{
  157. Swarm: swarm.Info{
  158. NodeID: "nodeID",
  159. },
  160. }, nil
  161. },
  162. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  163. return *Node(Manager()), []byte{}, nil
  164. },
  165. swarmInspectFunc: func() (swarm.Swarm, error) {
  166. return *Swarm(), nil
  167. },
  168. },
  169. {
  170. name: "worker-quiet",
  171. args: []string{"worker"},
  172. flags: map[string]string{
  173. flagQuiet: "true",
  174. },
  175. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  176. return *Node(Manager()), []byte{}, nil
  177. },
  178. swarmInspectFunc: func() (swarm.Swarm, error) {
  179. return *Swarm(), nil
  180. },
  181. },
  182. {
  183. name: "manager-quiet",
  184. args: []string{"manager"},
  185. flags: map[string]string{
  186. flagQuiet: "true",
  187. },
  188. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  189. return *Node(Manager()), []byte{}, nil
  190. },
  191. swarmInspectFunc: func() (swarm.Swarm, error) {
  192. return *Swarm(), nil
  193. },
  194. },
  195. }
  196. for _, tc := range testCases {
  197. buf := new(bytes.Buffer)
  198. cmd := newJoinTokenCommand(
  199. test.NewFakeCli(&fakeClient{
  200. swarmInspectFunc: tc.swarmInspectFunc,
  201. infoFunc: tc.infoFunc,
  202. nodeInspectFunc: tc.nodeInspectFunc,
  203. }, buf))
  204. cmd.SetArgs(tc.args)
  205. for key, value := range tc.flags {
  206. cmd.Flags().Set(key, value)
  207. }
  208. assert.NoError(t, cmd.Execute())
  209. actual := buf.String()
  210. expected := golden.Get(t, []byte(actual), fmt.Sprintf("jointoken-%s.golden", tc.name))
  211. testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
  212. }
  213. }