opts_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package swarm
  2. import (
  3. "testing"
  4. "github.com/docker/docker/pkg/testutil/assert"
  5. )
  6. func TestNodeAddrOptionSetHostAndPort(t *testing.T) {
  7. opt := NewNodeAddrOption("old:123")
  8. addr := "newhost:5555"
  9. assert.NilError(t, opt.Set(addr))
  10. assert.Equal(t, opt.Value(), addr)
  11. }
  12. func TestNodeAddrOptionSetHostOnly(t *testing.T) {
  13. opt := NewListenAddrOption()
  14. assert.NilError(t, opt.Set("newhost"))
  15. assert.Equal(t, opt.Value(), "newhost:2377")
  16. }
  17. func TestNodeAddrOptionSetHostOnlyIPv6(t *testing.T) {
  18. opt := NewListenAddrOption()
  19. assert.NilError(t, opt.Set("::1"))
  20. assert.Equal(t, opt.Value(), "[::1]:2377")
  21. }
  22. func TestNodeAddrOptionSetPortOnly(t *testing.T) {
  23. opt := NewListenAddrOption()
  24. assert.NilError(t, opt.Set(":4545"))
  25. assert.Equal(t, opt.Value(), "0.0.0.0:4545")
  26. }
  27. func TestNodeAddrOptionSetInvalidFormat(t *testing.T) {
  28. opt := NewListenAddrOption()
  29. assert.Error(t, opt.Set("http://localhost:4545"), "Invalid")
  30. }
  31. func TestExternalCAOptionErrors(t *testing.T) {
  32. testCases := []struct {
  33. externalCA string
  34. expectedError string
  35. }{
  36. {
  37. externalCA: "",
  38. expectedError: "EOF",
  39. },
  40. {
  41. externalCA: "anything",
  42. expectedError: "invalid field 'anything' must be a key=value pair",
  43. },
  44. {
  45. externalCA: "foo=bar",
  46. expectedError: "the external-ca option needs a protocol= parameter",
  47. },
  48. {
  49. externalCA: "protocol=baz",
  50. expectedError: "unrecognized external CA protocol baz",
  51. },
  52. {
  53. externalCA: "protocol=cfssl",
  54. expectedError: "the external-ca option needs a url= parameter",
  55. },
  56. }
  57. for _, tc := range testCases {
  58. opt := &ExternalCAOption{}
  59. assert.Error(t, opt.Set(tc.externalCA), tc.expectedError)
  60. }
  61. }
  62. func TestExternalCAOption(t *testing.T) {
  63. testCases := []struct {
  64. externalCA string
  65. expected string
  66. }{
  67. {
  68. externalCA: "protocol=cfssl,url=anything",
  69. expected: "cfssl: anything",
  70. },
  71. {
  72. externalCA: "protocol=CFSSL,url=anything",
  73. expected: "cfssl: anything",
  74. },
  75. {
  76. externalCA: "protocol=Cfssl,url=https://example.com",
  77. expected: "cfssl: https://example.com",
  78. },
  79. {
  80. externalCA: "protocol=Cfssl,url=https://example.com,foo=bar",
  81. expected: "cfssl: https://example.com",
  82. },
  83. {
  84. externalCA: "protocol=Cfssl,url=https://example.com,foo=bar,foo=baz",
  85. expected: "cfssl: https://example.com",
  86. },
  87. }
  88. for _, tc := range testCases {
  89. opt := &ExternalCAOption{}
  90. assert.NilError(t, opt.Set(tc.externalCA))
  91. assert.Equal(t, opt.String(), tc.expected)
  92. }
  93. }
  94. func TestExternalCAOptionMultiple(t *testing.T) {
  95. opt := &ExternalCAOption{}
  96. assert.NilError(t, opt.Set("protocol=cfssl,url=https://example.com"))
  97. assert.NilError(t, opt.Set("protocol=CFSSL,url=anything"))
  98. assert.Equal(t, len(opt.Value()), 2)
  99. assert.Equal(t, opt.String(), "cfssl: https://example.com, cfssl: anything")
  100. }