init_test.go 3.8 KB

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