join_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package swarm
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "strings"
  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/docker/docker/pkg/testutil"
  11. "github.com/pkg/errors"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestSwarmJoinErrors(t *testing.T) {
  15. testCases := []struct {
  16. name string
  17. args []string
  18. swarmJoinFunc func() error
  19. infoFunc func() (types.Info, error)
  20. expectedError string
  21. }{
  22. {
  23. name: "not-enough-args",
  24. expectedError: "requires exactly 1 argument",
  25. },
  26. {
  27. name: "too-many-args",
  28. args: []string{"remote1", "remote2"},
  29. expectedError: "requires exactly 1 argument",
  30. },
  31. {
  32. name: "join-failed",
  33. args: []string{"remote"},
  34. swarmJoinFunc: func() error {
  35. return errors.Errorf("error joining the swarm")
  36. },
  37. expectedError: "error joining the swarm",
  38. },
  39. {
  40. name: "join-failed-on-init",
  41. args: []string{"remote"},
  42. infoFunc: func() (types.Info, error) {
  43. return types.Info{}, errors.Errorf("error asking for node info")
  44. },
  45. expectedError: "error asking for node info",
  46. },
  47. }
  48. for _, tc := range testCases {
  49. buf := new(bytes.Buffer)
  50. cmd := newJoinCommand(
  51. test.NewFakeCli(&fakeClient{
  52. swarmJoinFunc: tc.swarmJoinFunc,
  53. infoFunc: tc.infoFunc,
  54. }, buf))
  55. cmd.SetArgs(tc.args)
  56. cmd.SetOutput(ioutil.Discard)
  57. testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
  58. }
  59. }
  60. func TestSwarmJoin(t *testing.T) {
  61. testCases := []struct {
  62. name string
  63. infoFunc func() (types.Info, error)
  64. expected string
  65. }{
  66. {
  67. name: "join-as-manager",
  68. infoFunc: func() (types.Info, error) {
  69. return types.Info{
  70. Swarm: swarm.Info{
  71. ControlAvailable: true,
  72. },
  73. }, nil
  74. },
  75. expected: "This node joined a swarm as a manager.",
  76. },
  77. {
  78. name: "join-as-worker",
  79. infoFunc: func() (types.Info, error) {
  80. return types.Info{
  81. Swarm: swarm.Info{
  82. ControlAvailable: false,
  83. },
  84. }, nil
  85. },
  86. expected: "This node joined a swarm as a worker.",
  87. },
  88. }
  89. for _, tc := range testCases {
  90. buf := new(bytes.Buffer)
  91. cmd := newJoinCommand(
  92. test.NewFakeCli(&fakeClient{
  93. infoFunc: tc.infoFunc,
  94. }, buf))
  95. cmd.SetArgs([]string{"remote"})
  96. assert.NoError(t, cmd.Execute())
  97. assert.Equal(t, strings.TrimSpace(buf.String()), tc.expected)
  98. }
  99. }