init_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/docker/docker/pkg/testutil"
  11. "github.com/docker/docker/pkg/testutil/golden"
  12. "github.com/pkg/errors"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
  16. testCases := []struct {
  17. name string
  18. flags map[string]string
  19. swarmInitFunc func() (string, error)
  20. swarmInspectFunc func() (swarm.Swarm, error)
  21. swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
  22. nodeInspectFunc func() (swarm.Node, []byte, error)
  23. expectedError string
  24. }{
  25. {
  26. name: "init-failed",
  27. swarmInitFunc: func() (string, error) {
  28. return "", errors.Errorf("error initializing the swarm")
  29. },
  30. expectedError: "error initializing the swarm",
  31. },
  32. {
  33. name: "init-failed-with-ip-choice",
  34. swarmInitFunc: func() (string, error) {
  35. return "", errors.Errorf("could not choose an IP address to advertise")
  36. },
  37. expectedError: "could not choose an IP address to advertise - specify one with --advertise-addr",
  38. },
  39. {
  40. name: "swarm-inspect-after-init-failed",
  41. swarmInspectFunc: func() (swarm.Swarm, error) {
  42. return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
  43. },
  44. expectedError: "error inspecting the swarm",
  45. },
  46. {
  47. name: "node-inspect-after-init-failed",
  48. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  49. return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node")
  50. },
  51. expectedError: "error inspecting the node",
  52. },
  53. {
  54. name: "swarm-get-unlock-key-after-init-failed",
  55. flags: map[string]string{
  56. flagAutolock: "true",
  57. },
  58. swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
  59. return types.SwarmUnlockKeyResponse{}, errors.Errorf("error getting swarm unlock key")
  60. },
  61. expectedError: "could not fetch unlock key: error getting swarm unlock key",
  62. },
  63. }
  64. for _, tc := range testCases {
  65. buf := new(bytes.Buffer)
  66. cmd := newInitCommand(
  67. test.NewFakeCli(&fakeClient{
  68. swarmInitFunc: tc.swarmInitFunc,
  69. swarmInspectFunc: tc.swarmInspectFunc,
  70. swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
  71. nodeInspectFunc: tc.nodeInspectFunc,
  72. }, buf))
  73. for key, value := range tc.flags {
  74. cmd.Flags().Set(key, value)
  75. }
  76. cmd.SetOutput(ioutil.Discard)
  77. assert.EqualError(t, cmd.Execute(), tc.expectedError)
  78. }
  79. }
  80. func TestSwarmInit(t *testing.T) {
  81. testCases := []struct {
  82. name string
  83. flags map[string]string
  84. swarmInitFunc func() (string, error)
  85. swarmInspectFunc func() (swarm.Swarm, error)
  86. swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
  87. nodeInspectFunc func() (swarm.Node, []byte, error)
  88. }{
  89. {
  90. name: "init",
  91. swarmInitFunc: func() (string, error) {
  92. return "nodeID", nil
  93. },
  94. },
  95. {
  96. name: "init-autolock",
  97. flags: map[string]string{
  98. flagAutolock: "true",
  99. },
  100. swarmInitFunc: func() (string, error) {
  101. return "nodeID", nil
  102. },
  103. swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
  104. return types.SwarmUnlockKeyResponse{
  105. UnlockKey: "unlock-key",
  106. }, nil
  107. },
  108. },
  109. }
  110. for _, tc := range testCases {
  111. buf := new(bytes.Buffer)
  112. cmd := newInitCommand(
  113. test.NewFakeCli(&fakeClient{
  114. swarmInitFunc: tc.swarmInitFunc,
  115. swarmInspectFunc: tc.swarmInspectFunc,
  116. swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
  117. nodeInspectFunc: tc.nodeInspectFunc,
  118. }, buf))
  119. for key, value := range tc.flags {
  120. cmd.Flags().Set(key, value)
  121. }
  122. assert.NoError(t, cmd.Execute())
  123. actual := buf.String()
  124. expected := golden.Get(t, []byte(actual), fmt.Sprintf("init-%s.golden", tc.name))
  125. testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
  126. }
  127. }