init_test.go 3.8 KB

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