join_token_test.go 5.6 KB

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