fluentd_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package fluentd // import "github.com/docker/docker/daemon/logger/fluentd"
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. "gotest.tools/v3/assert"
  6. )
  7. func TestValidateLogOptReconnectInterval(t *testing.T) {
  8. invalidIntervals := []string{"-1", "1", "-1s", "99ms", "11s"}
  9. for _, v := range invalidIntervals {
  10. t.Run("invalid "+v, func(t *testing.T) {
  11. err := ValidateLogOpt(map[string]string{asyncReconnectIntervalKey: v})
  12. assert.ErrorContains(t, err, "invalid value for fluentd-async-reconnect-interval:")
  13. })
  14. }
  15. validIntervals := []string{"100ms", "10s"}
  16. for _, v := range validIntervals {
  17. t.Run("valid "+v, func(t *testing.T) {
  18. err := ValidateLogOpt(map[string]string{asyncReconnectIntervalKey: v})
  19. assert.NilError(t, err)
  20. })
  21. }
  22. }
  23. func TestValidateLogOptAddress(t *testing.T) {
  24. // paths to try
  25. paths := []string{"/", "/some-path"}
  26. tests := []struct {
  27. addr string
  28. paths []string // paths to append to addr, should be an error for tcp/udp
  29. expected location
  30. expectedErr string
  31. }{
  32. {
  33. addr: "",
  34. expected: location{
  35. protocol: defaultProtocol,
  36. host: defaultHost,
  37. port: defaultPort,
  38. },
  39. },
  40. {
  41. addr: "192.168.1.1",
  42. paths: paths,
  43. expected: location{
  44. port: defaultPort,
  45. protocol: defaultProtocol,
  46. },
  47. },
  48. {
  49. addr: "[::1]",
  50. paths: paths,
  51. expected: location{
  52. port: defaultPort,
  53. protocol: defaultProtocol,
  54. },
  55. },
  56. {
  57. addr: "example.com",
  58. paths: paths,
  59. expected: location{
  60. port: defaultPort,
  61. protocol: defaultProtocol,
  62. },
  63. },
  64. {
  65. addr: "tcp://",
  66. paths: paths,
  67. expected: location{
  68. protocol: "tcp",
  69. port: defaultPort,
  70. },
  71. },
  72. {
  73. addr: "tcp://example.com",
  74. paths: paths,
  75. expected: location{
  76. protocol: "tcp",
  77. port: defaultPort,
  78. },
  79. },
  80. {
  81. addr: "tcp://example.com:65535",
  82. paths: paths,
  83. expected: location{
  84. protocol: "tcp",
  85. host: "example.com",
  86. port: 65535,
  87. },
  88. },
  89. {
  90. addr: "://",
  91. expectedErr: "invalid syntax",
  92. },
  93. {
  94. addr: "something://",
  95. expectedErr: "invalid syntax",
  96. },
  97. {
  98. addr: "corrupted:c",
  99. expectedErr: "invalid syntax",
  100. },
  101. {
  102. addr: "tcp://example.com:port",
  103. expectedErr: "invalid port",
  104. },
  105. {
  106. addr: "tcp://example.com:-1",
  107. expectedErr: "invalid port",
  108. },
  109. {
  110. addr: "unix:///some/socket.sock",
  111. expected: location{
  112. protocol: "unix",
  113. path: "/some/socket.sock",
  114. },
  115. },
  116. {
  117. addr: "unix:///some/socket.sock:80", // unusual, but technically valid
  118. expected: location{
  119. protocol: "unix",
  120. path: "/some/socket.sock:80",
  121. },
  122. },
  123. }
  124. for _, tc := range tests {
  125. tc := tc
  126. if len(tc.paths) == 0 {
  127. tc.paths = []string{""}
  128. }
  129. for _, path := range tc.paths {
  130. address := tc.addr + path
  131. t.Run(address, func(t *testing.T) {
  132. err := ValidateLogOpt(map[string]string{addressKey: address})
  133. if tc.expectedErr != "" {
  134. assert.ErrorContains(t, err, tc.expectedErr)
  135. return
  136. }
  137. assert.NilError(t, err)
  138. addr, _ := parseAddress(address)
  139. assert.DeepEqual(t, tc.expected, *addr, cmp.AllowUnexported(location{}))
  140. })
  141. }
  142. }
  143. }